File: unit_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 (105 lines) | stat: -rw-r--r-- 3,525 bytes parent folder | download | duplicates (3)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2013 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.

package main

import (
	"fmt"
	"go/build"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"testing"
)

// Unit tests for internal guru functions

func TestIssue17515(t *testing.T) {
	// Tests handling of symlinks in function guessImportPath
	// If we have Go code inside $HOME/go/src and create a symlink $HOME/src to it
	// there are 4 possible cases that need to be tested:
	// (1) absolute & absolute: GOPATH=$HOME/go/src file=$HOME/go/src/test/test.go
	// (2) absolute & symlink:  GOPATH=$HOME/go/src file=$HOME/src/test/test.go
	// (3) symlink & symlink:   GOPATH=$HOME/src file=$HOME/src/test/test.go
	// (4) symlink & absolute:  GOPATH=$HOME/src file= $HOME/go/src/test/test.go

	// Create a temporary home directory under /tmp
	home, err := ioutil.TempDir(os.TempDir(), "home")
	if err != nil {
		t.Errorf("Unable to create a temporary directory in %s", os.TempDir())
	}

	defer os.RemoveAll(home)

	// create filepath /tmp/home/go/src/test/test.go
	if err = os.MkdirAll(home+"/go/src/test", 0755); err != nil {
		t.Fatal(err)
	}

	var buildContext = build.Default

	// Success test cases
	type SuccessTest struct {
		gopath, filename, wantSrcdir string
	}

	successTests := []SuccessTest{
		{home + "/go", home + "/go/src/test/test.go", filepath.FromSlash(home + "/go/src")},
	}

	// Add symlink cases if not on Windows, Plan 9
	if runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
		// symlink between /tmp/home/go/src and /tmp/home/src
		if err := os.Symlink(home+"/go/src", home+"/src"); err != nil {
			t.Fatal(err)
		}

		successTests = append(successTests, []SuccessTest{
			{home + "/go", home + "/src/test/test.go", filepath.FromSlash(home + "/go/src")},
			{home, home + "/go/src/test/test.go", filepath.FromSlash(home + "/src")},
			{home, home + "/src/test/test.go", filepath.FromSlash(home + "/src")},
		}...)
	}

	for _, test := range successTests {
		buildContext.GOPATH = test.gopath
		srcdir, importPath, err := guessImportPath(test.filename, &buildContext)
		if srcdir != test.wantSrcdir || importPath != "test" || err != nil {
			t.Errorf("guessImportPath(%q, %q) = %q, %q, %q; want %q, %q, %q",
				test.filename, test.gopath, srcdir, importPath, err, test.wantSrcdir, "test", "nil")
		}
	}
	// Function to format expected error message
	errFormat := func(fpath string) string {
		return fmt.Sprintf("can't evaluate symlinks of %s", fpath)
	}

	// Failure test cases
	type FailTest struct {
		gopath, filename, wantErr string
	}

	failTests := []FailTest{
		{home + "/go", home + "/go/src/fake/test.go", errFormat(filepath.FromSlash(home + "/go/src/fake"))},
	}

	if runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
		failTests = append(failTests, []FailTest{
			{home + "/go", home + "/src/fake/test.go", errFormat(filepath.FromSlash(home + "/src/fake"))},
			{home, home + "/src/fake/test.go", errFormat(filepath.FromSlash(home + "/src/fake"))},
			{home, home + "/go/src/fake/test.go", errFormat(filepath.FromSlash(home + "/go/src/fake"))},
		}...)
	}

	for _, test := range failTests {
		buildContext.GOPATH = test.gopath
		srcdir, importPath, err := guessImportPath(test.filename, &buildContext)
		if !strings.HasPrefix(fmt.Sprint(err), test.wantErr) {
			t.Errorf("guessImportPath(%q, %q) = %q, %q, %q; want %q, %q, %q",
				test.filename, test.gopath, srcdir, importPath, err, "", "", test.wantErr)
		}
	}
}