diff --git a/.changes/unreleased/BUG FIXES-20230404-100828.yaml b/.changes/unreleased/BUG FIXES-20230404-100828.yaml new file mode 100644 index 000000000..65b69ab7f --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20230404-100828.yaml @@ -0,0 +1,5 @@ +kind: BUG FIXES +body: 'helper/resource: Fix path used when persisting working directory' +time: 2023-04-04T10:08:28.3828+01:00 +custom: + Issue: "113" diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index d671a0a6f..da4785f0a 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -460,11 +460,13 @@ func copyWorkingDir(ctx context.Context, t testing.T, stepNumber int, wd *plugin } workingDir := wd.GetHelper().WorkingDirectory() - parentDir := filepath.Dir(workingDir) - dest := parentDir + "_" + strconv.Itoa(stepNumber) + dest := filepath.Join(workingDir, fmt.Sprintf("%s%s", "step_", strconv.Itoa(stepNumber))) - err := plugintest.CopyDir(wd.GetHelper().WorkingDirectory(), dest) + baseDir := wd.BaseDir() + rootBaseDir := strings.TrimLeft(baseDir, workingDir) + + err := plugintest.CopyDir(workingDir, dest, rootBaseDir) if err != nil { logging.HelperResourceError(ctx, "Unexpected error copying working directory files", diff --git a/helper/resource/teststep_providers_test.go b/helper/resource/teststep_providers_test.go index 986cf7b0c..44960abc0 100644 --- a/helper/resource/teststep_providers_test.go +++ b/helper/resource/teststep_providers_test.go @@ -1819,10 +1819,8 @@ func TestTest_TestStep_ProviderFactories_Import_External_WithPersistMatch_WithPe Steps: testSteps, }) - workingDirPath := filepath.Dir(workingDir) - for testStepIndex := range testSteps { - dir := workingDirPath + "_" + strconv.Itoa(testStepIndex+1) + dir := filepath.Join(workingDir, fmt.Sprintf("step_%s", strconv.Itoa(testStepIndex+1))) dirEntries, err := os.ReadDir(dir) if err != nil { @@ -1929,10 +1927,8 @@ func TestTest_TestStep_ProviderFactories_Import_External_WithoutPersistNonMatch_ Steps: testSteps, }) - workingDirPath := filepath.Dir(workingDir) - for testStepIndex := range testSteps { - dir := workingDirPath + "_" + strconv.Itoa(testStepIndex+1) + dir := filepath.Join(workingDir, fmt.Sprintf("step_%s", strconv.Itoa(testStepIndex+1))) dirEntries, err := os.ReadDir(dir) if err != nil { @@ -2075,10 +2071,8 @@ func TestTest_TestStep_ProviderFactories_CopyWorkingDir_EachTestStep(t *testing. Steps: testSteps, }) - workingDirPath := filepath.Dir(workingDir) - for k := range testSteps { - dir := workingDirPath + "_" + strconv.Itoa(k+1) + dir := filepath.Join(workingDir, fmt.Sprintf("step_%s", strconv.Itoa(k+1))) _, err := os.ReadDir(dir) if err != nil { diff --git a/internal/plugintest/helper.go b/internal/plugintest/helper.go index 04b12fe9c..ef14a0596 100644 --- a/internal/plugintest/helper.go +++ b/internal/plugintest/helper.go @@ -92,6 +92,10 @@ func InitHelper(ctx context.Context, config *Config) (*Helper, error) { // Call this before returning from TestMain to minimize the amount of detritus // left behind in the filesystem after the tests complete. func (h *Helper) Close() error { + if os.Getenv(EnvTfAccPersistWorkingDir) != "" { + return nil + } + if h.execTempDir != "" { err := os.RemoveAll(h.execTempDir) if err != nil { diff --git a/internal/plugintest/util.go b/internal/plugintest/util.go index bd1d00abb..067dc74af 100644 --- a/internal/plugintest/util.go +++ b/internal/plugintest/util.go @@ -9,6 +9,7 @@ import ( "os" "path" "path/filepath" + "strings" ) func symlinkFile(src string, dest string) error { @@ -100,7 +101,7 @@ func CopyFile(src, dest string) error { // CopyDir recursively copies directories and files // from src to dest. -func CopyDir(src string, dest string) error { +func CopyDir(src, dest, baseDirName string) error { srcInfo, err := os.Stat(src) if err != nil { return fmt.Errorf("unable to stat: %w", err) @@ -119,8 +120,12 @@ func CopyDir(src string, dest string) error { srcFilepath := path.Join(src, dirEntry.Name()) destFilepath := path.Join(dest, dirEntry.Name()) + if !strings.Contains(srcFilepath, baseDirName) { + continue + } + if dirEntry.IsDir() { - if err = CopyDir(srcFilepath, destFilepath); err != nil { + if err = CopyDir(srcFilepath, destFilepath, baseDirName); err != nil { return fmt.Errorf("unable to copy directory: %w", err) } } else { diff --git a/internal/plugintest/util_test.go b/internal/plugintest/util_test.go index 58c0dc12f..ceb3badc0 100644 --- a/internal/plugintest/util_test.go +++ b/internal/plugintest/util_test.go @@ -50,7 +50,7 @@ func TestCopyDir(t *testing.T) { t.Fatalf("cannot create src file: %s", err) } - err = CopyDir(srcDir, srcDir+"_1") + err = CopyDir(srcDir, srcDir+"_1", "") if err != nil { t.Fatalf("cannot copy dir: %s", err) } diff --git a/internal/plugintest/working_dir.go b/internal/plugintest/working_dir.go index 734b6c9b5..9ee2046f2 100644 --- a/internal/plugintest/working_dir.go +++ b/internal/plugintest/working_dir.go @@ -47,10 +47,19 @@ type WorkingDir struct { reattachInfo tfexec.ReattachInfo } +// BaseDir returns the path to the root of the working directory tree. +func (wd *WorkingDir) BaseDir() string { + return wd.baseDir +} + // Close deletes the directories and files created to represent the receiving // working directory. After this method is called, the working directory object // is invalid and may no longer be used. func (wd *WorkingDir) Close() error { + if os.Getenv(EnvTfAccPersistWorkingDir) != "" { + return nil + } + return os.RemoveAll(wd.baseDir) }