Skip to content

Commit 7a926cb

Browse files
authored
Merge pull request #3209 from Gedochao/maintenance/refactor-repl-tests
NIT Extract REPL tests relying on Ammonite into dedicated traits
2 parents 646e2be + 498b051 commit 7a926cb

File tree

8 files changed

+328
-319
lines changed

8 files changed

+328
-319
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
package scala.cli.integration
2+
3+
import com.eed3si9n.expecty.Expecty.expect
4+
5+
import scala.cli.integration.TestUtil.removeAnsiColors
6+
import scala.util.Properties
7+
8+
trait ReplAmmoniteTestDefinitions { _: ReplTestDefinitions =>
9+
protected val retrieveScalaVersionCode: String = if (actualScalaVersion.startsWith("2."))
10+
"scala.util.Properties.versionNumberString"
11+
else "dotty.tools.dotc.config.Properties.simpleVersionString"
12+
13+
def expectedScalaVersionForAmmonite: String =
14+
actualScalaVersion match {
15+
case s
16+
if s.startsWith("2.12") &&
17+
Constants.maxAmmoniteScala212Version.coursierVersion < s.coursierVersion =>
18+
Constants.maxAmmoniteScala212Version
19+
case s
20+
if s.startsWith("2.13") &&
21+
Constants.maxAmmoniteScala213Version.coursierVersion < s.coursierVersion =>
22+
Constants.maxAmmoniteScala213Version
23+
case s
24+
if s.startsWith("3") &&
25+
Constants.maxAmmoniteScala3Version.coursierVersion < s.coursierVersion =>
26+
Constants.maxAmmoniteScala3Version
27+
case s => s
28+
}
29+
30+
def actualMaxAmmoniteScalaVersion: String =
31+
if (actualScalaVersion.startsWith("3")) Constants.maxAmmoniteScala3Version
32+
else if (actualScalaVersion.startsWith("2.13")) Constants.maxAmmoniteScala213Version
33+
else Constants.maxAmmoniteScala212Version
34+
35+
def ammoniteExtraOptions: Seq[String] =
36+
Seq("--scala", actualMaxAmmoniteScalaVersion) ++ TestUtil.extraOptions
37+
38+
def ammoniteTest(useMaxAmmoniteScalaVersion: Boolean): Unit = {
39+
TestInputs.empty.fromRoot { root =>
40+
val testExtraOptions = if (useMaxAmmoniteScalaVersion) ammoniteExtraOptions else extraOptions
41+
val ammArgs = Seq(
42+
"-c",
43+
s"""println("Hello" + " from Scala " + $retrieveScalaVersionCode)"""
44+
)
45+
.map {
46+
if (Properties.isWin)
47+
a => if (a.contains(" ")) "\"" + a.replace("\"", "\\\"") + "\"" else a
48+
else
49+
identity
50+
}
51+
.flatMap(arg => Seq("--ammonite-arg", arg))
52+
val res =
53+
os.proc(TestUtil.cli, "--power", "repl", testExtraOptions, "--ammonite", ammArgs)
54+
.call(cwd = root, stderr = os.Pipe)
55+
val output = res.out.trim()
56+
val expectedSv =
57+
if (useMaxAmmoniteScalaVersion) actualMaxAmmoniteScalaVersion
58+
else expectedScalaVersionForAmmonite
59+
expect(output == s"Hello from Scala $expectedSv")
60+
if (useMaxAmmoniteScalaVersion) {
61+
// the maximum Scala version supported by ammonite is being used, so we shouldn't downgrade
62+
val errOutput = res.err.trim()
63+
expect(!errOutput.contains("not yet supported with this version of Ammonite"))
64+
}
65+
}
66+
}
67+
68+
def ammoniteTestScope(useMaxAmmoniteScalaVersion: Boolean): Unit = {
69+
val message = "something something ammonite"
70+
TestInputs(
71+
os.rel / "example" / "TestScopeExample.test.scala" ->
72+
s"""package example
73+
|
74+
|object TestScopeExample {
75+
| def message: String = "$message"
76+
|}
77+
|""".stripMargin
78+
).fromRoot { root =>
79+
val testExtraOptions = if (useMaxAmmoniteScalaVersion) ammoniteExtraOptions else extraOptions
80+
val ammArgs = Seq("-c", "println(example.TestScopeExample.message)")
81+
.map {
82+
if (Properties.isWin)
83+
a => if (a.contains(" ")) "\"" + a.replace("\"", "\\\"") + "\"" else a
84+
else
85+
identity
86+
}
87+
.flatMap(arg => Seq("--ammonite-arg", arg))
88+
val res =
89+
os.proc(
90+
TestUtil.cli,
91+
"--power",
92+
"repl",
93+
".",
94+
testExtraOptions,
95+
"--test",
96+
"--ammonite",
97+
ammArgs
98+
)
99+
.call(cwd = root, stderr = os.Pipe)
100+
val output = res.out.trim()
101+
expect(output == message)
102+
if (useMaxAmmoniteScalaVersion) {
103+
// the maximum Scala version supported by ammonite is being used, so we shouldn't downgrade
104+
val errOutput = res.err.trim()
105+
expect(!errOutput.contains("not yet supported with this version of Ammonite"))
106+
}
107+
}
108+
}
109+
110+
def ammoniteScalapyTest(useMaxAmmoniteScalaVersion: Boolean): Unit = {
111+
val testExtraOptions = if (useMaxAmmoniteScalaVersion) ammoniteExtraOptions else extraOptions
112+
val inputs = TestInputs(
113+
os.rel / "foo" / "something.py" ->
114+
"""messageStart = 'Hello from'
115+
|messageEnd = 'ScalaPy'
116+
|""".stripMargin
117+
)
118+
inputs.fromRoot { root =>
119+
val ammArgs = Seq(
120+
"-c",
121+
s"""println("Hello" + " from Scala " + $retrieveScalaVersionCode)
122+
|val sth = py.module("foo.something")
123+
|py.Dynamic.global.applyDynamicNamed("print")("" -> sth.messageStart, "" -> sth.messageEnd, "flush" -> py.Any.from(true))
124+
|""".stripMargin
125+
)
126+
.map {
127+
if (Properties.isWin)
128+
a => if (a.contains(" ")) "\"" + a.replace("\"", "\\\"") + "\"" else a
129+
else
130+
identity
131+
}
132+
.flatMap(arg => Seq("--ammonite-arg", arg))
133+
134+
val errorRes = os.proc(
135+
TestUtil.cli,
136+
"--power",
137+
"repl",
138+
testExtraOptions,
139+
"--ammonite",
140+
"--python",
141+
ammArgs
142+
).call(
143+
cwd = root,
144+
env = Map("PYTHONSAFEPATH" -> "foo"),
145+
mergeErrIntoOut = true,
146+
check = false
147+
)
148+
expect(errorRes.exitCode != 0)
149+
val errorOutput = errorRes.out.text()
150+
expect(errorOutput.contains("No module named 'foo'"))
151+
152+
val res = os.proc(
153+
TestUtil.cli,
154+
"--power",
155+
"repl",
156+
testExtraOptions,
157+
"--ammonite",
158+
"--python",
159+
ammArgs
160+
).call(cwd = root, stderr = os.Pipe)
161+
val expectedSv =
162+
if (useMaxAmmoniteScalaVersion) actualMaxAmmoniteScalaVersion
163+
else expectedScalaVersionForAmmonite
164+
val lines = res.out.trim().linesIterator.toVector
165+
expect(lines == Seq(s"Hello from Scala $expectedSv", "Hello from ScalaPy"))
166+
if (useMaxAmmoniteScalaVersion)
167+
// the maximum Scala version supported by ammonite is being used, so we shouldn't downgrade
168+
expect(!res.err.trim().contains("not yet supported with this version of Ammonite"))
169+
}
170+
}
171+
172+
def ammoniteMaxVersionString: String =
173+
if (actualScalaVersion == actualMaxAmmoniteScalaVersion) ""
174+
else s" with Scala $actualMaxAmmoniteScalaVersion (the latest supported version)"
175+
176+
test(s"ammonite$ammoniteMaxVersionString") {
177+
ammoniteTest(useMaxAmmoniteScalaVersion = true)
178+
}
179+
180+
test(s"ammonite scalapy$ammoniteMaxVersionString") {
181+
ammoniteScalapyTest(useMaxAmmoniteScalaVersion = true)
182+
}
183+
184+
test("ammonite with test scope sources") {
185+
ammoniteTestScope(useMaxAmmoniteScalaVersion = true)
186+
}
187+
188+
test("default ammonite version in help") {
189+
TestInputs.empty.fromRoot { root =>
190+
val res = os.proc(TestUtil.cli, "--power", "repl", extraOptions, "--help").call(cwd = root)
191+
val lines = removeAnsiColors(res.out.trim()).linesIterator.toVector
192+
val ammVersionHelp = lines.find(_.contains("--ammonite-ver")).getOrElse("")
193+
expect(ammVersionHelp.contains(s"(${Constants.ammoniteVersion} by default)"))
194+
}
195+
}
196+
197+
def ammoniteWithExtraJarTest(): Unit = {
198+
TestInputs.empty.fromRoot { root =>
199+
val ammArgs = Seq(
200+
"-c",
201+
"""import shapeless._; println("Here's an HList: " + (2 :: true :: "a" :: HNil))"""
202+
)
203+
.map {
204+
if (Properties.isWin)
205+
a => if (a.contains(" ")) "\"" + a.replace("\"", "\\\"") + "\"" else a
206+
else
207+
identity
208+
}
209+
.flatMap(arg => Seq("--ammonite-arg", arg))
210+
val shapelessJar =
211+
os.proc(TestUtil.cs, "fetch", "--intransitive", "com.chuusai:shapeless_2.13:2.3.7")
212+
.call()
213+
.out
214+
.text()
215+
.trim
216+
val cmd = Seq[os.Shellable](
217+
TestUtil.cli,
218+
"--power",
219+
"repl",
220+
ammoniteExtraOptions,
221+
"--jar",
222+
shapelessJar,
223+
"--ammonite",
224+
ammArgs
225+
)
226+
val res = os.proc(cmd).call(cwd = root)
227+
val output = res.out.trim()
228+
expect(output == "Here's an HList: 2 :: true :: a :: HNil")
229+
}
230+
}
231+
232+
if (actualScalaVersion.startsWith("2.13"))
233+
test(s"ammonite with extra JAR$ammoniteMaxVersionString") {
234+
ammoniteWithExtraJarTest()
235+
}
236+
}

