golang.org/x/tools/gopls@v0.15.3/internal/test/integration/misc/generate_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  // TODO(rfindley): figure out why go generate fails on android builders.
     6  
     7  //go:build !android
     8  // +build !android
     9  
    10  package misc
    11  
    12  import (
    13  	"testing"
    14  
    15  	. "golang.org/x/tools/gopls/internal/test/integration"
    16  )
    17  
    18  func TestGenerateProgress(t *testing.T) {
    19  	const generatedWorkspace = `
    20  -- go.mod --
    21  module fake.test
    22  
    23  go 1.14
    24  -- generate.go --
    25  // +build ignore
    26  
    27  package main
    28  
    29  import (
    30  	"os"
    31  )
    32  
    33  func main() {
    34  	os.WriteFile("generated.go", []byte("package " + os.Args[1] + "\n\nconst Answer = 21"), 0644)
    35  }
    36  
    37  -- lib1/lib.go --
    38  package lib1
    39  
    40  //` + `go:generate go run ../generate.go lib1
    41  
    42  -- lib2/lib.go --
    43  package lib2
    44  
    45  //` + `go:generate go run ../generate.go lib2
    46  
    47  -- main.go --
    48  package main
    49  
    50  import (
    51  	"fake.test/lib1"
    52  	"fake.test/lib2"
    53  )
    54  
    55  func main() {
    56  	println(lib1.Answer + lib2.Answer)
    57  }
    58  `
    59  
    60  	Run(t, generatedWorkspace, func(t *testing.T, env *Env) {
    61  		env.OnceMet(
    62  			InitialWorkspaceLoad,
    63  			Diagnostics(env.AtRegexp("main.go", "lib1.(Answer)")),
    64  		)
    65  		env.RunGenerate("./lib1")
    66  		env.RunGenerate("./lib2")
    67  		env.AfterChange(
    68  			NoDiagnostics(ForFile("main.go")),
    69  		)
    70  	})
    71  }
    72  
    73  func TestGenerateUseNetwork(t *testing.T) {
    74  	const proxy = `
    75  -- example.com@v1.2.3/go.mod --
    76  module example.com
    77  
    78  go 1.21
    79  -- example.com@v1.2.3/main.go --
    80  package main
    81  
    82  func main() {
    83  	println("hello world")
    84  }
    85  `
    86  	const generatedWorkspace = `
    87  -- go.mod --
    88  module fake.test
    89  
    90  go 1.21
    91  -- main.go --
    92  
    93  package main
    94  
    95  //go:` + /* hide this string from the go command */ `generate go run example.com@latest
    96  
    97  `
    98  	WithOptions(ProxyFiles(proxy)).
    99  		Run(t, generatedWorkspace, func(t *testing.T, env *Env) {
   100  			env.OnceMet(
   101  				InitialWorkspaceLoad,
   102  			)
   103  			env.RunGenerate("./")
   104  		})
   105  }