Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows / test-bat-template #1314

Merged
merged 8 commits into from
Mar 10, 2020
36 changes: 22 additions & 14 deletions src/sbt-test/windows/test-bat-template/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ TaskKey[Unit]("checkScript") := {
}
def crlf2cr(txt: String) = txt.trim.replaceAll("\\\r\\\n", "\n")
def checkOutput(testName: String,
args: String,
args: Seq[String],
expected: String,
env: Map[String, String] = Map.empty,
expectedRC: Int = 0) = {
val pr = new StringBuilder()
val logger = ProcessLogger((o: String) => pr.append(o + "\n"), (e: String) => pr.append("error < " + e + "\n"))
val cmd = Seq("cmd", "/c", script.getAbsolutePath + " " + args)
val cmd = Seq("cmd", "/c", script.getAbsolutePath) ++ args
// val cmd = Seq("cmd", "/c", script.getAbsolutePath, "-Dtest.hoge=C:\\Program Files\\Java", "'C:\\Program Files\\Java'")
val result = sys.process.Process(cmd, None, env.toSeq: _*) ! logger
if (result != expectedRC) {
pr.append("error code: " + result + "\n")
Expand Down Expand Up @@ -85,43 +86,50 @@ TaskKey[Unit]("checkScript") := {
debugOutFile.delete()
}
}
checkOutput("normal argmument", "OK", "arg #0 is [OK]\nSUCCESS!")
checkOutput("with -D", "-Dtest.hoge=\"huga\" OK", "arg #0 is [OK]\nproperty(test.hoge) is [huga]\nSUCCESS!")

checkOutput("normal argmument", Seq("OK"), "arg #0 is [OK]\nSUCCESS!")
checkOutput("with -D", Seq("-Dtest.hoge=huga", "OK"), "arg #0 is [OK]\nproperty(test.hoge) is [huga]\nSUCCESS!")
checkOutput(
"with -J java-opt",
"-J-Xms6m OK",
Seq("-J-Xms6m", "OK"),
"arg #0 is [OK]\nvmarg #0 is [-Xms6m]\nSUCCESS!",
Map("show-vmargs" -> "true")
)
checkOutput(
"complex",
"first -Dtest.hoge=\"huga\" -J-Xms6m -XX last",
Seq("first", "-Dtest.hoge=huga", "-J-Xms6m", "-XX", "last"),
"arg #0 is [first]\narg #1 is [-XX]\narg #2 is [last]\nproperty(test.hoge) is [huga]\nvmarg #0 is [-Dtest.hoge=huga]\nvmarg #1 is [-Xms6m]\nSUCCESS!",
Map("show-vmargs" -> "true")
)
checkOutput(
"include space",
"""-Dtest.hoge="C:\Program Files\Java" "C:\Program Files\Java" """,
Seq("""-Dtest.hoge=C:\Program Files\Java""", """"C:\Program Files\Java""""),
"arg #0 is [C:\\Program Files\\Java]\nproperty(test.hoge) is [C:\\Program Files\\Java]\nSUCCESS!"
)
checkOutput("include symbols on -D", "\"-Dtest.hoge=\\[]!< >%\"", "property(test.hoge) is [\\[]!< >%]\nSUCCESS!")
checkOutput("include symbols on normal args", """ "\[]!< >%" """, "arg #0 is [\\[]!< >%]\nSUCCESS!")
checkOutput("include symbols on -D", Seq("-Dtest.hoge=\\[]!< >%"), "property(test.hoge) is [\\[]!< >%]\nSUCCESS!")
checkOutput("include symbols on normal args", Seq("\"\\[]!< >%\""), "arg #0 is [\\[]!< >%]\nSUCCESS!")

/* fails test because symbols '<' and '>' cannot be properly escaped during cmd execution
checkOutput(
"include symbols with double quote",
"-Dtest.huga=\"[]!<>%\"",
Seq("-Dtest.huga=\"[]!<>%\""),
"property(test.huga) is [[]!<>%]\nSUCCESS!"
)
*/

checkOutput(
"include symbols with double quote2",
""" "-Dtest.hoge=\[]!< >%" "\[]!< >%" -Dtest.huga="\[]!<>%" """,
Seq("-Dtest.hoge=\\[]!< >%", "\"\\[]!< >%\"", "-Dtest.huga=\\[]!<>%"),
"arg #0 is [\\[]!< >%]\nproperty(test.hoge) is [\\[]!< >%]\nproperty(test.huga) is [\\[]!<>%]\nSUCCESS!"
)

// can't success include double-quote. arguments pass from Process(Seq("-Da=xx\"yy", "aa\"bb")) is parsed (%1="-Da", %2="xx\"yy aa\"bb") by cmd.exe ...
//checkOutput("include space and double-quote",
// "-Dtest.hoge=aa\"bb xx\"yy",
// "arg #0 is [xx\"yy]\nproperty(test.hoge) is [aa\"bb]\nvmarg #0 is [-Dtest.hoge=aa\"bb]\nSUCCESS!")
checkOutput("return-cord not 0", "RC1", "arg #0 is [RC1]\nFAILURE!", Map("return-code" -> "1"), 1)
checkOutput("return-cord not 0 and 1", "RC2", "arg #0 is [RC2]\nFAILURE!", Map("return-code" -> "2"), 2)
checkOutput("return-code negative", "RC-1", "arg #0 is [RC-1]\nFAILURE!", Map("return-code" -> "-1"), -1)

checkOutput("return-cord not 0", Seq("RC1"), "arg #0 is [RC1]\nFAILURE!", Map("return-code" -> "1"), 1)
checkOutput("return-cord not 0 and 1", Seq("RC2"), "arg #0 is [RC2]\nFAILURE!", Map("return-code" -> "2"), 2)
checkOutput("return-code negative", Seq("RC-1"), "arg #0 is [RC-1]\nFAILURE!", Map("return-code" -> "-1"), -1)
assert(fails.toString == "", fails.toString)
}