-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcli_test.go
More file actions
72 lines (60 loc) · 1.49 KB
/
cli_test.go
File metadata and controls
72 lines (60 loc) · 1.49 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
package cli_test
import (
"fmt"
"os"
"testing"
"github.com/ryancurrah/gomodguard"
"github.com/ryancurrah/gomodguard/internal/cli"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
err := os.Chdir("../../_example/allOptions")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(m.Run())
}
func TestCmdRun(t *testing.T) {
wantExitCode := 2
exitCode := cli.Run()
if exitCode != wantExitCode {
t.Errorf("got exit code '%d' want '%d'", exitCode, wantExitCode)
}
}
func TestWriteCheckstyle(t *testing.T) {
outFile, err := os.CreateTemp(t.TempDir(), "checkstyle-*.xml")
require.NoError(t, err)
defer func() {
err := outFile.Close()
require.NoError(t, err)
}()
issues := []gomodguard.Issue{
{
FileName: "first.go",
LineNumber: 10,
Reason: "first test reason",
},
{
FileName: "second.go",
LineNumber: 20,
Reason: "second test reason",
},
}
err = cli.WriteCheckstyle(outFile.Name(), issues)
require.NoError(t, err)
got, err := os.ReadFile(outFile.Name())
require.NoError(t, err)
want := `
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="1.0.0">
<file name="first.go">
<error line="10" column="1" severity="error" message="first test reason" source="gomodguard"></error>
</file>
<file name="second.go">
<error line="20" column="1" severity="error" message="second test reason" source="gomodguard"></error>
</file>
</checkstyle>`
assert.Equal(t, want, string(got))
}