| 1 |
# Test basic features of "go vet"/"go fix" CLI. |
| 2 |
# |
| 3 |
# The example relies on two analyzers: |
| 4 |
# - hostport (which is included in both the fix and vet suites), and |
| 5 |
# - printf (which is only in the vet suite). |
| 6 |
# Each reports one diagnostic with a fix. |
| 7 |
|
| 8 |
# vet default flags print diagnostics to stderr. Diagnostic => nonzero exit. |
| 9 |
! go vet example.com/x |
| 10 |
stderr 'does not work with IPv6' |
| 11 |
stderr 'non-constant format string in call to fmt.Sprintf' |
| 12 |
|
| 13 |
# -hostport runs only one analyzer. Diagnostic => failure. |
| 14 |
! go vet -hostport example.com/x |
| 15 |
stderr 'does not work with IPv6' |
| 16 |
! stderr 'non-constant format string' |
| 17 |
|
| 18 |
# -timeformat runs only one analyzer. No diagnostics => success. |
| 19 |
go vet -timeformat example.com/x |
| 20 |
! stderr . |
| 21 |
|
| 22 |
# JSON output includes diagnostics and fixes. Always success. |
| 23 |
go vet -json example.com/x |
| 24 |
! stderr . |
| 25 |
stdout '"example.com/x": {' |
| 26 |
stdout '"hostport":' |
| 27 |
stdout '"message": "address format .* does not work with IPv6",' |
| 28 |
stdout '"suggested_fixes":' |
| 29 |
stdout '"message": "Replace fmt.Sprintf with net.JoinHostPort",' |
| 30 |
|
| 31 |
# vet -fix -diff displays a diff. Diff => nonzero exit. |
| 32 |
! go vet -fix -diff example.com/x |
| 33 |
stdout '\-var _ = fmt.Sprintf\(s\)' |
| 34 |
stdout '\+var _ = fmt.Sprintf\("%s", s\)' |
| 35 |
stdout '\-var _, _ = net.Dial\("tcp", fmt.Sprintf\("%s:%d", s, 80\)\)' |
| 36 |
stdout '\+var _, _ = net.Dial\("tcp", net.JoinHostPort\(s, "80"\)\)' |
| 37 |
|
| 38 |
# vet -fix quietly applies the vet suite fixes. |
| 39 |
cp x.go x.go.bak |
| 40 |
go vet -fix example.com/x |
| 41 |
grep 'fmt.Sprintf\("%s", s\)' x.go |
| 42 |
grep 'net.JoinHostPort' x.go |
| 43 |
! stderr . |
| 44 |
cp x.go.bak x.go |
| 45 |
|
| 46 |
! go vet -diff example.com/x |
| 47 |
stderr 'go vet -diff flag requires -fix' |
| 48 |
|
| 49 |
# go fix applies the fix suite fixes. |
| 50 |
go fix example.com/x |
| 51 |
grep 'net.JoinHostPort' x.go |
| 52 |
! grep 'fmt.Sprintf\("%s", s\)' x.go |
| 53 |
! stderr . |
| 54 |
cp x.go.bak x.go |
| 55 |
|
| 56 |
# Show diff of fixes from the fix suite. Diff => nonzero exit. |
| 57 |
! go fix -diff example.com/x |
| 58 |
! stdout '\-var _ = fmt.Sprintf\(s\)' |
| 59 |
stdout '\-var _, _ = net.Dial\("tcp", fmt.Sprintf\("%s:%d", s, 80\)\)' |
| 60 |
stdout '\+var _, _ = net.Dial\("tcp", net.JoinHostPort\(s, "80"\)\)' |
| 61 |
|
| 62 |
# Show fix-suite fixes in JSON form. |
| 63 |
go fix -json example.com/x |
| 64 |
! stderr . |
| 65 |
stdout '"example.com/x": {' |
| 66 |
stdout '"hostport":' |
| 67 |
stdout '"message": "address format .* does not work with IPv6",' |
| 68 |
stdout '"suggested_fixes":' |
| 69 |
stdout '"message": "Replace fmt.Sprintf with net.JoinHostPort",' |
| 70 |
! stdout '"printf":' |
| 71 |
! stdout '"message": "non-constant format string.*",' |
| 72 |
! stdout '"message": "Insert.*%s.*format.string",' |
| 73 |
|
| 74 |
# Show vet-suite fixes in JSON form. |
| 75 |
go vet -fix -json example.com/x |
| 76 |
! stderr . |
| 77 |
stdout '"example.com/x": {' |
| 78 |
stdout '"hostport":' |
| 79 |
stdout '"message": "address format .* does not work with IPv6",' |
| 80 |
stdout '"suggested_fixes":' |
| 81 |
stdout '"message": "Replace fmt.Sprintf with net.JoinHostPort",' |
| 82 |
stdout '"printf":' |
| 83 |
stdout '"message": "non-constant format string.*",' |
| 84 |
stdout '"suggested_fixes":' |
| 85 |
stdout '"message": "Insert.*%s.*format.string",' |
| 86 |
|
| 87 |
# Reject -diff + -json. |
| 88 |
! go fix -diff -json example.com/x |
| 89 |
stderr '-json and -diff cannot be used together' |
| 90 |
|
| 91 |
# Legacy way of selecting fixers is a no-op. |
| 92 |
go fix -fix=old1,old2 example.com/x |
| 93 |
stderr 'go fix: the -fix=old1,old2 flag is obsolete and has no effect' |
| 94 |
cp x.go.bak x.go |
| 95 |
|
| 96 |
# -c=n flag shows n lines of context. |
| 97 |
! go vet -c=2 -printf example.com/x |
| 98 |
stderr 'x.go:12:21: non-constant format string in call to fmt.Sprintf' |
| 99 |
! stderr '9 ' |
| 100 |
stderr '10 ' |
| 101 |
stderr '11 // This call...' |
| 102 |
stderr '12 var _ = fmt.Sprintf\(s\)' |
| 103 |
stderr '13 ' |
| 104 |
stderr '14 ' |
| 105 |
! stderr '15 ' |
| 106 |
|
| 107 |
-- go.mod -- |
| 108 |
module example.com/x |
| 109 |
go 1.25 |
| 110 |
|
| 111 |
-- x.go -- |
| 112 |
package x |
| 113 |
|
| 114 |
|
| 115 |
import ( |
| 116 |
"fmt" |
| 117 |
"net" |
| 118 |
) |
| 119 |
|
| 120 |
var s string |
| 121 |
|
| 122 |
// This call yields a "non-constant format string" diagnostic, with a fix (go vet only). |
| 123 |
var _ = fmt.Sprintf(s) |
| 124 |
|
| 125 |
// This call yields a hostport diagnostic, with a fix (go vet and go fix). |
| 126 |
var _, _ = net.Dial("tcp", fmt.Sprintf("%s:%d", s, 80)) |