@@ -7,6 +7,11 @@ import Settings._
7
7
import org .junit .Test
8
8
import org .junit .Assert ._
9
9
import core .Decorators .toMessage
10
+ import dotty .tools .io .{Path , PlainFile }
11
+
12
+ import java .net .URI
13
+ import java .nio .file .Files
14
+ import scala .util .Using
10
15
11
16
class ScalaSettingsTests :
12
17
@@ -96,5 +101,98 @@ class ScalaSettingsTests:
96
101
assertEquals(Action .Silent , sut.action(depr))
97
102
98
103
104
+ private def wconfSrcFilterTest (argsStr : String ,
105
+ expectedOutcome : Either [List [String ], reporting.Action ],
106
+ warning : reporting.Diagnostic .Warning ): Unit =
107
+ import reporting .Diagnostic
108
+ val settings = new ScalaSettings
109
+ val args = ArgsSummary (settings.defaultState, List (argsStr), errors = Nil , warnings = Nil )
110
+ val proc = settings.processArguments(args, processAll = true , skipped = Nil )
111
+ val wconfStr = settings.Wconf .valueIn(proc.sstate)
112
+ val wconf = reporting.WConf .fromSettings(wconfStr)
113
+ assertEquals(expectedOutcome, wconf.map(_.action(warning)))
114
+
115
+ @ Test def `WConf src filter silences warnings from a matching path for virtual file` : Unit =
116
+
117
+ wconfSrcFilterTest(
118
+ argsStr = " -Wconf:src=path/.*:s" ,
119
+ warning = reporting.Diagnostic .Warning (
120
+ " A warning" .toMessage,
121
+ util.SourcePosition (
122
+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
123
+ span = util.Spans .Span (1L )
124
+ )
125
+ ),
126
+ expectedOutcome = Right (reporting.Action .Silent )
127
+ )
128
+
129
+ @ Test def `WConf src filter doesn't silence warnings from a non-matching path` : Unit =
130
+ wconfSrcFilterTest(
131
+ argsStr = " -Wconf:src=another/.*:s" ,
132
+ warning = reporting.Diagnostic .Warning (
133
+ " A warning" .toMessage,
134
+ util.SourcePosition (
135
+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
136
+ span = util.Spans .Span (1L )
137
+ )
138
+ ),
139
+ expectedOutcome = Right (reporting.Action .Warning )
140
+ )
141
+
142
+ @ Test def `WConf src filter silences warnings from a matching path for real file` : Unit =
143
+ Using .resource(Files .createTempFile(" myfile" , " .scala" ).nn) { file =>
144
+ wconfSrcFilterTest(
145
+ argsStr = " -Wconf:src=myfile.*?\\ .scala:s" ,
146
+ warning = reporting.Diagnostic .Warning (
147
+ " A warning" .toMessage,
148
+ util.SourcePosition (
149
+ source = util.SourceFile (new PlainFile (Path (file)), " UTF-8" ),
150
+ span = util.Spans .Span (1L )
151
+ )
152
+ ),
153
+ expectedOutcome = Right (reporting.Action .Silent )
154
+ )
155
+ }(Files .deleteIfExists(_))
156
+
157
+ @ Test def `WConf src filter doesn't silence warnings from a non-matching path for real file` : Unit =
158
+ Using .resource(Files .createTempFile(" myfile" , " .scala" ).nn) { file =>
159
+ wconfSrcFilterTest(
160
+ argsStr = " -Wconf:src=another.*?\\ .scala:s" ,
161
+ warning = reporting.Diagnostic .Warning (
162
+ " A warning" .toMessage,
163
+ util.SourcePosition (
164
+ source = util.SourceFile (new PlainFile (Path (file)), " UTF-8" ),
165
+ span = util.Spans .Span (1L )
166
+ )
167
+ ),
168
+ expectedOutcome = Right (reporting.Action .Warning )
169
+ )
170
+ }(Files .deleteIfExists(_))
171
+
172
+ @ Test def `WConf src filter reports an error on an invalid regex` : Unit =
173
+ wconfSrcFilterTest(
174
+ argsStr = """ -Wconf:src=\:s""" ,
175
+ warning = reporting.Diagnostic .Warning (
176
+ " A warning" .toMessage,
177
+ util.SourcePosition (
178
+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
179
+ span = util.Spans .Span (1L )
180
+ )
181
+ ),
182
+ expectedOutcome = Left (List (" invalid pattern `\\ `: Unescaped trailing backslash near index 1\n\\ " ))
183
+ )
184
+
185
+ @ Test def `WConf src filter can be mixed with other filters with rightmost taking precedence` : Unit =
186
+ wconfSrcFilterTest(
187
+ argsStr = " -Wconf:src=.*:s,cat=deprecation:e" ,
188
+ warning = reporting.Diagnostic .DeprecationWarning (
189
+ " A warning" .toMessage,
190
+ util.SourcePosition (
191
+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
192
+ span = util.Spans .Span (1L )
193
+ )
194
+ ),
195
+ expectedOutcome = Right (reporting.Action .Error ),
196
+ )
99
197
100
198
end ScalaSettingsTests
0 commit comments