Debian Codegraph @

gopls/internal/protocol/uri_windows_test.go

raw symbols SCIP
1 // Copyright 2020 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 //go:build windows
6
7 package protocol_test
8
9 import (
10 "path/filepath"
11 "testing"
12
13 "golang.org/x/tools/gopls/internal/protocol"
14 )
15
16 // TestURIFromPath tests the conversion between URIs and filenames. The test cases
17 // include Windows-style URIs and filepaths, but we avoid having OS-specific
18 // tests by using only forward slashes, assuming that the standard library
19 // functions filepath.ToSlash and filepath.FromSlash do not need testing.
20 func TestURIFromPath(t *testing.T) {
21 rootPath, err := filepath.Abs("/")
22 if err != nil {
23 t.Fatal(err)
24 }
25 if len(rootPath) < 2 || rootPath[1] != ':' {
26 t.Fatalf("malformed root path %q", rootPath)
27 }
28 driveLetter := string(rootPath[0])
29
30 for _, test := range []struct {
31 path, wantFile string
32 wantURI protocol.DocumentURI
33 }{
34 {
35 path: ``,
36 wantFile: ``,
37 wantURI: protocol.DocumentURI(""),
38 },
39 {
40 path: `C:\Windows\System32`,
41 wantFile: `C:\Windows\System32`,
42 wantURI: protocol.DocumentURI("file:///C:/Windows/System32"),
43 },
44 {
45 path: `C:\Go\src\bob.go`,
46 wantFile: `C:\Go\src\bob.go`,
47 wantURI: protocol.DocumentURI("file:///C:/Go/src/bob.go"),
48 },
49 {
50 path: `c:\Go\src\bob.go`,
51 wantFile: `C:\Go\src\bob.go`,
52 wantURI: protocol.DocumentURI("file:///C:/Go/src/bob.go"),
53 },
54 {
55 path: `\path\to\dir`,
56 wantFile: driveLetter + `:\path\to\dir`,
57 wantURI: protocol.DocumentURI("file:///" + driveLetter + ":/path/to/dir"),
58 },
59 {
60 path: `\a\b\c\src\bob.go`,
61 wantFile: driveLetter + `:\a\b\c\src\bob.go`,
62 wantURI: protocol.DocumentURI("file:///" + driveLetter + ":/a/b/c/src/bob.go"),
63 },
64 {
65 path: `c:\Go\src\bob george\george\george.go`,
66 wantFile: `C:\Go\src\bob george\george\george.go`,
67 wantURI: protocol.DocumentURI("file:///C:/Go/src/bob%20george/george/george.go"),
68 },
69 } {
70 got := protocol.URIFromPath(test.path)
71 if got != test.wantURI {
72 t.Errorf("URIFromPath(%q): got %q, expected %q", test.path, got, test.wantURI)
73 }
74 gotFilename := got.Path()
75 if gotFilename != test.wantFile {
76 t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
77 }
78 }
79 }
80
81 func TestParseDocumentURI(t *testing.T) {
82 for _, test := range []struct {
83 input string
84 want string // string(DocumentURI) on success or error.Error() on failure
85 wantPath string // expected DocumentURI.Path on success
86 }{
87 {
88 input: `file:///c:/Go/src/bob%20george/george/george.go`,
89 want: "file:///C:/Go/src/bob%20george/george/george.go",
90 wantPath: `C:\Go\src\bob george\george\george.go`,
91 },
92 {
93 input: `file:///C%3A/Go/src/bob%20george/george/george.go`,
94 want: "file:///C:/Go/src/bob%20george/george/george.go",
95 wantPath: `C:\Go\src\bob george\george\george.go`,
96 },
97 {
98 input: `file:///c:/path/to/%25p%25ercent%25/per%25cent.go`,
99 want: `file:///C:/path/to/%25p%25ercent%25/per%25cent.go`,
100 wantPath: `C:\path\to\%p%ercent%\per%cent.go`,
101 },
102 {
103 input: `file:///C%3A/`,
104 want: `file:///C:/`,
105 wantPath: `C:\`,
106 },
107 {
108 input: `file:///`,
109 want: `file:///`,
110 wantPath: `\`,
111 },
112 {
113 input: "",
114 want: "",
115 wantPath: "",
116 },
117 // Errors:
118 {
119 input: "https://go.dev/",
120 want: "DocumentURI scheme is not 'file': https://go.dev/",
121 },
122 } {
123 uri, err := protocol.ParseDocumentURI(test.input)
124 var got string
125 if err != nil {
126 got = err.Error()
127 } else {
128 got = string(uri)
129 }
130 if got != test.want {
131 t.Errorf("ParseDocumentURI(%q): got %q, want %q", test.input, got, test.want)
132 }
133 if err == nil && uri.Path() != test.wantPath {
134 t.Errorf("DocumentURI(%s).Path = %q, want %q", uri,
135 uri.Path(), test.wantPath)
136 }
137 }
138 }