diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..040f517 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +test/*_SUITE_data/* text eol=lf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00fb424..3ff5a3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,9 @@ jobs: - os: ubuntu-22.04 otp-version: 26 rebar3-version: 3.22 + - os: windows-2022 + otp-version: 26 + rebar3-version: 3.22 runs-on: ${{ matrix.os }} steps: - name: Checkout diff --git a/test/assert_diagnostic.erl b/test/assert_diagnostic.erl index 40ee344..03bd346 100644 --- a/test/assert_diagnostic.erl +++ b/test/assert_diagnostic.erl @@ -81,15 +81,18 @@ assert_snapshot_match(Expected, Output) -> ct:fail("unexpected: ~p~n", [Other]) end. -assert_binary_match(Expected, Formatted) -> - case Formatted of - Expected -> - ok; - Other -> - % Split by lines (preserving empty lines). - Expected2 = string:split(Expected, "\n", all), - Other2 = string:split(Other, "\n", all), - % We already know they are not equal, - % this macro gives a better diagnostic. - ?assertListEqual(Expected2, Other2) - end. +assert_binary_match(Expected0, Formatted0) -> + Expected = unicode:characters_to_binary(string:replace(Expected0, "\r\n", "\n", all)), + Formatted = unicode:characters_to_binary(Formatted0), + ?assertEqual(Expected, Formatted). +% case string:equal(Expected, Formatted) of + % true -> + % ok; + % false -> + % % Split by lines (preserving empty lines). + % Expected2 = string:split(Expected, "\n", all), + % Other = string:split(Formatted, "\n", all), + % % We already know they are not equal, + % % this macro gives a better diagnostic. + % ?assertListEqual(Expected2, Other) + % end. diff --git a/test/erlfmt_SUITE.erl b/test/erlfmt_SUITE.erl index d751c92..15ec823 100644 --- a/test/erlfmt_SUITE.erl +++ b/test/erlfmt_SUITE.erl @@ -1112,9 +1112,8 @@ snapshot_formatted(Module, Config) -> snapshot_match(FormattedModule, Module, Config, Options) -> % Format `Module` and check it matches `FormattedModule`. DataDir = ?config(data_dir, Config), - {ok, FormattedBin} = file:read_file(filename:join([DataDir, FormattedModule])), + {ok, Formatted} = file:read_file(filename:join([DataDir, FormattedModule])), {ok, OriginalBin} = file:read_file(filename:join([DataDir, Module])), - Formatted = unicode:characters_to_list(FormattedBin), Original = unicode:characters_to_list(OriginalBin), Output = erlfmt:format_string(Original, Options), assert_diagnostic:assert_snapshot_match(Formatted, Output). @@ -1127,7 +1126,7 @@ format_string_unicode(_) -> Options = [], Output = erlfmt:format_string(Original, Options), % Already formatted: we just check encoding is still ok. - assert_diagnostic:assert_snapshot_match(Original, Output). + assert_diagnostic:assert_snapshot_match(unicode:characters_to_binary(Original), Output). error_ignore_begin_ignore(_) -> assert_error( diff --git a/test/erlfmt_cli_SUITE.erl b/test/erlfmt_cli_SUITE.erl index 8afcdc1..c32ad3c 100644 --- a/test/erlfmt_cli_SUITE.erl +++ b/test/erlfmt_cli_SUITE.erl @@ -133,7 +133,7 @@ smoke_test_stdio_unicode(Config) -> stdio_test("unicode.erl", "--require-pragma", Config). smoke_test_stdio_insert_pragma_without(Config) when is_list(Config) -> - Formatted = os:cmd("echo '-module(nopragma).' | " ++ escript() ++ " - --insert-pragma"), + Formatted = os:cmd("echo \"-module(nopragma).\" | " ++ escript() ++ " - --insert-pragma"), Expected = "%%% % @format\n" "\n" @@ -142,21 +142,21 @@ smoke_test_stdio_insert_pragma_without(Config) when is_list(Config) -> smoke_test_stdio_delete_pragma(Config) when is_list(Config) -> Formatted = os:cmd( - "echo '%% @format\n\n-module(nopragma).' | " ++ escript() ++ " - --delete-pragma" + "echo \"%% @format\n\n-module(nopragma).\" | " ++ escript() ++ " - --delete-pragma" ), Expected = "-module(nopragma).\n", ?assertEqual(Expected, Formatted). smoke_test_stdio_delete_pragma_without(Config) when is_list(Config) -> - Formatted = os:cmd("echo '-module(nopragma).' | " ++ escript() ++ " - --delete-pragma"), + Formatted = os:cmd("echo \"-module(nopragma).\" | " ++ escript() ++ " - --delete-pragma"), Expected = "-module(nopragma).\n", ?assertEqual(Expected, Formatted). smoke_test_stdio_delete_pragma_with_copyright(Config) when is_list(Config) -> Formatted = os:cmd( - "echo '%% @format\n%% copyright\n\n-module(nopragma).' | " ++ escript() ++ + "echo \"%% @format\n%% copyright\n\n-module(nopragma).\" | " ++ escript() ++ " - --delete-pragma" ), Expected = @@ -167,7 +167,7 @@ smoke_test_stdio_delete_pragma_with_copyright(Config) when is_list(Config) -> smoke_test_stdio_reinsert_pragma(Config) when is_list(Config) -> Formatted = os:cmd( - "echo '%% @format\n%%% copyright\n\n-module(nopragma).' | " ++ escript() ++ + "echo \"%% @format\n%%% copyright\n\n-module(nopragma).\" | " ++ escript() ++ " - --insert-pragma" ), Expected = @@ -180,7 +180,7 @@ smoke_test_stdio_reinsert_pragma(Config) when is_list(Config) -> %% respect the number of percentages when replacing the pragma smoke_test_stdio_reinsert_pragma_second(Config) when is_list(Config) -> Formatted = os:cmd( - "echo '%% copyright\n%% @format\n\n-module(nopragma).' | " ++ escript() ++ + "echo \"%% copyright\n%% @format\n\n-module(nopragma).\" | " ++ escript() ++ " - --insert-pragma" ), Expected = @@ -192,7 +192,7 @@ smoke_test_stdio_reinsert_pragma_second(Config) when is_list(Config) -> smoke_test_stdio_reinsert_pragma_config(Config) when is_list(Config) -> Formatted = os:cmd( - "echo '%% @format\n\n%%% actual comment\n{}.\n' | " ++ escript() ++ + "echo \"%% @format\n\n%%% actual comment\n{}.\n\" | " ++ escript() ++ " - --insert-pragma" ), Expected = @@ -238,7 +238,7 @@ noformat_pragma_file(Config) when is_list(Config) -> noformat_pragma(Config) when is_list(Config) -> Formatted = os:cmd( - "echo '%% @noformat\n\n%%% actual comment\n{ }.' | " ++ escript() ++ " -" + "echo \"%% @noformat\n\n%%% actual comment\n{ }.\" | " ++ escript() ++ " -" ), Expected = "%% @noformat\n" @@ -280,10 +280,9 @@ stdio_test(FileName, Options, Config) -> DataDir = ?config(data_dir, Config), Path = filename:join(DataDir, FileName), Formatted = os:cmd("cat " ++ Path ++ " | " ++ escript() ++ " - " ++ Options), - % ?assertEqual(toto, Path), {ok, Expected} = file:read_file(Path), assert_diagnostic:assert_binary_match(Expected, unicode:characters_to_binary(Formatted)). escript() -> %% this relies on the _build structure rebar3 uses - filename:join(code:lib_dir(erlfmt), "../../bin/erlfmt"). + "escript " ++ filename:join(code:lib_dir(erlfmt), "../../bin/erlfmt").