Skip to content

Commit

Permalink
Merge pull request #107 from hagay3/bugfix/hagai/removeWhitesspaceVal…
Browse files Browse the repository at this point in the history
…idationForParams

ignore whitespace in case there are params
  • Loading branch information
mdedetrich authored Mar 26, 2023
2 parents fa55927 + f06791e commit 9a4ea98
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/scala/sbtghactions/GenerativePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ object GenerativePlugin extends AutoPlugin {
s"environment: ${wrap(environment.name)}"
}

def compileEnv(env: Map[String, String], prefix: String = "env"): String =
def compileEnv(env: Map[String, String], prefix: String = "env", ignoreWhiteSpace: Boolean = false): String =
if (env.isEmpty) {
""
} else {
val rendered = env map {
case (key, value) =>
if (!isSafeString(key) || key.indexOf(' ') >= 0)
val whitSpaceValidation = if(ignoreWhiteSpace) false else key.indexOf(' ') >= 0

if (!isSafeString(key) || whitSpaceValidation)
sys.error(s"'$key' is not a valid environment variable name")

s"""$key: ${wrap(value)}"""
Expand Down Expand Up @@ -315,7 +317,7 @@ ${indent(rendered.mkString("\n"), 1)}"""
renderedShell + "run: " + wrap(commands.mkString("\n")) + renderParams(params)

def renderParams(params: Map[String, String]): String = {
val renderedParamsPre = compileEnv(params, prefix = "with")
val renderedParamsPre = compileEnv(params, prefix = "with", ignoreWhiteSpace = true)
val renderedParams = if (renderedParamsPre.isEmpty)
""
else
Expand Down
16 changes: 16 additions & 0 deletions src/test/scala/sbtghactions/GenerativePluginSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,22 @@ class GenerativePluginSpec extends Specification {
Run(List("users"), timeout = Some(1.hour)),
"") mustEqual "- timeout-minutes: 60\n run: users"
}

"compile a run with parameters with space" in {
compileStep(
Run(List("echo foo"), params = Map("abc space" -> "def", "cafe test 1" -> "@42")),
"") mustEqual """- run: echo foo
| with:
| abc space: def
| cafe test 1: '@42'""".stripMargin
}

"throw error run with environment variables with space" in {
compileStep(
Run(List("echo foo"), env = Map("abc space" -> "def", "cafe test 1" -> "@42")),
"") must throwA[RuntimeException]
}

}

"job compilation" should {
Expand Down

0 comments on commit 9a4ea98

Please sign in to comment.