From 827f693e32061fdcb71c5ffd17e588a1bd850c5c Mon Sep 17 00:00:00 2001 From: Sarah French <15078782+SarahFrench@users.noreply.github.com> Date: Wed, 14 Jan 2026 15:57:10 +0000 Subject: [PATCH] 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 https://github.com/hashicorp/terraform/commit/8a7a0a74597ce2edde783d706d8402ccfbb7101e. 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 84f5b863efa139cefb956afbf750a78883360346 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 --- internal/command/init_test.go | 58 +++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 16 deletions(-) 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) {