.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / cmd / getgo / main_test.go
1 // Copyright 2017 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 //go:build !plan9
6 // +build !plan9
7
8 package main
9
10 import (
11         "bytes"
12         "fmt"
13         "io/ioutil"
14         "os"
15         "os/exec"
16         "runtime"
17         "testing"
18 )
19
20 const (
21         testbin = "testgetgo"
22 )
23
24 var (
25         exeSuffix string // ".exe" on Windows
26 )
27
28 func init() {
29         if runtime.GOOS == "windows" {
30                 exeSuffix = ".exe"
31         }
32 }
33
34 // TestMain creates a getgo command for testing purposes and
35 // deletes it after the tests have been run.
36 func TestMain(m *testing.M) {
37         if os.Getenv("GOGET_INTEGRATION") == "" {
38                 fmt.Fprintln(os.Stderr, "main_test: Skipping integration tests with GOGET_INTEGRATION unset")
39                 return
40         }
41
42         args := []string{"build", "-tags", testbin, "-o", testbin + exeSuffix}
43         out, err := exec.Command("go", args...).CombinedOutput()
44         if err != nil {
45                 fmt.Fprintf(os.Stderr, "building %s failed: %v\n%s", testbin, err, out)
46                 os.Exit(2)
47         }
48
49         // Don't let these environment variables confuse the test.
50         os.Unsetenv("GOBIN")
51         os.Unsetenv("GOPATH")
52         os.Unsetenv("GIT_ALLOW_PROTOCOL")
53         os.Unsetenv("PATH")
54
55         r := m.Run()
56
57         os.Remove(testbin + exeSuffix)
58
59         os.Exit(r)
60 }
61
62 func createTmpHome(t *testing.T) string {
63         tmpd, err := ioutil.TempDir("", "testgetgo")
64         if err != nil {
65                 t.Fatalf("creating test tempdir failed: %v", err)
66         }
67
68         os.Setenv("HOME", tmpd)
69         return tmpd
70 }
71
72 // doRun runs the test getgo command, recording stdout and stderr and
73 // returning exit status.
74 func doRun(t *testing.T, args ...string) error {
75         var stdout, stderr bytes.Buffer
76         t.Logf("running %s %v", testbin, args)
77         cmd := exec.Command("./"+testbin+exeSuffix, args...)
78         cmd.Stdout = &stdout
79         cmd.Stderr = &stderr
80         cmd.Env = os.Environ()
81         status := cmd.Run()
82         if stdout.Len() > 0 {
83                 t.Log("standard output:")
84                 t.Log(stdout.String())
85         }
86         if stderr.Len() > 0 {
87                 t.Log("standard error:")
88                 t.Log(stderr.String())
89         }
90         return status
91 }
92
93 func TestCommandVerbose(t *testing.T) {
94         tmpd := createTmpHome(t)
95         defer os.RemoveAll(tmpd)
96
97         err := doRun(t, "-v")
98         if err != nil {
99                 t.Fatal(err)
100         }
101         // make sure things are in path
102         shellConfig, err := shellConfigFile()
103         if err != nil {
104                 t.Fatal(err)
105         }
106         b, err := ioutil.ReadFile(shellConfig)
107         if err != nil {
108                 t.Fatal(err)
109         }
110         home, err := getHomeDir()
111         if err != nil {
112                 t.Fatal(err)
113         }
114
115         expected := fmt.Sprintf(`
116 export PATH=$PATH:%s/.go/bin
117
118 export GOPATH=%s/go
119
120 export PATH=$PATH:%s/go/bin
121 `, home, home, home)
122
123         if string(b) != expected {
124                 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
125         }
126 }
127
128 func TestCommandPathExists(t *testing.T) {
129         tmpd := createTmpHome(t)
130         defer os.RemoveAll(tmpd)
131
132         // run once
133         err := doRun(t, "-skip-dl")
134         if err != nil {
135                 t.Fatal(err)
136         }
137         // make sure things are in path
138         shellConfig, err := shellConfigFile()
139         if err != nil {
140                 t.Fatal(err)
141         }
142         b, err := ioutil.ReadFile(shellConfig)
143         if err != nil {
144                 t.Fatal(err)
145         }
146         home, err := getHomeDir()
147         if err != nil {
148                 t.Fatal(err)
149         }
150
151         expected := fmt.Sprintf(`
152 export GOPATH=%s/go
153
154 export PATH=$PATH:%s/go/bin
155 `, home, home)
156
157         if string(b) != expected {
158                 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
159         }
160
161         // run twice
162         if err := doRun(t, "-skip-dl"); err != nil {
163                 t.Fatal(err)
164         }
165
166         b, err = ioutil.ReadFile(shellConfig)
167         if err != nil {
168                 t.Fatal(err)
169         }
170
171         if string(b) != expected {
172                 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
173         }
174 }