Skip to content

Commit 5bed066

Browse files
author
blackmarllbor0
committed
the tests run and run fine individually, but if you run them together, you get confused with the test directories.
1 parent 23a6832 commit 5bed066

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

internal/pkg/git/git.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package git
22

33
import (
44
"fmt"
5-
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
65
"os"
76
"os/exec"
7+
8+
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
89
)
910

1011
func CreateLocalGitRepository(projectName args.GetProjectName) error {

internal/pkg/git/tests/git_test.go

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,76 @@
11
package tests
22

33
import (
4+
"fmt"
45
"github.com/blackmarllboro/create-project-struct/internal/pkg/git"
56
"github.com/blackmarllboro/create-project-struct/internal/pkg/git/tests/mock"
6-
77
"os"
88
"testing"
99
)
1010

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+
1214
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+
}()
1531
}
16-
defer os.RemoveAll(mock.TestDir)
1732

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+
}
2038
}
2139

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+
}
2446
}
2547

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)
2958
}
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")
3076
}

internal/pkg/git/tests/mock/name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ func (i IsCurrentDirIsFalse) GetProjectName() (string, bool, error) {
1111
type IsCurrentDirIsTrue struct{}
1212

1313
func (i IsCurrentDirIsTrue) GetProjectName() (string, bool, error) {
14-
return TestDir, false, nil
14+
return "", true, nil
1515
}

0 commit comments

Comments
 (0)