Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.TempDir to create temporary test directory #2671

Merged
merged 1 commit into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ func TestGitHubWorkflow(t *testing.T) {

ctrl, vcsClient, githubGetter, atlantisWorkspace := setupE2E(t, c.RepoDir)
// Set the repo to be cloned through the testing backdoor.
repoDir, headSHA, cleanup := initializeRepo(t, c.RepoDir)
defer cleanup()
repoDir, headSHA := initializeRepo(t, c.RepoDir)
atlantisWorkspace.TestingOverrideHeadCloneURL = fmt.Sprintf("file://%s", repoDir)

// Setup test dependencies.
Expand Down Expand Up @@ -544,8 +543,7 @@ func TestSimlpleWorkflow_terraformLockFile(t *testing.T) {

ctrl, vcsClient, githubGetter, atlantisWorkspace := setupE2E(t, c.RepoDir)
// Set the repo to be cloned through the testing backdoor.
repoDir, headSHA, cleanup := initializeRepo(t, c.RepoDir)
defer cleanup()
repoDir, headSHA := initializeRepo(t, c.RepoDir)

oldLockFilePath, err := filepath.Abs(filepath.Join("testfixtures", "null_provider_lockfile_old_version"))
Ok(t, err)
Expand Down Expand Up @@ -789,8 +787,7 @@ func TestGitHubWorkflowWithPolicyCheck(t *testing.T) {
ctrl, vcsClient, githubGetter, atlantisWorkspace := setupE2E(t, c.RepoDir)

// Set the repo to be cloned through the testing backdoor.
repoDir, headSHA, cleanup := initializeRepo(t, c.RepoDir)
defer cleanup()
repoDir, headSHA := initializeRepo(t, c.RepoDir)
atlantisWorkspace.TestingOverrideHeadCloneURL = fmt.Sprintf("file://%s", repoDir)

// Setup test dependencies.
Expand Down Expand Up @@ -866,8 +863,7 @@ func TestGitHubWorkflowWithPolicyCheck(t *testing.T) {

func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsController, *vcsmocks.MockClient, *mocks.MockGithubPullGetter, *events.FileWorkspace) {
allowForkPRs := false
dataDir, binDir, cacheDir, cleanup := mkSubDirs(t)
defer cleanup()
dataDir, binDir, cacheDir := mkSubDirs(t)

//env vars

Expand Down Expand Up @@ -1259,11 +1255,11 @@ func absRepoPath(t *testing.T, repoDir string) string {
// The purpose of this function is to create a real git repository with a branch
// called 'branch' from the files under repoDir. This is so we can check in
// those files normally to this repo without needing a .git directory.
func initializeRepo(t *testing.T, repoDir string) (string, string, func()) {
func initializeRepo(t *testing.T, repoDir string) (string, string) {
originRepo := absRepoPath(t, repoDir)

// Copy the files to the temp dir.
destDir, cleanup := TempDir(t)
destDir := t.TempDir()
runCmd(t, "", "cp", "-r", fmt.Sprintf("%s/.", originRepo), destDir)

// Initialize the git repo.
Expand All @@ -1279,7 +1275,7 @@ func initializeRepo(t *testing.T, repoDir string) (string, string, func()) {
headSHA := runCmd(t, destDir, "git", "rev-parse", "HEAD")
headSHA = strings.Trim(headSHA, "\n")

return destDir, headSHA, cleanup
return destDir, headSHA
}

func runCmd(t *testing.T, dir string, name string, args ...string) string {
Expand Down Expand Up @@ -1348,8 +1344,8 @@ func assertCommentEquals(t *testing.T, expReplies []string, act string, repoDir
}

// returns parent, bindir, cachedir, cleanup func
func mkSubDirs(t *testing.T) (string, string, string, func()) {
tmp, cleanup := TempDir(t)
func mkSubDirs(t *testing.T) (string, string, string) {
tmp := t.TempDir()
binDir := filepath.Join(tmp, "bin")
err := os.MkdirAll(binDir, 0700)
Ok(t, err)
Expand All @@ -1358,7 +1354,7 @@ func mkSubDirs(t *testing.T) (string, string, string, func()) {
err = os.MkdirAll(cachedir, 0700)
Ok(t, err)

return tmp, binDir, cachedir, cleanup
return tmp, binDir, cachedir
}

// Will fail test if conftest isn't in path and isn't version >= 0.25.0
Expand Down
9 changes: 3 additions & 6 deletions server/controllers/locks_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ func TestDeleteLock_UpdateProjectStatus(t *testing.T) {
},
}, nil)
var backend locking.Backend
tmp, cleanup := TempDir(t)
defer cleanup()
tmp := t.TempDir()
backend, err := db.New(tmp)
Ok(t, err)
// Seed the DB with a successful plan for that project (that is later discarded).
Expand Down Expand Up @@ -354,8 +353,7 @@ func TestDeleteLock_CommentFailed(t *testing.T) {
workingDir := mocks2.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
var backend locking.Backend
tmp, cleanup := TempDir(t)
defer cleanup()
tmp := t.TempDir()
backend, err := db.New(tmp)
Ok(t, err)
When(cp.CreateComment(AnyRepo(), AnyInt(), AnyString(), AnyString())).ThenReturn(errors.New("err"))
Expand All @@ -382,8 +380,7 @@ func TestDeleteLock_CommentSuccess(t *testing.T) {
workingDir := mocks2.NewMockWorkingDir()
workingDirLocker := events.NewDefaultWorkingDirLocker()
var backend locking.Backend
tmp, cleanup := TempDir(t)
defer cleanup()
tmp := t.TempDir()
backend, err := db.New(tmp)
Ok(t, err)
pull := models.PullRequest{
Expand Down
30 changes: 10 additions & 20 deletions server/core/config/parser_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ func TestHasRepoCfg_DirDoesNotExist(t *testing.T) {
}

func TestHasRepoCfg_FileDoesNotExist(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
r := config.ParserValidator{}
exists, err := r.HasRepoCfg(tmpDir)
Ok(t, err)
Equals(t, false, exists)
}

func TestHasRepoCfg_InvalidFileExtension(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
_, err := os.Create(filepath.Join(tmpDir, "atlantis.yml"))
Ok(t, err)

Expand All @@ -57,16 +55,14 @@ func TestParseRepoCfg_DirDoesNotExist(t *testing.T) {
}

func TestParseRepoCfg_FileDoesNotExist(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
r := config.ParserValidator{}
_, err := r.ParseRepoCfg(tmpDir, globalCfg, "", "")
Assert(t, os.IsNotExist(err), "exp not exist err")
}

func TestParseRepoCfg_BadPermissions(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
err := os.WriteFile(filepath.Join(tmpDir, "atlantis.yaml"), nil, 0000)
Ok(t, err)

Expand Down Expand Up @@ -96,8 +92,7 @@ func TestParseCfgs_InvalidYAML(t *testing.T) {
},
}

tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()

for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
Expand Down Expand Up @@ -1062,8 +1057,7 @@ workflows:
},
}

tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()

for _, c := range cases {
t.Run(c.description, func(t *testing.T) {
Expand All @@ -1085,8 +1079,7 @@ workflows:
// Test that we fail if the global validation fails. We test global validation
// more completely in GlobalCfg.ValidateRepoCfg().
func TestParseRepoCfg_GlobalValidation(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()

repoCfg := `
version: 3
Expand Down Expand Up @@ -1482,8 +1475,7 @@ workflows:
for name, c := range cases {
t.Run(name, func(t *testing.T) {
r := config.ParserValidator{}
tmp, cleanup := TempDir(t)
defer cleanup()
tmp := t.TempDir()
path := filepath.Join(tmp, "conf.yaml")
Ok(t, os.WriteFile(path, []byte(c.input), 0600))

Expand Down Expand Up @@ -1732,10 +1724,8 @@ func TestParseRepoCfg_V2ShellParsing(t *testing.T) {

for _, c := range cases {
t.Run(c.in, func(t *testing.T) {
v2Dir, cleanup2 := TempDir(t)
defer cleanup2()
v3Dir, cleanup3 := TempDir(t)
defer cleanup3()
v2Dir := t.TempDir()
v3Dir := t.TempDir()
v2Path := filepath.Join(v2Dir, "atlantis.yaml")
v3Path := filepath.Join(v3Dir, "atlantis.yaml")
cfg := fmt.Sprintf(`workflows:
Expand Down
6 changes: 2 additions & 4 deletions server/core/config/valid/global_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,7 @@ policies:
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
tmp, cleanup := TempDir(t)
defer cleanup()
tmp := t.TempDir()
var global valid.GlobalCfg
if c.gCfg != "" {
path := filepath.Join(tmp, "config.yaml")
Expand Down Expand Up @@ -857,8 +856,7 @@ repos:
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
tmp, cleanup := TempDir(t)
defer cleanup()
tmp := t.TempDir()
var global valid.GlobalCfg
if c.gCfg != "" {
path := filepath.Join(tmp, "config.yaml")
Expand Down
23 changes: 8 additions & 15 deletions server/core/db/boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ func TestGetLock(t *testing.T) {

// Test we can create a status and then getCommandLock it.
func TestPullStatus_UpdateGet(t *testing.T) {
b, cleanup := newTestDB2(t)
defer cleanup()
b := newTestDB2(t)

pull := models.PullRequest{
Num: 1,
Expand Down Expand Up @@ -483,8 +482,7 @@ func TestPullStatus_UpdateGet(t *testing.T) {
// Test we can create a status, delete it, and then we shouldn't be able to getCommandLock
// it.
func TestPullStatus_UpdateDeleteGet(t *testing.T) {
b, cleanup := newTestDB2(t)
defer cleanup()
b := newTestDB2(t)

pull := models.PullRequest{
Num: 1,
Expand Down Expand Up @@ -529,8 +527,7 @@ func TestPullStatus_UpdateDeleteGet(t *testing.T) {
// pull status, and when we getCommandLock all the project statuses, that specific project
// should be updated.
func TestPullStatus_UpdateProject(t *testing.T) {
b, cleanup := newTestDB2(t)
defer cleanup()
b := newTestDB2(t)

pull := models.PullRequest{
Num: 1,
Expand Down Expand Up @@ -593,8 +590,7 @@ func TestPullStatus_UpdateProject(t *testing.T) {
// Test that if we update an existing pull status and our new status is for a
// different HeadSHA, that we just overwrite the old status.
func TestPullStatus_UpdateNewCommit(t *testing.T) {
b, cleanup := newTestDB2(t)
defer cleanup()
b := newTestDB2(t)

pull := models.PullRequest{
Num: 1,
Expand Down Expand Up @@ -656,8 +652,7 @@ func TestPullStatus_UpdateNewCommit(t *testing.T) {
// Test that if we update an existing pull status and our new status is for a
// the same commit, that we merge the statuses.
func TestPullStatus_UpdateMerge(t *testing.T) {
b, cleanup := newTestDB2(t)
defer cleanup()
b := newTestDB2(t)

pull := models.PullRequest{
Num: 1,
Expand Down Expand Up @@ -796,13 +791,11 @@ func newTestDB() (*bolt.DB, *db.BoltDB) {
return boltDB, b
}

func newTestDB2(t *testing.T) (*db.BoltDB, func()) {
tmp, cleanup := TempDir(t)
func newTestDB2(t *testing.T) *db.BoltDB {
tmp := t.TempDir()
boltDB, err := db.New(tmp)
Ok(t, err)
return boltDB, func() {
cleanup()
}
return boltDB
}

func cleanupDB(db *bolt.DB) {
Expand Down
21 changes: 7 additions & 14 deletions server/core/runtime/apply_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func TestRun_NoDir(t *testing.T) {
}

func TestRun_NoPlanFile(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
o := runtime.ApplyStepRunner{
TerraformExecutor: nil,
}
Expand All @@ -49,8 +48,7 @@ func TestRun_NoPlanFile(t *testing.T) {
}

func TestRun_Success(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
err := os.WriteFile(planPath, nil, 0600)
logger := logging.NewNoopLogger(t)
Expand Down Expand Up @@ -80,8 +78,7 @@ func TestRun_Success(t *testing.T) {

func TestRun_AppliesCorrectProjectPlan(t *testing.T) {
// When running for a project, the planfile has a different name.
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "projectname-default.tfplan")
err := os.WriteFile(planPath, nil, 0600)

Expand Down Expand Up @@ -112,8 +109,7 @@ func TestRun_AppliesCorrectProjectPlan(t *testing.T) {
}

func TestRun_UsesConfiguredTFVersion(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
err := os.WriteFile(planPath, nil, 0600)
Ok(t, err)
Expand Down Expand Up @@ -199,8 +195,7 @@ func TestRun_UsingTarget(t *testing.T) {
descrip := fmt.Sprintf("comments flags: %s extra args: %s",
strings.Join(c.commentFlags, ", "), strings.Join(c.extraArgs, ", "))
t.Run(descrip, func(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
err := os.WriteFile(planPath, nil, 0600)
Ok(t, err)
Expand All @@ -227,8 +222,7 @@ func TestRun_UsingTarget(t *testing.T) {

// Test that apply works for remote applies.
func TestRun_RemoteApply_Success(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
planFileContents := `
An execution plan has been generated and is shown below.
Expand Down Expand Up @@ -285,8 +279,7 @@ Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

// Test that if the plan is different, we error out.
func TestRun_RemoteApply_PlanChanged(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
planPath := filepath.Join(tmpDir, "workspace.tfplan")
planFileContents := `
An execution plan has been generated and is shown below.
Expand Down
9 changes: 4 additions & 5 deletions server/core/runtime/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func runCmd(t *testing.T, dir string, name string, args ...string) string {
return string(cpOut)
}

func initRepo(t *testing.T) (string, func()) {
repoDir, cleanup := TempDir(t)
func initRepo(t *testing.T) string {
repoDir := t.TempDir()
runCmd(t, repoDir, "git", "init")
runCmd(t, repoDir, "touch", ".gitkeep")
runCmd(t, repoDir, "git", "add", ".gitkeep")
Expand All @@ -108,13 +108,12 @@ func initRepo(t *testing.T) (string, func()) {
runCmd(t, repoDir, "git", "config", "--local", "commit.gpgsign", "false")
runCmd(t, repoDir, "git", "commit", "-m", "initial commit")
runCmd(t, repoDir, "git", "branch", "branch")
return repoDir, cleanup
return repoDir
}

func TestIsFileTracked(t *testing.T) {
// Initialize the git repo.
repoDir, cleanup := initRepo(t)
defer cleanup()
repoDir := initRepo(t)

// file1 should not be tracked
tracked, err := IsFileTracked(repoDir, "file1")
Expand Down
3 changes: 1 addition & 2 deletions server/core/runtime/env_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func TestEnvStepRunner_Run(t *testing.T) {
}
for _, c := range cases {
t.Run(c.Command, func(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
tmpDir := t.TempDir()
ctx := command.ProjectContext{
BaseRepo: models.Repo{
Name: "basename",
Expand Down
Loading