func CopyTerraformFolderToTempAndCleanUp()

in setuptest/tfutils.go [61:88]


func CopyTerraformFolderToTempAndCleanUp(t *testing.T, moduleDir string, testDir string) (string, func() error, error) {
	tmp := test_structure.CopyTerraformFolderToTemp(t, moduleDir, testDir)
	// We normalise, then work out the depth of the test directory relative
	// to the test so we know how many/ directories to go up to get to the root.
	// We can then delete the right directory when cleaning up.
	absTestPath := filepath.Join(moduleDir, testDir)
	relPath, err := filepath.Rel(moduleDir, absTestPath)
	if err != nil {
		err = fmt.Errorf("could not get relative path to test directory: %v", err)
		return "", nil, err
	}
	list := strings.Split(relPath, string(os.PathSeparator))
	depth := len(list)
	if len(list) > 1 || list[0] != "." {
		depth++
	}
	dir := tmp
	for i := 0; i < depth; i++ {
		dir = filepath.Dir(dir)
	}

	f := func() error {
		err := os.RemoveAll(dir)
		return err
	}

	return tmp, f, nil
}