|
| 1 | +package getter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + tggetter "github.com/gruntwork-io/terragrunt/internal/getter" |
| 13 | + "github.com/gruntwork-io/terragrunt/internal/vfs" |
| 14 | + getter "github.com/hashicorp/go-getter/v2" |
| 15 | +) |
| 16 | + |
| 17 | +// TestFileCopyGetterHandlesSpacesInLocalPaths pins that a local source whose |
| 18 | +// path contains spaces still resolves. The URL reaching the getter carries |
| 19 | +// that path percent-encoded (`file:///projects/my%20local%20module`), so |
| 20 | +// recovering the filesystem path from it is the getter's job, and it has to |
| 21 | +// arrive at the same answer go-getter's own FileGetter would. |
| 22 | +func TestFileCopyGetterHandlesSpacesInLocalPaths(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + base := t.TempDir() |
| 26 | + srcDir := filepath.Join(base, "my local module") |
| 27 | + |
| 28 | + require.NoError(t, os.MkdirAll(srcDir, 0o755)) |
| 29 | + require.NoError(t, os.WriteFile(filepath.Join(srcDir, "main.tf"), []byte("# hi\n"), 0o644)) |
| 30 | + |
| 31 | + client := &getter.Client{ |
| 32 | + Getters: []getter.Getter{tggetter.NewFileCopyGetter(vfs.NewOSFS())}, |
| 33 | + } |
| 34 | + |
| 35 | + testCases := []struct { |
| 36 | + name string |
| 37 | + src string |
| 38 | + dst string |
| 39 | + want string |
| 40 | + mode getter.Mode |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "single file", |
| 44 | + src: "file://" + filepath.Join(srcDir, "main.tf"), |
| 45 | + dst: filepath.Join(base, "out-file", "main.tf"), |
| 46 | + mode: getter.ModeFile, |
| 47 | + want: filepath.Join(base, "out-file", "main.tf"), |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "directory", |
| 51 | + src: "file://" + srcDir, |
| 52 | + dst: filepath.Join(base, "out-dir"), |
| 53 | + mode: getter.ModeDir, |
| 54 | + want: filepath.Join(base, "out-dir", "main.tf"), |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "mode decided by the getter", |
| 58 | + src: "file://" + srcDir, |
| 59 | + dst: filepath.Join(base, "out-any"), |
| 60 | + mode: getter.ModeAny, |
| 61 | + want: filepath.Join(base, "out-any", "main.tf"), |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range testCases { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + |
| 69 | + _, err := client.Get(context.Background(), &getter.Request{ |
| 70 | + Src: tc.src, |
| 71 | + Dst: tc.dst, |
| 72 | + GetMode: tc.mode, |
| 73 | + }) |
| 74 | + require.NoError(t, err) |
| 75 | + |
| 76 | + contents, err := os.ReadFile(tc.want) |
| 77 | + require.NoError(t, err) |
| 78 | + assert.Equal(t, "# hi\n", string(contents)) |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
0 commit comments