Skip to content

Skip empty files during mix format #11802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/elixir/test/elixir/code_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ defmodule CodeTest do
end
end

test "format_string/2 returns empty iodata for empty string" do
assert Code.format_string!("") == []
end

test "ensure_loaded?/1" do
assert Code.ensure_loaded?(__MODULE__)
refute Code.ensure_loaded?(Code.NoFile)
Expand Down
2 changes: 2 additions & 0 deletions lib/mix/lib/mix/tasks/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ defmodule Mix.Tasks.Format do
defp stdin_or_wildcard("-"), do: [:stdin]
defp stdin_or_wildcard(path), do: path |> Path.expand() |> Path.wildcard(match_dot: true)

defp elixir_format("", _formatter_opts), do: ""

defp elixir_format(content, formatter_opts) do
IO.iodata_to_binary([Code.format_string!(content, formatter_opts), ?\n])
end
Expand Down
10 changes: 10 additions & 0 deletions lib/mix/test/mix/tasks/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ defmodule Mix.Tasks.FormatTest do
end
end

test "doesn't format empty lines into line breaks", context do
in_tmp(context.test, fn ->
File.write!("a.exs", "")

Mix.Tasks.Format.run(["a.exs"])

assert File.read!("a.exs") == ""
end)
end

test "formats the given files", context do
in_tmp(context.test, fn ->
File.write!("a.ex", """
Expand Down