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.
24 exeSuffix string // ".exe" on Windows
28 if runtime.GOOS == "windows" {
33 // TestMain creates a getgo command for testing purposes and
34 // deletes it after the tests have been run.
35 func TestMain(m *testing.M) {
36 if os.Getenv("GOGET_INTEGRATION") == "" {
37 fmt.Fprintln(os.Stderr, "main_test: Skipping integration tests with GOGET_INTEGRATION unset")
41 args := []string{"build", "-tags", testbin, "-o", testbin + exeSuffix}
42 out, err := exec.Command("go", args...).CombinedOutput()
44 fmt.Fprintf(os.Stderr, "building %s failed: %v\n%s", testbin, err, out)
48 // Don't let these environment variables confuse the test.
51 os.Unsetenv("GIT_ALLOW_PROTOCOL")
56 os.Remove(testbin + exeSuffix)
61 func createTmpHome(t *testing.T) string {
62 tmpd, err := ioutil.TempDir("", "testgetgo")
64 t.Fatalf("creating test tempdir failed: %v", err)
67 os.Setenv("HOME", tmpd)
71 // doRun runs the test getgo command, recording stdout and stderr and
72 // returning exit status.
73 func doRun(t *testing.T, args ...string) error {
74 var stdout, stderr bytes.Buffer
75 t.Logf("running %s %v", testbin, args)
76 cmd := exec.Command("./"+testbin+exeSuffix, args...)
79 cmd.Env = os.Environ()
82 t.Log("standard output:")
83 t.Log(stdout.String())
86 t.Log("standard error:")
87 t.Log(stderr.String())
92 func TestCommandVerbose(t *testing.T) {
93 tmpd := createTmpHome(t)
94 defer os.RemoveAll(tmpd)
100 // make sure things are in path
101 shellConfig, err := shellConfigFile()
105 b, err := ioutil.ReadFile(shellConfig)
109 home, err := getHomeDir()
114 expected := fmt.Sprintf(`
115 export PATH=$PATH:%s/.go/bin
119 export PATH=$PATH:%s/go/bin
122 if string(b) != expected {
123 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
127 func TestCommandPathExists(t *testing.T) {
128 tmpd := createTmpHome(t)
129 defer os.RemoveAll(tmpd)
132 err := doRun(t, "-skip-dl")
136 // make sure things are in path
137 shellConfig, err := shellConfigFile()
141 b, err := ioutil.ReadFile(shellConfig)
145 home, err := getHomeDir()
150 expected := fmt.Sprintf(`
153 export PATH=$PATH:%s/go/bin
156 if string(b) != expected {
157 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))
161 if err := doRun(t, "-skip-dl"); err != nil {
165 b, err = ioutil.ReadFile(shellConfig)
170 if string(b) != expected {
171 t.Fatalf("%s expected %q, got %q", shellConfig, expected, string(b))