@@ -6,8 +6,7 @@ import java.io.{ByteArrayOutputStream, File}
6
6
import java .nio .charset .Charset
7
7
8
8
import scala .cli .integration .util .DockerServer
9
- import scala .concurrent .ExecutionContext
10
- import scala .concurrent .duration .Duration
9
+ import scala .concurrent .duration .DurationInt
11
10
import scala .io .Codec
12
11
import scala .jdk .CollectionConverters .*
13
12
import scala .util .Properties
@@ -1509,14 +1508,12 @@ abstract class RunTestDefinitions
1509
1508
1510
1509
test(" watch with interactive, with multiple main classes" ) {
1511
1510
val fileName = " watch.scala"
1512
-
1513
- val inputs = TestInputs (
1511
+ TestInputs (
1514
1512
os.rel / fileName ->
1515
1513
""" object Run1 extends App {println("Run1 launched")}
1516
1514
|object Run2 extends App {println("Run2 launched")}
1517
1515
|""" .stripMargin
1518
- )
1519
- inputs.fromRoot { root =>
1516
+ ).fromRoot { root =>
1520
1517
val confDir = root / " config"
1521
1518
val confFile = confDir / " test-config.json"
1522
1519
@@ -1527,76 +1524,66 @@ abstract class RunTestDefinitions
1527
1524
1528
1525
val configEnv = Map (" SCALA_CLI_CONFIG" -> confFile.toString)
1529
1526
1530
- val proc = os.proc(TestUtil .cli, " run" , " --watch" , " --interactive" , fileName)
1531
- .spawn(
1532
- cwd = root,
1533
- mergeErrIntoOut = true ,
1534
- stdout = os.Pipe ,
1535
- stdin = os.Pipe ,
1536
- env = Map (" SCALA_CLI_INTERACTIVE" -> " true" ) ++ configEnv
1537
- )
1538
-
1539
- try
1540
- TestUtil .withThreadPool(" run-watch-interactive-multi-main-class-test" , 2 ) { pool =>
1541
- val timeout = Duration (" 60 seconds" )
1542
- implicit val ec = ExecutionContext .fromExecutorService(pool)
1543
-
1544
- def lineReaderIter = Iterator .continually {
1545
- val line = TestUtil .readLine(proc.stdout, ec, timeout)
1546
- println(s " Line read: $line" )
1547
- line
1548
- }
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
+ }
1549
1543
1550
- def checkLinesForError (lines : Seq [String ]) = munit.Assertions .assert(
1551
- ! lines.exists { line =>
1552
- TestUtil .removeAnsiColors(line).contains(" [error]" )
1553
- },
1554
- clues(lines.toSeq)
1555
- )
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
+ )
1556
1550
1557
- def answerInteractivePrompt (id : Int ) = {
1558
- val interactivePromptLines = lineReaderIter
1559
- .takeWhile(! _.startsWith(" [1]" /* probably [1] Run2 or [1] No*/ ))
1560
- .toList
1561
- expect(interactivePromptLines.nonEmpty)
1562
- checkLinesForError(interactivePromptLines)
1563
- proc.stdin.write(s " $id\n " )
1564
- proc.stdin.flush()
1565
- }
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
+ }
1566
1560
1567
- def analyzeRunOutput (restart : Boolean ) = {
1568
- val runResultLines = lineReaderIter
1569
- .takeWhile(! _.contains(" press Enter to re-run" ))
1570
- .toList
1571
- expect(runResultLines.nonEmpty)
1572
- checkLinesForError(runResultLines)
1573
- if (restart)
1574
- proc.stdin.write(" \n " )
1575
- proc.stdin.flush()
1576
- }
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
+ }
1577
1571
1578
- // You have run the current scala-cli command with the --interactive mode turned on.
1579
- // Would you like to leave it on permanently?
1580
- answerInteractivePrompt(0 )
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 )
1581
1575
1582
- // Found several main classes. Which would you like to run?
1583
- answerInteractivePrompt(0 )
1584
- expect(TestUtil .readLine(proc.stdout, ec, timeout) == " Run1 launched" )
1576
+ // Found several main classes. Which would you like to run?
1577
+ answerInteractivePrompt(0 )
1578
+ expect(TestUtil .readLine(proc.stdout, ec, timeout) == " Run1 launched" )
1585
1579
1586
- analyzeRunOutput( /* restart */ true )
1580
+ analyzeRunOutput( /* restart */ true )
1587
1581
1588
- answerInteractivePrompt(1 )
1589
- expect(TestUtil .readLine(proc.stdout, ec, timeout) == " Run2 launched" )
1582
+ answerInteractivePrompt(1 )
1583
+ expect(TestUtil .readLine(proc.stdout, ec, timeout) == " Run2 launched" )
1590
1584
1591
- analyzeRunOutput( /* restart */ false )
1592
- }
1593
- finally
1594
- if (proc.isAlive()) {
1595
- proc.destroy()
1596
- Thread .sleep(200L )
1597
- if (proc.isAlive())
1598
- proc.destroyForcibly()
1599
- }
1585
+ analyzeRunOutput( /* restart */ false )
1586
+ }
1600
1587
}
1601
1588
}
1602
1589
@@ -2360,31 +2347,22 @@ abstract class RunTestDefinitions
2360
2347
val inputPath = os.rel / " smth.scala"
2361
2348
TestInputs (inputPath -> s """ object Smth extends App { println(" $expectedMessage1") } """ )
2362
2349
.fromRoot { root =>
2363
- val proc = os.proc(TestUtil .cli, " run" , " ." , " --watch" , extraOptions)
2364
- .spawn(cwd = root, stderr = os.Pipe )
2365
- try
2366
- TestUtil .withThreadPool(" simple-watch-scala-source-test" , 2 ) { pool =>
2367
- val timeout = Duration (" 90 seconds" )
2368
- val ec = ExecutionContext .fromExecutorService(pool)
2369
- val output1 = TestUtil .readLine(proc.stdout, ec, timeout)
2370
- expect(output1 == expectedMessage1)
2371
- val expectedMessage2 = " World"
2372
- while (! TestUtil .readLine(proc.stderr, ec, timeout).contains(" re-run" ))
2373
- Thread .sleep(100L )
2374
- os.write.over(
2375
- root / inputPath,
2376
- s """ object Smth extends App { println(" $expectedMessage2") } """ .stripMargin
2377
- )
2378
- val output2 = TestUtil .readLine(proc.stdout, ec, timeout)
2379
- expect(output2 == expectedMessage2)
2380
- }
2381
- finally
2382
- if (proc.isAlive()) {
2383
- proc.destroy()
2384
- Thread .sleep(200L )
2385
- if (proc.isAlive())
2386
- proc.destroyForcibly()
2387
- }
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
+ }
2388
2366
}
2389
2367
}
2390
2368
}
0 commit comments