Text file src/cmd/go/testdata/script/vet_basic.txt

     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.
    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.
    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  
    95  # -c=n flag shows n lines of context.
    96  ! go vet -c=2 -printf example.com/x
    97  stderr 'x.go:12:21: non-constant format string in call to fmt.Sprintf'
    98  ! stderr '9	'
    99  stderr '10	'
   100  stderr '11	// This call...'
   101  stderr '12	var _ = fmt.Sprintf\(s\)'
   102  stderr '13	'
   103  stderr '14	'
   104  ! stderr '15	'
   105  
   106  -- go.mod --
   107  module example.com/x
   108  go 1.25
   109  
   110  -- x.go --
   111  package x
   112  
   113  
   114  import (
   115  	"fmt"
   116  	"net"
   117  )
   118  
   119  var s string
   120  
   121  // This call yields a "non-constant format string" diagnostic, with a fix (go vet only).
   122  var _ = fmt.Sprintf(s)
   123  
   124  // This call yields a hostport diagnostic, with a fix (go vet and go fix).
   125  var _, _ = net.Dial("tcp", fmt.Sprintf("%s:%d", s, 80))
   126  

View as plain text