Skip to content

Commit

Permalink
fixed environment values population (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
trung authored and nehashri committed Oct 23, 2018
1 parent ba08379 commit 62b94b8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
18 changes: 10 additions & 8 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (

var envVars map[string]string

var currentEnv = "default"
var currentEnvironments = []string{"default"}

// LoadEnv first generates the map of the env vars that needs to be set.
// It starts by populating the map with the env passed by the user in --env flag.
Expand All @@ -69,16 +69,18 @@ func LoadEnv(envName string) error {
envVars = make(map[string]string)

defaultEnvLoaded := false
for _, currentEnv := range allEnvs {
currentEnv = strings.TrimSpace(currentEnv)
for _, env := range allEnvs {
env = strings.TrimSpace(env)

err := loadEnvDir(currentEnv)
err := loadEnvDir(env)
if err != nil {
return fmt.Errorf("Failed to load env. %s", err.Error())
}

if currentEnv == "default" {
if env == "default" {
defaultEnvLoaded = true
} else {
currentEnvironments = append(currentEnvironments, env)
}
}

Expand Down Expand Up @@ -206,9 +208,9 @@ func containsEnvVar(value string) (contains bool, matches [][]string) {
return
}

// CurrentEnv returns the value of currentEnv
func CurrentEnv() string {
return currentEnv
// comma-separated value of environments
func CurrentEnvironments() string {
return strings.Join(currentEnvironments, ",")
}

func convertToBool(property string, defaultValue bool) bool {
Expand Down
10 changes: 10 additions & 0 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,13 @@ func (s *MySuite) TestLoadDefaultEnvWithInvalidSubstitutedVariable(c *C) {
e := LoadEnv("default")
c.Assert(e, ErrorMatches, ".*env variable was not set")
}

func (s *MySuite) TestCurrentEnvironmentIsPopulated(c *C) {
os.Clearenv()
config.ProjectRoot = "_testdata/proj1"

e := LoadEnv("foo")

c.Assert(e, Equals, nil)
c.Assert(CurrentEnvironments(), Equals, "default,foo")
}
2 changes: 1 addition & 1 deletion execution/result/suiteResult.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewSuiteResult(tags string, startTime time.Time) *SuiteResult {
result.SpecResults = make([]*SpecResult, 0)
result.Timestamp = startTime.Format(config.LayoutForTimeStamp)
result.ProjectName = filepath.Base(config.ProjectRoot)
result.Environment = env.CurrentEnv()
result.Environment = env.CurrentEnvironments()
result.Tags = tags
return result
}
Expand Down

0 comments on commit 62b94b8

Please sign in to comment.