diff --git a/internal/command/init_test.go b/internal/command/init_test.go index 1bb6a401d6..a522e49912 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -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) {