Skip to content

Commit 9132c1d

Browse files
authored
Merge pull request #1172 from Gedochao/fix-snippets-for-windows
Fix snippet tests for Windows
2 parents 8ba155b + 961a950 commit 9132c1d

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,22 +2004,24 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
20042004

20052005
test("correctly run a script snippet") {
20062006
emptyInputs.fromRoot { root =>
2007-
val msg =
2008-
"123456" // FIXME: change this to a a non-numeric string when Windows encoding is handled properly
2009-
val res = os.proc(TestUtil.cli, "-e", s"println($msg)", extraOptions).call(cwd = root)
2007+
val msg = "Hello world"
2008+
val quotation = TestUtil.argQuotationMark
2009+
val res =
2010+
os.proc(TestUtil.cli, "-e", s"println($quotation$msg$quotation)", extraOptions)
2011+
.call(cwd = root)
20102012
expect(res.out.text().trim == msg)
20112013
}
20122014
}
20132015

20142016
test("correctly run a scala snippet") {
20152017
emptyInputs.fromRoot { root =>
2016-
val msg =
2017-
"123456" // FIXME: change this to a a non-numeric string when Windows encoding is handled properly
2018+
val msg = "Hello world"
2019+
val quotation = TestUtil.argQuotationMark
20182020
val res =
20192021
os.proc(
20202022
TestUtil.cli,
20212023
"--scala-snippet",
2022-
s"object Hello extends App { println($msg) }",
2024+
s"object Hello extends App { println($quotation$msg$quotation) }",
20232025
extraOptions
20242026
)
20252027
.call(cwd = root)
@@ -2029,12 +2031,12 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
20292031

20302032
test("correctly run a java snippet") {
20312033
emptyInputs.fromRoot { root =>
2032-
val msg =
2033-
"123456" // FIXME: change this to a a non-numeric string when Windows encoding is handled properly
2034+
val quotation = TestUtil.argQuotationMark
2035+
val msg = "Hello world"
20342036
val res = os.proc(
20352037
TestUtil.cli,
20362038
"--java-snippet",
2037-
s"public class Main { public static void main(String[] args) { System.out.println($msg); } }",
2039+
s"public class Main { public static void main(String[] args) { System.out.println($quotation$msg$quotation); } }",
20382040
extraOptions
20392041
)
20402042
.call(cwd = root)

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scala.cli.integration
22

3+
import os.{CommandResult, Path}
4+
35
import java.io.File
46
import java.util.concurrent.atomic.AtomicInteger
57
import java.util.concurrent.{ExecutorService, Executors, ScheduledExecutorService, ThreadFactory}
@@ -10,11 +12,11 @@ import scala.util.Properties
1012

1113
object TestUtil {
1214

13-
val cliKind = sys.props("test.scala-cli.kind")
14-
val isNativeCli = cliKind.startsWith("native")
15-
val isCI = System.getenv("CI") != null
16-
val cliPath = sys.props("test.scala-cli.path")
17-
val cli = cliCommand(cliPath)
15+
val cliKind: String = sys.props("test.scala-cli.kind")
16+
val isNativeCli: Boolean = cliKind.startsWith("native")
17+
val isCI: Boolean = System.getenv("CI") != null
18+
val cliPath: String = sys.props("test.scala-cli.path")
19+
val cli: Seq[String] = cliCommand(cliPath)
1820

1921
def cliCommand(cliPath: String): Seq[String] =
2022
if (isNativeCli)
@@ -23,7 +25,7 @@ object TestUtil {
2325
Seq("java", "-Xmx512m", "-Xms128m", "-jar", cliPath)
2426

2527
// format: off
26-
val extraOptions = List(
28+
val extraOptions: List[String] = List(
2729
"--bloop-startup-timeout", "2min",
2830
"--bloop-bsp-timeout", "1min"
2931
)
@@ -58,7 +60,7 @@ object TestUtil {
5860
.map(_.getAbsolutePath)
5961
}
6062

61-
def cs = Constants.cs
63+
def cs: String = Constants.cs
6264

6365
def threadPool(prefix: String, size: Int): ExecutorService =
6466
Executors.newFixedThreadPool(size, daemonThreadFactory(prefix))
@@ -68,9 +70,9 @@ object TestUtil {
6870

6971
private def daemonThreadFactory(prefix: String): ThreadFactory =
7072
new ThreadFactory {
71-
val counter = new AtomicInteger
72-
def threadNumber() = counter.incrementAndGet()
73-
def newThread(r: Runnable) =
73+
val counter = new AtomicInteger
74+
def threadNumber(): Int = counter.incrementAndGet()
75+
def newThread(r: Runnable): Thread =
7476
new Thread(r, s"$prefix-thread-${threadNumber()}") {
7577
setDaemon(true)
7678
setPriority(Thread.NORM_PRIORITY)
@@ -105,21 +107,27 @@ object TestUtil {
105107

106108
def retryOnCi[T](maxAttempts: Int = 3, waitDuration: FiniteDuration = 5.seconds)(
107109
run: => T
108-
) = retry(if (isCI) maxAttempts else 1, waitDuration)(run)
110+
): T = retry(if (isCI) maxAttempts else 1, waitDuration)(run)
109111

110112
// Same as os.RelPath.toString, but for the use of File.separator instead of "/"
111113
def relPathStr(relPath: os.RelPath): String =
112114
(Seq.fill(relPath.ups)("..") ++ relPath.segments).mkString(File.separator)
113115

114-
def kill(pid: Int) =
116+
def kill(pid: Int): CommandResult =
115117
if (Properties.isWin)
116118
os.proc("taskkill", "/F", "/PID", pid).call()
117119
else
118120
os.proc("kill", pid).call()
119121

120-
def pwd =
122+
def pwd: Path =
121123
if (Properties.isWin)
122124
os.Path(os.pwd.toIO.getCanonicalFile)
123125
else
124126
os.pwd
127+
128+
/** @return
129+
* 2 quotation marks (") when run for Windows, or a single one otherwise. This is necessary to
130+
* escape the quotation marks passed in args for the Windows command line.
131+
*/
132+
def argQuotationMark: String = if (Properties.isWin) "\"\"" else "\""
125133
}

0 commit comments

Comments
 (0)