-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils_test.go
108 lines (91 loc) · 3.11 KB
/
utils_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ctoc
import (
"os"
"regexp"
"testing"
"time"
"github.com/spf13/afero"
)
func TestContainsComment(t *testing.T) {
if !containsComment(`int a; /* A takes care of counts */`, [][]string{{"/*", "*/"}}) {
t.Errorf("invalid")
}
if !containsComment(`bool f; /* `, [][]string{{"/*", "*/"}}) {
t.Errorf("invalid")
}
if containsComment(`}`, [][]string{{"/*", "*/"}}) {
t.Errorf("invalid")
}
}
func TestCheckMD5SumIgnore(t *testing.T) {
fileCache := make(map[string]struct{})
if checkMD5Sum("./utils_test.go", fileCache) {
t.Errorf("invalid sequence")
}
if !checkMD5Sum("./utils_test.go", fileCache) {
t.Errorf("invalid sequence")
}
}
func TestCheckDefaultIgnore(t *testing.T) {
appFS := afero.NewMemMapFs()
if err := appFS.Mkdir("/test", os.ModeDir); err != nil {
t.Fatal(err)
}
_, _ = appFS.Create("/test/one.go")
fileInfo, _ := appFS.Stat("/")
if !checkDefaultIgnore("/", fileInfo, false) {
t.Errorf("invalid logic: this is directory")
}
if !checkDefaultIgnore("/", fileInfo, true) {
t.Errorf("invalid logic: this is vcs file or directory")
}
fileInfo, _ = appFS.Stat("/test/one.go")
if checkDefaultIgnore("/test/one.go", fileInfo, false) {
t.Errorf("invalid logic: should not ignore this file")
}
}
type MockFileInfo struct {
FileName string
IsDirectory bool
}
func (mfi MockFileInfo) Name() string { return mfi.FileName }
func (mfi MockFileInfo) Size() int64 { return int64(8) }
func (mfi MockFileInfo) Mode() os.FileMode { return os.ModePerm }
func (mfi MockFileInfo) ModTime() time.Time { return time.Now() }
func (mfi MockFileInfo) IsDir() bool { return mfi.IsDirectory }
func (mfi MockFileInfo) Sys() interface{} { return nil }
func TestCheckOptionMatch(t *testing.T) {
opts := &ClocOptions{}
fi := MockFileInfo{FileName: "/", IsDirectory: true}
if !checkOptionMatch("/", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is nil")
}
opts.ReNotMatchDir = regexp.MustCompile("thisisdir-not-match")
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is nil")
}
opts.ReNotMatchDir = regexp.MustCompile("thisisdir")
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is ignore")
}
opts = &ClocOptions{}
opts.ReMatchDir = regexp.MustCompile("thisisdir")
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is not ignore")
}
opts.ReMatchDir = regexp.MustCompile("thisisdir-not-match")
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is ignore")
}
opts = &ClocOptions{}
opts.ReNotMatchDir = regexp.MustCompile("thisisdir-not-match")
opts.ReMatchDir = regexp.MustCompile("thisisdir")
fi = MockFileInfo{FileName: "one.go", IsDirectory: false}
if !checkOptionMatch("/thisisdir/one.go", fi, opts) {
t.Errorf("invalid logic: renotmatchdir is not ignore")
}
}