Debian Codegraph @

go/buildutil/tags_test.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package buildutil_test
6
7 import (
8 "bytes"
9 "flag"
10 "go/build"
11 "os/exec"
12 "reflect"
13 "strings"
14 "testing"
15
16 "golang.org/x/tools/go/buildutil"
17 "golang.org/x/tools/internal/testenv"
18 )
19
20 func TestTags(t *testing.T) {
21
22 type tagTestCase struct {
23 tags string
24 want []string
25 wantErr bool
26 }
27
28 for name, tc := range map[string]tagTestCase{
29 // Normal valid cases
30 "empty": {
31 tags: "",
32 want: []string{},
33 },
34 "commas": {
35 tags: "tag1,tag_2,🐹,tag/3,tag-4",
36 want: []string{"tag1", "tag_2", "🐹", "tag/3", "tag-4"},
37 },
38 "delimiters are spaces": {
39 tags: "a b\tc\rd\ne",
40 want: []string{"a", "b", "c", "d", "e"},
41 },
42 "old quote and space form": {
43 tags: "'a' 'b' 'c'",
44 want: []string{"a", "b", "c"},
45 },
46
47 // Normal error cases
48 "unterminated": {
49 tags: `"missing closing quote`,
50 want: []string{},
51 wantErr: true,
52 },
53 "unterminated single": {
54 tags: `'missing closing quote`,
55 want: []string{},
56 wantErr: true,
57 },
58
59 // Maybe surprising difference for unterminated quotes, no spaces
60 "unterminated no spaces": {
61 tags: `"missing_closing_quote`,
62 want: []string{"\"missing_closing_quote"},
63 },
64 "unterminated no spaces single": {
65 tags: `'missing_closing_quote`,
66 want: []string{},
67 wantErr: true,
68 },
69
70 // Permitted but not recommended
71 "delimiters contiguous spaces": {
72 tags: "a \t\r\n, b \t\r\nc,d\te\tf",
73 want: []string{"a", ",", "b", "c,d", "e", "f"},
74 },
75 "quotes and spaces": {
76 tags: ` 'one'"two" 'three "four"'`,
77 want: []string{"one", "two", "three \"four\""},
78 },
79 "quotes single no spaces": {
80 tags: `'t1','t2',"t3"`,
81 want: []string{"t1", ",'t2',\"t3\""},
82 },
83 "quotes double no spaces": {
84 tags: `"t1","t2","t3"`,
85 want: []string{`"t1"`, `"t2"`, `"t3"`},
86 },
87 } {
88 t.Run(name, func(t *testing.T) {
89 f := flag.NewFlagSet("TestTags", flag.ContinueOnError)
90 var ctxt build.Context
91 f.Var((*buildutil.TagsFlag)(&ctxt.BuildTags), "tags", buildutil.TagsFlagDoc)
92
93 // Normal case valid parsed tags
94 f.Parse([]string{"-tags", tc.tags, "rest"})
95
96 // BuildTags
97 if !reflect.DeepEqual(ctxt.BuildTags, tc.want) {
98 t.Errorf("Case = %s, BuildTags = %q, want %q", name, ctxt.BuildTags, tc.want)
99 }
100
101 // Args()
102 if want := []string{"rest"}; !reflect.DeepEqual(f.Args(), want) {
103 t.Errorf("Case = %s, f.Args() = %q, want %q", name, f.Args(), want)
104 }
105
106 // Regression check against base go tooling
107 cmd := testenv.Command(t, "go", "list", "-f", "{{context.BuildTags}}", "-tags", tc.tags, ".")
108 var out bytes.Buffer
109 cmd.Stdout = &out
110 if err := cmd.Run(); err != nil {
111 if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
112 t.Logf("stderr:\n%s", ee.Stderr)
113 }
114 if !tc.wantErr {
115 t.Errorf("%v: %v", cmd, err)
116 }
117 } else if tc.wantErr {
118 t.Errorf("Expected failure for %v", cmd)
119 } else {
120 wantDescription := strings.Join(tc.want, " ")
121 output := strings.Trim(strings.TrimSuffix(out.String(), "\n"), "[]")
122 if output != wantDescription {
123 t.Errorf("Output = %s, want %s", output, wantDescription)
124 }
125 }
126 })
127 }
128 }