GoPLS Viewer

Home|gopls/cmd/guru/unit_test.go
1// Copyright 2013 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
5package main
6
7import (
8    "fmt"
9    "go/build"
10    "io/ioutil"
11    "os"
12    "path/filepath"
13    "runtime"
14    "strings"
15    "testing"
16)
17
18// Unit tests for internal guru functions
19
20func TestIssue17515(t *testing.T) {
21    // Tests handling of symlinks in function guessImportPath
22    // If we have Go code inside $HOME/go/src and create a symlink $HOME/src to it
23    // there are 4 possible cases that need to be tested:
24    // (1) absolute & absolute: GOPATH=$HOME/go/src file=$HOME/go/src/test/test.go
25    // (2) absolute & symlink:  GOPATH=$HOME/go/src file=$HOME/src/test/test.go
26    // (3) symlink & symlink:   GOPATH=$HOME/src file=$HOME/src/test/test.go
27    // (4) symlink & absolute:  GOPATH=$HOME/src file= $HOME/go/src/test/test.go
28
29    // Create a temporary home directory under /tmp
30    homeerr := ioutil.TempDir(os.TempDir(), "home")
31    if err != nil {
32        t.Errorf("Unable to create a temporary directory in %s"os.TempDir())
33    }
34
35    defer os.RemoveAll(home)
36
37    // create filepath /tmp/home/go/src/test/test.go
38    if err = os.MkdirAll(home+"/go/src/test"0755); err != nil {
39        t.Fatal(err)
40    }
41
42    var buildContext = build.Default
43
44    // Success test cases
45    type SuccessTest struct {
46        gopathfilenamewantSrcdir string
47    }
48
49    successTests := []SuccessTest{
50        {home + "/go"home + "/go/src/test/test.go"filepath.FromSlash(home + "/go/src")},
51    }
52
53    // Add symlink cases if not on Windows, Plan 9
54    if runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
55        // symlink between /tmp/home/go/src and /tmp/home/src
56        if err := os.Symlink(home+"/go/src"home+"/src"); err != nil {
57            t.Fatal(err)
58        }
59
60        successTests = append(successTests, []SuccessTest{
61            {home + "/go"home + "/src/test/test.go"filepath.FromSlash(home + "/go/src")},
62            {homehome + "/go/src/test/test.go"filepath.FromSlash(home + "/src")},
63            {homehome + "/src/test/test.go"filepath.FromSlash(home + "/src")},
64        }...)
65    }
66
67    for _test := range successTests {
68        buildContext.GOPATH = test.gopath
69        srcdirimportPatherr := guessImportPath(test.filename, &buildContext)
70        if srcdir != test.wantSrcdir || importPath != "test" || err != nil {
71            t.Errorf("guessImportPath(%q, %q) = %q, %q, %q; want %q, %q, %q",
72                test.filenametest.gopathsrcdirimportPatherrtest.wantSrcdir"test""nil")
73        }
74    }
75    // Function to format expected error message
76    errFormat := func(fpath stringstring {
77        return fmt.Sprintf("can't evaluate symlinks of %s"fpath)
78    }
79
80    // Failure test cases
81    type FailTest struct {
82        gopathfilenamewantErr string
83    }
84
85    failTests := []FailTest{
86        {home + "/go"home + "/go/src/fake/test.go"errFormat(filepath.FromSlash(home + "/go/src/fake"))},
87    }
88
89    if runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
90        failTests = append(failTests, []FailTest{
91            {home + "/go"home + "/src/fake/test.go"errFormat(filepath.FromSlash(home + "/src/fake"))},
92            {homehome + "/src/fake/test.go"errFormat(filepath.FromSlash(home + "/src/fake"))},
93            {homehome + "/go/src/fake/test.go"errFormat(filepath.FromSlash(home + "/go/src/fake"))},
94        }...)
95    }
96
97    for _test := range failTests {
98        buildContext.GOPATH = test.gopath
99        srcdirimportPatherr := guessImportPath(test.filename, &buildContext)
100        if !strings.HasPrefix(fmt.Sprint(err), test.wantErr) {
101            t.Errorf("guessImportPath(%q, %q) = %q, %q, %q; want %q, %q, %q",
102                test.filenametest.gopathsrcdirimportPatherr""""test.wantErr)
103        }
104    }
105}
106
MembersX
TestIssue17515.RangeStmt_3168.test
TestIssue17515.RangeStmt_3168.BlockStmt.srcdir
TestIssue17515.SuccessTest.gopath
TestIssue17515.SuccessTest.wantSrcdir
TestIssue17515.RangeStmt_2013.BlockStmt.srcdir
TestIssue17515.RangeStmt_2013.BlockStmt.importPath
TestIssue17515.RangeStmt_2013.BlockStmt.err
TestIssue17515.FailTest
TestIssue17515.FailTest.gopath
TestIssue17515.FailTest.filename
TestIssue17515.err
TestIssue17515.SuccessTest
TestIssue17515.failTests
TestIssue17515.BlockStmt.err
TestIssue17515.RangeStmt_2013.test
TestIssue17515.FailTest.wantErr
TestIssue17515.home
TestIssue17515.successTests
TestIssue17515.buildContext
TestIssue17515.SuccessTest.filename
TestIssue17515.RangeStmt_3168.BlockStmt.importPath
TestIssue17515.RangeStmt_3168.BlockStmt.err
TestIssue17515
TestIssue17515.t
Members
X