-
Notifications
You must be signed in to change notification settings - Fork 31
/
initialiser.git_test.go
94 lines (84 loc) · 2.4 KB
/
initialiser.git_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type GitInitialiserTestSuite struct {
suite.Suite
gitInitialiser *GitInitialiser
pathWithGit string
pathWithoutGit string
logs bytes.Buffer
}
func TestGitInitialiserTestSuite(t *testing.T) {
suite.Run(t, new(GitInitialiserTestSuite))
}
func (s *GitInitialiserTestSuite) SetupTest() {
t := s.T()
s.pathWithGit = path.Join(getCurrentWorkingDirectory(), "/data/test-initialiser/git/exists")
if !directoryExists(path.Join(s.pathWithGit, "/.git")) {
if err := os.MkdirAll(path.Join(s.pathWithGit, "/.git"), os.ModePerm); err != nil {
panic(err)
}
}
s.pathWithoutGit = path.Join(getCurrentWorkingDirectory(), "/data/test-initialiser/git/non-existent")
if directoryExists(path.Join(s.pathWithoutGit, "/.git")) {
if err := removeDir(t, path.Join(s.pathWithoutGit, "/.git")); err != nil {
panic(err)
}
}
s.gitInitialiser = InitGitInitialiser(&GitInitialiserConfig{})
s.gitInitialiser.logger.SetOutput(&s.logs)
}
func (s *GitInitialiserTestSuite) TearDownTest() {
t := s.T()
if directoryExists(path.Join(s.pathWithGit, "/.git")) {
if err := removeDir(t, path.Join(s.pathWithGit, "/.git")); err != nil {
panic(err)
}
}
if directoryExists(path.Join(s.pathWithoutGit, "/.git")) {
if err := removeDir(t, path.Join(s.pathWithoutGit, "/.git")); err != nil {
panic(err)
}
}
}
func (s *GitInitialiserTestSuite) TestCheck() {
t := s.T()
s.gitInitialiser.Path = s.pathWithGit
assert.True(t, s.gitInitialiser.Check())
s.gitInitialiser.Path = s.pathWithoutGit
assert.False(t, s.gitInitialiser.Check())
}
func (s *GitInitialiserTestSuite) TestConfirm() {
reader := bufio.NewReader(strings.NewReader("y\n"))
assert.True(s.T(), s.gitInitialiser.Confirm(reader))
reader = bufio.NewReader(strings.NewReader("n\n"))
assert.False(s.T(), s.gitInitialiser.Confirm(reader))
}
func (s *GitInitialiserTestSuite) TestHandle_skip() {
s.gitInitialiser.Path = s.pathWithGit
err := s.gitInitialiser.Handle(true)
assert.Nil(s.T(), err)
assert.Contains(
s.T(),
s.logs.String(),
fmt.Sprintf(
"skipping git repository initialisation at '%s'",
s.pathWithGit,
),
)
}
func (s *GitInitialiserTestSuite) TestHandle_initialiseGit() {
t := s.T()
s.gitInitialiser.Path = s.pathWithoutGit
err := s.gitInitialiser.Handle()
assert.Nil(t, err)
}