forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(multienv): allow commas and quoted values (runatlantis#3542)
* Make code more Go-idiomatic While at it makes it more readable. Signed-off-by: Leandro López (inkel) <[email protected]> * Add internal function to parse multienv step input This new function properly deals with quotes and commas in values. Signed-off-by: Leandro López (inkel) <[email protected]> * Add regression test for multienv output with comma in values See runatlantis#2765 for an issue report. Signed-off-by: Leandro López (inkel) <[email protected]> * Use parseMultienvLine for parsing multienv steps output Signed-off-by: Leandro López (inkel) <[email protected]> * Add internal function to parse multienv step input This new function properly deals with quotes and commas in values. Signed-off-by: Leandro López (inkel) <[email protected]> --------- Signed-off-by: Leandro López (inkel) <[email protected]> Co-authored-by: PePe Amengual <[email protected]>
- Loading branch information
Showing
3 changed files
with
208 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package runtime | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestMultiEnvStepRunner_Run_parser(t *testing.T) { | ||
t.Run("success", func(t *testing.T) { | ||
tests := map[string][]string{ | ||
"": nil, | ||
"KEY=value": {"KEY", "value"}, | ||
`KEY="value"`: {"KEY", "value"}, | ||
"KEY==": {"KEY", "="}, | ||
`KEY="'"`: {"KEY", "'"}, | ||
`KEY=""`: {"KEY", ""}, | ||
`KEY=a\"b`: {"KEY", `a"b`}, | ||
`KEY="va\"l\"ue"`: {"KEY", `va"l"ue`}, | ||
|
||
"KEY='value'": {"KEY", "value"}, | ||
`KEY='va"l"ue'`: {"KEY", `va"l"ue`}, | ||
`KEY='"'`: {"KEY", `"`}, | ||
"KEY=a'b": {"KEY", "a'b"}, | ||
"KEY=''": {"KEY", ""}, | ||
"KEY='a\\'b'": {"KEY", "a\\'b"}, | ||
|
||
"FOO=bar,QUUX=baz": {"FOO", "bar", "QUUX", "baz"}, | ||
"FOO='bar',QUUX=baz": {"FOO", "bar", "QUUX", "baz"}, | ||
"FOO=bar,QUUX='baz'": {"FOO", "bar", "QUUX", "baz"}, | ||
`FOO="bar",QUUX=baz`: {"FOO", "bar", "QUUX", "baz"}, | ||
`FOO=bar,QUUX="baz"`: {"FOO", "bar", "QUUX", "baz"}, | ||
`FOO="bar",QUUX='baz'`: {"FOO", "bar", "QUUX", "baz"}, | ||
`FOO='bar',QUUX="baz"`: {"FOO", "bar", "QUUX", "baz"}, | ||
|
||
"FOO=\"bar\nbaz\"": {"FOO", "bar\nbaz"}, | ||
|
||
`KEY="foo='bar',lorem=ipsum"`: {"KEY", "foo='bar',lorem=ipsum"}, | ||
`FOO=bar,QUUX="lorem ipsum"`: {"FOO", "bar", "QUUX", "lorem ipsum"}, | ||
|
||
`JSON="{\"ID\":1,\"Name\":\"Reds\",\"Colors\":[\"Crimson\",\"Red\",\"Ruby\",\"Maroon\"]}"`: {"JSON", `{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`}, | ||
|
||
`JSON='{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}'`: {"JSON", `{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`}, | ||
} | ||
|
||
for in, exp := range tests { | ||
t.Run(in, func(t *testing.T) { | ||
got, err := parseMultienvLine(in) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
t.Logf("\n%q\n%q", exp, got) | ||
|
||
if e, g := len(exp), len(got); e != g { | ||
t.Fatalf("expecting %d elements, got %d", e, g) | ||
} | ||
|
||
for i, e := range exp { | ||
if g := got[i]; g != e { | ||
t.Errorf("expecting %q at index %d, got %q", e, i, g) | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
tests := map[string]error{ | ||
"BAD KEY": errInvalidKeySyntax, | ||
"KEY='missingquote": errMisquoted, | ||
`KEY="missingquote`: errMisquoted, | ||
`KEY="missquoted'`: errMisquoted, | ||
`KEY=a"b`: errMisquoted, | ||
`KEY=value,rem`: errRemaining, | ||
} | ||
|
||
for in, exp := range tests { | ||
t.Run(in, func(t *testing.T) { | ||
if _, err := parseMultienvLine(in); !errors.Is(err, exp) { | ||
t.Fatalf("expecting error %v, got %v", exp, err) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters