|
1 | 1 | package tests |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "github.com/blackmarllboro/create-project-struct/internal/pkg/git" |
5 | 6 | "github.com/blackmarllboro/create-project-struct/internal/pkg/git/tests/mock" |
6 | | - |
7 | 7 | "os" |
8 | 8 | "testing" |
9 | 9 | ) |
10 | 10 |
|
11 | | -// TODO we need to figure out how to create and navigate to the test dirs. |
| 11 | +//TODO the tests run and run fine individually, but if you run them together, |
| 12 | +// you get confused with the test directories. |
| 13 | + |
12 | 14 | func TestCreateLocalGitRepository_CurrentDir(t *testing.T) { |
13 | | - if err := os.Mkdir(mock.TestDir, 0755); err != nil { |
14 | | - t.Fatalf("Failed to create test directory: %v", err) |
| 15 | + t.Log("create dir for test") |
| 16 | + { |
| 17 | + if err := os.Mkdir(mock.TestDir, 0755); err != nil { |
| 18 | + t.Fatalf("Failed to create test directory: %v", err) |
| 19 | + } |
| 20 | + defer os.RemoveAll(fmt.Sprintf("../%s", mock.TestDir)) |
| 21 | + |
| 22 | + if err := os.Chdir(mock.TestDir); err != nil { |
| 23 | + t.Fatalf("Expected no error, but got: %s", err) |
| 24 | + } |
| 25 | + |
| 26 | + defer func() { |
| 27 | + if err := os.Chdir(".."); err != nil { |
| 28 | + t.Fatalf("Expected no error, but got: %s", err) |
| 29 | + } |
| 30 | + }() |
15 | 31 | } |
16 | | - defer os.RemoveAll(mock.TestDir) |
17 | 32 |
|
18 | | - if err := os.Chdir(mock.TestDir); err != nil { |
19 | | - t.Fatalf("Expected no error, but got: %s", err) |
| 33 | + t.Log("create repository in current dir") |
| 34 | + { |
| 35 | + if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsTrue{}); err != nil { |
| 36 | + t.Fatalf("Expected no error, but got: %s", err) |
| 37 | + } |
20 | 38 | } |
21 | 39 |
|
22 | | - if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsTrue{}); err != nil { |
23 | | - t.Fatalf("Expected no error, but got: %s", err) |
| 40 | + t.Log("check the .git in the current directory") |
| 41 | + { |
| 42 | + _, err := os.Stat(".git") |
| 43 | + if os.IsNotExist(err) { |
| 44 | + t.Fatal(".git directory not found in the expected location") |
| 45 | + } |
24 | 46 | } |
25 | 47 |
|
26 | | - _, err := os.Stat(mock.TestDir) |
27 | | - if os.IsNotExist(err) { |
28 | | - t.Fatal(".git directory not found in the expected location") |
| 48 | + t.Log("SUCCESS") |
| 49 | +} |
| 50 | + |
| 51 | +func TestCreateLocalGitRepository_NewDir(t *testing.T) { |
| 52 | + t.Log("create dir for test") |
| 53 | + { |
| 54 | + if err := os.Mkdir(mock.TestDir, 0755); err != nil { |
| 55 | + t.Fatalf("Failed to create test directory: %v", err) |
| 56 | + } |
| 57 | + defer os.RemoveAll(mock.TestDir) |
29 | 58 | } |
| 59 | + |
| 60 | + t.Log("create repository in new dir") |
| 61 | + { |
| 62 | + if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsTrue{}); err != nil { |
| 63 | + t.Fatalf("Expected no error, but got: %s", err) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + t.Log("check the .git in the current directory") |
| 68 | + { |
| 69 | + _, err := os.Stat(".git") |
| 70 | + if os.IsNotExist(err) { |
| 71 | + t.Fatal(".git directory not found in the expected location") |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + t.Log("SUCCESS") |
30 | 76 | } |
0 commit comments