test: Update `TestInit_fromModule_dstInSrc` to reflect current behaviour when -from-module references the location of the current directory (#38059)

* test: Update `TestInit_fromModule_dstInSrc` to use modern `t.Chdir` approach, add context through comments

* test: Add context about how `TestInit_fromModule_dstInSrc` has been refactored since it was first created

Originally the test's args were []string{".", "foo"} (https://github.com/hashicorp/terraform/pull/547), where the second arg is the directory TF should use as the root directory. Then the test was changed to use []string{"-from-module=.", "foo"} when the -from-module flag was added in 8a7a0a7459. Finally, the second arg was removed in https://github.com/hashicorp/terraform/pull/27664, and replaced with the use of os.Chdir in this test.

* test: Update `TestInit_fromModule_dstInSrc` to define the new behavior of -from-module in this scenario; the current directory is copied once, but not recursively.

If you run this test from commit 84f5b863ef with a debugger we can see that foo is never copied into the current directory foo (./foo/foo is never made). Now, after refactoring ./foo/foo IS made, but it's not copied recursively like in the original issue.

* docs: Add comment about emergent behavior when using -chdir with -from-module in this scenario
pull/38063/head
Sarah French 4 months ago committed by GitHub
parent 1186262c8c
commit 827f693e32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -324,23 +324,19 @@ func TestInit_fromModule_cwdDest(t *testing.T) {
}
}
// https://github.com/hashicorp/terraform/issues/518
// Regression test to check that Terraform doesn't recursively copy
// a directory when the source module includes the current directory.
// See: https://github.com/hashicorp/terraform/issues/518
func TestInit_fromModule_dstInSrc(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("err: %s", err)
}
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(dir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
// Change to a temporary directory
td := t.TempDir()
t.Chdir(td)
// Create contents
// .
// ├── issue518.tf
// └── foo/
// └── (empty)
if err := os.Mkdir("foo", os.ModePerm); err != nil {
t.Fatal(err)
}
@ -349,6 +345,11 @@ func TestInit_fromModule_dstInSrc(t *testing.T) {
t.Fatalf("err: %s", err)
}
// Instead of using the -chdir flag, we change directory into the directory foo.
// .
// ├── issue518.tf
// └── foo/ << current directory
// └── (empty)
if err := os.Chdir("foo"); err != nil {
t.Fatalf("err: %s", err)
}
@ -363,6 +364,7 @@ func TestInit_fromModule_dstInSrc(t *testing.T) {
},
}
// The path ./.. includes the current directory foo.
args := []string{
"-from-module=./..",
}
@ -370,9 +372,33 @@ func TestInit_fromModule_dstInSrc(t *testing.T) {
t.Fatalf("bad: \n%s", done(t).All())
}
if _, err := os.Stat(filepath.Join(dir, "foo", "issue518.tf")); err != nil {
// Assert this outcome
// .
// ├── issue518.tf
// └── foo/ << current directory
// ├── issue518.tf
// └── foo/
// └── (empty)
if _, err := os.Stat(filepath.Join(td, "foo", "issue518.tf")); err != nil {
t.Fatalf("err: %s", err)
}
if _, err := os.Stat(filepath.Join(td, "foo", "foo")); err != nil {
// Note: originally foo was never copied into itself in this scenario,
// but behavior changed sometime around when -chdir replaced legacy positional
// path arguments. We may want to revert to the original behavior in a
// future major release.
// See: https://github.com/hashicorp/terraform/pull/38059
t.Fatalf("err: %s", err)
}
// We don't expect foo to be copied into itself multiple times
_, err := os.Stat(filepath.Join(td, "foo", "foo", "foo"))
if err == nil {
t.Fatal("expected directory ./foo/foo/foo to not exist, but it does")
}
if _, ok := err.(*os.PathError); !ok {
t.Fatalf("unexpected err: %s", err)
}
}
func TestInit_get(t *testing.T) {

Loading…
Cancel
Save