From b80ae5e13edc34ac55a4f33f775ea8a2f68c4ac9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Mon, 6 Feb 2017 13:26:03 -0500 Subject: [PATCH] Add source path argument to testLockState The new test pattern is to chdir into a temp location for the test, but the prevents us from locating the testdata directory in the source. Add a source path to testLockState so we can find the statelocker.go source. --- command/apply_destroy_test.go | 2 +- command/apply_test.go | 2 +- command/command_test.go | 8 ++++++-- command/plan_test.go | 2 +- command/refresh_test.go | 2 +- command/taint_test.go | 2 +- command/untaint_test.go | 2 +- 7 files changed, 12 insertions(+), 8 deletions(-) diff --git a/command/apply_destroy_test.go b/command/apply_destroy_test.go index 6e1277ccaa..f8e37f8cb2 100644 --- a/command/apply_destroy_test.go +++ b/command/apply_destroy_test.go @@ -111,7 +111,7 @@ func TestApply_destroyLockedState(t *testing.T) { statePath := testStateFile(t, originalState) - unlock, err := testLockState(statePath) + unlock, err := testLockState("./testdata", statePath) if err != nil { t.Fatal(err) } diff --git a/command/apply_test.go b/command/apply_test.go index 3981b01ad1..86028d52fb 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -63,7 +63,7 @@ func TestApply(t *testing.T) { func TestApply_lockedState(t *testing.T) { statePath := testTempFile(t) - unlock, err := testLockState(statePath) + unlock, err := testLockState("./testdata", statePath) if err != nil { t.Fatal(err) } diff --git a/command/command_test.go b/command/command_test.go index d3bded69a8..abf134331b 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -535,7 +535,9 @@ func testRemoteState(t *testing.T, s *terraform.State, c int) (*terraform.Remote // testlockState calls a separate process to the lock the state file at path. // deferFunc should be called in the caller to properly unlock the file. -func testLockState(path string) (func(), error) { +// Since many tests change the working durectory, the sourcedir argument must be +// supplied to locate the statelocker.go source. +func testLockState(sourceDir, path string) (func(), error) { // build and run the binary ourselves so we can quickly terminate it for cleanup buildDir, err := ioutil.TempDir("", "locker") if err != nil { @@ -545,8 +547,10 @@ func testLockState(path string) (func(), error) { os.RemoveAll(buildDir) } + source := filepath.Join(sourceDir, "statelocker.go") lockBin := filepath.Join(buildDir, "statelocker") - out, err := exec.Command("go", "build", "-o", lockBin, "testdata/statelocker.go").CombinedOutput() + + out, err := exec.Command("go", "build", "-o", lockBin, source).CombinedOutput() if err != nil { cleanFunc() return nil, fmt.Errorf("%s %s", err, out) diff --git a/command/plan_test.go b/command/plan_test.go index b9e25bf22e..b42b07710a 100644 --- a/command/plan_test.go +++ b/command/plan_test.go @@ -46,7 +46,7 @@ func TestPlan_lockedState(t *testing.T) { } testPath := testFixturePath("plan") - unlock, err := testLockState(filepath.Join(testPath, DefaultStateFilename)) + unlock, err := testLockState("./testdata", filepath.Join(testPath, DefaultStateFilename)) if err != nil { t.Fatal(err) } diff --git a/command/refresh_test.go b/command/refresh_test.go index 4c79aec686..52aa33112d 100644 --- a/command/refresh_test.go +++ b/command/refresh_test.go @@ -63,7 +63,7 @@ func TestRefresh_lockedState(t *testing.T) { state := testState() statePath := testStateFile(t, state) - unlock, err := testLockState(statePath) + unlock, err := testLockState("./testdata", statePath) if err != nil { t.Fatal(err) } diff --git a/command/taint_test.go b/command/taint_test.go index 26c42b6a5d..9aa9b2664e 100644 --- a/command/taint_test.go +++ b/command/taint_test.go @@ -63,7 +63,7 @@ func TestTaint_lockedState(t *testing.T) { } statePath := testStateFile(t, state) - unlock, err := testLockState(statePath) + unlock, err := testLockState("./testdata", statePath) if err != nil { t.Fatal(err) } diff --git a/command/untaint_test.go b/command/untaint_test.go index ed63de78d4..80bf7bd72a 100644 --- a/command/untaint_test.go +++ b/command/untaint_test.go @@ -68,7 +68,7 @@ func TestUntaint_lockedState(t *testing.T) { }, } statePath := testStateFile(t, state) - unlock, err := testLockState(statePath) + unlock, err := testLockState("./testdata", statePath) if err != nil { t.Fatal(err) }