| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package main |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/google/go-cmp/cmp" |
| 21 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 22 |
"golang.org/x/tools/go/packages" |
| 23 |
"golang.org/x/tools/go/packages/packagestest" |
| 24 |
) |
| 25 |
|
| 26 |
func TestGather(t *testing.T) { |
| 27 |
tests := []struct { |
| 28 |
name string |
| 29 |
module packagestest.Module |
| 30 |
want map[string]example |
| 31 |
wantErr bool |
| 32 |
}{ |
| 33 |
{ |
| 34 |
name: "NoExamples", |
| 35 |
module: packagestest.Module{ |
| 36 |
Name: "example.com/foo", |
| 37 |
Files: map[string]any{ |
| 38 |
"foo.go": "package foo\nfunc main() {}\n", |
| 39 |
}, |
| 40 |
}, |
| 41 |
want: map[string]example{}, |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "EmptyExample", |
| 45 |
module: packagestest.Module{ |
| 46 |
Name: "example.com/foo", |
| 47 |
Files: map[string]any{ |
| 48 |
"foo.go": "package foo\n", |
| 49 |
"example_test.go": `package foo_test |
| 50 |
|
| 51 |
func Example() {}`, |
| 52 |
}, |
| 53 |
}, |
| 54 |
want: map[string]example{}, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "EmptyExampleFoo", |
| 58 |
module: packagestest.Module{ |
| 59 |
Name: "example.com/foo", |
| 60 |
Files: map[string]any{ |
| 61 |
"foo.go": "package foo\n", |
| 62 |
"example_test.go": `package foo_test |
| 63 |
|
| 64 |
func ExampleFoo() { |
| 65 |
}`, |
| 66 |
}, |
| 67 |
}, |
| 68 |
want: map[string]example{}, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "NonSignifiedExampleWithPragma", |
| 72 |
module: packagestest.Module{ |
| 73 |
Name: "example.com/foo", |
| 74 |
Files: map[string]any{ |
| 75 |
"foo.go": "package foo\n", |
| 76 |
"example_test.go": `package foo_test |
| 77 |
|
| 78 |
func ExampleFoo() { |
| 79 |
// PRAGMA: Do something. |
| 80 |
}`, |
| 81 |
}, |
| 82 |
}, |
| 83 |
want: map[string]example{}, |
| 84 |
wantErr: true, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "EmptyExampleWithComment", |
| 88 |
module: packagestest.Module{ |
| 89 |
Name: "example.com/foo", |
| 90 |
Files: map[string]any{ |
| 91 |
"foo.go": "package foo\n", |
| 92 |
"example_test.go": `package foo_test |
| 93 |
|
| 94 |
func Example() { |
| 95 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 96 |
}`, |
| 97 |
}, |
| 98 |
}, |
| 99 |
want: map[string]example{ |
| 100 |
"example.com/foo.Example": {Code: ""}, |
| 101 |
}, |
| 102 |
}, |
| 103 |
{ |
| 104 |
name: "EmptyExampleFooWithComment", |
| 105 |
module: packagestest.Module{ |
| 106 |
Name: "example.com/foo", |
| 107 |
Files: map[string]any{ |
| 108 |
"foo.go": "package foo\n", |
| 109 |
"example_test.go": `package foo_test |
| 110 |
|
| 111 |
func ExampleFoo() { |
| 112 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 113 |
}`, |
| 114 |
}, |
| 115 |
}, |
| 116 |
want: map[string]example{ |
| 117 |
"example.com/foo.ExampleFoo": {Code: ""}, |
| 118 |
}, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "NoImportsExample", |
| 122 |
module: packagestest.Module{ |
| 123 |
Name: "example.com/foo", |
| 124 |
Files: map[string]any{ |
| 125 |
"foo.go": "package foo\n", |
| 126 |
"example_test.go": `package foo_test |
| 127 |
|
| 128 |
func Example() { |
| 129 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 130 |
|
| 131 |
// Unattached comment. |
| 132 |
|
| 133 |
// Outside inner block comment. |
| 134 |
panic("ohai") |
| 135 |
if false { |
| 136 |
// something |
| 137 |
} |
| 138 |
return |
| 139 |
}`, |
| 140 |
}, |
| 141 |
}, |
| 142 |
want: map[string]example{ |
| 143 |
"example.com/foo.Example": {Code: "// Unattached comment.\n\n" + |
| 144 |
"// Outside inner block comment.\n" + |
| 145 |
"panic(\"ohai\")\n" + |
| 146 |
"if false {\n\t// something\n}\n" + |
| 147 |
"return"}, |
| 148 |
}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
name: "OneImportExample", |
| 152 |
module: packagestest.Module{ |
| 153 |
Name: "example.com/foo", |
| 154 |
Files: map[string]any{ |
| 155 |
"foo.go": "package foo\n", |
| 156 |
"example_test.go": `package foo_test |
| 157 |
|
| 158 |
import "fmt" |
| 159 |
|
| 160 |
func Example() { |
| 161 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 162 |
fmt.Println(42) |
| 163 |
}`, |
| 164 |
}, |
| 165 |
}, |
| 166 |
want: map[string]example{ |
| 167 |
"example.com/foo.Example": { |
| 168 |
Imports: "import \"fmt\"", |
| 169 |
Code: "fmt.Println(42)", |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "TwoImportsExample", |
| 175 |
module: packagestest.Module{ |
| 176 |
Name: "example.com/foo", |
| 177 |
Files: map[string]any{ |
| 178 |
"foo.go": "package foo\n", |
| 179 |
"example_test.go": `package foo_test |
| 180 |
|
| 181 |
import "fmt" |
| 182 |
import "math" |
| 183 |
|
| 184 |
func Example() { |
| 185 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 186 |
fmt.Println(math.Pi) |
| 187 |
}`, |
| 188 |
}, |
| 189 |
}, |
| 190 |
want: map[string]example{ |
| 191 |
"example.com/foo.Example": { |
| 192 |
Imports: "import (\n\t\"fmt\"\n\t\"math\"\n)", |
| 193 |
Code: "fmt.Println(math.Pi)", |
| 194 |
}, |
| 195 |
}, |
| 196 |
}, |
| 197 |
{ |
| 198 |
name: "LogFatalToReturnErr", |
| 199 |
module: packagestest.Module{ |
| 200 |
Name: "example.com/foo", |
| 201 |
Files: map[string]any{ |
| 202 |
"foo.go": "package foo\n", |
| 203 |
"example_test.go": `package foo_test |
| 204 |
|
| 205 |
import "log" |
| 206 |
|
| 207 |
func Example() { |
| 208 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 209 |
var err error |
| 210 |
if err != nil { |
| 211 |
log.Fatal(err) |
| 212 |
} |
| 213 |
}`, |
| 214 |
}, |
| 215 |
}, |
| 216 |
want: map[string]example{ |
| 217 |
"example.com/foo.Example": {Code: "var err error\n" + |
| 218 |
"if err != nil {\n\treturn err\n}"}, |
| 219 |
}, |
| 220 |
}, |
| 221 |
{ |
| 222 |
name: "IgnoreSections", |
| 223 |
module: packagestest.Module{ |
| 224 |
Name: "example.com/foo", |
| 225 |
Files: map[string]any{ |
| 226 |
"foo.go": "package foo\n", |
| 227 |
"example_test.go": `package foo_test |
| 228 |
|
| 229 |
import "context" |
| 230 |
|
| 231 |
func Example() { |
| 232 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 233 |
|
| 234 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 235 |
ctx := context.Background() |
| 236 |
|
| 237 |
// do something |
| 238 |
|
| 239 |
// PRAGMA: On gocloud.dev, hide the rest of the function. |
| 240 |
_ = ctx |
| 241 |
}`, |
| 242 |
}, |
| 243 |
}, |
| 244 |
want: map[string]example{ |
| 245 |
"example.com/foo.Example": { |
| 246 |
Imports: "import \"context\"", |
| 247 |
Code: "// do something", |
| 248 |
}, |
| 249 |
}, |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "BlankImports", |
| 253 |
module: packagestest.Module{ |
| 254 |
Name: "example.com/foo", |
| 255 |
Files: map[string]any{ |
| 256 |
"foo.go": "package foo\n", |
| 257 |
"example_test.go": `package foo_test |
| 258 |
|
| 259 |
func Example() { |
| 260 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 261 |
|
| 262 |
// PRAGMA: On gocloud.dev, add a blank import: _ "example.com/bar" |
| 263 |
_ = 42 |
| 264 |
}`, |
| 265 |
}, |
| 266 |
}, |
| 267 |
want: map[string]example{ |
| 268 |
"example.com/foo.Example": { |
| 269 |
Imports: "import _ \"example.com/bar\"", |
| 270 |
Code: "_ = 42", |
| 271 |
}, |
| 272 |
}, |
| 273 |
}, |
| 274 |
} |
| 275 |
for _, test := range tests { |
| 276 |
t.Run(test.name, func(t *testing.T) { |
| 277 |
exported := packagestest.Export(t, packagestest.Modules, []packagestest.Module{test.module}) |
| 278 |
defer exported.Cleanup() |
| 279 |
exported.Config.Mode = gatherLoadMode |
| 280 |
pkgs, err := packages.Load(exported.Config, "./...") |
| 281 |
if err != nil { |
| 282 |
t.Fatal(err) |
| 283 |
} |
| 284 |
|
| 285 |
got, err := gather(pkgs) |
| 286 |
if (err != nil) != test.wantErr { |
| 287 |
t.Errorf("gather(pkgs) got err %v want err? %v", err, test.wantErr) |
| 288 |
} |
| 289 |
if diff := cmp.Diff(test.want, got, cmpopts.EquateEmpty()); diff != "" { |
| 290 |
t.Errorf("gather(pkgs) diff (-want +got):\n%s", diff) |
| 291 |
} |
| 292 |
}) |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
func TestFormatImports(t *testing.T) { |
| 297 |
tests := []struct { |
| 298 |
name string |
| 299 |
usedPackages map[string]string |
| 300 |
want string |
| 301 |
}{ |
| 302 |
{ |
| 303 |
name: "Empty", |
| 304 |
usedPackages: nil, |
| 305 |
want: "", |
| 306 |
}, |
| 307 |
{ |
| 308 |
name: "One", |
| 309 |
usedPackages: map[string]string{"fmt": ""}, |
| 310 |
want: "import \"fmt\"", |
| 311 |
}, |
| 312 |
{ |
| 313 |
name: "Two", |
| 314 |
usedPackages: map[string]string{ |
| 315 |
"fmt": "", |
| 316 |
"log": "", |
| 317 |
}, |
| 318 |
want: "import (\n\t\"fmt\"\n\t\"log\"\n)", |
| 319 |
}, |
| 320 |
{ |
| 321 |
name: "Renamed", |
| 322 |
usedPackages: map[string]string{ |
| 323 |
"fmt": "zzz", |
| 324 |
"log": "aaa", |
| 325 |
}, |
| 326 |
want: "import (\n\tzzz \"fmt\"\n\taaa \"log\"\n)", |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "StdlibSeparateFromThirdParty", |
| 330 |
usedPackages: map[string]string{ |
| 331 |
"context": "", |
| 332 |
"fmt": "", |
| 333 |
"log": "", |
| 334 |
"github.com/google/go-cmp/cmp": "", |
| 335 |
"gocloud.dev/blob": "", |
| 336 |
}, |
| 337 |
want: "import (\n" + |
| 338 |
"\t\"context\"\n" + |
| 339 |
"\t\"fmt\"\n" + |
| 340 |
"\t\"log\"\n" + |
| 341 |
"\n" + |
| 342 |
"\t\"github.com/google/go-cmp/cmp\"\n" + |
| 343 |
"\t\"gocloud.dev/blob\"\n" + |
| 344 |
")", |
| 345 |
}, |
| 346 |
} |
| 347 |
for _, test := range tests { |
| 348 |
t.Run(test.name, func(t *testing.T) { |
| 349 |
got := formatImports(test.usedPackages) |
| 350 |
if got != test.want { |
| 351 |
t.Errorf("formatImports(%+v) =\n%s\n// want:\n%s", test.usedPackages, got, test.want) |
| 352 |
} |
| 353 |
}) |
| 354 |
} |
| 355 |
} |