Skip to content
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
22 changes: 17 additions & 5 deletions lib/mix/lib/mix/tasks/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ defmodule Mix.Tasks.Format do

* `--dry-run` - does not save files after formatting.

* `--verbose` - prints the names of files that were formatted.

* `--dot-formatter` - path to the file with formatter configuration.
Defaults to `.formatter.exs` if one is available. See the
"Formatting options" section above for more information.
Expand Down Expand Up @@ -199,6 +201,7 @@ defmodule Mix.Tasks.Format do
no_exit: :boolean,
dot_formatter: :string,
dry_run: :boolean,
verbose: :boolean,
stdin_filename: :string,
force: :boolean,
migrate: :boolean
Expand Down Expand Up @@ -764,18 +767,27 @@ defmodule Mix.Tasks.Format do
:ok

true ->
write_or_print(file, input, output)
write_or_print(file, input, output, task_opts)
end
rescue
exception ->
{:exit, file, exception, __STACKTRACE__}
end

defp write_or_print(file, input, output) do
defp write_or_print(file, input, output, task_opts) do
cond do
file == :stdin -> IO.write(output)
input == output -> :ok
true -> File.write!(file, output)
file == :stdin ->
IO.write(output)

input == output ->
:ok

true ->
File.write!(file, output)
Comment thread
georgeguimaraes marked this conversation as resolved.

if task_opts[:verbose] do
Mix.shell().info([:green, "* formatting ", :reset, Path.relative_to_cwd(file)])
end
end

:ok
Expand Down
29 changes: 29 additions & 0 deletions lib/mix/test/mix/tasks/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ defmodule Mix.Tasks.FormatTest do
end)
end

test "prints formatted file names with --verbose", context do
in_tmp(context.test, fn ->
File.write!("a.ex", """
foo bar
""")

File.write!("b.ex", """
foo(bar)
""")

Mix.Tasks.Format.run(["a.ex", "b.ex", "--verbose"])

assert_received {:mix_shell, :info, ["* formatting a.ex"]}
refute_received {:mix_shell, :info, ["* formatting b.ex"]}
end)
end

test "does not print formatted file names by default", context do
in_tmp(context.test, fn ->
File.write!("a.ex", """
foo bar
""")

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

refute_received {:mix_shell, :info, ["* formatting a.ex"]}
end)
end

Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage for --check-formatted flag interaction with the new formatting output feature. While the implementation correctly doesn't print formatting messages when --check-formatted is used (since it doesn't actually format files), this behavior should be explicitly tested to prevent regressions. Consider adding a test similar to the --dry-run and --quiet tests that verifies no formatting messages are printed when using --check-formatted.

Suggested change
test "does not print formatted file names with --check-formatted", context do
in_tmp(context.test, fn ->
File.write!("a.ex", """
foo bar
""")
Mix.Tasks.Format.run(["a.ex, "--check-formatted"])
refute_received {:mix_shell, :info, ["* formatting a.ex"]}
end)
end

Copilot uses AI. Check for mistakes.
test "does not try to format a directory that matches a given pattern", context do
in_tmp(context.test, fn ->
File.mkdir_p!("a.ex")
Expand Down
Loading