Add test cases for `addEnv` which is a bit complicated function

Signed-off-by: Yuichiro Kaneko <spiketeika@gmail.com>
docker-18.09
Yuichiro Kaneko 2018-07-19 09:39:22 +09:00
parent f45ad4e9f1
commit f79f28cf83
1 changed files with 50 additions and 0 deletions

View File

@ -62,3 +62,53 @@ COPY --from=0 f2 /
_, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.Error(t, err)
}
func TestAddEnv(t *testing.T) {
// k exists in env as key
// override = false
env := []string{"key1=val1", "key2=val2"}
result := addEnv(env, "key1", "value1", false)
assert.Equal(t, []string{"key1=val1", "key2=val2"}, result)
// k exists in env as key
// override = true
env = []string{"key1=val1", "key2=val2"}
result = addEnv(env, "key1", "value1", true)
assert.Equal(t, []string{"key1=value1", "key2=val2"}, result)
// k does not exist in env as key
// override = false
env = []string{"key1=val1", "key2=val2"}
result = addEnv(env, "key3", "val3", false)
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result)
// k does not exist in env as key
// override = true
env = []string{"key1=val1", "key2=val2"}
result = addEnv(env, "key3", "val3", true)
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result)
// env has same keys
// override = false
env = []string{"key1=val1", "key1=val2"}
result = addEnv(env, "key1", "value1", false)
assert.Equal(t, []string{"key1=val1", "key1=val2"}, result)
// env has same keys
// override = true
env = []string{"key1=val1", "key1=val2"}
result = addEnv(env, "key1", "value1", true)
assert.Equal(t, []string{"key1=value1", "key1=val2"}, result)
// k matches with key only string in env
// override = false
env = []string{"key1=val1", "key2=val2", "key3"}
result = addEnv(env, "key3", "val3", false)
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3"}, result)
// k matches with key only string in env
// override = true
env = []string{"key1=val1", "key2=val2", "key3"}
result = addEnv(env, "key3", "val3", true)
assert.Equal(t, []string{"key1=val1", "key2=val2", "key3=val3"}, result)
}