github.com/april1989/origin-go-tools@v0.0.32/cmd/getgo/path_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  	"io/ioutil"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func TestAppendPath(t *testing.T) {
    18  	tmpd, err := ioutil.TempDir("", "go")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer os.RemoveAll(tmpd)
    23  
    24  	if err := os.Setenv("HOME", tmpd); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  
    28  	GOPATH := os.Getenv("GOPATH")
    29  	if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	shellConfig, err := shellConfigFile()
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	b, err := ioutil.ReadFile(shellConfig)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	expected := "export PATH=" + pathVar + envSeparator + filepath.Join(GOPATH, "bin")
    43  	if strings.TrimSpace(string(b)) != expected {
    44  		t.Fatalf("expected: %q, got %q", expected, strings.TrimSpace(string(b)))
    45  	}
    46  
    47  	// Check that appendToPATH is idempotent.
    48  	if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	b, err = ioutil.ReadFile(shellConfig)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	if strings.TrimSpace(string(b)) != expected {
    56  		t.Fatalf("expected: %q, got %q", expected, strings.TrimSpace(string(b)))
    57  	}
    58  }