File: path_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.0~git20190125.d66bd3c%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 8,912 kB
  • sloc: asm: 1,394; yacc: 155; makefile: 109; sh: 108; ansic: 17; xml: 11
file content (58 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !plan9

package main

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestAppendPath(t *testing.T) {
	tmpd, err := ioutil.TempDir("", "go")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpd)

	if err := os.Setenv("HOME", tmpd); err != nil {
		t.Fatal(err)
	}

	GOPATH := os.Getenv("GOPATH")
	if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
		t.Fatal(err)
	}

	shellConfig, err := shellConfigFile()
	if err != nil {
		t.Fatal(err)
	}
	b, err := ioutil.ReadFile(shellConfig)
	if err != nil {
		t.Fatal(err)
	}

	expected := "export PATH=" + pathVar + envSeparator + filepath.Join(GOPATH, "bin")
	if strings.TrimSpace(string(b)) != expected {
		t.Fatalf("expected: %q, got %q", expected, strings.TrimSpace(string(b)))
	}

	// Check that appendToPATH is idempotent.
	if err := appendToPATH(filepath.Join(GOPATH, "bin")); err != nil {
		t.Fatal(err)
	}
	b, err = ioutil.ReadFile(shellConfig)
	if err != nil {
		t.Fatal(err)
	}
	if strings.TrimSpace(string(b)) != expected {
		t.Fatalf("expected: %q, got %q", expected, strings.TrimSpace(string(b)))
	}
}