Skip to content

Commit

Permalink
Adding more unit testing
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Bustamante <jbustamante@vmware.com>
  • Loading branch information
jjbustamante committed Sep 2, 2022
1 parent d6a81be commit 9b07b40
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 11 deletions.
12 changes: 11 additions & 1 deletion pkg/cnb/env_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ func serializeEnvVars(envVars []envVariable, platformDir string) error {
return nil
}

func mergeEnvVariables(old []envVariable, new []envVariable) []envVariable {
mergedEnv := new
for _, v := range old {
if !containsEnvVar(new, v) {
mergedEnv = append(mergedEnv, v)
}
}
return mergedEnv
}

func containsEnvVar(envVars []envVariable, v envVariable) bool {
var found bool = false
var found = false
for _, x := range envVars {
if x.Name == v.Name {
found = true
Expand Down
121 changes: 121 additions & 0 deletions pkg/cnb/env_vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package cnb

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/sclevine/spec"
)

func TestEnvVariable(t *testing.T) {
spec.Run(t, "EnvVariable", testEnvVariable)
}

func testEnvVariable(t *testing.T, when spec.G, it spec.S) {

var (
variableA, variableB envVariable
envVariables, otherEnvVariables []envVariable
)

when("#containsEnvVar", func() {
when("slide is undefined", func() {
it.Before(func() {
envVariables = nil
variableA = envVariable{}
})

it("returns false", func() {
assert.False(t, containsEnvVar(envVariables, variableA))
})
})

when("slide is defined", func() {
it.Before(func() {
variableA = envVariable{Name: "NameA", Value: "ValueA"}
envVariables = []envVariable{variableA}
})

when("element is present in the slide", func() {
it("returns true", func() {
assert.True(t, containsEnvVar(envVariables, envVariable{Name: "NameA", Value: "Whatever"}))
})
})

when("element is not present in the slide", func() {
it("returns false", func() {
assert.False(t, containsEnvVar(envVariables, envVariable{Name: "NameB", Value: "Whatever"}))
})
})
})
})

when("#mergeEnvVariables", func() {
when("slides are undefined", func() {
when("both slides are undefined", func() {
it.Before(func() {
envVariables = nil
otherEnvVariables = nil
})

it("returns nil", func() {
merged := mergeEnvVariables(envVariables, otherEnvVariables)
assert.Nil(t, merged)
})
})

when("one of the slide is undefined", func() {
it.Before(func() {
envVariables = nil
variableA = envVariable{Name: "NameA", Value: "ValueA"}
otherEnvVariables = []envVariable{variableA}
})

when("old slide is undefined", func() {
it("returns new slide", func() {
merged := mergeEnvVariables(envVariables, otherEnvVariables)
assert.Equal(t, otherEnvVariables, merged)
})
})

when("new slide is undefined", func() {
it("returns old slide", func() {
merged := mergeEnvVariables(otherEnvVariables, envVariables)
assert.Equal(t, otherEnvVariables, merged)
})
})
})

when("slides are defined", func() {
when("element in old slide does not exist in new slide", func() {
it.Before(func() {
variableA = envVariable{Name: "NameA", Value: "ValueA"}
variableB = envVariable{Name: "NameB", Value: "ValueB"}
envVariables = []envVariable{variableA}
otherEnvVariables = []envVariable{variableB}
})

it("element is added to the merge slide", func() {
merged := mergeEnvVariables(envVariables, otherEnvVariables)
expected := []envVariable{variableB, variableA}
assert.Equal(t, expected, merged)
})
})

when("element in old slide is also defined in new slide", func() {
it.Before(func() {
variableA = envVariable{Name: "NameA", Value: "ValueA"}
variableB = envVariable{Name: "NameA", Value: "OtherValue"}
envVariables = []envVariable{variableA}
otherEnvVariables = []envVariable{variableB}
})

it("element in new slide has precedence", func() {
merged := mergeEnvVariables(envVariables, otherEnvVariables)
assert.Equal(t, otherEnvVariables, merged)
})
})
})
})
})
}
10 changes: 0 additions & 10 deletions pkg/cnb/project_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,6 @@ func getEnvVariables(d descriptorV2) []envVariable {
return env
}

func mergeEnvVariables(old []envVariable, new []envVariable) []envVariable {
mergedEnv := new
for _, v := range old {
if !containsEnvVar(new, v) {
mergedEnv = append(mergedEnv, v)
}
}
return mergedEnv
}

func getFileFilter(d build) (func(string) bool, error) {
if d.Exclude != nil && d.Include != nil {
return nil, fmt.Errorf("project descriptor cannot have both include and exclude defined")
Expand Down

0 comments on commit 9b07b40

Please sign in to comment.