Skip to content

Commit

Permalink
Adding tests debugging dir and subdir names (#44)
Browse files Browse the repository at this point in the history
* Adding extra tests

Signed-off-by: Adam Pietrzycki <[email protected]>

* Fix test

Signed-off-by: Adam Pietrzycki <[email protected]>

* Remove redundant test cases from project_config_test.go

This commit eliminates unnecessary test cases for modified files, specifically removing references to "aws/workspaces/main.tf" and "gcp/workspaces/main.tf". This streamlines the test suite by ensuring it only focuses on the relevant "workspaces/main.tf" file.

Signed-off-by: Adam Pietrzycki <[email protected]>

* Add tests for workspaceForDir in project_config_test.go

Introduced unit tests for the workspaceForDir method in project_config_test.go to ensure accurate mapping of directories to TFC workspaces. Tests cover matching, non-matching, and similarly named subdirectory cases.

Signed-off-by: Adam Pietrzycki <[email protected]>

* Add unit tests for workspacesForTriggerDir function

Introduce a series of unit tests to verify the functionality of the workspacesForTriggerDir method in the ProjectConfig class. Tests include scenarios with matching trigger directories, multiple workspaces, multiple trigger directories, and cases with no matching triggers.

Signed-off-by: Adam Pietrzycki <[email protected]>

---------

Signed-off-by: Adam Pietrzycki <[email protected]>
  • Loading branch information
adampie authored Nov 11, 2024
1 parent ba35ac8 commit 6e1c4d6
Showing 1 changed file with 301 additions and 0 deletions.
301 changes: 301 additions & 0 deletions pkg/tfc_trigger/project_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,232 @@ import (
"github.com/kr/pretty"
)

func TestProjectConfig_workspaceForDir(t *testing.T) {
type fields struct {
Workspaces []*TFCWorkspace
}
type args struct {
dir string
}
tests := []struct {
name string
fields fields
args args
want *TFCWorkspace
}{
{
name: "workspace-for-dir-matching",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "service-tfbuddy-dev",
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
},
},
},
args: args{
dir: "terraform/dev/",
},
want: &TFCWorkspace{
Name: "service-tfbuddy-dev",
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
},
},
{
name: "workspace-for-non-matching-dir",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "service-tfbuddy-dev",
Organization: "foo-corp",
Dir: "terraform/dev/",
Mode: "apply-before-merge",
},
},
},
args: args{
dir: "extra/workspaces/",
},
want: nil,
},
{
name: "different-dir-same-subdir-name",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "a-compute",
Organization: "foo-corp",
Dir: "a/compute/",
Mode: "apply-before-merge",
},
{
Name: "b-compute",
Organization: "foo-corp",
Dir: "b/compute/",
Mode: "apply-before-merge",
},
},
},
args: args{
dir: "b/compute/",
},
want: &TFCWorkspace{
Name: "b-compute",
Organization: "foo-corp",
Dir: "b/compute/",
Mode: "apply-before-merge",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &ProjectConfig{
Workspaces: tt.fields.Workspaces,
}
if got := cfg.workspaceForDir(tt.args.dir); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ProjectConfig.workspaceForDir() = %v, want %v", got, tt.want)
}
})
}
}

func TestProjectConfig_workspacesForTriggerDir(t *testing.T) {
type fields struct {
Workspaces []*TFCWorkspace
}
type args struct {
dir string
}
tests := []struct {
name string
fields fields
args args
want []*TFCWorkspace
}{
{
name: "trigger-dir-match",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{"modules/"},
},
},
},
args: args{
dir: "modules/",
},
want: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{"modules/"},
},
},
},
{
name: "trigger-dir-multiple-workspace-match",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{"modules/"},
},
{
Name: "service-b",
Organization: "foo-corp",
Dir: "b/",
TriggerDirs: []string{"modules/"},
},
},
},
args: args{
dir: "modules/",
},
want: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{"modules/"},
},
{
Name: "service-b",
Organization: "foo-corp",
Dir: "b/",
TriggerDirs: []string{"modules/"},
},
},
},
{
name: "multiple-trigger-dir-workspace-match",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{
"modules/a",
"modules/c",
},
},
},
},
args: args{
dir: "modules/c",
},
want: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{"" +
"modules/a",
"modules/c",
},
},
},
},
{
name: "trigger-no-match",
fields: fields{
Workspaces: []*TFCWorkspace{
{
Name: "service-a",
Organization: "foo-corp",
Dir: "a/",
TriggerDirs: []string{"modules/"},
},
},
},
args: args{
dir: "docs/",
},
want: make([]*TFCWorkspace, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &ProjectConfig{
Workspaces: tt.fields.Workspaces,
}
if got := cfg.workspacesForTriggerDir(tt.args.dir); !reflect.DeepEqual(got, tt.want) {
t.Errorf("workspacesForTriggerDir() = %v, want %v", got, tt.want)
}
})
}
}

func TestProjectConfig_triggeredWorkspaces(t *testing.T) {

type args struct {
Expand Down Expand Up @@ -138,6 +364,42 @@ func TestProjectConfig_triggeredWorkspaces(t *testing.T) {
testLoadConfig(t, tfbuddyYamlSharedTriggerDirMultipleWorkspaces).Workspaces[1],
},
},
{
name: "subdir-and-dir-same-name",
cfgYaml: tfbuddyYamlSubdirAndDirSameName,
args: args{
modifiedFiles: []string{
"workspaces/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlSubdirAndDirSameName).Workspaces[1],
},
},
{
name: "different-dir-same-subdir-name",
cfgYaml: tfbuddyYamlDifferentDirSameSubdir,
args: args{
modifiedFiles: []string{
"gcp/workspaces/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlDifferentDirSameSubdir).Workspaces[1],
},
},
{
name: "multiple-dir-and-subdir",
cfgYaml: tfbuddyYamlMultipleDirAndSubdir,
args: args{
modifiedFiles: []string{
"workspaces/main.tf",
},
},
want: []*TFCWorkspace{
testLoadConfig(t, tfbuddyYamlMultipleDirAndSubdir).Workspaces[2],
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -455,3 +717,42 @@ workspaces:
- modules/database
`

const tfbuddyYamlSubdirAndDirSameName = `
---
workspaces:
- name: aws-workspaces
organization: foo-corp
dir: aws/workspaces
- name: workspaces
organization: foo-corp
dir: workspaces
`

const tfbuddyYamlDifferentDirSameSubdir = `
---
workspaces:
- name: aws-workspaces
organization: foo-corp
dir: aws/workspaces
- name: gcp-workspaces
organization: foo-corp
dir: gcp/workspaces
`

const tfbuddyYamlMultipleDirAndSubdir = `
---
workspaces:
- name: aws-workspaces
organization: foo-corp
dir: aws/workspaces
- name: gcp-workspaces
organization: foo-corp
dir: gcp/workspaces
- name: workspaces
organization: foo-corp
dir: workspaces
`

0 comments on commit 6e1c4d6

Please sign in to comment.