modules/integration/src/test/scala/scala/cli/integration/ReplTests3StableDefinitions.scala renamed to modules/integration/src/test/scala/scala/cli/integration/ReplAmmoniteTests3StableDefinitions.scala

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import com.eed3si9n.expecty.Expecty.expect
44

55
import scala.util.Properties
66

7-
trait ReplTests3StableDefinitions { _: ReplTestDefinitions =>
7+
trait ReplAmmoniteTests3StableDefinitions {
8+
_: ReplTestDefinitions with ReplAmmoniteTestDefinitions =>
89
if (!actualScalaVersion.equals(actualMaxAmmoniteScalaVersion)) {
910
lazy val defaultScalaVersionString =
1011
s" with Scala $actualScalaVersion (the default version, may downgrade)"
@@ -47,4 +48,55 @@ trait ReplTests3StableDefinitions { _: ReplTestDefinitions =>
4748
expect(res.out.trim().nonEmpty)
4849
}
4950
}
51+
52+
test("as jar") {
53+
val inputs = TestInputs(
54+
os.rel / "CheckCp.scala" ->
55+
"""//> using lib "com.lihaoyi::os-lib:0.9.1"
56+
|package checkcp
57+
|object CheckCp {
58+
| def hasDir: Boolean =
59+
| sys.props("java.class.path")
60+
| .split(java.io.File.pathSeparator)
61+
| .toVector
62+
| .map(os.Path(_, os.pwd))
63+
| .exists(os.isDir(_))
64+
|}
65+
|""".stripMargin
66+
)
67+
68+
inputs.fromRoot { root =>
69+
val ammArgs = Seq(
70+
"-c",
71+
"""println("hasDir=" + checkcp.CheckCp.hasDir)"""
72+
)
73+
.map {
74+
if (Properties.isWin)
75+
a => if (a.contains(" ")) "\"" + a.replace("\"", "\\\"") + "\"" else a
76+
else
77+
identity
78+
}
79+
.flatMap(arg => Seq("--ammonite-arg", arg))
80+
81+
val output =
82+
os.proc(TestUtil.cli, "--power", "repl", ".", TestUtil.extraOptions, "--ammonite", ammArgs)
83+
.call(cwd = root)
84+
.out.trim()
85+
expect(output == "hasDir=true")
86+
87+
val asJarOutput = os.proc(
88+
TestUtil.cli,
89+
"--power",
90+
"repl",
91+
".",
92+
TestUtil.extraOptions,
93+
"--ammonite",
94+
ammArgs,
95+
"--as-jar"
96+
)
97+
.call(cwd = root)
98+
.out.trim()
99+
expect(asJarOutput == "hasDir=false")
100+
}
101+
}
50102
}

0 commit comments

Comments
 (0)