Merge pull request #518 from yui-knk/add_test_for_addEnv

Add test cases for `addEnv` which is a bit complicated function
docker-18.09
Tõnis Tiigi 2018-07-18 22:41:39 -07:00 committed by GitHub
commit db8b382274
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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{}) _, _, err = Dockerfile2LLB(appcontext.Context(), []byte(df), ConvertOpt{})
assert.Error(t, err) 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)
}