Skip to content

Commit

Permalink
Fix ash template (#1303)
Browse files Browse the repository at this point in the history
* Remove bashisms from process_args

Signed-off-by: Greg Symons <gsymons@affinipay.com>

* Implement missing `add_residual` function

Signed-off-by: Greg Symons <gsymons@affinipay.com>

* Rework command-line-settings tests

* The original test only coincidentally passed because the broken arg
  parsing didn't actually modify the command-line:

  * The -D argument _should_ have been consumed during argument parsing,
    but wasn't, so remained in residual.
  * The test app would not have outputted the -D argument had argument
    parsing been working, since it only output the app args, not the
    system properties.

* There is now a specific test each for the system properties and the
  residual args.

Signed-off-by: Greg Symons <gsymons@affinipay.com>
  • Loading branch information
gregsymons committed Feb 20, 2020
1 parent 4522634 commit c8c04fb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ addJava () {
java_opts="$java_opts $1"
}

addResidual () {
residual_args="$residual_args $1"
}

# Allow user to specify java options. These get listed first per bash-template.
if [ -n "$JAVA_OPTS" ]
then
Expand All @@ -58,7 +62,7 @@ get_java_cmd() {
# Processes incoming arguments and places them in appropriate global variables. called by the run method.
process_args () {
local no_more_snp_opts=0
while [[ $# -gt 0 ]]; do
while [ $# -gt 0 ]; do
case "$1" in
--) shift && no_more_snp_opts=1 && break ;;
-h|-help) usage; exit 1 ;;
Expand All @@ -80,13 +84,14 @@ process_args () {
esac
done

if [[ no_more_snp_opts ]]; then
while [[ $# -gt 0 ]]; do
if [ $no_more_snp_opts ]; then
while [ $# -gt 0 ]; do
addResidual "$1" && shift
done
fi
}

residual_args=""
real_script_path="$(realpath "$0")"
app_home="$(realpath "$(dirname "$real_script_path")")"
lib_dir="$(realpath "${app_home}/../lib")"
Expand All @@ -102,4 +107,4 @@ java_cmd="$(get_java_cmd)"
# If a configuration file exist, read the contents to $opts
[ -f "$script_conf_file" ] && opts=$(loadConfigFile "$script_conf_file")

exec "$java_cmd" $java_opts -classpath $app_classpath $opts $app_mainclass "$@"
exec "$java_cmd" $java_opts -classpath $app_classpath $opts $app_mainclass "$residual_args"
17 changes: 13 additions & 4 deletions src/sbt-test/ash/command-line-settings/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ name := "command-line-app"

version := "0.1.0-SNAPSHOT"

TaskKey[Unit]("runCheck") := {
val configArg = "-Dconfig.resource=/config.conf"
TaskKey[Unit]("checkSystemProperty") := {
val configArg = "config.resource=/config.conf"
val cwd = (stagingDirectory in Universal).value
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, configArg)
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, s"-D$configArg")

val output = (sys.process.Process(cmd, cwd).!!).replaceAll("\n", "")
assert(output.contains(configArg), s"Application did not receive command line configuration resource $configArg")
assert(output.contains(configArg), s"Application did not receive system property arg '$configArg'")
}

TaskKey[Unit]("checkResidual") := {
val arg = "residualArg"
val cwd = (stagingDirectory in Universal).value
val cmd = Seq((cwd / "bin" / packageName.value).getAbsolutePath, arg)

val output = (sys.process.Process(cmd, cwd).!!).replaceAll("\n", "")
assert(output.contains(arg), s"Application did not receive residual arg '$arg'")
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
object MainApp extends App {
object MainApp extends App {
println(sys.props.collect { case (k, v) => s"$k=$v" } mkString "\n")
println(args.mkString("|"))
}
3 changes: 2 additions & 1 deletion src/sbt-test/ash/command-line-settings/test
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Run the staging and check the script.
> stage
$ exists target/universal/stage/bin/command-line-app
> runCheck
> checkSystemProperty
> checkResidual

0 comments on commit c8c04fb

Please sign in to comment.