Skip to content

Commit

Permalink
Update tests to remove duplicate code by adding helper function.
Browse files Browse the repository at this point in the history
Also added setter for Autoplan type to allow overriding enabled from default struct
  • Loading branch information
JoshiiSinfield committed Nov 28, 2019
1 parent 87cd0c1 commit c2275ed
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 109 deletions.
15 changes: 3 additions & 12 deletions server/events/project_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
Projects: []valid.Project{
{
Dir: ".",
Autoplan: valid.Autoplan{
Enabled: false,
WhenModified: []string{"**/*.tf"},
},
Autoplan: DefaultValidAutoplan().SetEnabled(false),
},
},
},
Expand All @@ -292,10 +289,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
Projects: []valid.Project{
{
Dir: ".",
Autoplan: valid.Autoplan{
Enabled: true,
WhenModified: []string{"**/*.tf"},
},
Autoplan: DefaultValidAutoplan(),
},
},
},
Expand All @@ -308,10 +302,7 @@ func TestDefaultProjectFinder_DetermineProjectsViaConfig(t *testing.T) {
Projects: []valid.Project{
{
Dir: "project",
Autoplan: valid.Autoplan{
Enabled: true,
WhenModified: []string{"**/*.tf"},
},
Autoplan: DefaultValidAutoplan(),
},
},
},
Expand Down
90 changes: 33 additions & 57 deletions server/events/yaml/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,7 @@ projects:
Workspace: "default",
WorkflowName: nil,
TerraformVersion: nil,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
ApplyRequirements: nil,
},
},
Expand All @@ -220,6 +217,24 @@ projects:
input: `
version: 3
projects:
- dir: "."`,
exp: valid.RepoCfg{
Version: 3,
Projects: []valid.Project{
{
Dir: ".",
Workspace: "default",
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: make(map[string]valid.Workflow),
},
},
{
description: "autoplan should be enabled by default if custom when_modified",
input: `
version: 3
projects:
- dir: "."
autoplan:
when_modified: ["**/*.tf*"]
Expand Down Expand Up @@ -252,10 +267,7 @@ projects:
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: make(map[string]valid.Workflow),
Expand All @@ -275,10 +287,7 @@ workflows: ~
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: make(map[string]valid.Workflow),
Expand All @@ -303,10 +312,7 @@ workflows:
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: map[string]valid.Workflow{
Expand Down Expand Up @@ -338,10 +344,7 @@ workflows:
Workspace: "myworkspace",
WorkflowName: String("myworkflow"),
TerraformVersion: tfVersion,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
ApplyRequirements: []string{"approved"},
},
},
Expand All @@ -355,7 +358,7 @@ workflows:
},
},
{
description: "project field with autoplan",
description: "project field with autoplan disabled",
input: `
version: 3
projects:
Expand All @@ -376,10 +379,7 @@ workflows:
Workspace: "myworkspace",
WorkflowName: String("myworkflow"),
TerraformVersion: tfVersion,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: false,
},
Autoplan: DefaultValidAutoplan().SetEnabled(false),
ApplyRequirements: []string{"approved"},
},
},
Expand Down Expand Up @@ -414,10 +414,7 @@ workflows:
Workspace: "myworkspace",
WorkflowName: String("myworkflow"),
TerraformVersion: tfVersion,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: false,
},
Autoplan: DefaultValidAutoplan().SetEnabled(false),
ApplyRequirements: []string{"mergeable"},
},
},
Expand Down Expand Up @@ -452,10 +449,7 @@ workflows:
Workspace: "myworkspace",
WorkflowName: String("myworkflow"),
TerraformVersion: tfVersion,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: false,
},
Autoplan: DefaultValidAutoplan().SetEnabled(false),
ApplyRequirements: []string{"mergeable", "approved"},
},
},
Expand Down Expand Up @@ -566,19 +560,13 @@ projects:
Name: String("myname"),
Dir: ".",
Workspace: "workspace",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
{
Name: String("myname2"),
Dir: ".",
Workspace: "workspace",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: map[string]valid.Workflow{},
Expand Down Expand Up @@ -607,10 +595,7 @@ workflows:
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: map[string]valid.Workflow{
Expand Down Expand Up @@ -669,10 +654,7 @@ workflows:
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: map[string]valid.Workflow{
Expand Down Expand Up @@ -727,10 +709,7 @@ workflows:
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: map[string]valid.Workflow{
Expand Down Expand Up @@ -781,10 +760,7 @@ workflows:
{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
Workflows: map[string]valid.Workflow{
Expand Down
15 changes: 3 additions & 12 deletions server/events/yaml/raw/autoplan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ func TestAutoplan_ToValid(t *testing.T) {
{
description: "nothing set",
input: raw.Autoplan{},
exp: valid.Autoplan{
Enabled: true,
WhenModified: []string{"**/*.tf*", "**/terragrunt.hcl"},
},
exp: DefaultValidAutoplan(),
},
{
description: "when modified empty",
Expand All @@ -127,20 +124,14 @@ func TestAutoplan_ToValid(t *testing.T) {
input: raw.Autoplan{
Enabled: Bool(false),
},
exp: valid.Autoplan{
Enabled: false,
WhenModified: []string{"**/*.tf*", "**/terragrunt.hcl"},
},
exp: DefaultValidAutoplan().SetEnabled(false),
},
{
description: "enabled true",
input: raw.Autoplan{
Enabled: Bool(true),
},
exp: valid.Autoplan{
Enabled: true,
WhenModified: []string{"**/*.tf*", "**/terragrunt.hcl"},
},
exp: DefaultValidAutoplan(),
},
}
for _, c := range cases {
Expand Down
33 changes: 9 additions & 24 deletions server/events/yaml/raw/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,7 @@ func TestProject_ToValid(t *testing.T) {
Workspace: "default",
WorkflowName: nil,
TerraformVersion: nil,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
ApplyRequirements: nil,
Name: nil,
},
Expand Down Expand Up @@ -275,10 +272,7 @@ func TestProject_ToValid(t *testing.T) {
Dir: ".",
Workspace: "default",
TerraformVersion: tfVersionPointEleven,
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
{
Expand All @@ -289,10 +283,7 @@ func TestProject_ToValid(t *testing.T) {
exp: valid.Project{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
{
Expand All @@ -303,10 +294,7 @@ func TestProject_ToValid(t *testing.T) {
exp: valid.Project{
Dir: "mydir",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
{
Expand All @@ -318,10 +306,7 @@ func TestProject_ToValid(t *testing.T) {
exp: valid.Project{
Dir: "mydir",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
{
Expand All @@ -333,10 +318,7 @@ func TestProject_ToValid(t *testing.T) {
exp: valid.Project{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
}
Expand All @@ -346,3 +328,6 @@ func TestProject_ToValid(t *testing.T) {
})
}
}



5 changes: 1 addition & 4 deletions server/events/yaml/raw/repo_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,7 @@ func TestConfig_ToValid(t *testing.T) {
{
Dir: "mydir",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
Autoplan: DefaultValidAutoplan(),
},
},
},
Expand Down
5 changes: 5 additions & 0 deletions server/events/yaml/valid/repo_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ type Autoplan struct {
Enabled bool
}

func (a Autoplan) SetEnabled(enabled bool) Autoplan {
a.Enabled = enabled
return a
}

type Stage struct {
Steps []Step
}
Expand Down
11 changes: 11 additions & 0 deletions testing/default_structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package testing

import "github.com/runatlantis/atlantis/server/events/yaml/valid"

// Helper function to return a frequently repeated valid Autoplan struct.
func DefaultValidAutoplan() valid.Autoplan {
return valid.Autoplan{
WhenModified: []string{"**/*.tf*", "**/terragrunt.hcl"},
Enabled: true,
}
}

0 comments on commit c2275ed

Please sign in to comment.