Skip to content

Commit

Permalink
refactor: Remove GetConfigForIgnoreEnvironment function
Browse files Browse the repository at this point in the history
The `GetConfigForIgnoreEnvironment` seems like it is a bug, so this PR removes the method and replaces the last leftover call with `GetConfigFor`.
The tests are also adopted to test the behavior.
  • Loading branch information
Laubi committed Dec 16, 2024
1 parent 3534012 commit fc5953a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/monaco/generate/deletefile/deletefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func createConfigAPIEntry(c config.Config, apis api.APIs, project project.Projec
return persistence.DeleteEntry{}, fmt.Errorf("scope parameter has no references")
}

refCfg, ok := project.GetConfigForIgnoreEnvironment(refs[0].Config)
refCfg, ok := project.GetConfigFor(c.Environment, refs[0].Config)
if !ok {
return persistence.DeleteEntry{}, fmt.Errorf("no config for referenced scope found")
}
Expand Down
17 changes: 0 additions & 17 deletions pkg/project/v2/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,6 @@ func (p Project) HasDependencyOn(environment string, project Project) bool {
return false
}

// GetConfigForIgnoreEnvironment searches a config object for matching the given coordinate in the
// current project, but does ignore the environment
func (p Project) GetConfigForIgnoreEnvironment(c coordinate.Coordinate) (config.Config, bool) {
for _, configsPerEnvironments := range p.Configs {
for cType, configsPerType := range configsPerEnvironments {
if c.Type == cType {
for _, cfg := range configsPerType {
if cfg.Coordinate.ConfigId == c.ConfigId {
return cfg, true
}
}
}
}
}
return config.Config{}, false
}

// GetConfigFor searches a config object for matching the given coordinate in the
// current project.
func (p Project) GetConfigFor(env string, c coordinate.Coordinate) (config.Config, bool) {
Expand Down
42 changes: 39 additions & 3 deletions pkg/project/v2/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ import (
project "github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/project/v2"
)

func TestGetConfigForIgnoreEnvironment(t *testing.T) {
func TestGetConfigFor(t *testing.T) {
tests := []struct {
name string
givenCoordinate coordinate.Coordinate
givenProject project.Project
givenEnv string
wantConfig config.Config
wantFound bool
}{
{
name: "Config found",
name: "Config found in same env",
givenCoordinate: coordinate.Coordinate{Project: "p1", Type: "t1", ConfigId: "c1"},
givenProject: project.Project{

Expand All @@ -45,10 +46,41 @@ func TestGetConfigForIgnoreEnvironment(t *testing.T) {
"env2": project.ConfigsPerType{"t2": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c2"}}}},
},
},
givenEnv: "env1",

wantFound: true,
wantConfig: config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c1"}},
},
{
name: "Config not found in different env",
givenCoordinate: coordinate.Coordinate{Project: "p1", Type: "t1", ConfigId: "c1"},
givenProject: project.Project{

Id: "p1",
Configs: project.ConfigsPerTypePerEnvironments{
"env1": project.ConfigsPerType{"t1": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c1"}}}},
"env2": project.ConfigsPerType{"t2": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c2"}}}},
},
},
givenEnv: "env2",

wantFound: false,
},
{
name: "Config not found as env does not exist",
givenCoordinate: coordinate.Coordinate{Project: "p1", Type: "t1", ConfigId: "c1"},
givenProject: project.Project{

Id: "p1",
Configs: project.ConfigsPerTypePerEnvironments{
"env1": project.ConfigsPerType{"t1": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c1"}}}},
"env2": project.ConfigsPerType{"t2": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c2"}}}},
},
},
givenEnv: "env3",

wantFound: false,
},
{
name: "Config not found - type mismatch",
givenCoordinate: coordinate.Coordinate{Project: "p1", Type: "t2", ConfigId: "c1"},
Expand All @@ -60,6 +92,7 @@ func TestGetConfigForIgnoreEnvironment(t *testing.T) {
"env2": project.ConfigsPerType{"t2": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c2"}}}},
},
},
givenEnv: "env1",

wantFound: false,
wantConfig: config.Config{},
Expand All @@ -75,14 +108,17 @@ func TestGetConfigForIgnoreEnvironment(t *testing.T) {
"env2": project.ConfigsPerType{"t2": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c2"}}}},
},
},
givenEnv: "env1",

wantFound: false,
wantConfig: config.Config{},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg, found := tc.givenProject.GetConfigForIgnoreEnvironment(tc.givenCoordinate)
t.Parallel()

cfg, found := tc.givenProject.GetConfigFor(tc.givenEnv, tc.givenCoordinate)
assert.Equal(t, tc.wantConfig, cfg)
assert.Equal(t, tc.wantFound, found)
})
Expand Down

0 comments on commit fc5953a

Please sign in to comment.