Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Jun 13, 2024
1 parent d75bac1 commit 15c7d15
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 75 deletions.
103 changes: 49 additions & 54 deletions src/lsp/DocumentManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import { expect } from 'chai';
import util from '../util';
import type { DocumentAction } from './DocumentManager';
import { DocumentManager } from './DocumentManager';

describe('DocumentManager', () => {
let manager: DocumentManager;
let results: DocumentAction[] = [];

beforeEach(() => {
results = [];
manager = new DocumentManager({
delay: 5
delay: 5,
flushHandler: (event) => {
results.push(...event.actions);
}
});
});

it('throttles multiple events', async () => {
const actionsPromise = manager.once('flush');
manager.set({ srcPath: 'alpha', fileContents: 'one' });
await util.sleep(1);
manager.set({ srcPath: 'alpha', fileContents: 'two' });
await util.sleep(1);
manager.set({ srcPath: 'alpha', fileContents: 'three' });

await manager.onIdle();

expect(
await actionsPromise
).to.eql({
actions: [
{
type: 'set',
srcPath: 'alpha',
fileContents: 'three',
allowStandaloneProject: false
}
]
});
results
).to.eql([{
type: 'set',
srcPath: 'alpha',
fileContents: 'three',
allowStandaloneProject: false
}]);
});

it('any file change delays the first one', async () => {
const actionsPromise = manager.once('flush');

manager.set({ srcPath: 'alpha', fileContents: 'one' });
await util.sleep(1);

Expand All @@ -46,53 +49,45 @@ describe('DocumentManager', () => {
manager.set({ srcPath: 'beta', fileContents: 'four' });
await util.sleep(1);

expect(
await actionsPromise
).to.eql({
actions: [
{
type: 'set',
srcPath: 'alpha',
fileContents: 'three',
allowStandaloneProject: false
}, {
type: 'set',
srcPath: 'beta',
fileContents: 'four',
allowStandaloneProject: false
}
]
});
await manager.onIdle();

expect(results).to.eql([{
type: 'set',
srcPath: 'alpha',
fileContents: 'three',
allowStandaloneProject: false
}, {
type: 'set',
srcPath: 'beta',
fileContents: 'four',
allowStandaloneProject: false
}]);
});

it('keeps the last-in change', async () => {
manager.set({ srcPath: 'alpha', fileContents: 'one' });
manager.delete('alpha');
expect(
await manager.once('flush')
).to.eql({
actions: [
{
type: 'delete',
srcPath: 'alpha'
}
]
});

await manager.onIdle();

expect(results).to.eql([{
type: 'delete',
srcPath: 'alpha'
}]);

results = [];

manager.set({ srcPath: 'alpha', fileContents: 'two' });
manager.delete('alpha');
manager.set({ srcPath: 'alpha', fileContents: 'three' });
expect(
await manager.once('flush')
).to.eql({
actions: [
{
type: 'set',
srcPath: 'alpha',
fileContents: 'three',
allowStandaloneProject: false
}
]
});

await manager.onIdle();

expect(results).to.eql([{
type: 'set',
srcPath: 'alpha',
fileContents: 'three',
allowStandaloneProject: false
}]);
});
});
5 changes: 2 additions & 3 deletions src/lsp/DocumentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class DocumentManager {
constructor(
private options: {
delay: number;
flushHandler?: (event: FlushEvent) => Promise<void>;
flushHandler?: (event: FlushEvent) => any;
}) {
}

Expand Down Expand Up @@ -102,9 +102,8 @@ export class DocumentManager {
* and will wait until files are flushed if there are files.
*/
public async onIdle() {
if (!this.isIdle) {
while (!this.isIdle) {
await this.once('flush');
return this.onIdle();
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/lsp/PathFilterer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ export class PathFilterer {
}

export class PathCollection {
constructor(options: {
rootDir: string;
globs: string[];
} | {
matcher: (path: string) => boolean;
}) {
constructor(
public options: {
rootDir: string;
globs: string[];
} | {
matcher: (path: string) => boolean;
}
) {
if ('globs' in options) {
//build matcher patterns from the globs
for (const glob of options.globs ?? []) {
Expand Down
25 changes: 15 additions & 10 deletions src/lsp/ProjectManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,32 +404,35 @@ describe('ProjectManager', () => {
workspaceFolder: rootDir
}]);

const stub = sinon.stub(manager as any, 'handleFileChange').callThrough();

//register an exclusion filter
pathFilterer.registerExcludeList(rootDir, [
'**/*.md'
]);
//make sure the .md file is ignored
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: `${rootDir}/source/file2.brs`, type: FileChangeType.Created }
{ srcPath: s`${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: s`${rootDir}/source/file2.brs`, type: FileChangeType.Created }
]);
let onFlush = manager['documentManager'].once('flush');
await manager.onIdle();
expect(
(await onFlush)?.actions.map(x => x.srcPath)
stub.getCalls().map(x => x.args[0]).map(x => x.srcPath)
).to.eql([
s`${rootDir}/source/file2.brs`
]);
stub.reset();

//remove all filters, make sure the markdown file is included
pathFilterer.clear();
await manager.handleFileChanges([
{ srcPath: `${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: `${rootDir}/source/file2.brs`, type: FileChangeType.Created }
{ srcPath: s`${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: s`${rootDir}/source/file2.brs`, type: FileChangeType.Created }
]);

onFlush = manager['documentManager'].once('flush');
await manager.onIdle();
expect(
(await onFlush)?.actions.map(x => x.srcPath)
stub.getCalls().flatMap(x => x.args[0]).map(x => x.srcPath)
).to.eql([
s`${rootDir}/source/file1.md`,
s`${rootDir}/source/file2.brs`
Expand All @@ -448,6 +451,8 @@ describe('ProjectManager', () => {
workspaceFolder: rootDir
}]);

const stub = sinon.stub(manager['projects'][0], 'applyFileChanges').callThrough();

//register an exclusion filter
pathFilterer.registerExcludeList(rootDir, [
'**/*.md'
Expand All @@ -457,9 +462,9 @@ describe('ProjectManager', () => {
{ srcPath: `${rootDir}/source/file1.md`, type: FileChangeType.Created },
{ srcPath: `${rootDir}/source/file2.brs`, type: FileChangeType.Created }
]);
let onFlush = manager['documentManager'].once('flush');
await manager.onIdle();
expect(
(await onFlush)?.actions.map(x => x.srcPath)
stub.getCalls().flatMap(x => x.args[0]).map(x => x.srcPath)
).to.eql([
s`${rootDir}/source/file1.md`,
s`${rootDir}/source/file2.brs`
Expand Down
2 changes: 0 additions & 2 deletions src/lsp/ProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ export class ProjectManager {
* This is safe to call any time. Changes will be queued and flushed at the correct times
*/
public async _handleFileChanges(changes: FileChange[]) {


//filter any changes that are not allowed by the path filterer
changes = this.pathFilterer.filter(changes, x => x.srcPath);

Expand Down

0 comments on commit 15c7d15

Please sign in to comment.