forked from jeremyjh/dialyxir
/
formatter_test.exs
144 lines (113 loc) · 4.75 KB
/
formatter_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
defmodule Dialyxir.FormatterTest do
use ExUnit.Case
import ExUnit.CaptureIO, only: [capture_io: 1]
alias Dialyxir.Formatter
alias Dialyxir.Project
defp in_project(app, f) when is_atom(app) do
Mix.Project.in_project(app, "test/fixtures/#{Atom.to_string(app)}", fn _ -> f.() end)
end
describe "exs ignore" do
test "evaluates an ignore file and ignores warnings matching the pattern" do
warnings = [
{:warn_return_no_exit, {'lib/short_description.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}},
{:warn_return_no_exit, {'lib/file/warning_type.ex', 18},
{:no_return, [:only_normal, :format_long, 1]}},
{:warn_return_no_exit, {'lib/file/warning_type/line.ex', 19},
{:no_return, [:only_normal, :format_long, 1]}}
]
in_project(:ignore, fn ->
{:error, remaining, _unused_filters_present} =
Formatter.format_and_filter(warnings, Project, [], :short)
assert remaining == []
end)
end
test "does not filter lines not matching the pattern" do
warning =
{:warn_return_no_exit, {'a/different_file.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}}
in_project(:ignore, fn ->
{:error, [remaining], _} = Formatter.format_and_filter([warning], Project, [], :short)
assert remaining =~ ~r/different_file.* no local return/
end)
end
test "can filter by regex" do
warning =
{:warn_return_no_exit, {'a/regex_file.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}}
in_project(:ignore, fn ->
{:error, remaining, _unused_filters_present} =
Formatter.format_and_filter([warning], Project, [], :short)
assert remaining == []
end)
end
test "lists unnecessary skips as warnings if ignoring exit status " do
warning =
{:warn_return_no_exit, {'a/regex_file.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}}
filter_args = [{:ignore_exit_status, true}]
in_project(:ignore, fn ->
assert {:warn, [], {:unused_filters_present, warning}} =
Formatter.format_and_filter([warning], Project, filter_args, :dialyxir)
assert warning =~ "Unused filters:"
end)
end
test "error on unnecessary skips without ignore_exit_status" do
warning =
{:warn_return_no_exit, {'a/regex_file.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}}
filter_args = [{:ignore_exit_status, false}]
in_project(:ignore, fn ->
{:error, [], {:unused_filters_present, error}} =
Formatter.format_and_filter([warning], Project, filter_args, :dialyxir)
assert error =~ "Unused filters:"
end)
end
test "overwrite ':list_unused_filters_present'" do
warning =
{:warn_return_no_exit, {'a/regex_file.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}}
filter_args = [{:list_unused_filters, false}]
in_project(:ignore, fn ->
assert {:warn, [], {:unused_filters_present, warning}} =
Formatter.format_and_filter([warning], Project, filter_args, :dialyxir)
refute warning =~ "Unused filters:"
end)
end
end
describe "simple string ignore" do
test "evaluates an ignore file and ignores warnings matching the pattern" do
warning =
{:warn_matching, {'a/file.ex', 17}, {:pattern_match, ['pattern \'ok\'', '\'error\'']}}
in_project(:ignore_string, fn ->
assert Formatter.format_and_filter([warning], Project, [], :dialyzer) ==
{:ok, [], :no_unused_filters}
end)
end
end
test "listing unused filter behaves the same for different formats" do
warnings = [
{:warn_return_no_exit, {'a/regex_file.ex', 17},
{:no_return, [:only_normal, :format_long, 1]}},
{:warn_return_no_exit, {'a/another-file.ex', 18}, {:unknown_type, {:M, :F, :A}}}
]
expected_warning = "a/another-file.ex:18"
expected_unused_filter = ~s(Unused filters:
{"lib/short_description.ex:17:no_return Function format_long/1 has no local return."}
{"lib/file/warning_type.ex", :no_return, 18}
{"lib/file/warning_type/line.ex", :no_return, 19})
filter_args = [{:list_unused_filters, true}]
for format <- [:short, :dialyxir, :dialyzer] do
in_project(:ignore, fn ->
capture_io(fn ->
result = Formatter.format_and_filter(warnings, Project, filter_args, format)
assert {:error, [warning], {:unused_filters_present, unused}} = result
assert warning =~ expected_warning
assert unused == expected_unused_filter
# A warning for regex_file.ex was explicitly put into format_and_filter.
refute unused =~ "regex_file.ex"
end)
end)
end
end
end