提交 732d7f3c 编写于 作者: J Johannes Rieken 提交者: GitHub

Merge pull request #22888 from Microsoft/joh/21684

Uri.file fix
......@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import platform = require('vs/base/common/platform');
import * as platform from 'vs/base/common/platform';
function _encode(ch: string): string {
......@@ -152,7 +152,7 @@ export default class URI {
return this;
}
let {scheme, authority, path, query, fragment} = change;
let { scheme, authority, path, query, fragment } = change;
if (scheme === void 0) {
scheme = this.scheme;
} else if (scheme === null) {
......@@ -217,8 +217,12 @@ export default class URI {
const ret = new URI();
ret._scheme = 'file';
// normalize to fwd-slashes
path = path.replace(/\\/g, URI._slash);
// normalize to fwd-slashes on windows,
// on other systems bwd-slaches are valid
// filename character, eg /f\oo/ba\r.txt
if (platform.isWindows) {
path = path.replace(/\\/g, URI._slash);
}
// check for authority as used in UNC shares
// or use the path as given
......@@ -325,7 +329,7 @@ export default class URI {
const parts: string[] = [];
let {scheme, authority, path, query, fragment} = uri;
let { scheme, authority, path, query, fragment } = uri;
if (scheme) {
parts.push(scheme, ':');
}
......
......@@ -7,6 +7,8 @@
import * as assert from 'assert';
import URI from 'vs/base/common/uri';
import { normalize } from 'vs/base/common/paths';
import { isWindows } from 'vs/base/common/platform';
suite('URI', () => {
test('file#toString', () => {
......@@ -14,18 +16,36 @@ suite('URI', () => {
assert.equal(URI.file('C:/win/path').toString(), 'file:///c%3A/win/path');
assert.equal(URI.file('c:/win/path/').toString(), 'file:///c%3A/win/path/');
assert.equal(URI.file('/c:/win/path').toString(), 'file:///c%3A/win/path');
assert.equal(URI.file('c:\\win\\path').toString(), 'file:///c%3A/win/path');
assert.equal(URI.file('c:\\win/path').toString(), 'file:///c%3A/win/path');
});
test('file#path', () => {
assert.equal(URI.file('c:/win/path').fsPath.replace(/\\/g, '/'), 'c:/win/path');
assert.equal(URI.file('c:/win/path/').fsPath.replace(/\\/g, '/'), 'c:/win/path/');
assert.equal(URI.file('C:/win/path').fsPath.replace(/\\/g, '/'), 'c:/win/path');
assert.equal(URI.file('/c:/win/path').fsPath.replace(/\\/g, '/'), 'c:/win/path');
assert.equal(URI.file('./c/win/path').fsPath.replace(/\\/g, '/'), '/./c/win/path');
assert.equal(URI.file('c:\\win\\path').fsPath.replace(/\\/g, '/'), 'c:/win/path');
assert.equal(URI.file('c:\\win/path').fsPath.replace(/\\/g, '/'), 'c:/win/path');
test('URI.file (win-special)', () => {
if (isWindows) {
assert.equal(URI.file('c:\\win\\path').toString(), 'file:///c%3A/win/path');
assert.equal(URI.file('c:\\win/path').toString(), 'file:///c%3A/win/path');
} else {
assert.equal(URI.file('c:\\win\\path').toString(), 'file:///c%3A%5Cwin%5Cpath');
assert.equal(URI.file('c:\\win/path').toString(), 'file:///c%3A%5Cwin/path');
}
});
test('file#fsPath (win-special)', () => {
if (isWindows) {
assert.equal(URI.file('c:\\win\\path').fsPath, 'c:\\win\\path');
assert.equal(URI.file('c:\\win/path').fsPath, 'c:\\win\\path');
assert.equal(URI.file('c:/win/path').fsPath, 'c:\\win\\path');
assert.equal(URI.file('c:/win/path/').fsPath, 'c:\\win\\path\\');
assert.equal(URI.file('C:/win/path').fsPath, 'c:\\win\\path');
assert.equal(URI.file('/c:/win/path').fsPath, 'c:\\win\\path');
assert.equal(URI.file('./c/win/path').fsPath, '\\.\\c\\win\\path');
} else {
assert.equal(URI.file('c:/win/path').fsPath, 'c:/win/path');
assert.equal(URI.file('c:/win/path/').fsPath, 'c:/win/path/');
assert.equal(URI.file('C:/win/path').fsPath, 'c:/win/path');
assert.equal(URI.file('/c:/win/path').fsPath, 'c:/win/path');
assert.equal(URI.file('./c/win/path').fsPath, '/./c/win/path');
}
});
test('URI#fsPath - no `fsPath` when no `path`', () => {
......@@ -244,69 +264,61 @@ suite('URI', () => {
assert.throws(() => URI.parse('file:////shares/files/p.cs'));
});
test('URI#file', () => {
var value = URI.file('\\\\shäres\\path\\c#\\plugin.json');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shäres');
assert.equal(value.path, '/path/c#/plugin.json');
assert.equal(value.fragment, '');
assert.equal(value.query, '');
assert.equal(value.toString(), 'file://sh%C3%A4res/path/c%23/plugin.json');
// identity toString -> parse -> toString
value = URI.parse(value.toString());
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shäres');
assert.equal(value.path, '/path/c#/plugin.json');
assert.equal(value.fragment, '');
assert.equal(value.query, '');
assert.equal(value.toString(), 'file://sh%C3%A4res/path/c%23/plugin.json');
value = URI.file('\\\\localhost\\c$\\GitDevelopment\\express');
assert.equal(value.scheme, 'file');
assert.equal(value.path, '/c$/GitDevelopment/express');
assert.equal(value.fsPath, normalize('//localhost/c$/GitDevelopment/express', true));
assert.equal(value.query, '');
assert.equal(value.fragment, '');
assert.equal(value.toString(), 'file://localhost/c%24/GitDevelopment/express');
value = URI.file('c:\\test with %\\path');
assert.equal(value.path, '/c:/test with %/path');
assert.equal(value.toString(), 'file:///c%3A/test%20with%20%25/path');
value = URI.file('c:\\test with %25\\path');
assert.equal(value.path, '/c:/test with %25/path');
assert.equal(value.toString(), 'file:///c%3A/test%20with%20%2525/path');
value = URI.file('c:\\test with %25\\c#code');
assert.equal(value.path, '/c:/test with %25/c#code');
assert.equal(value.toString(), 'file:///c%3A/test%20with%20%2525/c%23code');
value = URI.file('\\\\shares');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shares');
assert.equal(value.path, '/'); // slash is always there
test('URI#file, win-speciale', () => {
if (isWindows) {
var value = URI.file('c:\\test\\drive');
assert.equal(value.path, '/c:/test/drive');
assert.equal(value.toString(), 'file:///c%3A/test/drive');
value = URI.file('\\\\shäres\\path\\c#\\plugin.json');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shäres');
assert.equal(value.path, '/path/c#/plugin.json');
assert.equal(value.fragment, '');
assert.equal(value.query, '');
assert.equal(value.toString(), 'file://sh%C3%A4res/path/c%23/plugin.json');
value = URI.file('\\\\localhost\\c$\\GitDevelopment\\express');
assert.equal(value.scheme, 'file');
assert.equal(value.path, '/c$/GitDevelopment/express');
assert.equal(value.fsPath, '\\\\localhost\\c$\\GitDevelopment\\express');
assert.equal(value.query, '');
assert.equal(value.fragment, '');
assert.equal(value.toString(), 'file://localhost/c%24/GitDevelopment/express');
value = URI.file('c:\\test with %\\path');
assert.equal(value.path, '/c:/test with %/path');
assert.equal(value.toString(), 'file:///c%3A/test%20with%20%25/path');
value = URI.file('c:\\test with %25\\path');
assert.equal(value.path, '/c:/test with %25/path');
assert.equal(value.toString(), 'file:///c%3A/test%20with%20%2525/path');
value = URI.file('c:\\test with %25\\c#code');
assert.equal(value.path, '/c:/test with %25/c#code');
assert.equal(value.toString(), 'file:///c%3A/test%20with%20%2525/c%23code');
value = URI.file('\\\\shares');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shares');
assert.equal(value.path, '/'); // slash is always there
value = URI.file('\\\\shares\\');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shares');
assert.equal(value.path, '/');
}
});
value = URI.file('\\\\shares\\');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, 'shares');
assert.equal(value.path, '/');
test('URI#file, no path-is-uri check', () => {
// we don't complain here
value = URI.file('file://path/to/file');
let value = URI.file('file://path/to/file');
assert.equal(value.scheme, 'file');
assert.equal(value.authority, '');
assert.equal(value.path, '/file://path/to/file');
});
test('URI#file, auto-slash windows drive letter', () => {
var value = URI.file('c:\\test\\drive');
assert.equal(value.path, '/c:/test/drive');
assert.equal(value.toString(), 'file:///c%3A/test/drive');
});
test('URI#file, always slash', () => {
var value = URI.file('a.file');
......@@ -419,4 +431,4 @@ suite('URI', () => {
// }
// console.profileEnd();
});
});
\ No newline at end of file
});
......@@ -32,18 +32,18 @@ suite('SearchResult', () => {
});
test('Line Match', function () {
let fileMatch = aFileMatch('folder\\file.txt', null);
let fileMatch = aFileMatch('folder/file.txt', null);
let lineMatch = new Match(fileMatch, 'foo bar', 1, 0, 3);
assert.equal(lineMatch.text(), 'foo bar');
assert.equal(lineMatch.range().startLineNumber, 2);
assert.equal(lineMatch.range().endLineNumber, 2);
assert.equal(lineMatch.range().startColumn, 1);
assert.equal(lineMatch.range().endColumn, 4);
assert.equal('file:///c%3A/folder/file.txt>1>0foo', lineMatch.id());
assert.equal('file:///folder/file.txt>1>0foo', lineMatch.id());
});
test('Line Match - Remove', function () {
let fileMatch = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let fileMatch = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo bar',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -54,19 +54,19 @@ suite('SearchResult', () => {
});
test('File Match', function () {
let fileMatch = aFileMatch('folder\\file.txt');
let fileMatch = aFileMatch('folder/file.txt');
assert.equal(fileMatch.matches(), 0);
assert.equal(fileMatch.resource().toString(), 'file:///c%3A/folder/file.txt');
assert.equal(fileMatch.resource().toString(), 'file:///folder/file.txt');
assert.equal(fileMatch.name(), 'file.txt');
fileMatch = aFileMatch('file.txt');
assert.equal(fileMatch.matches(), 0);
assert.equal(fileMatch.resource().toString(), 'file:///c%3A/file.txt');
assert.equal(fileMatch.resource().toString(), 'file:///file.txt');
assert.equal(fileMatch.name(), 'file.txt');
});
test('File Match: Select an existing match', function () {
let testObject = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let testObject = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -82,7 +82,7 @@ suite('SearchResult', () => {
});
test('File Match: Select non existing match', function () {
let testObject = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let testObject = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -100,7 +100,7 @@ suite('SearchResult', () => {
});
test('File Match: isSelected return true for selected match', function () {
let testObject = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let testObject = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -116,7 +116,7 @@ suite('SearchResult', () => {
});
test('File Match: isSelected return false for un-selected match', function () {
let testObject = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let testObject = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -132,7 +132,7 @@ suite('SearchResult', () => {
});
test('File Match: unselect', function () {
let testObject = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let testObject = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -149,7 +149,7 @@ suite('SearchResult', () => {
});
test('File Match: unselect when not selected', function () {
let testObject = aFileMatch('folder\\file.txt', aSearchResult(), ...[{
let testObject = aFileMatch('folder/file.txt', aSearchResult(), ...[{
preview: 'foo',
lineNumber: 1,
offsetAndLengths: [[0, 3]]
......@@ -166,7 +166,7 @@ suite('SearchResult', () => {
test('Alle Drei Zusammen', function () {
let searchResult = instantiationService.createInstance(SearchResult, null);
let fileMatch = aFileMatch('far\\boo', searchResult);
let fileMatch = aFileMatch('far/boo', searchResult);
let lineMatch = new Match(fileMatch, 'foo bar', 1, 0, 3);
assert(lineMatch.parent() === fileMatch);
......@@ -360,7 +360,7 @@ suite('SearchResult', () => {
function aFileMatch(path: string, searchResult?: SearchResult, ...lineMatches: ILineMatch[]): FileMatch {
let rawMatch: IFileMatch = {
resource: URI.file('C:\\' + path),
resource: URI.file('/' + path),
lineMatches: lineMatches
};
return instantiationService.createInstance(FileMatch, null, searchResult, rawMatch);
......@@ -383,4 +383,4 @@ suite('SearchResult', () => {
instantiationService.stub(IConfigurationService, new TestConfigurationService());
return instantiationService.createInstance(ModelServiceImpl);
}
});
\ No newline at end of file
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册