-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
95 lines (79 loc) · 2.32 KB
/
main_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
package main
import (
"github.com/stretchr/testify/assert"
"os"
"strconv"
"sync"
"testing"
"time"
)
func TestGetEndGitUsername(t *testing.T) {
_ = os.Setenv("GIT.USERNAME", "test")
username := getEndGitUsername()
expectedUsername := "test"
if username != expectedUsername {
t.Errorf("Expected %s, but got %s", expectedUsername, username)
}
assert.Equal(t, expectedUsername, username)
}
func TestGetGitLogAfter(t *testing.T) {
repoPath := "C:\\Web\\zhuzhu-game\\.git"
gitLog := "This is a git log message"
gitLogMap := sync.Map{}
maxLogLen := int64(0)
getGitLogAfter(repoPath, &gitLog, &gitLogMap, &maxLogLen)
actual, _ := gitLogMap.Load("zhuzhu-game")
assert.Equal(t, gitLog, actual)
assert.Equal(t, int64(len(gitLog)+len("zhuzhu-game")), maxLogLen)
}
func TestMakeAiReq(t *testing.T) {
// 创建一个 sync.Map 并添加测试数据
gitLogMap := &sync.Map{}
gitLogMap.Store("repo1", "git log 1")
gitLogMap.Store("repo2", "git log 2")
// 设置 maxLogLen 和 prompt
maxLogLen := int64(100)
prompt := "prompt"
// 调用被测试函数
result := makeAiReq(gitLogMap, maxLogLen, prompt)
// 验证结果是否符合预期
expected := "\n以下是repo1仓库的GIT日志\ngit log 1\n以下是repo2仓库的GIT日志\ngit log 2"
assert.Equal(t, expected, result)
}
func TestGetCmdInfoDate(t *testing.T) {
var d1 = time.Now().AddDate(0, 0, -6).Format(time.DateOnly) + " 00:00:00"
var d2 = time.Now().AddDate(0, 0, 0).Format(time.DateOnly) + " 00:00:00"
var d3 = time.Now().AddDate(0, 0, -1).Format(time.DateOnly) + " 00:00:00"
testCases := []struct {
id string
reportCycle string
expected string
env string
}{
{"1", "week", d1, "1"},
{"2", "day", d2, "1"},
{"3", "day", d3, "2"},
}
for _, tc := range testCases {
t.Run(tc.id, func(t *testing.T) {
_ = os.Setenv("report.intervalDay", tc.env)
result := getCmdInfoDate(tc.reportCycle)
expected := tc.expected
assert.Equal(t, expected, result)
})
}
}
func TestWriteFile(t *testing.T) {
testText := "hahahahah"
writeFile(&testText)
testText2 := strconv.FormatInt(time.Now().Unix(), 10)
writeFile(&testText2)
var fileName = time.Now().Format(time.DateOnly) + ".txt"
file, err := os.ReadFile(fileName)
if err != nil {
assert.Fail(t, "")
}
assert.Equal(t, string(file), testText2)
// 清理
_ = os.Remove(fileName)
}