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

Fix path used when persisting working directory #113

Merged
merged 4 commits into from
Apr 5, 2023
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
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20230404-100828.yaml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 5 additions & 3 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 3 additions & 9 deletions helper/resource/teststep_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/plugintest/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions internal/plugintest/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path"
"path/filepath"
"strings"
)

func symlinkFile(src string, dest string) error {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugintest/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions internal/plugintest/working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down