Skip to content

Commit

Permalink
fix: test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaaaash committed Jan 8, 2025
1 parent 5feff59 commit 091a344
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const sleepTime = 1000;
watcherServer.setClient(watcherClient);

/* Unwatch root */
await watcherServer.unwatchFileChanges(watcherId);
await watcherServer.unwatchFileChanges(root.toString());

fse.mkdirSync(FileUri.fsPath(root.resolve('foo')), { recursive: true });
expect(fse.statSync(FileUri.fsPath(root.resolve('foo'))).isDirectory()).toBe(true);
Expand Down Expand Up @@ -146,13 +146,13 @@ const sleepTime = 1000;
await fse.ensureFile(fileA);
await sleep(sleepTime);
expect(watcherClient.onDidFilesChanged).toHaveBeenCalledTimes(1);
await watcherServer.unwatchFileChanges(id);
await watcherServer.unwatchFileChanges(newFolder.toString());

id = await watcherServer.watchFileChanges(newFolder, { excludes: ['**/b/**'] });
await fse.ensureFile(fileB);
await sleep(sleepTime);
expect(watcherClient.onDidFilesChanged).toHaveBeenCalledTimes(1);
await watcherServer.unwatchFileChanges(id);
await watcherServer.unwatchFileChanges(newFolder.toString());
watcherServerList.push(watcherServer);
});
});
Expand Down
5 changes: 2 additions & 3 deletions packages/file-service/__tests__/node/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { expectThrowsAsync } from '@opensumi/ide-core-node/__tests__/helper';
import { MockInjector } from '@opensumi/ide-dev-tool/src/mock-injector';
import { createNodeInjector } from '@opensumi/ide-dev-tool/src/mock-injector';

import { FileSystemWatcherServer } from '../../lib/node/hosted/recursive/file-service-watcher';
import { WatcherProcessManagerToken } from '../../lib/node/watcher-process-manager';
import { RecursiveFileSystemWatcher } from '../../lib/node/hosted/recursive/file-service-watcher';
import { FileChangeType, IDiskFileProvider, IFileService } from '../../src/common';
import { FileService, FileServiceModule } from '../../src/node';

Expand All @@ -28,7 +27,7 @@ describe('FileService', () => {

injector = createNodeInjector([FileServiceModule]);
// @ts-ignore
injector.mock(FileSystemWatcherServer, 'isEnableNSFW', () => false);
injector.mock(RecursiveFileSystemWatcher, 'isEnableNSFW', () => false);
fileService = injector.get(IFileService);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ export interface WatcherOptions {
excludes: string[];
}

const watcherPlaceHolder = {
disposable: {
dispose: () => {},
},
handlers: [],
};

/**
* @deprecated
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {
this.logger.log('[Un-Recursive] start watching', basePath);
const isDirectory = fs.lstatSync(basePath).isDirectory();

const docChildren = new Set<string>();
let signalDoc = '';
if (!isDirectory) {
if (isDirectory) {
try {
for (const child of fs.readdirSync(basePath)) {
const base = join(basePath, String(child));
if (!fs.lstatSync(base).isDirectory()) {
docChildren.add(child);
}
}
} catch (error) {
this.logger.error(error);
}
} else {
signalDoc = basename(basePath);
}

Expand Down Expand Up @@ -70,16 +82,20 @@ export class UnRecursiveFileSystemWatcher implements IWatcher {
if (isDirectory) {
setTimeout(async () => {
// 监听的目录如果是文件夹,那么只对其下面的文件改动做出响应
if ((type === 'rename' || type === 'change') && changeFileName === filename) {
const fileExists = fs.existsSync(changePath);
if (fileExists) {
this.pushUpdated(changePath);
} else {
this.pushDeleted(changePath);
if (docChildren.has(changeFileName)) {
if ((type === 'rename' || type === 'change') && changeFileName === filename) {
const fileExists = fs.existsSync(changePath);
if (fileExists) {
this.pushUpdated(changePath);
} else {
docChildren.delete(changeFileName);
this.pushDeleted(changePath);
}
}
} else if (fs.pathExistsSync(changePath)) {
if (!fs.lstatSync(changePath).isDirectory()) {
this.pushAdded(changePath);
docChildren.add(changeFileName);
}
}
}, UnRecursiveFileSystemWatcher.FILE_DELETE_HANDLER_DELAY);
Expand Down

0 comments on commit 091a344

Please sign in to comment.