GoPLS Viewer

Home|gopls/go/analysis/internal/checker/fix_test.go
1// Copyright 2022 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
5package checker_test
6
7import (
8    "flag"
9    "io/ioutil"
10    "os"
11    "os/exec"
12    "path"
13    "runtime"
14    "testing"
15
16    "golang.org/x/tools/go/analysis"
17    "golang.org/x/tools/go/analysis/analysistest"
18    "golang.org/x/tools/go/analysis/internal/checker"
19    "golang.org/x/tools/internal/testenv"
20)
21
22func main() {
23    checker.Fix = true
24    patterns := flag.Args()
25
26    code := checker.Run(patterns, []*analysis.Analyzer{analyzer})
27    os.Exit(code)
28}
29
30// TestFixes ensures that checker.Run applies fixes correctly.
31// This test fork/execs the main function above.
32func TestFixes(t *testing.T) {
33    oses := map[string]bool{"darwin"true"linux"true}
34    if !oses[runtime.GOOS] {
35        t.Skipf("skipping fork/exec test on this platform")
36    }
37
38    if os.Getenv("TESTFIXES_CHILD") == "1" {
39        // child process
40
41        // replace [progname -test.run=TestFixes -- ...]
42        //      by [progname ...]
43        os.Args = os.Args[2:]
44        os.Args[0] = "vet"
45        main()
46        panic("unreachable")
47    }
48
49    testenv.NeedsTool(t"go")
50
51    files := map[string]string{
52        "rename/foo.go"`package rename
53
54func Foo() {
55    bar := 12
56    _ = bar
57}
58
59// the end
60`,
61        "rename/intestfile_test.go"`package rename
62
63func InTestFile() {
64    bar := 13
65    _ = bar
66}
67
68// the end
69`,
70        "rename/foo_test.go"`package rename_test
71
72func Foo() {
73    bar := 14
74    _ = bar
75}
76
77// the end
78`,
79    }
80    fixed := map[string]string{
81        "rename/foo.go"`package rename
82
83func Foo() {
84    baz := 12
85    _ = baz
86}
87
88// the end
89`,
90        "rename/intestfile_test.go"`package rename
91
92func InTestFile() {
93    baz := 13
94    _ = baz
95}
96
97// the end
98`,
99        "rename/foo_test.go"`package rename_test
100
101func Foo() {
102    baz := 14
103    _ = baz
104}
105
106// the end
107`,
108    }
109    dircleanuperr := analysistest.WriteFiles(files)
110    if err != nil {
111        t.Fatalf("Creating test files failed with %s"err)
112    }
113    defer cleanup()
114
115    args := []string{"-test.run=TestFixes""--""rename"}
116    cmd := exec.Command(os.Args[0], args...)
117    cmd.Env = append(os.Environ(), "TESTFIXES_CHILD=1""GOPATH="+dir"GO111MODULE=off""GOPROXY=off")
118
119    outerr := cmd.CombinedOutput()
120    if len(out) > 0 {
121        t.Logf("%s: out=<<%s>>"argsout)
122    }
123    var exitcode int
124    if errok := err.(*exec.ExitError); ok {
125        exitcode = err.ExitCode() // requires go1.12
126    }
127
128    const diagnosticsExitCode = 3
129    if exitcode != diagnosticsExitCode {
130        t.Errorf("%s: exited %d, want %d"argsexitcodediagnosticsExitCode)
131    }
132
133    for namewant := range fixed {
134        path := path.Join(dir"src"name)
135        contentserr := ioutil.ReadFile(path)
136        if err != nil {
137            t.Errorf("error reading %s: %v"patherr)
138        }
139        if got := string(contents); got != want {
140            t.Errorf("contents of %s file did not match expectations. got=%s, want=%s"pathgotwant)
141        }
142    }
143}
144
MembersX
TestFixes.RangeStmt_2449.BlockStmt.path
main.code
TestFixes
TestFixes.oses
TestFixes.files
TestFixes.dir
TestFixes.err
TestFixes.RangeStmt_2449.want
TestFixes.RangeStmt_2449.BlockStmt.got
TestFixes.RangeStmt_2449.name
TestFixes.RangeStmt_2449.BlockStmt.err
exec
main
main.patterns
TestFixes.t
TestFixes.cmd
TestFixes.exitcode
TestFixes.diagnosticsExitCode
path
TestFixes.fixed
TestFixes.cleanup
TestFixes.args
TestFixes.out
TestFixes.RangeStmt_2449.BlockStmt.contents
Members
X