-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit_test.go
More file actions
91 lines (77 loc) · 2.04 KB
/
Copy pathgit_test.go
File metadata and controls
91 lines (77 loc) · 2.04 KB
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
package main
import (
"errors"
"io/fs"
"os"
"testing"
"github.com/stretchr/testify/require"
)
// func TestCommandX(t *testing.T) {
// cmd := OsCmd{}
// s, _ := cmd.command(".", "git", "rev-list", "--max-parents=0", "HEAD")
// require.New(t).Equal("5ca59ca92ae4b21408aa6146f5b183a1c5edc195", s)
// }
func TestReadWrite(t *testing.T) {
defer os.Remove("a.txt")
rio := RealIO{}
if err := rio.WriteFile("a.txt", []byte("hello"), 0x755); err != nil {
t.Fail()
}
b, _ := rio.ReadFile("a.txt")
require.New(t).Equal("hello", string(b))
}
type FakeIO struct {
isExist bool
result string
}
func (io FakeIO) ReadFile(_ string) ([]byte, error) {
if io.isExist {
return []byte(io.result), nil
}
return []byte{}, errors.New("can't work with 42")
}
func (io FakeIO) WriteFile(_ string, _ []byte, _ fs.FileMode) error {
return nil
}
type FakeCmd struct {
count int
result string
}
func (c *FakeCmd) command(_, _ string, _ ...string) (string, error) {
c.count++
return c.result, nil
}
func TestMakeRelease(t *testing.T) {
fakeCmd := FakeCmd{result: "1"}
Git{cmd: &fakeCmd, io: FakeIO{}}.makeRelease("V1_1", "1.1", "sha1")
assertions := require.New(t)
assertions.Equal(6, fakeCmd.count)
}
func TestDiff(t *testing.T) {
s := Git{cmd: &FakeCmd{result: "M\t1.txt"}}.diff("1", "1", true)
require.New(t).Len(s, 2)
}
func TestGetCurrentVersion(t *testing.T) {
s := Git{cmd: &FakeCmd{result: "1"}}.getCurrentVersion()
require.New(t).Equal(s, "1")
}
func TestGetLastRelease(t *testing.T) {
dataset := []struct {
curr string
fileName string
isExist bool
fileStr string
cmdRes string
result string
}{
{curr: "sha1", fileName: "tmp/last_commit", isExist: true, fileStr: "1111", cmdRes: "", result: "1111"},
{curr: "sha2", fileName: "tmp/last_commit", isExist: false, fileStr: "", cmdRes: "2222", result: "2222"},
}
for _, data := range dataset {
s, _ := Git{
io: FakeIO{isExist: data.isExist, result: data.fileStr},
cmd: &FakeCmd{result: data.cmdRes},
}.getLastRelease(data.fileName)
require.New(t).Equal(s, data.result)
}
}