Skip to content

Commit d33c4b2

Browse files
Since the tests in go are run in parallel, there is a problem with creating directories.
So I made one of the tests a simple function, and then they run fine.
1 parent 5bed066 commit d33c4b2

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

internal/pkg/git/tests/git_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,19 @@ import (
88
"testing"
99
)
1010

11-
//TODO the tests run and run fine individually, but if you run them together,
12-
// you get confused with the test directories.
13-
14-
func TestCreateLocalGitRepository_CurrentDir(t *testing.T) {
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) {
1514
t.Log("create dir for test")
1615
{
1716
if err := os.Mkdir(mock.TestDir, 0755); err != nil {
1817
t.Fatalf("Failed to create test directory: %v", err)
1918
}
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-
}()
3119
}
3220

33-
t.Log("create repository in current dir")
21+
t.Log("create repository in new dir")
3422
{
35-
if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsTrue{}); err != nil {
23+
if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsFalse{}); err != nil {
3624
t.Fatalf("Expected no error, but got: %s", err)
3725
}
3826
}
@@ -48,16 +36,28 @@ func TestCreateLocalGitRepository_CurrentDir(t *testing.T) {
4836
t.Log("SUCCESS")
4937
}
5038

51-
func TestCreateLocalGitRepository_NewDir(t *testing.T) {
39+
func TestCreateLocalGitRepository_CurrentDir(t *testing.T) {
40+
testCreateLocalGitRepositoryNewdir(t)
41+
5242
t.Log("create dir for test")
5343
{
5444
if err := os.Mkdir(mock.TestDir, 0755); err != nil {
5545
t.Fatalf("Failed to create test directory: %v", err)
5646
}
57-
defer os.RemoveAll(mock.TestDir)
47+
defer os.RemoveAll(fmt.Sprintf("../%s", mock.TestDir))
48+
49+
if err := os.Chdir(mock.TestDir); err != nil {
50+
t.Fatalf("Expected no error, but got: %s", err)
51+
}
52+
53+
defer func() {
54+
if err := os.Chdir(".."); err != nil {
55+
t.Fatalf("Expected no error, but got: %s", err)
56+
}
57+
}()
5858
}
5959

60-
t.Log("create repository in new dir")
60+
t.Log("create repository in current dir")
6161
{
6262
if err := git.CreateLocalGitRepository(mock.IsCurrentDirIsTrue{}); err != nil {
6363
t.Fatalf("Expected no error, but got: %s", err)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package mock
22

3-
type IsCurrentDirIsFalse struct{}
4-
53
const TestDir = "test_dir"
64

5+
type IsCurrentDirIsFalse struct{}
6+
77
func (i IsCurrentDirIsFalse) GetProjectName() (string, bool, error) {
88
return TestDir, false, nil
99
}

0 commit comments

Comments
 (0)