From 20886c763765bdc24a1e5e141a3d609eb3389eef Mon Sep 17 00:00:00 2001 From: Keegan Carruthers-Smith Date: Wed, 14 Feb 2018 12:37:43 +0200 Subject: [PATCH] Test tip on travis (#236) * Test tip on travis * Only test if godef to GOROOT matches path * Install all instead of ... go1.10 introduces the cache directory. So now objects will only appear in `pkg` if they are explicitly installed. Previous versions of go would create entries for the transitive dependencies. So for our vendor tests running `go install -i ...` would not create a corresponding file in `pkg` for the vendored packages since `...` does not list vendor. Using `all` instead does include the vendored pkg, so will include the vendored pkgs. This will likely have wider effects on go tooling that rely on pkg. Already filed https://github.com/nsf/gocode/issues/500 for gocode. * Handle differing diagnostic output * Correctly remove range in def test on windows --- .travis.yml | 1 + langserver/langserver_test.go | 14 +++++++++++--- langserver/loader_test.go | 30 ++++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e19a5c2..bb8e1e31 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: go go: - 1.8.x - 1.9.x + - tip go_import_path: github.com/sourcegraph/go-langserver diff --git a/langserver/langserver_test.go b/langserver/langserver_test.go index aaa43968..0f27f12b 100644 --- a/langserver/langserver_test.go +++ b/langserver/langserver_test.go @@ -449,7 +449,7 @@ package main; import "test/pkg"; func B() { p.A(); B() }`, // "a.go:1:53": "type int int", }, overrideGodefDefinition: map[string]string{ - "a.go:1:40": "/goroot/src/fmt/print.go:256:6-256:13", // hitting the real GOROOT + "a.go:1:40": "/goroot/src/fmt/print.go", // hitting the real GOROOT "a.go:1:53": "/goroot/src/builtin/builtin.go:1:1-1:1", // TODO: accurate builtin positions }, wantDefinition: map[string]string{ @@ -1257,12 +1257,12 @@ func lspTests(t testing.TB, ctx context.Context, h *LangHandler, c *jsonrpc2.Con // Install all Go packages in the $GOPATH. oldGOPATH := os.Getenv("GOPATH") os.Setenv("GOPATH", tmpDir) - out, err := exec.Command("go", "install", "-v", "...").CombinedOutput() + out, err := exec.Command("go", "install", "-v", "all").CombinedOutput() os.Setenv("GOPATH", oldGOPATH) if err != nil { t.Fatal(err) } - t.Logf("$ go install -v ...\n%s", out) + t.Logf("$ go install -v all\n%s", out) testOSToVFSPath = func(osPath string) string { return strings.TrimPrefix(osPath, util.UriToPath(util.PathToURI(tmpDir))) @@ -1389,6 +1389,14 @@ func definitionTest(t testing.TB, ctx context.Context, c *jsonrpc2.Conn, rootURI definition = strings.TrimPrefix(definition, util.UriToPath(util.PathToURI(trimPrefix))) } } + if want != "" && !strings.Contains(path.Base(want), ":") { + // our want is just a path, so we only check that matches. This is + // used by our godef tests into GOROOT. The GOROOT changes over time, + // but the file for a symbol is usually pretty stable. + dir := path.Dir(definition) + base := strings.Split(path.Base(definition), ":")[0] + definition = path.Join(dir, base) + } if definition != want { t.Errorf("got %q, want %q", definition, want) } diff --git a/langserver/loader_test.go b/langserver/loader_test.go index df12e871..e2a53f56 100644 --- a/langserver/loader_test.go +++ b/langserver/loader_test.go @@ -90,7 +90,9 @@ func TestLoaderDiagnostics(t *testing.T) { cases := []struct { Name string FS map[string]string - Want diagnostics + // Want is a slice to cater for slight changes in error messages + // across go versions. + Want []diagnostics }{ { Name: "none", @@ -99,12 +101,17 @@ func TestLoaderDiagnostics(t *testing.T) { { Name: "malformed", FS: map[string]string{"/src/p/f.go": `234ljsdfjb2@#%$`}, - Want: m(`{"/src/p/f.go":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}},"severity":1,"source":"go","message":"expected 'package', found 'INT' 234 (and 4 more errors)"}]}`), + Want: []diagnostics{ + m(`{"/src/p/f.go":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}},"severity":1,"source":"go","message":"expected 'package', found 'INT' 234 (and 4 more errors)"}]}`), + m(`{"/src/p/f.go":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":0}},"severity":1,"source":"go","message":"expected 'package', found 234 (and 4 more errors)"}]}`), + }, }, { Name: "undeclared", FS: map[string]string{"/src/p/f.go": `package p; var _ = http.Get`}, - Want: m(`{"/src/p/f.go":[{"range":{"start":{"line":0,"character":19},"end":{"line":0,"character":23}},"severity":1,"source":"go","message":"undeclared name: http"}]}`), + Want: []diagnostics{ + m(`{"/src/p/f.go":[{"range":{"start":{"line":0,"character":19},"end":{"line":0,"character":23}},"severity":1,"source":"go","message":"undeclared name: http"}]}`), + }, }, } ctx := context.Background() @@ -115,10 +122,21 @@ func TestLoaderDiagnostics(t *testing.T) { if err != nil { t.Error(err) } - if !reflect.DeepEqual(diag, tc.Want) { + found := false + for _, want := range tc.Want { + found = found || reflect.DeepEqual(diag, want) + } + if found { + return + } + var want diagnostics + if len(tc.Want) > 0 { + want = tc.Want[0] + } + if !reflect.DeepEqual(diag, want) { got, _ := json.Marshal(diag) - want, _ := json.Marshal(tc.Want) - t.Errorf("got %s\nwant %s", string(got), string(want)) + wantS, _ := json.Marshal(want) + t.Errorf("got %s\nwant %s", string(got), string(wantS)) } }) }