Skip to content

Commit 5eac30c

Browse files
committed
Convert should_format? test to use format/3 and fixtures
This function is really an implementation detail, so let’s test the public API. The function also relies on interaction with the filesystem, so testing without fixtures is a bad idea because it will lead to incorrect results.
1 parent db38656 commit 5eac30c

File tree

4 files changed

+38
-12
lines changed

4 files changed

+38
-12
lines changed

apps/language_server/lib/language_server/providers/formatting.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
5353

5454
defp can_format?(_uri, _project_dir), do: false
5555

56-
def should_format?(file_uri, project_dir, inputs) when is_list(inputs) do
56+
defp should_format?(file_uri, project_dir, inputs) when is_list(inputs) do
5757
file_path = file_uri |> SourceFile.abs_path_from_uri()
5858

5959
inputs
@@ -67,7 +67,7 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
6767
|> Enum.any?(&(file_path == &1))
6868
end
6969

70-
def should_format?(_file_uri, _project_dir, _inputs), do: true
70+
defp should_format?(_file_uri, _project_dir, _inputs), do: true
7171

7272
defp myers_diff_to_text_edits(myers_diff) do
7373
myers_diff_to_text_edits(myers_diff, {0, 0}, [])
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[
22
line_length: 20,
3-
subdirectories: ["lib"]
3+
subdirectories: [
4+
"lib",
5+
"test"
6+
]
47
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
inputs: ["**/*.exs"]
3+
]

apps/language_server/test/providers/formatting_test.exs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -474,18 +474,38 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do
474474
end)
475475
end
476476

477+
@tag :fixture
477478
test "honors :inputs when deciding to format" do
478-
file = __ENV__.file
479-
uri = SourceFile.path_to_uri(file)
480-
project_dir = Path.dirname(file)
479+
in_fixture(Path.join(__DIR__, ".."), "formatter", fn ->
480+
project_dir = Path.expand(".")
481+
482+
assert_formatted("file.ex", project_dir)
483+
484+
# test/.formatter.exs has [inputs: ["**/*.exs"]]
485+
assert_formatted("test/file.exs", project_dir)
486+
refute_formatted("test/file.ex", project_dir)
487+
end)
488+
end
489+
490+
def assert_formatted(path, project_dir) do
491+
assert {:ok, [%{}]} = format(path, project_dir)
492+
end
493+
494+
def refute_formatted(path, project_dir) do
495+
assert {:ok, []} = format(path, project_dir)
496+
end
481497

482-
opts = []
483-
assert Formatting.should_format?(uri, project_dir, opts[:inputs])
498+
defp format(path, project_dir) do
499+
project_dir = maybe_convert_path_separators(project_dir)
500+
path = maybe_convert_path_separators("#{project_dir}/#{path}")
484501

485-
opts = [inputs: ["*.exs"]]
486-
assert Formatting.should_format?(uri, project_dir, opts[:inputs])
502+
source_file = %SourceFile{
503+
text: "",
504+
version: 1,
505+
dirty?: true
506+
}
487507

488-
opts = [inputs: ["*.ex"]]
489-
refute Formatting.should_format?(uri, project_dir, opts[:inputs])
508+
File.write!(path, "")
509+
Formatting.format(source_file, SourceFile.path_to_uri(path), project_dir)
490510
end
491511
end

0 commit comments

Comments
 (0)