From fc5953aeb81a7a23bde67506086df65cd79a047f Mon Sep 17 00:00:00 2001 From: David Laubreiter Date: Mon, 16 Dec 2024 12:39:37 +0100 Subject: [PATCH] refactor: Remove `GetConfigForIgnoreEnvironment` function 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. --- cmd/monaco/generate/deletefile/deletefile.go | 2 +- pkg/project/v2/project.go | 17 -------- pkg/project/v2/project_test.go | 42 ++++++++++++++++++-- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/cmd/monaco/generate/deletefile/deletefile.go b/cmd/monaco/generate/deletefile/deletefile.go index f5940ab1c..f8f77cc05 100644 --- a/cmd/monaco/generate/deletefile/deletefile.go +++ b/cmd/monaco/generate/deletefile/deletefile.go @@ -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") } diff --git a/pkg/project/v2/project.go b/pkg/project/v2/project.go index a269d6df2..ef6ae8eaa 100644 --- a/pkg/project/v2/project.go +++ b/pkg/project/v2/project.go @@ -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) { diff --git a/pkg/project/v2/project_test.go b/pkg/project/v2/project_test.go index 4f29fd81f..fa0cfcb4b 100644 --- a/pkg/project/v2/project_test.go +++ b/pkg/project/v2/project_test.go @@ -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{ @@ -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"}, @@ -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{}, @@ -75,6 +108,7 @@ func TestGetConfigForIgnoreEnvironment(t *testing.T) { "env2": project.ConfigsPerType{"t2": {config.Config{Coordinate: coordinate.Coordinate{ConfigId: "c2"}}}}, }, }, + givenEnv: "env1", wantFound: false, wantConfig: config.Config{}, @@ -82,7 +116,9 @@ func TestGetConfigForIgnoreEnvironment(t *testing.T) { } 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) })