Skip to content

Commit a6fc18d

Browse files
committed
Refactor ScalaSipTests to ensure all tests are run on Windows and all relevant binary names are checked
1 parent 67af4ec commit a6fc18d

File tree

1 file changed

+67
-85
lines changed

1 file changed

+67
-85
lines changed

modules/integration/src/test/scala/scala/cli/integration/SipScalaTests.scala

Lines changed: 67 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,40 @@ import scala.util.Properties
66

77
class SipScalaTests extends ScalaCliSuite {
88

9-
def noDirectoriesCommandTest(binaryName: String): Unit =
10-
TestInputs.empty.fromRoot { root =>
11-
val cliPath = os.Path(TestUtil.cliPath, os.pwd)
9+
implicit class BinaryNameOps(binaryName: String) {
10+
def prepareBinary(root: os.Path): os.Path = {
11+
val cliPath = os.Path(TestUtil.cliPath, os.pwd)
12+
val ext = if (Properties.isWin) ".exe" else ""
13+
val newCliPath = root / s"$binaryName$ext"
14+
os.copy(cliPath, newCliPath)
15+
if (!Properties.isWin) os.perms.set(newCliPath, "rwxr-xr-x")
16+
newCliPath
17+
}
1218

13-
os.proc(cliPath, "directories").call(cwd = root)
14-
os.proc(cliPath, "compile", "--help").call(cwd = root)
15-
os.proc(cliPath, "run", "--help").call(cwd = root)
19+
def isSip: Boolean = binaryName == "scala" || binaryName.endsWith("sip")
20+
}
1621

17-
os.copy(cliPath, root / binaryName)
18-
os.perms.set(root / binaryName, "rwxr-xr-x")
22+
def testDirectoriesCommand(binaryName: String): Unit =
23+
TestInputs.empty.fromRoot { root =>
24+
val binary = binaryName.prepareBinary(root)
1925

20-
os.proc(root / binaryName, "compile", "--help").call(cwd = root)
21-
os.proc(root / binaryName, "run", "--help").call(cwd = root)
26+
os.proc(binary, "compile", "--help").call(cwd = root)
27+
os.proc(binary, "run", "--help").call(cwd = root)
2228

23-
val res = os.proc(root / binaryName, "directories").call(
29+
val res = os.proc(binary, "directories").call(
2430
cwd = root,
2531
check = false,
2632
mergeErrIntoOut = true
2733
)
28-
expect(res.exitCode == 1)
29-
val output = res.out.text()
30-
expect(output.contains(s"directories: not found"))
31-
34+
if (binaryName.isSip) {
35+
expect(res.exitCode == 1)
36+
val output = res.out.text()
37+
expect(output.contains(s"directories: not found"))
38+
}
39+
else expect(res.exitCode == 0)
3240
}
3341

34-
def noPublishDirectives(): Unit = TestInputs.empty.fromRoot { root =>
42+
def testPublishDirectives(binaryName: String): Unit = TestInputs.empty.fromRoot { root =>
3543
val code =
3644
"""
3745
| //> using publish.name "my-library"
@@ -41,24 +49,24 @@ class SipScalaTests extends ScalaCliSuite {
4149
val source = root / "A.scala"
4250
os.write(source, code)
4351

44-
val cliPath = os.Path(TestUtil.cliPath, os.pwd)
52+
val binary = binaryName.prepareBinary(root)
4553

46-
os.proc(cliPath, "compile", source).call(cwd = root)
47-
48-
os.copy(cliPath, root / "scala")
49-
os.perms.set(root / "scala", "rwxr-xr-x")
50-
51-
val res = os.proc(root / "scala", "compile", source).call(
54+
val res = os.proc(binary, "compile", source).call(
5255
cwd = root,
5356
check = false,
5457
mergeErrIntoOut = true
5558
)
56-
expect(res.exitCode == 1)
57-
val output = res.out.text()
58-
expect(output.contains(s"directive is not supported"))
59+
60+
if (binaryName.isSip) {
61+
expect(res.exitCode == 1)
62+
val output = res.out.text()
63+
expect(output.contains(s"directive is not supported"))
64+
}
65+
else
66+
expect(res.exitCode == 0)
5967
}
6068

61-
def noMarkdownOptions(): Unit = TestInputs.empty.fromRoot { root =>
69+
def testMarkdownOptions(binaryName: String): Unit = TestInputs.empty.fromRoot { root =>
6270
val code =
6371
"""
6472
| println("ala")
@@ -67,90 +75,64 @@ class SipScalaTests extends ScalaCliSuite {
6775
val source = root / "A.sc"
6876
os.write(source, code)
6977

70-
val cliPath = os.Path(TestUtil.cliPath, os.pwd)
78+
val binary = binaryName.prepareBinary(root)
7179

72-
os.proc(cliPath, "--markdown", source).call(cwd = root)
73-
74-
os.copy(cliPath, root / "scala")
75-
os.perms.set(root / "scala", "rwxr-xr-x")
76-
77-
val res = os.proc(root / "scala", "--markdown", source).call(
80+
val res = os.proc(binary, "--markdown", source).call(
7881
cwd = root,
7982
check = false,
8083
mergeErrIntoOut = true
8184
)
82-
expect(res.exitCode == 1)
83-
val output = res.out.text()
84-
expect(output.contains(s"option is not supported"))
85-
}
86-
87-
if (TestUtil.isNativeCli && !Properties.isWin) {
88-
test("no directories command when run as scala") {
89-
noDirectoriesCommandTest("scala")
90-
}
91-
test("no directories command when run as scala-cli-sip") {
92-
noDirectoriesCommandTest("scala-cli-sip")
93-
}
94-
95-
test("no publish directives when run as scala") {
96-
noPublishDirectives()
97-
}
98-
99-
test("no markdown option when run as scala") {
100-
noMarkdownOptions()
85+
if (binaryName.isSip) {
86+
expect(res.exitCode == 1)
87+
val output = res.out.text()
88+
expect(output.contains(s"option is not supported"))
10189
}
90+
else expect(res.exitCode == 0)
10291
}
10392

104-
def runVersionCommand(binaryName: String) =
93+
def testVersionCommand(binaryName: String): Unit =
10594
TestInputs.empty.fromRoot { root =>
106-
val cliPath = os.Path(TestUtil.cliPath, os.pwd)
107-
val ext = if (Properties.isWin) ".exe" else ""
108-
val newCliPath = root / s"$binaryName$ext"
109-
os.copy(cliPath, newCliPath)
110-
95+
val binary = binaryName.prepareBinary(root)
11196
for { versionOption <- Seq("version", "-version", "--version") } {
112-
val version = os.proc(newCliPath, versionOption).call(check = false)
97+
val version = os.proc(binary, versionOption).call(check = false)
11398
assert(
11499
version.exitCode == 0,
115100
clues(version, version.out.text(), version.err.text(), version.exitCode)
116101
)
117102
val expectedLauncherVersion =
118-
if (binaryName == "scala") "Scala code runner version:"
103+
if (binaryName.isSip) "Scala code runner version:"
119104
else "Scala CLI version:"
120105
expect(version.out.text().contains(expectedLauncherVersion))
121106
expect(version.out.text().contains(s"Scala version (default): ${Constants.defaultScala}"))
122107
}
123108
}
124109

125-
def checkHelp(binaryName: String): Unit = TestInputs.empty.fromRoot { root =>
126-
val cliPath = os.Path(TestUtil.cliPath, os.pwd)
127-
val ext = if (Properties.isWin) ".exe" else ""
128-
val newCliPath = root / s"$binaryName$ext"
129-
os.copy(cliPath, newCliPath)
130-
110+
def testHelpOutput(binaryName: String): Unit = TestInputs.empty.fromRoot { root =>
111+
val binary = binaryName.prepareBinary(root)
131112
for { helpOption <- Seq("help", "-help", "--help") } {
132-
val res = os.proc(newCliPath, helpOption).call(cwd = root)
113+
val res = os.proc(binary, helpOption).call(cwd = root)
133114
val restrictedFeaturesMentioned = res.out.trim().contains("package")
134-
if (binaryName == "scala") expect(!restrictedFeaturesMentioned)
115+
if (binaryName.isSip) expect(!restrictedFeaturesMentioned)
135116
else expect(restrictedFeaturesMentioned)
136117
}
137118
}
138119

139-
if (TestUtil.isNativeCli) {
140-
test("version command print detailed info run as scala") {
141-
runVersionCommand("scala")
142-
}
143-
144-
test("version command print detailed info run as scala-cli") {
145-
runVersionCommand("scala-cli")
146-
}
147-
148-
test("help command mentions non-restricted features only when run as scala") {
149-
checkHelp("scala")
150-
}
151-
152-
test("help command mentions all core features when run as scala") {
153-
checkHelp("scala-cli")
120+
if (TestUtil.isNativeCli)
121+
for (binaryName <- Seq("scala", "scala-cli", "scala-cli-sip")) {
122+
test(s"test directories command when run as $binaryName") {
123+
testDirectoriesCommand(binaryName)
124+
}
125+
test(s"test publish directives when run as $binaryName") {
126+
testPublishDirectives(binaryName)
127+
}
128+
test(s"test markdown options when run as $binaryName") {
129+
testMarkdownOptions(binaryName)
130+
}
131+
test(s"test version command when run as $binaryName") {
132+
testVersionCommand(binaryName)
133+
}
134+
test(s"test help when run as $binaryName") {
135+
testHelpOutput(binaryName)
136+
}
154137
}
155-
}
156138
}

0 commit comments

Comments
 (0)