github.com/april1989/origin-go-tools@v0.0.32/cmd/getgo/main_test.go (about)

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