1 [short] skip
2
3 # Install an echo command because Windows doesn't have it.
4 env GOBIN=$WORK/tmp/bin
5 go install echo.go
6 env PATH=$GOBIN${:}$PATH
7
8 # Test go generate for directory with no go files
9 go generate ./nogo
10 ! stdout 'Fail'
11
12 # Test go generate for package where all .go files are excluded by build
13 # constraints
14 go generate -v ./excluded
15 ! stdout 'Fail'
16 ! stderr 'go' # -v shouldn't list any files
17
18 # Test go generate for "package" with no package clause in any file
19 go generate ./nopkg
20 stdout 'Success a'
21 ! stdout 'Fail'
22
23 # Test go generate for package with inconsistent package clauses
24 # $GOPACKAGE should depend on each file's package clause
25 go generate ./inconsistent
26 stdout 'Success a'
27 stdout 'Success b'
28 stdout -count=2 'Success c'
29 ! stdout 'Fail'
30
31 # Test go generate for syntax errors before and after package clauses
32 go generate ./syntax
33 stdout 'Success a'
34 stdout 'Success b'
35 ! stdout 'Fail'
36
37 # Test go generate for files importing non-existent packages
38 go generate ./importerr
39 stdout 'Success a'
40 stdout 'Success b'
41 stdout 'Success c'
42
43 -- echo.go --
44 package main
45
46 import (
47 "fmt"
48 "os"
49 "strings"
50 )
51
52 func main() {
53 fmt.Println(strings.Join(os.Args[1:], " "))
54 fmt.Println()
55 }
56
57 -- go.mod --
58 module m
59
60 go 1.16
61 -- nogo/foo.txt --
62 Text file in a directory without go files.
63 Go generate should ignore this directory.
64 //go:generate echo Fail nogo
65
66 -- excluded/a.go --
67 // Include a build tag that go generate should exclude.
68 // Go generate should ignore this file.
69
70 // +build a
71
72 //go:generate echo Fail a
73
74 package excluded
75
76 -- excluded/b.go --
77 // Include a build tag that go generate should exclude.
78 // Go generate should ignore this file.
79
80 //go:generate echo Fail b
81
82 // +build b
83
84 package excluded
85
86
87 -- nopkg/a.go --
88 // Go file with package clause after comment.
89 // Go generate should process this file.
90
91 /* Pre-comment */ package nopkg
92 //go:generate echo Success a
93
94 -- nopkg/b.go --
95 // Go file with commented package clause.
96 // Go generate should ignore this file.
97
98 //package nopkg
99
100 //go:generate echo Fail b
101
102 -- nopkg/c.go --
103 // Go file with package clause inside multiline comment.
104 // Go generate should ignore this file.
105
106 /*
107 package nopkg
108 */
109
110 //go:generate echo Fail c
111
112 -- nopkg/d.go --
113 // Go file with package clause inside raw string literal.
114 // Go generate should ignore this file.
115
116 const foo = `
117 package nopkg
118 `
119 //go:generate echo Fail d
120
121 -- nopkg/e.go --
122 // Go file without package clause.
123 // Go generate should ignore this file.
124
125 //go:generate echo Fail e
126
127 -- inconsistent/a.go --
128 // Valid go file with inconsistent package name.
129 // Go generate should process this file with GOPACKAGE=a
130
131 package a
132 //go:generate echo Success $GOPACKAGE
133
134 -- inconsistent/b.go --
135 // Valid go file with inconsistent package name.
136 // Go generate should process this file with GOPACKAGE=b
137
138 //go:generate echo Success $GOPACKAGE
139 package b
140
141 -- inconsistent/c.go --
142 // Go file with two package clauses.
143 // Go generate should process this file with GOPACKAGE=c
144
145 //go:generate echo Success $GOPACKAGE
146 package c
147 // Invalid package clause, should be ignored:
148 package cinvalid
149 //go:generate echo Success $GOPACKAGE
150
151 -- inconsistent/d.go --
152 // Go file with invalid package name.
153 // Go generate should ignore this file.
154
155 package +d+
156 //go:generate echo Fail $GOPACKAGE
157
158 -- syntax/a.go --
159 // Go file with syntax error after package clause.
160 // Go generate should process this file.
161
162 package syntax
163 123
164 //go:generate echo Success a
165
166 -- syntax/b.go --
167 // Go file with syntax error after package clause.
168 // Go generate should process this file.
169
170 package syntax; 123
171 //go:generate echo Success b
172
173 -- syntax/c.go --
174 // Go file with syntax error before package clause.
175 // Go generate should ignore this file.
176
177 foo
178 package syntax
179 //go:generate echo Fail c
180
181 -- importerr/a.go --
182 // Go file which imports non-existing package.
183 // Go generate should process this file.
184
185 package importerr
186 //go:generate echo Success a
187 import "foo"
188
189 -- importerr/b.go --
190 // Go file which imports non-existing package.
191 // Go generate should process this file.
192
193 //go:generate echo Success b
194 package importerr
195 import "bar"
196
197 -- importerr/c.go --
198 // Go file which imports non-existing package.
199 // Go generate should process this file.
200
201 package importerr
202 import "moo"
203 //go:generate echo Success c
204
View as plain text