forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsTests.scala
184 lines (152 loc) · 6.52 KB
/
SettingsTests.scala
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package dotty.tools
package dotc
import reporting.StoreReporter
import vulpix.TestConfiguration
import core.Contexts.{Context, ContextBase}
import dotty.tools.dotc.config.Settings._
import dotty.tools.vulpix.TestConfiguration.mkClasspath
import java.nio.file._
import org.junit.Test
import org.junit.Assert._
class SettingsTests {
@Test def missingOutputDir: Unit =
val options = Array("-d", "not_here")
val reporter = Main.process(options, reporter = StoreReporter())
assertEquals(1, reporter.errorCount)
assertEquals("'not_here' does not exist or is not a directory or .jar file", reporter.allErrors.head.message)
@Test def jarOutput: Unit =
val source = "tests/pos/Foo.scala"
val out = Paths.get("out/jaredFoo.jar").normalize
if (Files.exists(out)) Files.delete(out)
val options = Array("-classpath", TestConfiguration.basicClasspath, "-d", out.toString, source)
val reporter = Main.process(options)
assertEquals(0, reporter.errorCount)
assertTrue(Files.exists(out))
@Test def `t8124 Don't crash on missing argument`: Unit =
val source = Paths.get("tests/pos/Foo.scala").normalize
val outputDir = Paths.get("out/testSettings").normalize
if Files.notExists(outputDir) then Files.createDirectory(outputDir)
// -encoding takes an arg!
val options = Array("-encoding", "-d", outputDir.toString, source.toString)
val reporter = Main.process(options, reporter = StoreReporter())
assertEquals(1, reporter.errorCount)
@Test def acceptUnconstrained: Unit =
object Settings extends SettingGroup:
val foo = StringSetting("-foo", "foo", "Foo", "a")
val bar = IntSetting("-bar", "Bar", 0)
val args = List("-foo", "b", "-bar", "1")
val summary = Settings.processArguments(args, true)
assertTrue(summary.errors.isEmpty)
withProcessedArgs(summary) {
assertEquals("b", Settings.foo.value)
assertEquals(1, Settings.bar.value)
}
@Test def `workaround dont crash on many files`: Unit =
object Settings extends SettingGroup
val args = "--" :: List.fill(6000)("file.scala")
val summary = Settings.processArguments(args, processAll = true)
assertTrue(summary.errors.isEmpty)
assertEquals(6000, summary.arguments.size)
@Test def `dont crash on many files`: Unit =
object Settings extends SettingGroup
val args = List.fill(6000)("file.scala")
val summary = Settings.processArguments(args, processAll = true)
assertTrue(summary.errors.isEmpty)
assertEquals(6000, summary.arguments.size)
@Test def `dont crash on many options`: Unit =
object Settings extends SettingGroup:
val option = BooleanSetting("-option", "Some option")
val limit = 6000
val args = List.fill(limit)("-option")
val summary = Settings.processArguments(args, processAll = true)
assertTrue(summary.errors.isEmpty)
assertEquals(limit-1, summary.warnings.size)
assertTrue(summary.warnings.head.contains("repeatedly"))
assertEquals(0, summary.arguments.size)
withProcessedArgs(summary) {
assertTrue(Settings.option.value)
}
@Test def `bad option warning consumes an arg`: Unit =
object Settings extends SettingGroup:
val option = BooleanSetting("-option", "Some option")
val args = List("-adoption", "dogs", "cats")
val summary = Settings.processArguments(args, processAll = true)
assertTrue(summary.errors.isEmpty)
assertFalse(summary.warnings.isEmpty)
assertEquals(2, summary.arguments.size)
@Test def `bad option settings throws`: Unit =
object Settings extends SettingGroup:
val option = BooleanSetting("-option", "Some option")
def checkMessage(s: String): (Throwable => Boolean) = t =>
if t.getMessage == s then true
else
println(s"Expected: $s, Actual: ${t.getMessage}")
false
val default = Settings.defaultState
assertThrows[IllegalArgumentException](checkMessage("found: not an option of type java.lang.String, required: Boolean")) {
Settings.option.updateIn(default, "not an option")
}
@Test def validateChoices: Unit =
object Settings extends SettingGroup:
val foo = ChoiceSetting("-foo", "foo", "Foo", List("a", "b"), "a")
val bar = IntChoiceSetting("-bar", "Bar", List(0, 1, 2), 0)
val baz = IntChoiceSetting("-baz", "Baz", 0 to 10, 10)
val quux = ChoiceSetting("-quux", "quux", "Quux", List(), "")
val quuz = IntChoiceSetting("-quuz", "Quuz", List(), 0)
locally {
val args = List("-foo", "b", "-bar", "1", "-baz", "5")
val summary = Settings.processArguments(args, true)
assertTrue(summary.errors.isEmpty)
withProcessedArgs(summary) {
assertEquals("b", Settings.foo.value)
assertEquals(1, Settings.bar.value)
assertEquals(5, Settings.baz.value)
}
}
locally {
val args = List("-foo:b")
val summary = Settings.processArguments(args, true)
assertTrue(summary.errors.isEmpty)
withProcessedArgs(summary) {
assertEquals("b", Settings.foo.value)
}
}
locally {
val args = List("-foo", "c", "-bar", "3", "-baz", "-1")
val summary = Settings.processArguments(args, true)
val expectedErrors = List(
"c is not a valid choice for -foo",
"3 is not a valid choice for -bar",
"-1 is out of legal range 0..10 for -baz"
)
assertEquals(expectedErrors, summary.errors)
}
locally {
val args = List("-foo:c")
val summary = Settings.processArguments(args, true)
val expectedErrors = List("c is not a valid choice for -foo")
assertEquals(expectedErrors, summary.errors)
}
locally {
val args = List("-quux", "a", "-quuz", "0")
val summary = Settings.processArguments(args, true)
val expectedErrors = List(
"a is not a valid choice for -quux",
"0 is not a valid choice for -quuz",
)
assertEquals(expectedErrors, summary.errors)
}
@Test def `Allow IntSetting's to be set with a colon`: Unit =
object Settings extends SettingGroup:
val foo = IntSetting("-foo", "foo", 80)
import Settings._
val args = List("-foo:100")
val summary = processArguments(args, processAll = true)
assertTrue(s"Setting args errors:\n ${summary.errors.take(5).mkString("\n ")}", summary.errors.isEmpty)
withProcessedArgs(summary) {
assertEquals(100, foo.value)
}
private def withProcessedArgs(summary: ArgsSummary)(f: SettingsState ?=> Unit) = f(using summary.sstate)
extension [T](setting: Setting[T])
private def value(using ss: SettingsState): T = setting.valueIn(ss)
}