golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/vendor_test.go (about)

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package misc
     6  
     7  import (
     8  	"testing"
     9  
    10  	. "golang.org/x/tools/gopls/internal/test/integration"
    11  
    12  	"golang.org/x/tools/gopls/internal/protocol"
    13  )
    14  
    15  const basicProxy = `
    16  -- golang.org/x/hello@v1.2.3/go.mod --
    17  module golang.org/x/hello
    18  
    19  go 1.14
    20  -- golang.org/x/hello@v1.2.3/hi/hi.go --
    21  package hi
    22  
    23  var Goodbye error
    24  `
    25  
    26  func TestInconsistentVendoring(t *testing.T) {
    27  	const pkgThatUsesVendoring = `
    28  -- go.mod --
    29  module mod.com
    30  
    31  go 1.14
    32  
    33  require golang.org/x/hello v1.2.3
    34  -- go.sum --
    35  golang.org/x/hello v1.2.3 h1:EcMp5gSkIhaTkPXp8/3+VH+IFqTpk3ZbpOhqk0Ncmho=
    36  golang.org/x/hello v1.2.3/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco=
    37  -- vendor/modules.txt --
    38  -- a/a1.go --
    39  package a
    40  
    41  import "golang.org/x/hello/hi"
    42  
    43  func _() {
    44  	_ = hi.Goodbye
    45  	var q int // hardcode a diagnostic
    46  }
    47  `
    48  	WithOptions(
    49  		Modes(Default),
    50  		ProxyFiles(basicProxy),
    51  	).Run(t, pkgThatUsesVendoring, func(t *testing.T, env *Env) {
    52  		env.OpenFile("a/a1.go")
    53  		d := &protocol.PublishDiagnosticsParams{}
    54  		env.AfterChange(
    55  			Diagnostics(env.AtRegexp("go.mod", "module mod.com"), WithMessage("Inconsistent vendoring")),
    56  			ReadDiagnostics("go.mod", d),
    57  		)
    58  		env.ApplyQuickFixes("go.mod", d.Diagnostics)
    59  
    60  		env.AfterChange(
    61  			Diagnostics(env.AtRegexp("a/a1.go", `q int`), WithMessage("not used")),
    62  		)
    63  	})
    64  }
    65  
    66  func TestWindowsVendoring_Issue56291(t *testing.T) {
    67  	const src = `
    68  -- go.mod --
    69  module mod.com
    70  
    71  go 1.14
    72  
    73  require golang.org/x/hello v1.2.3
    74  -- go.sum --
    75  golang.org/x/hello v1.2.3 h1:EcMp5gSkIhaTkPXp8/3+VH+IFqTpk3ZbpOhqk0Ncmho=
    76  golang.org/x/hello v1.2.3/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco=
    77  -- main.go --
    78  package main
    79  
    80  import "golang.org/x/hello/hi"
    81  
    82  func main() {
    83  	_ = hi.Goodbye
    84  }
    85  `
    86  	WithOptions(
    87  		Modes(Default),
    88  		ProxyFiles(basicProxy),
    89  	).Run(t, src, func(t *testing.T, env *Env) {
    90  		env.OpenFile("main.go")
    91  		env.AfterChange(NoDiagnostics())
    92  		env.RunGoCommand("mod", "tidy")
    93  		env.RunGoCommand("mod", "vendor")
    94  		env.AfterChange(NoDiagnostics())
    95  		env.RegexpReplace("main.go", `import "golang.org/x/hello/hi"`, "")
    96  		env.AfterChange(
    97  			Diagnostics(env.AtRegexp("main.go", "hi.Goodbye")),
    98  		)
    99  		env.SaveBuffer("main.go")
   100  		env.AfterChange(NoDiagnostics())
   101  	})
   102  }