Skip to content

Commit 86c24af

Browse files
optimized the writing of tests and put layouts in the right place, so you can reuse them and not get tangled up in packages
1 parent 1d45fab commit 86c24af

File tree

2 files changed

+50
-57
lines changed

2 files changed

+50
-57
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
package mock
1+
package mocks
22

3-
const TestDir = "test_dir"
3+
const ProjName = "new"
44

55
type IsCurrentDirIsFalse struct{}
66

77
func (i IsCurrentDirIsFalse) GetProjectName() (string, bool, error) {
8-
return TestDir, false, nil
8+
return ProjName, false, nil
99
}
1010

1111
type IsCurrentDirIsTrue struct{}
1212

1313
func (i IsCurrentDirIsTrue) GetProjectName() (string, bool, error) {
1414
return "", true, nil
15-
}
15+
}

internal/pkg/git/tests/git_test.go

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,68 @@ package tests
22

33
import (
44
"fmt"
5-
"github.com/blackmarllboro/create-project-struct/internal/pkg/git"
6-
"github.com/blackmarllboro/create-project-struct/internal/pkg/git/tests/mock"
75
"os"
86
"testing"
9-
)
107

11-
// Since the tests in Go are run in parallel, there is a problem with creating
12-
// dirs. So i made one of the tests a simple function, and then they run fine.
13-
func testCreateLocalGitRepositoryNewdir(t *testing.T) {
14-
t.Log("create dir for test")
15-
{
16-
if err := os.Mkdir(mock.TestDir, 0755); err != nil {
17-
t.Fatalf("Failed to create test directory: %v", err)
18-
}
19-
}
8+
"github.com/blackmarllboro/create-project-struct/internal/pkg/args"
9+
"github.com/blackmarllboro/create-project-struct/internal/pkg/args/tests/mocks"
10+
"github.com/blackmarllboro/create-project-struct/internal/pkg/git"
11+
)
2012

21-
t.Log("create repository in new dir")
22-
{
23-
if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsFalse{}); err != nil {
24-
t.Fatalf("Expected no error, but got: %s", err)
25-
}
26-
}
13+
func TestCreateLocalGitRepository(t *testing.T) {
14+
const currentDir = "current_dir"
2715

28-
t.Log("check the .git in the current directory")
29-
{
30-
_, err := os.Stat(".git")
31-
if os.IsNotExist(err) {
32-
t.Fatal(".git directory not found in the expected location")
33-
}
16+
data := []struct {
17+
name string
18+
mock args.GetProjectName
19+
}{
20+
{
21+
name: currentDir,
22+
mock: mocks.IsCurrentDirIsTrue{},
23+
},
24+
{
25+
name: mocks.ProjName,
26+
mock: mocks.IsCurrentDirIsFalse{},
27+
},
3428
}
3529

36-
t.Log("SUCCESS")
37-
}
30+
for _, dataT := range data {
31+
{
32+
t.Log("create dir for ", dataT.name)
33+
{
34+
if err := os.Mkdir(dataT.name, 0755); err != nil {
35+
t.Fatalf("Failed to create test directory: %v", err)
36+
}
3837

39-
func TestCreateLocalGitRepository_CurrentDir(t *testing.T) {
40-
testCreateLocalGitRepositoryNewdir(t)
38+
if dataT.name == currentDir {
39+
if err := os.Chdir(fmt.Sprintf("./%s", currentDir)); err != nil {
40+
t.Fatalf("Expected no error, but got: %s", err)
41+
}
42+
}
43+
}
4144

42-
t.Log("create dir for test")
43-
{
44-
if err := os.Mkdir(mock.TestDir, 0755); err != nil {
45-
t.Fatalf("Failed to create test directory: %v", err)
46-
}
47-
defer os.RemoveAll(fmt.Sprintf("../%s", mock.TestDir))
45+
t.Log("create repository in dir: ", dataT.name)
46+
{
47+
if err := git.CreateLocalGitRepository(dataT.mock); err != nil {
48+
t.Fatalf("Expected no error, but got: %s", err)
49+
}
50+
}
4851

49-
if err := os.Chdir(mock.TestDir); err != nil {
50-
t.Fatalf("Expected no error, but got: %s", err)
51-
}
52+
t.Log("check the .git in the directory: ", dataT.name)
53+
{
54+
_, err := os.Stat(".git")
55+
if os.IsNotExist(err) {
56+
t.Fatal(".git directory not found in the expected location")
57+
}
58+
}
5259

53-
defer func() {
5460
if err := os.Chdir(".."); err != nil {
5561
t.Fatalf("Expected no error, but got: %s", err)
5662
}
57-
}()
58-
}
5963

60-
t.Log("create repository in current dir")
61-
{
62-
if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsTrue{}); err != nil {
63-
t.Fatalf("Expected no error, but got: %s", err)
64-
}
65-
}
64+
os.RemoveAll(fmt.Sprintf("./%s", dataT.name))
6665

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")
66+
t.Log("SUCCESS")
7267
}
7368
}
74-
75-
t.Log("SUCCESS")
7669
}

0 commit comments

Comments
 (0)