Skip to content

Commit

Permalink
Merge pull request #496 from Nezteb/add-no-debug-print
Browse files Browse the repository at this point in the history
Add --quiet-with-result flag
  • Loading branch information
jeremyjh committed May 11, 2023
2 parents f99635c + 5aaaa84 commit 3008741
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
15 changes: 12 additions & 3 deletions lib/dialyxir/dialyzer.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Dialyxir.Dialyzer do
import Dialyxir.Output, only: [color: 2, info: 1]
import Dialyxir.Output
alias String.Chars
alias Dialyxir.Formatter
alias Dialyxir.Project
Expand All @@ -10,13 +10,16 @@ defmodule Dialyxir.Dialyzer do
:raw,
:format,
:list_unused_filters,
:ignore_exit_status
:ignore_exit_status,
:quiet_with_result
]

def run(args, filterer) do
try do
{split, args} = Keyword.split(args, @dialyxir_args)

quiet_with_result? = split[:quiet_with_result]

formatter =
cond do
split[:format] == "dialyzer" ->
Expand Down Expand Up @@ -59,7 +62,13 @@ defmodule Dialyxir.Dialyzer do

filter_map_args = FilterMap.to_args(split)

case Formatter.format_and_filter(result, filterer, filter_map_args, formatter) do
case Formatter.format_and_filter(
result,
filterer,
filter_map_args,
formatter,
quiet_with_result?
) do
{:ok, formatted_warnings, :no_unused_filters} ->
{:ok, {formatted_time_elapsed, formatted_warnings, ""}}

Expand Down
32 changes: 23 additions & 9 deletions lib/dialyxir/formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Dialyxir.Formatter do
Wrapper around normal Dialyzer warning messages that provides
example output for error messages.
"""
import Dialyxir.Output, only: [info: 1]
import Dialyxir.Output

alias Dialyxir.FilterMap

Expand All @@ -21,8 +21,16 @@ defmodule Dialyxir.Formatter do
"done in #{minutes}m#{seconds}s"
end

@spec format_and_filter([tuple], module, Keyword.t(), t()) :: tuple
def format_and_filter(warnings, filterer, filter_map_args, formatter) do
@spec format_and_filter([tuple], module, Keyword.t(), t(), boolean()) :: tuple
def format_and_filter(
warnings,
filterer,
filter_map_args,
formatter,
quiet_with_result? \\ false
)

def format_and_filter(warnings, filterer, filter_map_args, formatter, quiet_with_result?) do
filter_map = filterer.filter_map(filter_map_args)

{filtered_warnings, filter_map} = filter_warnings(warnings, filterer, filter_map)
Expand All @@ -33,7 +41,7 @@ defmodule Dialyxir.Formatter do
|> Enum.map(&formatter.format/1)
|> Enum.uniq()

show_count_skipped(warnings, formatted_warnings, filter_map)
show_count_skipped(warnings, formatted_warnings, filter_map, quiet_with_result?)
formatted_unnecessary_skips = format_unnecessary_skips(filter_map)

result(formatted_warnings, filter_map, formatted_unnecessary_skips)
Expand All @@ -52,16 +60,22 @@ defmodule Dialyxir.Formatter do
end
end

defp show_count_skipped(warnings, filtered_warnings, filter_map) do
defp show_count_skipped(warnings, filtered_warnings, filter_map, quiet_with_result?) do
warnings_count = Enum.count(warnings)
filtered_warnings_count = Enum.count(filtered_warnings)
skipped_count = warnings_count - filtered_warnings_count
unnecessary_skips_count = count_unnecessary_skips(filter_map)

info(
"Total errors: #{warnings_count}, Skipped: #{skipped_count}, " <>
"Unnecessary Skips: #{unnecessary_skips_count}"
)
message =
"Total errors: #{warnings_count}, Skipped: #{skipped_count}, Unnecessary Skips: #{unnecessary_skips_count}"

if quiet_with_result? do
Mix.shell(Mix.Shell.IO)
info(message)
Mix.shell(Mix.Shell.Quiet)
else
info(message)
end

:ok
end
Expand Down
29 changes: 25 additions & 4 deletions lib/mix/tasks/dialyzer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Mix.Tasks.Dialyzer do
* `--format github` - format the warnings in the Github Actions message format
* `--format ignore_file` - format the warnings to be suitable for adding to Elixir Format ignore file
* `--quiet` - suppress all informational messages
* `--quiet-with-result` - suppress all informational messages except for the final result message
Warning flags passed to this task are passed on to `:dialyzer` - e.g.
Expand Down Expand Up @@ -94,7 +95,7 @@ defmodule Mix.Tasks.Dialyzer do

use Mix.Task
import System, only: [user_home!: 0]
import Dialyxir.Output, only: [info: 1, error: 1]
import Dialyxir.Output
alias Dialyxir.Project
alias Dialyxir.Plt
alias Dialyxir.Dialyzer
Expand Down Expand Up @@ -150,14 +151,15 @@ defmodule Mix.Tasks.Dialyzer do
no_compile: :boolean,
plt: :boolean,
quiet: :boolean,
quiet_with_result: :boolean,
raw: :boolean,
format: :string
)

def run(args) do
{opts, _, dargs} = OptionParser.parse(args, strict: @command_options)
original_shell = Mix.shell()
if opts[:quiet], do: Mix.shell(Mix.Shell.Quiet)
if opts[:quiet] || opts[:quiet_with_result], do: Mix.shell(Mix.Shell.Quiet)
opts = Keyword.delete(opts, :quiet)
check_dialyzer()
compatibility_notice()
Expand Down Expand Up @@ -265,12 +267,31 @@ defmodule Mix.Tasks.Dialyzer do
{:format, opts[:format]},
{:raw, opts[:raw]},
{:list_unused_filters, opts[:list_unused_filters]},
{:ignore_exit_status, opts[:ignore_exit_status]}
{:ignore_exit_status, opts[:ignore_exit_status]},
{:quiet_with_result, opts[:quiet_with_result]}
]

{status, exit_status, [time | result]} = Dialyzer.dialyze(args)
info(time)
report = if status == :ok, do: &info/1, else: &error/1

quiet_with_result? = opts[:quiet_with_result]

report =
cond do
status == :ok && quiet_with_result? ->
fn text ->
Mix.shell(Mix.Shell.IO)
info(text)
Mix.shell(Mix.Shell.Quiet)
end

status == :ok ->
&info/1

true ->
&error/1
end

Enum.each(result, report)

unless exit_status == 0 || opts[:ignore_exit_status] do
Expand Down
10 changes: 10 additions & 0 deletions test/mix/tasks/dialyzer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ defmodule Mix.Tasks.DialyzerTest do
{result, 0} = System.cmd("mix", args, env: env)
assert result == ""
end

@tag :output_tests
test "Only final result is printed with --quiet-with-result" do
args = ["dialyzer", "--quiet-with-result"]
env = [{"MIX_ENV", "prod"}]
{result, 0} = System.cmd("mix", args, env: env)

assert result =~
~r/Total errors: ., Skipped: ., Unnecessary Skips: .\ndone \(passed successfully\)\n/
end
end

0 comments on commit 3008741

Please sign in to comment.