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