Path: blob/main/src/vs/base/test/common/extpath.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { CharCode } from '../../common/charCode.js';7import * as extpath from '../../common/extpath.js';8import { isWindows } from '../../common/platform.js';9import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';1011suite('Paths', () => {1213test('toForwardSlashes', () => {14assert.strictEqual(extpath.toSlashes('\\\\server\\share\\some\\path'), '//server/share/some/path');15assert.strictEqual(extpath.toSlashes('c:\\test'), 'c:/test');16assert.strictEqual(extpath.toSlashes('foo\\bar'), 'foo/bar');17assert.strictEqual(extpath.toSlashes('/user/far'), '/user/far');18});1920test('getRoot', () => {21assert.strictEqual(extpath.getRoot('/user/far'), '/');22assert.strictEqual(extpath.getRoot('\\\\server\\share\\some\\path'), '//server/share/');23assert.strictEqual(extpath.getRoot('//server/share/some/path'), '//server/share/');24assert.strictEqual(extpath.getRoot('//server/share'), '/');25assert.strictEqual(extpath.getRoot('//server'), '/');26assert.strictEqual(extpath.getRoot('//server//'), '/');27assert.strictEqual(extpath.getRoot('c:/user/far'), 'c:/');28assert.strictEqual(extpath.getRoot('c:user/far'), 'c:');29assert.strictEqual(extpath.getRoot('http://www'), '');30assert.strictEqual(extpath.getRoot('http://www/'), 'http://www/');31assert.strictEqual(extpath.getRoot('file:///foo'), 'file:///');32assert.strictEqual(extpath.getRoot('file://foo'), '');33});3435(!isWindows ? test.skip : test)('isUNC', () => {36assert.ok(!extpath.isUNC('foo'));37assert.ok(!extpath.isUNC('/foo'));38assert.ok(!extpath.isUNC('\\foo'));39assert.ok(!extpath.isUNC('\\\\foo'));40assert.ok(extpath.isUNC('\\\\a\\b'));41assert.ok(!extpath.isUNC('//a/b'));42assert.ok(extpath.isUNC('\\\\server\\share'));43assert.ok(extpath.isUNC('\\\\server\\share\\'));44assert.ok(extpath.isUNC('\\\\server\\share\\path'));45});4647test('isValidBasename', () => {48assert.ok(!extpath.isValidBasename(null));49assert.ok(!extpath.isValidBasename(''));50assert.ok(extpath.isValidBasename('test.txt'));51assert.ok(!extpath.isValidBasename('/test.txt'));5253if (isWindows) {54assert.ok(!extpath.isValidBasename('\\test.txt'));55assert.ok(!extpath.isValidBasename('aux'));56assert.ok(!extpath.isValidBasename('Aux'));57assert.ok(!extpath.isValidBasename('LPT0'));58assert.ok(!extpath.isValidBasename('aux.txt'));59assert.ok(!extpath.isValidBasename('com0.abc'));60assert.ok(extpath.isValidBasename('LPT00'));61assert.ok(extpath.isValidBasename('aux1'));62assert.ok(extpath.isValidBasename('aux1.txt'));63assert.ok(extpath.isValidBasename('aux1.aux.txt'));6465assert.ok(!extpath.isValidBasename('test.txt.'));66assert.ok(!extpath.isValidBasename('test.txt..'));67assert.ok(!extpath.isValidBasename('test.txt '));68assert.ok(!extpath.isValidBasename('test.txt\t'));69assert.ok(!extpath.isValidBasename('tes:t.txt'));70assert.ok(!extpath.isValidBasename('tes"t.txt'));71} else {72assert.ok(extpath.isValidBasename('\\test.txt'));73}74});7576test('sanitizeFilePath', () => {77if (isWindows) {78assert.strictEqual(extpath.sanitizeFilePath('.', 'C:\\the\\cwd'), 'C:\\the\\cwd');79assert.strictEqual(extpath.sanitizeFilePath('', 'C:\\the\\cwd'), 'C:\\the\\cwd');8081assert.strictEqual(extpath.sanitizeFilePath('C:', 'C:\\the\\cwd'), 'C:\\');82assert.strictEqual(extpath.sanitizeFilePath('C:\\', 'C:\\the\\cwd'), 'C:\\');83assert.strictEqual(extpath.sanitizeFilePath('C:\\\\', 'C:\\the\\cwd'), 'C:\\');8485assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my.txt', 'C:\\the\\cwd'), 'C:\\folder\\my.txt');86assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my', 'C:\\the\\cwd'), 'C:\\folder\\my');87assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\..\\my', 'C:\\the\\cwd'), 'C:\\my');88assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my\\', 'C:\\the\\cwd'), 'C:\\folder\\my');89assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my\\\\\\', 'C:\\the\\cwd'), 'C:\\folder\\my');9091assert.strictEqual(extpath.sanitizeFilePath('my.txt', 'C:\\the\\cwd'), 'C:\\the\\cwd\\my.txt');92assert.strictEqual(extpath.sanitizeFilePath('my.txt\\', 'C:\\the\\cwd'), 'C:\\the\\cwd\\my.txt');9394assert.strictEqual(extpath.sanitizeFilePath('\\\\localhost\\folder\\my', 'C:\\the\\cwd'), '\\\\localhost\\folder\\my');95assert.strictEqual(extpath.sanitizeFilePath('\\\\localhost\\folder\\my\\', 'C:\\the\\cwd'), '\\\\localhost\\folder\\my');96} else {97assert.strictEqual(extpath.sanitizeFilePath('.', '/the/cwd'), '/the/cwd');98assert.strictEqual(extpath.sanitizeFilePath('', '/the/cwd'), '/the/cwd');99assert.strictEqual(extpath.sanitizeFilePath('/', '/the/cwd'), '/');100101assert.strictEqual(extpath.sanitizeFilePath('/folder/my.txt', '/the/cwd'), '/folder/my.txt');102assert.strictEqual(extpath.sanitizeFilePath('/folder/my', '/the/cwd'), '/folder/my');103assert.strictEqual(extpath.sanitizeFilePath('/folder/../my', '/the/cwd'), '/my');104assert.strictEqual(extpath.sanitizeFilePath('/folder/my/', '/the/cwd'), '/folder/my');105assert.strictEqual(extpath.sanitizeFilePath('/folder/my///', '/the/cwd'), '/folder/my');106107assert.strictEqual(extpath.sanitizeFilePath('my.txt', '/the/cwd'), '/the/cwd/my.txt');108assert.strictEqual(extpath.sanitizeFilePath('my.txt/', '/the/cwd'), '/the/cwd/my.txt');109}110});111112test('isRootOrDriveLetter', () => {113if (isWindows) {114assert.ok(extpath.isRootOrDriveLetter('c:'));115assert.ok(extpath.isRootOrDriveLetter('D:'));116assert.ok(extpath.isRootOrDriveLetter('D:/'));117assert.ok(extpath.isRootOrDriveLetter('D:\\'));118assert.ok(!extpath.isRootOrDriveLetter('D:\\path'));119assert.ok(!extpath.isRootOrDriveLetter('D:/path'));120} else {121assert.ok(extpath.isRootOrDriveLetter('/'));122assert.ok(!extpath.isRootOrDriveLetter('/path'));123}124});125126test('hasDriveLetter', () => {127if (isWindows) {128assert.ok(extpath.hasDriveLetter('c:'));129assert.ok(extpath.hasDriveLetter('D:'));130assert.ok(extpath.hasDriveLetter('D:/'));131assert.ok(extpath.hasDriveLetter('D:\\'));132assert.ok(extpath.hasDriveLetter('D:\\path'));133assert.ok(extpath.hasDriveLetter('D:/path'));134} else {135assert.ok(!extpath.hasDriveLetter('/'));136assert.ok(!extpath.hasDriveLetter('/path'));137}138});139140test('getDriveLetter', () => {141if (isWindows) {142assert.strictEqual(extpath.getDriveLetter('c:'), 'c');143assert.strictEqual(extpath.getDriveLetter('D:'), 'D');144assert.strictEqual(extpath.getDriveLetter('D:/'), 'D');145assert.strictEqual(extpath.getDriveLetter('D:\\'), 'D');146assert.strictEqual(extpath.getDriveLetter('D:\\path'), 'D');147assert.strictEqual(extpath.getDriveLetter('D:/path'), 'D');148} else {149assert.ok(!extpath.getDriveLetter('/'));150assert.ok(!extpath.getDriveLetter('/path'));151}152});153154test('isWindowsDriveLetter', () => {155assert.ok(!extpath.isWindowsDriveLetter(0));156assert.ok(!extpath.isWindowsDriveLetter(-1));157assert.ok(extpath.isWindowsDriveLetter(CharCode.A));158assert.ok(extpath.isWindowsDriveLetter(CharCode.z));159});160161test('indexOfPath', () => {162assert.strictEqual(extpath.indexOfPath('/foo', '/bar', true), -1);163assert.strictEqual(extpath.indexOfPath('/foo', '/FOO', false), -1);164assert.strictEqual(extpath.indexOfPath('/foo', '/FOO', true), 0);165assert.strictEqual(extpath.indexOfPath('/some/long/path', '/some/long', false), 0);166assert.strictEqual(extpath.indexOfPath('/some/long/path', '/PATH', true), 10);167});168169test('parseLineAndColumnAware', () => {170let res = extpath.parseLineAndColumnAware('/foo/bar');171assert.strictEqual(res.path, '/foo/bar');172assert.strictEqual(res.line, undefined);173assert.strictEqual(res.column, undefined);174175res = extpath.parseLineAndColumnAware('/foo/bar:33');176assert.strictEqual(res.path, '/foo/bar');177assert.strictEqual(res.line, 33);178assert.strictEqual(res.column, 1);179180res = extpath.parseLineAndColumnAware('/foo/bar:33:34');181assert.strictEqual(res.path, '/foo/bar');182assert.strictEqual(res.line, 33);183assert.strictEqual(res.column, 34);184185res = extpath.parseLineAndColumnAware('C:\\foo\\bar');186assert.strictEqual(res.path, 'C:\\foo\\bar');187assert.strictEqual(res.line, undefined);188assert.strictEqual(res.column, undefined);189190res = extpath.parseLineAndColumnAware('C:\\foo\\bar:33');191assert.strictEqual(res.path, 'C:\\foo\\bar');192assert.strictEqual(res.line, 33);193assert.strictEqual(res.column, 1);194195res = extpath.parseLineAndColumnAware('C:\\foo\\bar:33:34');196assert.strictEqual(res.path, 'C:\\foo\\bar');197assert.strictEqual(res.line, 33);198assert.strictEqual(res.column, 34);199200res = extpath.parseLineAndColumnAware('/foo/bar:abb');201assert.strictEqual(res.path, '/foo/bar:abb');202assert.strictEqual(res.line, undefined);203assert.strictEqual(res.column, undefined);204});205206test('randomPath', () => {207let res = extpath.randomPath('/foo/bar');208assert.ok(res);209210res = extpath.randomPath('/foo/bar', 'prefix-');211assert.ok(res.indexOf('prefix-'));212213const r1 = extpath.randomPath('/foo/bar');214const r2 = extpath.randomPath('/foo/bar');215216assert.notStrictEqual(r1, r2);217218const r3 = extpath.randomPath('', '', 3);219assert.strictEqual(r3.length, 3);220221const r4 = extpath.randomPath();222assert.ok(r4);223});224225ensureNoDisposablesAreLeakedInTestSuite();226});227228229