Skip to content

Commit 6a201f3

Browse files
lifubangthaJeztah
authored andcommitted
fixes when no equal sign in label file
Signed-off-by: Lifubang <lifubang@acmcoder.com> Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2ea4dc1 commit 6a201f3

4 files changed

Lines changed: 46 additions & 1 deletion

File tree

cli/command/container/opts_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,25 @@ func TestParseEnvfileVariables(t *testing.T) {
965965
}
966966
}
967967

968+
func TestParseEnvfileVariablesWithoutEqualSign(t *testing.T) {
969+
// env without equal sign
970+
config, _, _, err := parseRun([]string{"--env-file=testdata/noequalsign.env", "img", "cmd"})
971+
if err != nil {
972+
t.Fatal(err)
973+
}
974+
if len(config.Env) != 2 || config.Env[0] != "bar=" || config.Env[1] != "demo=true" {
975+
t.Fatalf("Expected a config with [bar= demo=true], got %v", config.Env)
976+
}
977+
t.Setenv("foo", "bar")
978+
config, _, _, err = parseRun([]string{"--env-file=testdata/noequalsign.env", "img", "cmd"})
979+
if err != nil {
980+
t.Fatal(err)
981+
}
982+
if len(config.Env) != 3 || config.Env[0] != "foo=bar" || config.Env[1] != "bar=" || config.Env[2] != "demo=true" {
983+
t.Fatalf("Expected a config with [foo=bar bar= demo=true], got %v", config.Env)
984+
}
985+
}
986+
968987
func TestParseEnvfileVariablesWithBOMUnicode(t *testing.T) {
969988
// UTF8 with BOM
970989
config, _, _, err := parseRun([]string{"--env-file=testdata/utf8.env", "img", "cmd"})
@@ -1018,6 +1037,24 @@ func TestParseLabelfileVariables(t *testing.T) {
10181037
}
10191038
}
10201039

1040+
func TestParseLabelfileVariablesWithoutEqualSign(t *testing.T) {
1041+
// label without equal sign
1042+
config, _, _, err := parseRun([]string{"--label-file=testdata/noequalsign.label", "img", "cmd"})
1043+
if err != nil {
1044+
t.Fatal(err)
1045+
}
1046+
if len(config.Labels) != 3 || config.Labels["demo"] != "true" || config.Labels["foo"] != "" || config.Labels["bar"] != "" {
1047+
t.Fatalf("Expected a config with [foo: bar: demo=true], got %v", config.Labels)
1048+
}
1049+
config, _, _, err = parseRun([]string{"--label-file=testdata/noequalsign.label", "--label=foo=bar", "img", "cmd"})
1050+
if err != nil {
1051+
t.Fatal(err)
1052+
}
1053+
if len(config.Labels) != 3 || config.Labels["demo"] != "true" || config.Labels["foo"] != "bar" || config.Labels["bar"] != "" {
1054+
t.Fatalf("Expected a config with [foo=bar bar: demo:true], got %v", config.Labels)
1055+
}
1056+
}
1057+
10211058
func TestParseEntryPoint(t *testing.T) {
10221059
config, _, _, err := parseRun([]string{"--entrypoint=anything", "cmd", "img"})
10231060
assert.NilError(t, err)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo
2+
bar=
3+
demo=true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo
2+
bar=
3+
demo=true

opts/parse.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
// ReadKVStrings reads a file of line terminated key=value pairs, and overrides any keys
1414
// present in the file with additional pairs specified in the override parameter
1515
func ReadKVStrings(files []string, override []string) ([]string, error) {
16-
return readKVStrings(files, override, nil)
16+
return readKVStrings(files, override, func(string) (string, bool) {
17+
return "", true
18+
})
1719
}
1820

1921
// ReadKVEnvStrings reads a file of line terminated key=value pairs, and overrides any keys

0 commit comments

Comments
 (0)