terminalLinkHandler.test.ts 4.2 KB
Newer Older
D
Daniel Imms 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import * as assert from 'assert';
import { Platform } from 'vs/base/common/platform';
import { TerminalLinkHandler } from 'vs/workbench/parts/terminal/electron-browser/terminalLinkHandler';
M
Mark Pearce 已提交
11 12 13 14
import { IWorkspace, WorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import URI from 'vs/base/common/uri';
import * as path from 'path';
import * as sinon from 'sinon';
D
Daniel Imms 已提交
15

D
Daniel Imms 已提交
16 17 18 19
class TestTerminalLinkHandler extends TerminalLinkHandler {
	public get localLinkRegex(): RegExp {
		return this._localLinkRegex;
	}
M
Mark Pearce 已提交
20 21 22
	public preprocessPath(link: string): string {
		return this._preprocessPath(link);
	}
D
Daniel Imms 已提交
23 24
}

D
Daniel Imms 已提交
25
class TestXterm {
D
Daniel Imms 已提交
26
	public setHypertextLinkHandler() { }
D
Daniel Imms 已提交
27
	public setHypertextValidationCallback() { }
D
Daniel Imms 已提交
28 29
}

M
Mark Pearce 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class TestURI extends URI {
	constructor(private _fakePath: string) {
		super();
	};

	get fsPath(): string {
		return this._fakePath;
	}
}

class TestWorkspace implements IWorkspace {
	resource: URI;
	constructor(basePath: string) {
		this.resource = new TestURI(basePath);
	}
}

D
Daniel Imms 已提交
47 48 49
suite('Workbench - TerminalLinkHandler', () => {
	suite('localLinkRegex', () => {
		test('Windows', () => {
50
			const regex = new TestTerminalLinkHandler(new TestXterm(), Platform.Windows, null, null).localLinkRegex;
D
Daniel Imms 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
			function testLink(link: string) {
				assert.equal(` ${link} `.match(regex)[1], link);
				assert.equal(`:${link}:`.match(regex)[1], link);
				assert.equal(`;${link};`.match(regex)[1], link);
				assert.equal(`(${link})`.match(regex)[1], link);
			}
			testLink('c:\\foo');
			testLink('c:/foo');
			testLink('.\\foo');
			testLink('./foo');
			testLink('..\\foo');
			testLink('../foo');
			testLink('~\\foo');
			testLink('~/foo');
			testLink('c:/a/long/path');
			testLink('c:\\a\\long\\path');
			testLink('c:\\mixed/slash\\path');
M
Mark Pearce 已提交
68
			testLink('a/relative/path');
D
Daniel Imms 已提交
69 70 71
		});

		test('Linux', () => {
72
			const regex = new TestTerminalLinkHandler(new TestXterm(), Platform.Linux, null, null).localLinkRegex;
D
Daniel Imms 已提交
73 74 75 76 77 78 79 80 81 82 83
			function testLink(link: string) {
				assert.equal(` ${link} `.match(regex)[1], link);
				assert.equal(`:${link}:`.match(regex)[1], link);
				assert.equal(`;${link};`.match(regex)[1], link);
				assert.equal(`(${link})`.match(regex)[1], link);
			}
			testLink('/foo');
			testLink('~/foo');
			testLink('./foo');
			testLink('../foo');
			testLink('/a/long/path');
M
Mark Pearce 已提交
84 85 86 87 88 89
			testLink('a/relative/path');
		});
	});

	suite('preprocessPath', () => {
		test('Windows', () => {
90
			const linkHandler = new TestTerminalLinkHandler(new TestXterm(), Platform.Windows, null,
M
Mark Pearce 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103
				new WorkspaceContextService(new TestWorkspace('C:\\base')));

			let stub = sinon.stub(path, 'join', function (arg1, arg2) {
				return arg1 + '\\' + arg2;
			});
			assert.equal(linkHandler.preprocessPath('./src/file1'), 'C:\\base\\./src/file1');
			assert.equal(linkHandler.preprocessPath('src\\file2'), 'C:\\base\\src\\file2');
			assert.equal(linkHandler.preprocessPath('C:\\absolute\\path\\file3'), 'C:\\absolute\\path\\file3');

			stub.restore();
		});

		test('Linux', () => {
104
			const linkHandler = new TestTerminalLinkHandler(new TestXterm(), Platform.Linux, null,
M
Mark Pearce 已提交
105 106 107 108 109 110 111 112 113 114
				new WorkspaceContextService(new TestWorkspace('/base')));

			let stub = sinon.stub(path, 'join', function (arg1, arg2) {
				return arg1 + '/' + arg2;
			});

			assert.equal(linkHandler.preprocessPath('./src/file1'), '/base/./src/file1');
			assert.equal(linkHandler.preprocessPath('src/file2'), '/base/src/file2');
			assert.equal(linkHandler.preprocessPath('/absolute/path/file3'), '/absolute/path/file3');
			stub.restore();
D
Daniel Imms 已提交
115
		});
116 117

		test('No Workspace', () => {
118
			const linkHandler = new TestTerminalLinkHandler(new TestXterm(), Platform.Linux, null, new WorkspaceContextService(null));
119 120 121 122 123

			assert.equal(linkHandler.preprocessPath('./src/file1'), null);
			assert.equal(linkHandler.preprocessPath('src/file2'), null);
			assert.equal(linkHandler.preprocessPath('/absolute/path/file3'), '/absolute/path/file3');
		});
D
Daniel Imms 已提交
124 125
	});
});