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