|
| 1 | +package getter |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + testing_helper "github.com/hashicorp/go-getter/v2/helper/testing" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +const basicMainTFExpectedContents = `# Hello |
| 12 | +
|
| 13 | +module "foo" { |
| 14 | + source = "./foo" |
| 15 | +} |
| 16 | +` |
| 17 | + |
| 18 | +func TestGitGetter_githubDirWithModeAny(t *testing.T) { |
| 19 | + if !testHasGit { |
| 20 | + t.Skip("git not found, skipping") |
| 21 | + } |
| 22 | + |
| 23 | + ctx := context.Background() |
| 24 | + dst := testing_helper.TempDir(t) |
| 25 | + defer os.RemoveAll(dst) |
| 26 | + |
| 27 | + req := &Request{ |
| 28 | + Src: "git::https://github.com/arikkfir/go-getter.git//testdata/basic?ref=v2", |
| 29 | + Dst: dst, |
| 30 | + GetMode: ModeAny, |
| 31 | + Copy: true, |
| 32 | + } |
| 33 | + client := Client{} |
| 34 | + result, err := client.Get(ctx, req) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("Failed fetching GitHub directory: %s", err) |
| 37 | + } else if stat, err := os.Stat(result.Dst); err != nil { |
| 38 | + t.Fatalf("Failed stat dst at '%s': %s", result.Dst, err) |
| 39 | + } else if !stat.IsDir() { |
| 40 | + t.Fatalf("Expected '%s' to be a directory", result.Dst) |
| 41 | + } else if entries, err := os.ReadDir(result.Dst); err != nil { |
| 42 | + t.Fatalf("Failed listing directory '%s': %s", result.Dst, err) |
| 43 | + } else if len(entries) != 3 { |
| 44 | + t.Fatalf("Expected dir '%s' to contain 3 items: %s", result.Dst, err) |
| 45 | + } else { |
| 46 | + testing_helper.AssertContents(t, filepath.Join(result.Dst, "main.tf"), basicMainTFExpectedContents) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func TestGitGetter_githubFileWithModeAny(t *testing.T) { |
| 51 | + if !testHasGit { |
| 52 | + t.Skip("git not found, skipping") |
| 53 | + } |
| 54 | + |
| 55 | + ctx := context.Background() |
| 56 | + dst := testing_helper.TempDir(t) |
| 57 | + defer os.RemoveAll(dst) |
| 58 | + |
| 59 | + req := &Request{ |
| 60 | + Src: "git::https://github.com/arikkfir/go-getter.git//testdata/basic/main.tf?ref=v2", |
| 61 | + Dst: dst, |
| 62 | + GetMode: ModeAny, |
| 63 | + Copy: true, |
| 64 | + } |
| 65 | + client := Client{} |
| 66 | + result, err := client.Get(ctx, req) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed fetching GitHub file: %s", err) |
| 69 | + } else if stat, err := os.Stat(result.Dst); err != nil { |
| 70 | + t.Fatalf("Failed stat dst at '%s': %s", result.Dst, err) |
| 71 | + } else if stat.IsDir() { |
| 72 | + t.Fatalf("Expected '%s' to be a file", result.Dst) |
| 73 | + } else { |
| 74 | + testing_helper.AssertContents(t, result.Dst, basicMainTFExpectedContents) |
| 75 | + } |
| 76 | +} |
0 commit comments