diff --git a/src/main/resources/com/typesafe/sbt/packager/archetypes/bat-template b/src/main/resources/com/typesafe/sbt/packager/archetypes/bat-template index 8c4860655..8e9dbb11d 100644 --- a/src/main/resources/com/typesafe/sbt/packager/archetypes/bat-template +++ b/src/main/resources/com/typesafe/sbt/packager/archetypes/bat-template @@ -10,7 +10,6 @@ @echo off if "%@@APP_ENV_NAME@@_HOME%"=="" set "@@APP_ENV_NAME@@_HOME=%~dp0\\.." -set ERROR_CODE=0 set "APP_LIB_DIR=%@@APP_ENV_NAME@@_HOME%\lib\" @@ -127,14 +126,10 @@ 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! -if ERRORLEVEL 1 goto error -goto end -:error -set ERROR_CODE=1 +@endlocal -:end -@endlocal +:end -exit /B %ERROR_CODE% +exit /B %ERRORLEVEL% diff --git a/src/sbt-test/windows/test-bat-template/build.sbt b/src/sbt-test/windows/test-bat-template/build.sbt index 9d26f4a85..ce18a3fe0 100644 --- a/src/sbt-test/windows/test-bat-template/build.sbt +++ b/src/sbt-test/windows/test-bat-template/build.sbt @@ -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") @@ -77,24 +79,27 @@ 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"), 1, "arg #0 is [RC1]\nFAILURE!", "RC1") + checkOutputEnv(Map("return-code"->"2"), 2, "arg #0 is [RC2]\nFAILURE!", "RC2") + checkOutputEnv(Map("return-code"->"-1"), -1, "arg #0 is [RC-1]\nFAILURE!", "RC-1") assert(fails.toString == "", fails.toString) } diff --git a/src/sbt-test/windows/test-bat-template/src/main/scala/test/Test.scala b/src/sbt-test/windows/test-bat-template/src/main/scala/test/Test.scala index 7de4f79a3..74d10d0f9 100644 --- a/src/sbt-test/windows/test-bat-template/src/main/scala/test/Test.scala +++ b/src/sbt-test/windows/test-bat-template/src/main/scala/test/Test.scala @@ -4,13 +4,19 @@ import scala.collection.JavaConversions._ object Test extends App { override def main(args: Array[String]): Unit = { - for((x,i) <- args.zipWithIndex) println("arg #" + i + " is [" + x + "]") - for((k,v) <- System.getProperties if k.startsWith("test.")) println("property(" + k + ") is [" + v + "]") - if(System.getenv("show-vmargs") == "true"){ - for((x,i) <- java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().zipWithIndex){ - println("vmarg #" + i + " is [" + x + "]") - } - } - println("SUCCESS!") + for((x,i) <- args.zipWithIndex) println("arg #" + i + " is [" + x + "]") + for((k,v) <- System.getProperties if k.startsWith("test.")) println("property(" + k + ") is [" + v + "]") + if(System.getenv("show-vmargs") == "true"){ + for((x,i) <- java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().zipWithIndex){ + println("vmarg #" + i + " is [" + x + "]") + } + } + if(System.getenv("return-code") != null){ + println("FAILURE!") + System.exit(System.getenv("return-code").toInt) + } else { + println("SUCCESS!") + System.exit(0) + } } }