@@ -7,6 +7,11 @@ import Settings._
77import org .junit .Test
88import org .junit .Assert ._
99import 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
1015
1116class ScalaSettingsTests :
1217
@@ -96,5 +101,97 @@ class ScalaSettingsTests:
96101 assertEquals(Action .Silent , sut.action(depr))
97102
98103
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+ wconfSrcFilterTest(
117+ argsStr = " -Wconf:src=path/.*:s" ,
118+ warning = reporting.Diagnostic .Warning (
119+ " A warning" .toMessage,
120+ util.SourcePosition (
121+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
122+ span = util.Spans .Span (1L )
123+ )
124+ ),
125+ expectedOutcome = Right (reporting.Action .Silent )
126+ )
127+
128+ @ Test def `WConf src filter doesn't silence warnings from a non-matching path` : Unit =
129+ wconfSrcFilterTest(
130+ argsStr = " -Wconf:src=another/.*:s" ,
131+ warning = reporting.Diagnostic .Warning (
132+ " A warning" .toMessage,
133+ util.SourcePosition (
134+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
135+ span = util.Spans .Span (1L )
136+ )
137+ ),
138+ expectedOutcome = Right (reporting.Action .Warning )
139+ )
140+
141+ @ Test def `WConf src filter silences warnings from a matching path for real file` : Unit =
142+ Using .resource(Files .createTempFile(" myfile" , " .scala" ).nn) { file =>
143+ wconfSrcFilterTest(
144+ argsStr = " -Wconf:src=myfile.*?\\ .scala:s" ,
145+ warning = reporting.Diagnostic .Warning (
146+ " A warning" .toMessage,
147+ util.SourcePosition (
148+ source = util.SourceFile (new PlainFile (Path (file)), " UTF-8" ),
149+ span = util.Spans .Span (1L )
150+ )
151+ ),
152+ expectedOutcome = Right (reporting.Action .Silent )
153+ )
154+ }(Files .deleteIfExists(_))
155+
156+ @ Test def `WConf src filter doesn't silence warnings from a non-matching path for real file` : Unit =
157+ Using .resource(Files .createTempFile(" myfile" , " .scala" ).nn) { file =>
158+ wconfSrcFilterTest(
159+ argsStr = " -Wconf:src=another.*?\\ .scala:s" ,
160+ warning = reporting.Diagnostic .Warning (
161+ " A warning" .toMessage,
162+ util.SourcePosition (
163+ source = util.SourceFile (new PlainFile (Path (file)), " UTF-8" ),
164+ span = util.Spans .Span (1L )
165+ )
166+ ),
167+ expectedOutcome = Right (reporting.Action .Warning )
168+ )
169+ }(Files .deleteIfExists(_))
170+
171+ @ Test def `WConf src filter reports an error on an invalid regex` : Unit =
172+ wconfSrcFilterTest(
173+ argsStr = """ -Wconf:src=\:s""" ,
174+ warning = reporting.Diagnostic .Warning (
175+ " A warning" .toMessage,
176+ util.SourcePosition (
177+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
178+ span = util.Spans .Span (1L )
179+ )
180+ ),
181+ expectedOutcome = Left (List (" invalid pattern `\\ `: Unescaped trailing backslash near index 1\n\\ " ))
182+ )
183+
184+ @ Test def `WConf src filter can be mixed with other filters with rightmost taking precedence` : Unit =
185+ wconfSrcFilterTest(
186+ argsStr = " -Wconf:src=.*:s,cat=deprecation:e" ,
187+ warning = reporting.Diagnostic .DeprecationWarning (
188+ " A warning" .toMessage,
189+ util.SourcePosition (
190+ source = util.SourceFile .virtual(new URI (" file:///some/path/file.scala" ), " " ),
191+ span = util.Spans .Span (1L )
192+ )
193+ ),
194+ expectedOutcome = Right (reporting.Action .Error ),
195+ )
99196
100197end ScalaSettingsTests
0 commit comments