Skip to content

Commit 2a37018

Browse files
committed
NIT Extract existing --watch tests to a dedicated trait and only run them for the default Scala version
1 parent ecee151 commit 2a37018

File tree

3 files changed

+227
-216
lines changed

3 files changed

+227
-216
lines changed

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

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import java.io.{ByteArrayOutputStream, File}
66
import java.nio.charset.Charset
77

88
import scala.cli.integration.util.DockerServer
9-
import scala.concurrent.duration.DurationInt
109
import scala.io.Codec
1110
import scala.jdk.CollectionConverters.*
1211
import scala.util.Properties
@@ -1506,87 +1505,6 @@ abstract class RunTestDefinitions
15061505
}
15071506
}
15081507

1509-
test("watch with interactive, with multiple main classes") {
1510-
val fileName = "watch.scala"
1511-
TestInputs(
1512-
os.rel / fileName ->
1513-
"""object Run1 extends App {println("Run1 launched")}
1514-
|object Run2 extends App {println("Run2 launched")}
1515-
|""".stripMargin
1516-
).fromRoot { root =>
1517-
val confDir = root / "config"
1518-
val confFile = confDir / "test-config.json"
1519-
1520-
os.write(confFile, "{}", createFolders = true)
1521-
1522-
if (!Properties.isWin)
1523-
os.perms.set(confDir, "rwx------")
1524-
1525-
val configEnv = Map("SCALA_CLI_CONFIG" -> confFile.toString)
1526-
1527-
TestUtil.withProcessWatching(
1528-
proc = os.proc(TestUtil.cli, "run", "--watch", "--interactive", fileName)
1529-
.spawn(
1530-
cwd = root,
1531-
mergeErrIntoOut = true,
1532-
stdout = os.Pipe,
1533-
stdin = os.Pipe,
1534-
env = Map("SCALA_CLI_INTERACTIVE" -> "true") ++ configEnv
1535-
),
1536-
timeout = 60.seconds
1537-
) { (proc, timeout, ec) =>
1538-
def lineReaderIter: Iterator[String] = Iterator.continually {
1539-
val line = TestUtil.readLine(proc.stdout, ec, timeout)
1540-
println(s"Line read: $line")
1541-
line
1542-
}
1543-
1544-
def checkLinesForError(lines: Seq[String]): Unit = munit.Assertions.assert(
1545-
!lines.exists { line =>
1546-
TestUtil.removeAnsiColors(line).contains("[error]")
1547-
},
1548-
clues(lines.toSeq)
1549-
)
1550-
1551-
def answerInteractivePrompt(id: Int): Unit = {
1552-
val interactivePromptLines = lineReaderIter
1553-
.takeWhile(!_.startsWith("[1]" /* probably [1] Run2 or [1] No*/ ))
1554-
.toList
1555-
expect(interactivePromptLines.nonEmpty)
1556-
checkLinesForError(interactivePromptLines)
1557-
proc.stdin.write(s"$id\n")
1558-
proc.stdin.flush()
1559-
}
1560-
1561-
def analyzeRunOutput(restart: Boolean): Unit = {
1562-
val runResultLines = lineReaderIter
1563-
.takeWhile(!_.contains("press Enter to re-run"))
1564-
.toList
1565-
expect(runResultLines.nonEmpty)
1566-
checkLinesForError(runResultLines)
1567-
if (restart)
1568-
proc.stdin.write("\n")
1569-
proc.stdin.flush()
1570-
}
1571-
1572-
// You have run the current scala-cli command with the --interactive mode turned on.
1573-
// Would you like to leave it on permanently?
1574-
answerInteractivePrompt(0)
1575-
1576-
// Found several main classes. Which would you like to run?
1577-
answerInteractivePrompt(0)
1578-
expect(TestUtil.readLine(proc.stdout, ec, timeout) == "Run1 launched")
1579-
1580-
analyzeRunOutput( /* restart */ true)
1581-
1582-
answerInteractivePrompt(1)
1583-
expect(TestUtil.readLine(proc.stdout, ec, timeout) == "Run2 launched")
1584-
1585-
analyzeRunOutput( /* restart */ false)
1586-
}
1587-
}
1588-
}
1589-
15901508
test("decoded classNames in interactive ask") {
15911509
val fileName = "watch.scala"
15921510

@@ -2341,28 +2259,4 @@ abstract class RunTestDefinitions
23412259
}
23422260
}
23432261
}
2344-
2345-
test("simple --watch .scala source") {
2346-
val expectedMessage1 = "Hello"
2347-
val inputPath = os.rel / "smth.scala"
2348-
TestInputs(inputPath -> s"""object Smth extends App { println("$expectedMessage1") }""")
2349-
.fromRoot { root =>
2350-
TestUtil.withProcessWatching(
2351-
os.proc(TestUtil.cli, "run", ".", "--watch", extraOptions)
2352-
.spawn(cwd = root, stderr = os.Pipe)
2353-
) { (proc, timeout, ec) =>
2354-
val output1 = TestUtil.readLine(proc.stdout, ec, timeout)
2355-
expect(output1 == expectedMessage1)
2356-
val expectedMessage2 = "World"
2357-
while (!TestUtil.readLine(proc.stderr, ec, timeout).contains("re-run"))
2358-
Thread.sleep(100L)
2359-
os.write.over(
2360-
root / inputPath,
2361-
s"""object Smth extends App { println("$expectedMessage2") }""".stripMargin
2362-
)
2363-
val output2 = TestUtil.readLine(proc.stdout, ec, timeout)
2364-
expect(output2 == expectedMessage2)
2365-
}
2366-
}
2367-
}
23682262
}

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

