-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
67 lines (59 loc) · 1.4 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
package main
import (
"bytes"
"os"
"testing"
)
func TestVersionFlag(t *testing.T) {
tests := []struct {
name string
version string
commit string
buildDate string
expectedOutput string
}{
{
name: "without ldflags",
version: "",
commit: "",
buildDate: "",
expectedOutput: "Software: flows2fim\nVersion: unknown\nCommit: unknown\nBuild Date: unknown\n",
},
{
name: "with ldflags",
version: "v1.2.3",
commit: "abcdefg",
buildDate: "2024-08-30",
expectedOutput: "Software: flows2fim\nVersion: v1.2.3\nCommit: abcdefg\nBuild Date: 2024-08-30\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Backup the original stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Set the ldflags variables for this test case
if tt.version != "" {
GitTag = tt.version
}
if tt.commit != "" {
GitCommit = tt.commit
}
if tt.buildDate != "" {
BuildDate = tt.buildDate
}
run([]string{"cmd", "--version"})
w.Close()
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
// Restore the original stdout
os.Stdout = oldStdout
// Check if the output matches the expected output
if output != tt.expectedOutput {
t.Errorf("expected '%s', got '%s'", tt.expectedOutput, output)
}
})
}
}