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