Skip to content

Commit

Permalink
test: fix a race condition in tests. (#5147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jan 4, 2024
1 parent e7daad0 commit 09ecc9e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/cspell-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"coverage": "vitest run --coverage",
"test-watch": "vitest",
"prepublishOnly": "pnpm run clean-build",
"#test": "vitest run --reporter=hanging-process --reporter=default",
"test": "vitest run",
"test:update-snapshot": "vitest run -u"
},
Expand Down Expand Up @@ -93,6 +94,7 @@
"@cspell/dict-python": "^4.1.11",
"@types/configstore": "^6.0.2",
"cspell-dict-nl-nl": "^1.1.2",
"leaked-handles": "^5.2.0",
"lorem-ipsum": "^2.0.8"
}
}
10 changes: 10 additions & 0 deletions packages/cspell-lib/src/lib/leaked-handles.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module 'leaked-handles' {
export interface Options {
timeout?: number;
fullStack?: boolean;
debugErrors?: boolean;
debugSockets?: boolean;
}

export function set(opts: Options);
}
29 changes: 23 additions & 6 deletions packages/cspell-lib/src/lib/util/resolveFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { parse } from 'comment-json';
import type { VFileSystemProvider } from 'cspell-io';
import { createRedirectProvider, createVirtualFS } from 'cspell-io';
import * as fs from 'fs';
import leakedHandles from 'leaked-handles';
import * as os from 'os';
import * as path from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
Expand Down Expand Up @@ -35,6 +36,8 @@ const rr = {
const oc = expect.objectContaining;
const sm = expect.stringMatching;

leakedHandles.set({ fullStack: true });

describe('Validate resolveFile', () => {
const redirects: [VFileSystemProvider, ...VFileSystemProvider[]] = [
createRedirectProvider('google', new URL('https://google.com/'), notFoundURL),
Expand Down Expand Up @@ -96,12 +99,26 @@ describe('Validate resolveFile', () => {
const urlIssue5034 = new URL('issue-5034/.cspell.json', issuesFolderURL);

test.each`
filename | relativeTo | expected | found
${'./frontend/src/cspell.config.yaml'} | ${urlIssue5034.href} | ${sm(/src[/\\]cspell\.config\.yaml$/)} | ${true}
${'./frontend/src/cspell.config.yaml'} | ${new URL('cspell.json', urlIssue5034).href} | ${sm(/src[/\\]cspell\.config\.yaml$/)} | ${true}
${'./frontend/node_modules/@cspell/dict-fr-fr/cspell-ext.json'} | ${urlIssue5034.href} | ${sm(/cspell-ext\.json$/)} | ${true}
${'@cspell/dict-fr-fr'} | ${new URL('frontend/src/cspell.json', urlIssue5034).href} | ${sm(/cspell-ext\.json$/)} | ${true}
${'@cspell/dict-mnemonics'} | ${new URL('frontend/src/cspell.json', urlIssue5034).href} | ${sm(/cspell-ext\.json$/)} | ${true}
filename | relativeTo | expected | found
${'./frontend/src/cspell.config.yaml'} | ${urlIssue5034.href} | ${sm(/src[/\\]cspell\.config\.yaml$/)} | ${true}
${'./frontend/src/cspell.config.yaml'} | ${new URL('cspell.json', urlIssue5034).href} | ${sm(/src[/\\]cspell\.config\.yaml$/)} | ${true}
${'@cspell/dict-fr-fr'} | ${new URL('frontend/src/cspell.json', urlIssue5034).href} | ${sm(/cspell-ext\.json$/)} | ${true}
${'@cspell/dict-mnemonics'} | ${new URL('frontend/src/cspell.json', urlIssue5034).href} | ${sm(/cspell-ext\.json$/)} | ${true}
`('resolveFile $filename rel $relativeTo', async ({ filename, relativeTo, expected, found }) => {
const r = await resolver.resolveFile(filename, toURL(relativeTo));
expect(r.filename).toEqual(expected);
expect(r.found).toBe(found);
expect(r.warning).toBeUndefined();
});

// Due to a circular reference it is not possible to make a dependency upon the issue.
const frExtFound = fs.existsSync(
new URL('./frontend/node_modules/@cspell/dict-fr-fr/cspell-ext.json', urlIssue5034),
);

test.each`
filename | relativeTo | expected | found
${'./frontend/node_modules/@cspell/dict-fr-fr/cspell-ext.json'} | ${urlIssue5034.href} | ${sm(/cspell-ext\.json$/)} | ${frExtFound}
`('resolveFile $filename rel $relativeTo', async ({ filename, relativeTo, expected, found }) => {
const r = await resolver.resolveFile(filename, toURL(relativeTo));
expect(r.filename).toEqual(expected);
Expand Down
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 09ecc9e

Please sign in to comment.