GoPLS Viewer

Home|gopls/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
8package main
9
10import (
11    "bytes"
12    "fmt"
13    "io/ioutil"
14    "os"
15    "os/exec"
16    "testing"
17)
18
19func TestMain(m *testing.M) {
20    if os.Getenv("GO_GETGO_TEST_IS_GETGO") != "" {
21        main()
22        os.Exit(0)
23    }
24
25    if os.Getenv("GOGET_INTEGRATION") == "" {
26        fmt.Fprintln(os.Stderr"main_test: Skipping integration tests with GOGET_INTEGRATION unset")
27        return
28    }
29
30    // Don't let these environment variables confuse the test.
31    os.Unsetenv("GOBIN")
32    os.Unsetenv("GOPATH")
33    os.Unsetenv("GIT_ALLOW_PROTOCOL")
34    os.Unsetenv("PATH")
35
36    os.Exit(m.Run())
37}
38
39func createTmpHome(t *testing.Tstring {
40    tmpderr := ioutil.TempDir("""testgetgo")
41    if err != nil {
42        t.Fatalf("creating test tempdir failed: %v"err)
43    }
44
45    os.Setenv("HOME"tmpd)
46    return tmpd
47}
48
49// doRun runs the test getgo command, recording stdout and stderr and
50// returning exit status.
51func doRun(t *testing.Targs ...stringerror {
52    exeerr := os.Executable()
53    if err != nil {
54        t.Fatal(err)
55    }
56    t.Helper()
57
58    t.Logf("running getgo %v"args)
59    var stdoutstderr bytes.Buffer
60    cmd := exec.Command(exeargs...)
61    cmd.Stdout = &stdout
62    cmd.Stderr = &stderr
63    cmd.Env = append(os.Environ(), "GO_GETGO_TEST_IS_GETGO=1")
64    status := cmd.Run()
65    if stdout.Len() > 0 {
66        t.Log("standard output:")
67        t.Log(stdout.String())
68    }
69    if stderr.Len() > 0 {
70        t.Log("standard error:")
71        t.Log(stderr.String())
72    }
73    return status
74}
75
76func TestCommandVerbose(t *testing.T) {
77    tmpd := createTmpHome(t)
78    defer os.RemoveAll(tmpd)
79
80    err := doRun(t"-v")
81    if err != nil {
82        t.Fatal(err)
83    }
84    // make sure things are in path
85    shellConfigerr := shellConfigFile()
86    if err != nil {
87        t.Fatal(err)
88    }
89    berr := ioutil.ReadFile(shellConfig)
90    if err != nil {
91        t.Fatal(err)
92    }
93    homeerr := getHomeDir()
94    if err != nil {
95        t.Fatal(err)
96    }
97
98    expected := fmt.Sprintf(`
99export PATH=$PATH:%s/.go/bin
100
101export GOPATH=%s/go
102
103export PATH=$PATH:%s/go/bin
104`homehomehome)
105
106    if string(b) != expected {
107        t.Fatalf("%s expected %q, got %q"shellConfigexpectedstring(b))
108    }
109}
110
111func TestCommandPathExists(t *testing.T) {
112    tmpd := createTmpHome(t)
113    defer os.RemoveAll(tmpd)
114
115    // run once
116    err := doRun(t"-skip-dl")
117    if err != nil {
118        t.Fatal(err)
119    }
120    // make sure things are in path
121    shellConfigerr := shellConfigFile()
122    if err != nil {
123        t.Fatal(err)
124    }
125    berr := ioutil.ReadFile(shellConfig)
126    if err != nil {
127        t.Fatal(err)
128    }
129    homeerr := getHomeDir()
130    if err != nil {
131        t.Fatal(err)
132    }
133
134    expected := fmt.Sprintf(`
135export GOPATH=%s/go
136
137export PATH=$PATH:%s/go/bin
138`homehome)
139
140    if string(b) != expected {
141        t.Fatalf("%s expected %q, got %q"shellConfigexpectedstring(b))
142    }
143
144    // run twice
145    if err := doRun(t"-skip-dl"); err != nil {
146        t.Fatal(err)
147    }
148
149    berr = ioutil.ReadFile(shellConfig)
150    if err != nil {
151        t.Fatal(err)
152    }
153
154    if string(b) != expected {
155        t.Fatalf("%s expected %q, got %q"shellConfigexpectedstring(b))
156    }
157}
158
MembersX
TestCommandPathExists.err
TestCommandPathExists.shellConfig
doRun.args
createTmpHome.tmpd
createTmpHome.err
doRun
doRun.err
TestCommandPathExists.expected
TestMain
TestCommandVerbose.err
createTmpHome
doRun.t
doRun.stdout
TestCommandVerbose
TestMain.m
TestCommandPathExists.tmpd
TestCommandVerbose.expected
TestCommandVerbose.home
TestCommandPathExists
TestCommandPathExists.b
TestCommandPathExists.home
createTmpHome.t
doRun.exe
doRun.cmd
doRun.status
TestCommandVerbose.shellConfig
TestCommandVerbose.b
bytes
TestCommandVerbose.t
TestCommandVerbose.tmpd
TestCommandPathExists.t
doRun.stderr
Members
X