Lines changed: 4 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package scala.cli.integration
22

33
import com.eed3si9n.expecty.Expecty.expect
44

5-
import scala.concurrent.duration.DurationInt
6-
import scala.util.{Properties, Try}
5+
import scala.util.Properties
76

8-
class RunTestsDefault extends RunTestDefinitions with TestDefault {
7+
class RunTestsDefault extends RunTestDefinitions
8+
with RunWithWatchTestDefinitions
9+
with TestDefault {
910
def archLinuxTest(): Unit = {
1011
val message = "Hello from Scala CLI on Arch Linux"
1112
val inputs = TestInputs(
@@ -68,113 +69,6 @@ class RunTestsDefault extends RunTestDefinitions with TestDefault {
6869
}
6970
}
7071

71-
if (!Properties.isMac || !TestUtil.isNativeCli || !TestUtil.isCI)
72-
// TODO make this pass reliably on Mac CI
73-
test("watch artifacts") {
74-
val libSourcePath = os.rel / "lib" / "Messages.scala"
75-
def libSource(hello: String) =
76-
s"""//> using publish.organization "test-org"
77-
|//> using publish.name "messages"
78-
|//> using publish.version "0.1.0"
79-
|
80-
|package messages
81-
|
82-
|object Messages {
83-
| def hello(name: String) = s"$hello $$name"
84-
|}
85-
|""".stripMargin
86-
TestInputs(
87-
libSourcePath -> libSource("Hello"),
88-
os.rel / "app" / "TestApp.scala" ->
89-
"""//> using lib "test-org::messages:0.1.0"
90-
|
91-
|package testapp
92-
|
93-
|import messages.Messages
94-
|
95-
|@main
96-
|def run(): Unit =
97-
| println(Messages.hello("user"))
98-
|""".stripMargin
99-
).fromRoot { root =>
100-
val testRepo = root / "test-repo"
101-
102-
def publishLib(): Unit =
103-
os.proc(
104-
TestUtil.cli,
105-
"--power",
106-
"publish",
107-
"--offline",
108-
"--publish-repo",
109-
testRepo,
110-
"lib"
111-
)
112-
.call(cwd = root)
113-
114-
publishLib()
115-
116-
TestUtil.withProcessWatching(
117-
os.proc(
118-
TestUtil.cli,
119-
"--power",
120-
"run",
121-
"--offline",
122-
"app",
123-
"-w",
124-
"-r",
125-
testRepo.toNIO.toUri.toASCIIString
126-
).spawn(cwd = root)
127-
) { (proc, timeout, ec) =>
128-
val output = TestUtil.readLine(proc.stdout, ec, timeout)
129-
expect(output == "Hello user")
130-
131-
os.write.over(root / libSourcePath, libSource("Hola"))
132-
publishLib()
133-
134-
val secondOutput = TestUtil.readLine(proc.stdout, ec, timeout)
135-
expect(secondOutput == "Hola user")
136-
}
137-
}
138-
}
139-
140-
test("watch test - no infinite loop") {
141-
val fileName = "watch.scala"
142-
TestInputs(
143-
os.rel / fileName ->
144-
"""//> using lib "org.scalameta::munit::0.7.29"
145-
|
146-
|class MyTests extends munit.FunSuite {
147-
| test("is true true") { assert(true) }
148-
|}
149-
|""".stripMargin
150-
).fromRoot { root =>
151-
TestUtil.withProcessWatching(
152-
proc = os.proc(TestUtil.cli, "test", "-w", "watch.scala")
153-
.spawn(cwd = root, mergeErrIntoOut = true),
154-
timeout = 10.seconds
155-
) { (proc, timeout, ec) =>
156-
val watchingMsg = "Watching sources, press Ctrl+C to exit, or press Enter to re-run."
157-
val testingMsg = "MyTests:"
158-
159-
def lineReadIter = Iterator.continually(Try(TestUtil.readLine(proc.stdout, ec, timeout)))
160-
.takeWhile(_.isSuccess)
161-
.map(_.get)
162-
163-
val beforeAppendOut = lineReadIter.toSeq
164-
expect(beforeAppendOut.count(_.contains(testingMsg)) == 1)
165-
expect(beforeAppendOut.count(_.contains(watchingMsg)) == 1)
166-
expect(beforeAppendOut.last.contains(watchingMsg))
167-
168-
os.write.append(root / fileName, "\n//comment")
169-
170-
val afterAppendOut = lineReadIter.toSeq
171-
expect(afterAppendOut.count(_.contains(testingMsg)) == 1)
172-
expect(afterAppendOut.count(_.contains(watchingMsg)) == 1)
173-
expect(afterAppendOut.last.contains(watchingMsg))
174-
}
175-
}
176-
}
177-
17872
test("as jar") {
17973
val inputs = TestInputs(
18074
os.rel / "CheckCp.scala" ->

0 commit comments

Comments
 (0)