Path: blob/main/src/vs/platform/files/test/common/files.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 { isEqual, isEqualOrParent } from '../../../../base/common/extpath.js';7import { isLinux, isMacintosh, isWindows } from '../../../../base/common/platform.js';8import { URI } from '../../../../base/common/uri.js';9import { ensureNoDisposablesAreLeakedInTestSuite, toResource } from '../../../../base/test/common/utils.js';10import { FileChangesEvent, FileChangeType, IFileChange, isParent } from '../../common/files.js';1112suite('Files', () => {1314test('FileChangesEvent - basics', function () {15const changes = [16{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED },17{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },18{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED },19{ resource: toResource.call(this, '/bar/deleted.txt'), type: FileChangeType.DELETED },20{ resource: toResource.call(this, '/bar/folder'), type: FileChangeType.DELETED },21{ resource: toResource.call(this, '/BAR/FOLDER'), type: FileChangeType.DELETED }22];2324for (const ignorePathCasing of [false, true]) {25const event = new FileChangesEvent(changes, ignorePathCasing);2627assert(!event.contains(toResource.call(this, '/foo'), FileChangeType.UPDATED));28assert(event.affects(toResource.call(this, '/foo'), FileChangeType.UPDATED));29assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED));30assert(event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED));31assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED));32assert(event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED));33assert(event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED, FileChangeType.ADDED, FileChangeType.DELETED));34assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED, FileChangeType.DELETED));35assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED));36assert(!event.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED));37assert(!event.affects(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED));3839assert(event.contains(toResource.call(this, '/bar/folder'), FileChangeType.DELETED));40assert(event.contains(toResource.call(this, '/BAR/FOLDER'), FileChangeType.DELETED));41assert(event.affects(toResource.call(this, '/BAR'), FileChangeType.DELETED));42if (ignorePathCasing) {43assert(event.contains(toResource.call(this, '/BAR/folder'), FileChangeType.DELETED));44assert(event.affects(toResource.call(this, '/bar'), FileChangeType.DELETED));45} else {46assert(!event.contains(toResource.call(this, '/BAR/folder'), FileChangeType.DELETED));47assert(event.affects(toResource.call(this, '/bar'), FileChangeType.DELETED));48}49assert(event.contains(toResource.call(this, '/bar/folder/somefile'), FileChangeType.DELETED));50assert(event.contains(toResource.call(this, '/bar/folder/somefile/test.txt'), FileChangeType.DELETED));51assert(event.contains(toResource.call(this, '/BAR/FOLDER/somefile/test.txt'), FileChangeType.DELETED));52if (ignorePathCasing) {53assert(event.contains(toResource.call(this, '/BAR/folder/somefile/test.txt'), FileChangeType.DELETED));54} else {55assert(!event.contains(toResource.call(this, '/BAR/folder/somefile/test.txt'), FileChangeType.DELETED));56}57assert(!event.contains(toResource.call(this, '/bar/folder2/somefile'), FileChangeType.DELETED));5859assert.strictEqual(1, event.rawAdded.length);60assert.strictEqual(2, event.rawUpdated.length);61assert.strictEqual(3, event.rawDeleted.length);62assert.strictEqual(true, event.gotAdded());63assert.strictEqual(true, event.gotUpdated());64assert.strictEqual(true, event.gotDeleted());65}66});6768test('FileChangesEvent - supports multiple changes on file tree', function () {69for (const type of [FileChangeType.ADDED, FileChangeType.UPDATED, FileChangeType.DELETED]) {70const changes = [71{ resource: toResource.call(this, '/foo/bar/updated.txt'), type },72{ resource: toResource.call(this, '/foo/bar/otherupdated.txt'), type },73{ resource: toResource.call(this, '/foo/bar'), type },74{ resource: toResource.call(this, '/foo'), type },75{ resource: toResource.call(this, '/bar'), type },76{ resource: toResource.call(this, '/bar/foo'), type },77{ resource: toResource.call(this, '/bar/foo/updated.txt'), type },78{ resource: toResource.call(this, '/bar/foo/otherupdated.txt'), type }79];8081for (const ignorePathCasing of [false, true]) {82const event = new FileChangesEvent(changes, ignorePathCasing);8384for (const change of changes) {85assert(event.contains(change.resource, type));86assert(event.affects(change.resource, type));87}8889assert(event.affects(toResource.call(this, '/foo'), type));90assert(event.affects(toResource.call(this, '/bar'), type));91assert(event.affects(toResource.call(this, '/'), type));92assert(!event.affects(toResource.call(this, '/foobar'), type));9394assert(!event.contains(toResource.call(this, '/some/foo/bar'), type));95assert(!event.affects(toResource.call(this, '/some/foo/bar'), type));96assert(!event.contains(toResource.call(this, '/some/bar'), type));97assert(!event.affects(toResource.call(this, '/some/bar'), type));9899switch (type) {100case FileChangeType.ADDED:101assert.strictEqual(8, event.rawAdded.length);102break;103case FileChangeType.DELETED:104assert.strictEqual(8, event.rawDeleted.length);105break;106}107}108}109});110111test('FileChangesEvent - correlation', function () {112let changes: IFileChange[] = [113{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED },114{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },115{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED },116];117118let event: FileChangesEvent = new FileChangesEvent(changes, true);119assert.strictEqual(event.hasCorrelation(), false);120assert.strictEqual(event.correlates(100), false);121122changes = [123{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED, cId: 100 },124{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED, cId: 100 },125{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED, cId: 100 },126];127128event = new FileChangesEvent(changes, true);129assert.strictEqual(event.hasCorrelation(), true);130assert.strictEqual(event.correlates(100), true);131assert.strictEqual(event.correlates(120), false);132133changes = [134{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED, cId: 100 },135{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },136{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED, cId: 100 },137];138139event = new FileChangesEvent(changes, true);140assert.strictEqual(event.hasCorrelation(), false);141assert.strictEqual(event.correlates(100), false);142assert.strictEqual(event.correlates(120), false);143144changes = [145{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED, cId: 100 },146{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED, cId: 120 },147{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED, cId: 100 },148];149150event = new FileChangesEvent(changes, true);151assert.strictEqual(event.hasCorrelation(), false);152assert.strictEqual(event.correlates(100), false);153assert.strictEqual(event.correlates(120), false);154});155156function testIsEqual(testMethod: (pA: string, pB: string, ignoreCase: boolean) => boolean): void {157158// corner cases159assert(testMethod('', '', true));160assert(!testMethod(null!, '', true));161assert(!testMethod(undefined!, '', true));162163// basics (string)164assert(testMethod('/', '/', true));165assert(testMethod('/some', '/some', true));166assert(testMethod('/some/path', '/some/path', true));167168assert(testMethod('c:\\', 'c:\\', true));169assert(testMethod('c:\\some', 'c:\\some', true));170assert(testMethod('c:\\some\\path', 'c:\\some\\path', true));171172assert(testMethod('/someöäü/path', '/someöäü/path', true));173assert(testMethod('c:\\someöäü\\path', 'c:\\someöäü\\path', true));174175assert(!testMethod('/some/path', '/some/other/path', true));176assert(!testMethod('c:\\some\\path', 'c:\\some\\other\\path', true));177assert(!testMethod('c:\\some\\path', 'd:\\some\\path', true));178179assert(testMethod('/some/path', '/some/PATH', true));180assert(testMethod('/someöäü/path', '/someÖÄÜ/PATH', true));181assert(testMethod('c:\\some\\path', 'c:\\some\\PATH', true));182assert(testMethod('c:\\someöäü\\path', 'c:\\someÖÄÜ\\PATH', true));183assert(testMethod('c:\\some\\path', 'C:\\some\\PATH', true));184}185186test('isEqual (ignoreCase)', function () {187testIsEqual(isEqual);188189// basics (uris)190assert(isEqual(URI.file('/some/path').fsPath, URI.file('/some/path').fsPath, true));191assert(isEqual(URI.file('c:\\some\\path').fsPath, URI.file('c:\\some\\path').fsPath, true));192193assert(isEqual(URI.file('/someöäü/path').fsPath, URI.file('/someöäü/path').fsPath, true));194assert(isEqual(URI.file('c:\\someöäü\\path').fsPath, URI.file('c:\\someöäü\\path').fsPath, true));195196assert(!isEqual(URI.file('/some/path').fsPath, URI.file('/some/other/path').fsPath, true));197assert(!isEqual(URI.file('c:\\some\\path').fsPath, URI.file('c:\\some\\other\\path').fsPath, true));198199assert(isEqual(URI.file('/some/path').fsPath, URI.file('/some/PATH').fsPath, true));200assert(isEqual(URI.file('/someöäü/path').fsPath, URI.file('/someÖÄÜ/PATH').fsPath, true));201assert(isEqual(URI.file('c:\\some\\path').fsPath, URI.file('c:\\some\\PATH').fsPath, true));202assert(isEqual(URI.file('c:\\someöäü\\path').fsPath, URI.file('c:\\someÖÄÜ\\PATH').fsPath, true));203assert(isEqual(URI.file('c:\\some\\path').fsPath, URI.file('C:\\some\\PATH').fsPath, true));204});205206test('isParent (ignorecase)', function () {207if (isWindows) {208assert(isParent('c:\\some\\path', 'c:\\', true));209assert(isParent('c:\\some\\path', 'c:\\some', true));210assert(isParent('c:\\some\\path', 'c:\\some\\', true));211assert(isParent('c:\\someöäü\\path', 'c:\\someöäü', true));212assert(isParent('c:\\someöäü\\path', 'c:\\someöäü\\', true));213assert(isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar', true));214assert(isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\', true));215216assert(isParent('c:\\some\\path', 'C:\\', true));217assert(isParent('c:\\some\\path', 'c:\\SOME', true));218assert(isParent('c:\\some\\path', 'c:\\SOME\\', true));219220assert(!isParent('c:\\some\\path', 'd:\\', true));221assert(!isParent('c:\\some\\path', 'c:\\some\\path', true));222assert(!isParent('c:\\some\\path', 'd:\\some\\path', true));223assert(!isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\barr', true));224assert(!isParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test', true));225}226227if (isMacintosh || isLinux) {228assert(isParent('/some/path', '/', true));229assert(isParent('/some/path', '/some', true));230assert(isParent('/some/path', '/some/', true));231assert(isParent('/someöäü/path', '/someöäü', true));232assert(isParent('/someöäü/path', '/someöäü/', true));233assert(isParent('/foo/bar/test.ts', '/foo/bar', true));234assert(isParent('/foo/bar/test.ts', '/foo/bar/', true));235236assert(isParent('/some/path', '/SOME', true));237assert(isParent('/some/path', '/SOME/', true));238assert(isParent('/someöäü/path', '/SOMEÖÄÜ', true));239assert(isParent('/someöäü/path', '/SOMEÖÄÜ/', true));240241assert(!isParent('/some/path', '/some/path', true));242assert(!isParent('/foo/bar/test.ts', '/foo/barr', true));243assert(!isParent('/foo/bar/test.ts', '/foo/bar/test', true));244}245});246247test('isEqualOrParent (ignorecase)', function () {248249// same assertions apply as with isEqual()250testIsEqual(isEqualOrParent); //251252if (isWindows) {253assert(isEqualOrParent('c:\\some\\path', 'c:\\', true));254assert(isEqualOrParent('c:\\some\\path', 'c:\\some', true));255assert(isEqualOrParent('c:\\some\\path', 'c:\\some\\', true));256assert(isEqualOrParent('c:\\someöäü\\path', 'c:\\someöäü', true));257assert(isEqualOrParent('c:\\someöäü\\path', 'c:\\someöäü\\', true));258assert(isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar', true));259assert(isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\', true));260assert(isEqualOrParent('c:\\some\\path', 'c:\\some\\path', true));261assert(isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test.ts', true));262263assert(isEqualOrParent('c:\\some\\path', 'C:\\', true));264assert(isEqualOrParent('c:\\some\\path', 'c:\\SOME', true));265assert(isEqualOrParent('c:\\some\\path', 'c:\\SOME\\', true));266267assert(!isEqualOrParent('c:\\some\\path', 'd:\\', true));268assert(!isEqualOrParent('c:\\some\\path', 'd:\\some\\path', true));269assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\barr', true));270assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test', true));271assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\bar\\test.', true));272assert(!isEqualOrParent('c:\\foo\\bar\\test.ts', 'c:\\foo\\BAR\\test.', true));273}274275if (isMacintosh || isLinux) {276assert(isEqualOrParent('/some/path', '/', true));277assert(isEqualOrParent('/some/path', '/some', true));278assert(isEqualOrParent('/some/path', '/some/', true));279assert(isEqualOrParent('/someöäü/path', '/someöäü', true));280assert(isEqualOrParent('/someöäü/path', '/someöäü/', true));281assert(isEqualOrParent('/foo/bar/test.ts', '/foo/bar', true));282assert(isEqualOrParent('/foo/bar/test.ts', '/foo/bar/', true));283assert(isEqualOrParent('/some/path', '/some/path', true));284285assert(isEqualOrParent('/some/path', '/SOME', true));286assert(isEqualOrParent('/some/path', '/SOME/', true));287assert(isEqualOrParent('/someöäü/path', '/SOMEÖÄÜ', true));288assert(isEqualOrParent('/someöäü/path', '/SOMEÖÄÜ/', true));289290assert(!isEqualOrParent('/foo/bar/test.ts', '/foo/barr', true));291assert(!isEqualOrParent('/foo/bar/test.ts', '/foo/bar/test', true));292assert(!isEqualOrParent('foo/bar/test.ts', 'foo/bar/test.', true));293assert(!isEqualOrParent('foo/bar/test.ts', 'foo/BAR/test.', true));294}295});296297ensureNoDisposablesAreLeakedInTestSuite();298});299300301