Skip to content

Commit cf490f8

Browse files
committed
Refactor run_step_runner test cases
1 parent 83c81db commit cf490f8

File tree

3 files changed

+15
-65
lines changed

3 files changed

+15
-65
lines changed

server/events/pending_plan_finder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package events
22

33
import (
4-
"github.com/runatlantis/atlantis/server/events/runtime"
54
"io/ioutil"
65
"os"
76
"os/exec"
87
"path/filepath"
98
"strings"
109

1110
"github.com/pkg/errors"
11+
"github.com/runatlantis/atlantis/server/events/runtime"
1212
)
1313

1414
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_pending_plan_finder.go PendingPlanFinder

server/events/runtime/run_step_runner_test.go

+12-62
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313

1414
func TestRunStepRunner_Run(t *testing.T) {
1515
cases := []struct {
16-
Command string
17-
ExpOut string
18-
ExpErr string
16+
Command string
17+
ProjectName string
18+
ExpOut string
19+
ExpErr string
1920
}{
2021
{
2122
Command: "",
@@ -46,8 +47,13 @@ func TestRunStepRunner_Run(t *testing.T) {
4647
ExpErr: "exit status 127: running \"lkjlkj\" in",
4748
},
4849
{
49-
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE",
50-
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan\n",
50+
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
51+
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan project=\n",
52+
},
53+
{
54+
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
55+
ProjectName: "my/project/name",
56+
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/my::project::name-myworkspace.tfplan project=my/project/name\n",
5157
},
5258
{
5359
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR",
@@ -58,21 +64,6 @@ func TestRunStepRunner_Run(t *testing.T) {
5864
ExpOut: "user_name=acme-user\n",
5965
},
6066
}
61-
casesWithProject := []struct {
62-
Command string
63-
ProjectName string
64-
ExpOut string
65-
ExpErr string
66-
}{
67-
{
68-
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
69-
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myproject-myworkspace.tfplan project=myproject\n",
70-
},
71-
{
72-
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR project_name=$PROJECT_NAME",
73-
ExpOut: "base_repo_name=basename base_repo_owner=baseowner head_repo_name=headname head_repo_owner=headowner head_branch_name=add-feat base_branch_name=master pull_num=2 pull_author=acme project_name=myproject\n",
74-
},
75-
}
7667

7768
projVersion, err := version.NewVersion("v0.11.0")
7869
Ok(t, err)
@@ -106,48 +97,7 @@ func TestRunStepRunner_Run(t *testing.T) {
10697
Workspace: "myworkspace",
10798
RepoRelDir: "mydir",
10899
TerraformVersion: projVersion,
109-
// ProjectName: "myproject",
110-
}
111-
out, err := r.Run(ctx, c.Command, tmpDir)
112-
if c.ExpErr != "" {
113-
ErrContains(t, c.ExpErr, err)
114-
return
115-
}
116-
Ok(t, err)
117-
// Replace $DIR in the exp with the actual temp dir. We do this
118-
// here because when constructing the cases we don't yet know the
119-
// temp dir.
120-
expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1)
121-
Equals(t, expOut, out)
122-
})
123-
}
124-
for _, c := range casesWithProject {
125-
t.Run(c.Command, func(t *testing.T) {
126-
tmpDir, cleanup := TempDir(t)
127-
defer cleanup()
128-
ctx := models.ProjectCommandContext{
129-
BaseRepo: models.Repo{
130-
Name: "basename",
131-
Owner: "baseowner",
132-
},
133-
HeadRepo: models.Repo{
134-
Name: "headname",
135-
Owner: "headowner",
136-
},
137-
Pull: models.PullRequest{
138-
Num: 2,
139-
HeadBranch: "add-feat",
140-
BaseBranch: "master",
141-
Author: "acme",
142-
},
143-
User: models.User{
144-
Username: "acme-user",
145-
},
146-
Log: logging.NewNoopLogger(),
147-
Workspace: "myworkspace",
148-
RepoRelDir: "mydir",
149-
TerraformVersion: projVersion,
150-
ProjectName: "myproject",
100+
ProjectName: c.ProjectName,
151101
}
152102
out, err := r.Run(ctx, c.Command, tmpDir)
153103
if c.ExpErr != "" {

server/events/runtime/runtime.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ package runtime
44

55
import (
66
"fmt"
7-
"github.com/pkg/errors"
87
"regexp"
98
"strings"
109

11-
"github.com/hashicorp/go-version"
10+
version "github.com/hashicorp/go-version"
11+
"github.com/pkg/errors"
1212
"github.com/runatlantis/atlantis/server/events/models"
1313
"github.com/runatlantis/atlantis/server/events/terraform"
1414
"github.com/runatlantis/atlantis/server/logging"

0 commit comments

Comments
 (0)