|
1 | 1 | package git |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "log" |
4 | 5 | "os" |
5 | 6 | "testing" |
| 7 | + |
| 8 | + "github.com/joho/godotenv" |
6 | 9 | ) |
7 | 10 |
|
8 | 11 | func TestCloneRepo(t *testing.T) { |
| 12 | + err := godotenv.Load("../.env.test") |
| 13 | + |
| 14 | + if err != nil { |
| 15 | + log.Fatal("Could not load test env!") |
| 16 | + return |
| 17 | + } |
| 18 | + |
9 | 19 | t.Run("should clone the repo in the dest directory", func(t *testing.T) { |
10 | 20 | repo := "Hack4Impact-UMD/professor" |
11 | 21 | dest := t.TempDir() |
@@ -33,4 +43,60 @@ func TestCloneRepo(t *testing.T) { |
33 | 43 | } |
34 | 44 | }) |
35 | 45 |
|
| 46 | + t.Run("should clone private repo with PAT in the dest directory", func(t *testing.T) { |
| 47 | + repo := "rk234/RamyKaddouri-h4i-assessment-Spring25" |
| 48 | + dest := t.TempDir() |
| 49 | + |
| 50 | + t.Cleanup(func() { |
| 51 | + os.RemoveAll(dest) |
| 52 | + }) |
| 53 | + |
| 54 | + if err := CloneRepo(repo, dest, os.Getenv("GITHUB_PAT")); err != nil { |
| 55 | + t.Error("Error when cloning:", err) |
| 56 | + t.Fail() |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + files, err := os.ReadDir(dest) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + t.Error("Error when reading dest dir:", err) |
| 64 | + t.Fail() |
| 65 | + return |
| 66 | + } else if len(files) <= 0 { |
| 67 | + t.Error("Dest dir is empty") |
| 68 | + t.Fail() |
| 69 | + return |
| 70 | + } |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("should error when cloning private repo with bad PAT", func(t *testing.T) { |
| 74 | + repo := "rk234/RamyKaddouri-h4i-assessment-Spring25" |
| 75 | + dest := t.TempDir() |
| 76 | + |
| 77 | + t.Cleanup(func() { |
| 78 | + os.RemoveAll(dest) |
| 79 | + }) |
| 80 | + |
| 81 | + if err := CloneRepo(repo, dest, "abc:abc"); err != nil { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + t.Fail() |
| 86 | + }) |
| 87 | + |
| 88 | + t.Run("should error when cloning bad repo path", func(t *testing.T) { |
| 89 | + repo := "" |
| 90 | + dest := t.TempDir() |
| 91 | + |
| 92 | + t.Cleanup(func() { |
| 93 | + os.RemoveAll(dest) |
| 94 | + }) |
| 95 | + |
| 96 | + if err := CloneRepo(repo, dest, ""); err != nil { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + t.Fail() |
| 101 | + }) |
36 | 102 | } |
0 commit comments