1
16
17 package main
18
19 import (
20 "flag"
21 "os"
22 "path/filepath"
23 "testing"
24
25 "golang.org/x/tools/go/packages"
26 )
27
28
29 var goBinary = flag.String("go", "", "path to a `go` binary")
30
31 func TestVerify(t *testing.T) {
32
33
34 setEnvVars(t)
35
36 tcs := []struct {
37 path string
38 expect int
39 }{
40 {"./testdata/good", 0},
41 {"./testdata/bad", 18},
42 }
43
44 for _, tc := range tcs {
45 errs, err := verify("linux/amd64", []string{tc.path}, nil)
46 if err != nil {
47 t.Errorf("unexpected error: %v", err)
48 } else if len(errs) != tc.expect {
49 t.Errorf("Expected %d errors, got %d: %v", tc.expect, len(errs), errs)
50 }
51 }
52 }
53
54 func setEnvVars(t testing.TB) {
55 t.Helper()
56 if *goBinary != "" {
57 newPath := filepath.Dir(*goBinary)
58 curPath := os.Getenv("PATH")
59 if curPath != "" {
60 newPath = newPath + ":" + curPath
61 }
62 t.Setenv("PATH", newPath)
63 }
64 if os.Getenv("HOME") == "" {
65 t.Setenv("HOME", "/tmp")
66 }
67 }
68
69 func TestDedup(t *testing.T) {
70 testcases := []struct {
71 input []packages.Error
72 expected int
73 }{{
74 input: nil,
75 expected: 0,
76 }, {
77 input: []packages.Error{
78 {Pos: "file:7", Msg: "message", Kind: packages.ParseError},
79 },
80 expected: 1,
81 }, {
82 input: []packages.Error{
83 {Pos: "file:7", Msg: "message1", Kind: packages.ParseError},
84 {Pos: "file:8", Msg: "message2", Kind: packages.ParseError},
85 },
86 expected: 2,
87 }, {
88 input: []packages.Error{
89 {Pos: "file:7", Msg: "message1", Kind: packages.ParseError},
90 {Pos: "file:8", Msg: "message2", Kind: packages.ParseError},
91 {Pos: "file:7", Msg: "message1", Kind: packages.ParseError},
92 },
93 expected: 2,
94 }}
95
96 for i, tc := range testcases {
97 out := dedup(tc.input)
98 if len(out) != tc.expected {
99 t.Errorf("[%d] dedup(%v) = '%v', expected %d",
100 i, tc.input, out, tc.expected)
101 }
102 }
103 }
104
View as plain text