| 1 | // Copyright (c) 2021, Daniel Martà <[email protected]> |
| 2 | // See LICENSE for licensing information |
| 3 | |
| 4 | package formatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/ |
| 5 | |
| 6 | import ( |
| 7 | "errorsscip-go gomod github.com/golang/go/src go1.20 errors/" |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.20 fmt/" |
| 9 | "go/scannerscip-go gomod github.com/golang/go/src go1.20 `go/scanner`/" |
| 10 | "path/filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src go1.20 strings/" |
| 12 | "testingscip-go gomod github.com/golang/go/src go1.20 testing/" |
| 13 | |
| 14 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/ "github.com/frankban/quicktestscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/" |
| 15 | "golang.org/x/tools/txtarscip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/" |
| 16 | ) |
| 17 | |
| 18 | func FuzzFormatscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/FuzzFormat().(f *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Fscip-go gomod github.com/golang/go/src go1.20 testing/F#) { |
| 19 | // Initialize the corpus with the Go files from our test scripts. |
| 20 | paths, err := filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Globscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Glob().(filepathscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.20 `path/filepath`/Join().("..", "testdata", "script", "*.txtar")) |
| 21 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Assertscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Assert().(f, err, qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.IsNilscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/IsNil.) |
| 22 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Assertscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Assert().(f, paths, qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Notscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Not().(qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.HasLenscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/HasLen.), 0) |
| 23 | for _, path := range paths { |
| 24 | archive, err := txtarscip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/.ParseFilescip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/ParseFile().(path) |
| 25 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Assertscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Assert().(f, err, qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.IsNilscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/IsNil.) |
| 26 | for _, file := range archive.Filesscip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/Archive#Files. { |
| 27 | f.Logfscip-go gomod github.com/golang/go/src go1.20 testing/common#Logf().("adding %s from %s", file.Namescip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/File#Name., path) |
| 28 | if stringsscip-go gomod github.com/golang/go/src go1.20 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.20 strings/HasSuffix().(file.Namescip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/File#Name., ".go") || stringsscip-go gomod github.com/golang/go/src go1.20 strings/.Containsscip-go gomod github.com/golang/go/src go1.20 strings/Contains().(file.Namescip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/File#Name., ".go.") { |
| 29 | f.Addscip-go gomod github.com/golang/go/src go1.20 testing/F#Add().(string(file.Datascip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/File#Data.), int8(18), false) // -lang=1.18 |
| 30 | f.Addscip-go gomod github.com/golang/go/src go1.20 testing/F#Add().(string(file.Datascip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/File#Data.), int8(1), false) // -lang=1.1 |
| 31 | f.Addscip-go gomod github.com/golang/go/src go1.20 testing/F#Add().(string(file.Datascip-go gomod golang.org/x/tools v0.17.0 `golang.org/x/tools/txtar`/File#Data.), int8(18), true) // -lang=1.18 -extra |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | f.Fuzzscip-go gomod github.com/golang/go/src go1.20 testing/F#Fuzz().(func(t *testingscip-go gomod github.com/golang/go/src go1.20 testing/.Tscip-go gomod github.com/golang/go/src go1.20 testing/T#, src string, |
| 37 | majorVersion int8, // Empty version if negative, 1.N otherwise. |
| 38 | extraRules bool, |
| 39 | ) { |
| 40 | // TODO: also fuzz Options.ModulePath |
| 41 | opts := Optionsscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#{ExtraRulesscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#ExtraRules.: extraRules} |
| 42 | if majorVersion >= 0 { |
| 43 | opts.LangVersionscip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Options#LangVersion. = fmtscip-go gomod github.com/golang/go/src go1.20 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.20 fmt/Sprintf().("1.%d", majorVersion) |
| 44 | } |
| 45 | |
| 46 | orig := []byte(src) |
| 47 | formatted, err := Sourcescip-go gomod mvdan.cc/gofumpt . `mvdan.cc/gofumpt/format`/Source().(orig, opts) |
| 48 | if errorsscip-go gomod github.com/golang/go/src go1.20 errors/.Asscip-go gomod github.com/golang/go/src go1.20 errors/As().(err, &scannerscip-go gomod github.com/golang/go/src go1.20 `go/scanner`/.ErrorListscip-go gomod github.com/golang/go/src go1.20 `go/scanner`/ErrorList#{}) { |
| 49 | return // invalid syntax from parsing |
| 50 | } |
| 51 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Assertscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Assert().(t, err, qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.IsNilscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/IsNil.) |
| 52 | _ = formatted |
| 53 | |
| 54 | // TODO: verify that the result is idempotent |
| 55 | |
| 56 | // TODO: verify that, if the input was valid Go 1.N syntax, |
| 57 | // so is the output (how? go/parser lacks an option) |
| 58 | |
| 59 | // TODO: check calling format.Node directly as well |
| 60 | |
| 61 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Assertscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Assert().(t, string(orig), qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Equalsscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Equals., src, |
| 62 | qtscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/.Commentfscip-go gomod github.com/frankban/quicktest v1.14.6 `github.com/frankban/quicktest`/Commentf().("input source bytes were modified")) |
| 63 | }) |
| 64 | } |