Skip to content

Commit

Permalink
Refactor run_step_runner test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Apr 8, 2019
1 parent 83c81db commit cf490f8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 65 deletions.
2 changes: 1 addition & 1 deletion server/events/pending_plan_finder.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package events

import (
"github.com/runatlantis/atlantis/server/events/runtime"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/runtime"
)

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_pending_plan_finder.go PendingPlanFinder
Expand Down
74 changes: 12 additions & 62 deletions server/events/runtime/run_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

func TestRunStepRunner_Run(t *testing.T) {
cases := []struct {
Command string
ExpOut string
ExpErr string
Command string
ProjectName string
ExpOut string
ExpErr string
}{
{
Command: "",
Expand Down Expand Up @@ -46,8 +47,13 @@ func TestRunStepRunner_Run(t *testing.T) {
ExpErr: "exit status 127: running \"lkjlkj\" in",
},
{
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan\n",
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan project=\n",
},
{
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
ProjectName: "my/project/name",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/my::project::name-myworkspace.tfplan project=my/project/name\n",
},
{
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",
Expand All @@ -58,21 +64,6 @@ func TestRunStepRunner_Run(t *testing.T) {
ExpOut: "user_name=acme-user\n",
},
}
casesWithProject := []struct {
Command string
ProjectName string
ExpOut string
ExpErr string
}{
{
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myproject-myworkspace.tfplan project=myproject\n",
},
{
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",
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",
},
}

projVersion, err := version.NewVersion("v0.11.0")
Ok(t, err)
Expand Down Expand Up @@ -106,48 +97,7 @@ func TestRunStepRunner_Run(t *testing.T) {
Workspace: "myworkspace",
RepoRelDir: "mydir",
TerraformVersion: projVersion,
// ProjectName: "myproject",
}
out, err := r.Run(ctx, c.Command, tmpDir)
if c.ExpErr != "" {
ErrContains(t, c.ExpErr, err)
return
}
Ok(t, err)
// Replace $DIR in the exp with the actual temp dir. We do this
// here because when constructing the cases we don't yet know the
// temp dir.
expOut := strings.Replace(c.ExpOut, "$DIR", tmpDir, -1)
Equals(t, expOut, out)
})
}
for _, c := range casesWithProject {
t.Run(c.Command, func(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
ctx := models.ProjectCommandContext{
BaseRepo: models.Repo{
Name: "basename",
Owner: "baseowner",
},
HeadRepo: models.Repo{
Name: "headname",
Owner: "headowner",
},
Pull: models.PullRequest{
Num: 2,
HeadBranch: "add-feat",
BaseBranch: "master",
Author: "acme",
},
User: models.User{
Username: "acme-user",
},
Log: logging.NewNoopLogger(),
Workspace: "myworkspace",
RepoRelDir: "mydir",
TerraformVersion: projVersion,
ProjectName: "myproject",
ProjectName: c.ProjectName,
}
out, err := r.Run(ctx, c.Command, tmpDir)
if c.ExpErr != "" {
Expand Down
4 changes: 2 additions & 2 deletions server/events/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package runtime

import (
"fmt"
"github.com/pkg/errors"
"regexp"
"strings"

"github.com/hashicorp/go-version"
version "github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/terraform"
"github.com/runatlantis/atlantis/server/logging"
Expand Down

0 comments on commit cf490f8

Please sign in to comment.