Skip to content

Commit

Permalink
windows bat exit code fix
Browse files Browse the repository at this point in the history
Return exit code 1 in windows bat script if java application exits with
a return code greater than 0.  Update windows bat regression tests to
check the returned code.
  • Loading branch information
bjuric committed Dec 1, 2014
1 parent b99e171 commit 6b44f17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ set _JAVA_OPTS=!_JAVA_OPTS! !_JAVA_PARAMS!

rem Call the application and pass all arguments unchanged.
"%_JAVACMD%" !_JAVA_OPTS! !@@APP_ENV_NAME@@_OPTS! -cp "%APP_CLASSPATH%" %APP_MAIN_CLASS% !_APP_ARGS!

@endlocal

if ERRORLEVEL 1 goto error
goto end

Expand All @@ -135,6 +138,4 @@ set ERROR_CODE=1

:end

@endlocal

exit /B %ERROR_CODE%
31 changes: 17 additions & 14 deletions src/sbt-test/windows/test-bat-template/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,24 @@ TaskKey[Unit]("check-script") <<= (stagingDirectory in Universal, name, streams)
d
}
def crlf2cr(txt:String) = txt.trim.replaceAll("\\\r\\\n", "\n")
def checkOutputEnv(env:Map[String,String], expected:String, args:String*) = {
def checkOutputEnv(env:Map[String,String], expectedRC: Int, expected:String, args:String*) = {
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 result = Process(cmd, None, env.toSeq:_*) ! logger
if ( result != 0 ) {
if ( result != expectedRC ) {
pr.append("error code: " + result+"\n")
}
val output = crlf2cr(pr.toString)
if(output != expected.trim){
if(result != expectedRC || output != expected.trim){
fails.append("\n---------------------------------\n")
fails.append("Failed to correctly run the main script!.\n")
fails.append("\""+cmd.mkString("\" \"")+"\"\n")
if(debugOutFile.exists){
fails.append(crlf2cr(scala.io.Source.fromFile(debugOutFile).mkString))
}
fails.append("\n--return code---------------------------\n")
fails.append("\ngot: " + result+", expected: "+expectedRC+"\n")
fails.append("\n--expected----------------------------\n")
fails.append(expected.trim+"\n")
fails.append("\n--found-------------------------------\n")
Expand All @@ -77,24 +79,25 @@ TaskKey[Unit]("check-script") <<= (stagingDirectory in Universal, name, streams)
debugOutFile.delete()
}
}
def checkOutput(expected:String, args:String*) = checkOutputEnv(Map.empty, expected, args:_*)
checkOutput("arg #0 is [OK]\nSUCCESS!", "OK")
checkOutput("arg #0 is [OK]\nproperty(test.hoge) is [huga]\nSUCCESS!", "-Dtest.hoge=\"huga\"", "OK")
checkOutputEnv(Map("show-vmargs"->"true"), "arg #0 is [OK]\nvmarg #0 is [-Xms6m]\nSUCCESS!","-J-Xms6m", "OK")
checkOutputEnv(Map("show-vmargs"->"true"), "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!",
def checkOutput(expectedRC: Int, expected:String, args:String*) = checkOutputEnv(Map.empty, expectedRC, expected, args:_*)
checkOutput(0, "arg #0 is [OK]\nSUCCESS!", "OK")
checkOutput(0, "arg #0 is [OK]\nproperty(test.hoge) is [huga]\nSUCCESS!", "-Dtest.hoge=\"huga\"", "OK")
checkOutputEnv(Map("show-vmargs"->"true"), 0, "arg #0 is [OK]\nvmarg #0 is [-Xms6m]\nSUCCESS!","-J-Xms6m", "OK")
checkOutputEnv(Map("show-vmargs"->"true"), 0, "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!",
"first", "-Dtest.hoge=\"huga\"", "-J-Xms6m", "-XX", "last")
// include space
checkOutput("arg #0 is [C:\\Program Files\\Java]\nproperty(test.hoge) is [C:\\Program Files\\Java]\nSUCCESS!",
checkOutput(0, "arg #0 is [C:\\Program Files\\Java]\nproperty(test.hoge) is [C:\\Program Files\\Java]\nSUCCESS!",
"-Dtest.hoge=C:\\Program Files\\Java", "C:\\Program Files\\Java")
// split "include symbols"
checkOutput("property(test.hoge) is [\\[]!< >%]\nSUCCESS!", "\"-Dtest.hoge=\\[]!< >%\"")
checkOutput("arg #0 is [\\[]!< >%]\nSUCCESS!", "\\[]!< >%")
checkOutput("property(test.huga) is [\\[]!<>%]\nSUCCESS!", "-Dtest.huga=\"\\[]!<>%\"")
checkOutput(0, "property(test.hoge) is [\\[]!< >%]\nSUCCESS!", "\"-Dtest.hoge=\\[]!< >%\"")
checkOutput(0, "arg #0 is [\\[]!< >%]\nSUCCESS!", "\\[]!< >%")
checkOutput(0, "property(test.huga) is [\\[]!<>%]\nSUCCESS!", "-Dtest.huga=\"\\[]!<>%\"")
// include symbols
checkOutput("arg #0 is [\\[]!< >%]\nproperty(test.hoge) is [\\[]!< >%]\nproperty(test.huga) is [\\[]!<>%]\nSUCCESS!",
checkOutput(0, "arg #0 is [\\[]!< >%]\nproperty(test.hoge) is [\\[]!< >%]\nproperty(test.huga) is [\\[]!<>%]\nSUCCESS!",
"\"-Dtest.hoge=\\[]!< >%\"", "\\[]!< >%", "-Dtest.huga=\"\\[]!<>%\"")
// include space and double-quote is failed...
// 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("arg #0 is [xx\"yy]\nproperty(test.hoge) is [aa\"bb]\nvmarg #0 is [-Dtest.hoge=aa\"bb]\nSUCCESS!", "-Dtest.hoge=aa\"bb", "xx\"yy")
//checkOutput(0, "arg #0 is [xx\"yy]\nproperty(test.hoge) is [aa\"bb]\nvmarg #0 is [-Dtest.hoge=aa\"bb]\nSUCCESS!", "-Dtest.hoge=aa\"bb", "xx\"yy")
checkOutputEnv(Map("return-code-1"->"true"), 1, "arg #0 is [RC1]\nSUCCESS!", "RC1")
assert(fails.toString == "", fails.toString)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ object Test extends App {
}
}
println("SUCCESS!")
if(System.getenv("return-code-1") == "true"){
System.exit(1)
} else {
System.exit(0)
}
}
}

0 comments on commit 6b44f17

Please sign in to comment.