Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unused-files-patterns check and add tests #1050

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More error logging
benibenj committed Sep 4, 2024

Verified

This commit was signed with the committer’s verified signature.
benibenj Benjamin Christopher Simmonds
commit 65f32a0ace117d29ccc669edaa275e4414762b18
41 changes: 34 additions & 7 deletions src/test/package.test.ts
Original file line number Diff line number Diff line change
@@ -89,33 +89,60 @@ function createManifest(extra: Partial<ManifestPackage> = {}): ManifestPackage {
};
}

const PROCESS_ERROR_MESSAGE = 'PROCESS ERROR';
async function testPrintAndValidatePackagedFiles(files: IFile[], cwd: string, manifest: ManifestPackage, options: IPackageOptions, errorExpected: boolean, warningExpected: boolean): Promise<void> {
const originalLogError = log.error;
const originalLogWarn = log.warn;
const originalProcessExit = process.exit;
const warns: string[] = [];
const errors: string[] = [];
let exited = false;
let errorThrown: string | undefined;
log.error = (message: string) => errors.push(message);
log.warn = (message: string) => warns.push(message);
process.exit = (() => { throw Error('Error'); }) as () => never;
process.exit = (() => { exited = true; throw Error(PROCESS_ERROR_MESSAGE); }) as () => never;

let exitedOrErrorThrown = false;
try {
await printAndValidatePackagedFiles(files, cwd, manifest, options);
} catch (e) {
exitedOrErrorThrown = true;
} catch (e: any) {
if (e instanceof Error && e.message !== PROCESS_ERROR_MESSAGE) {
errorThrown = e.message + '\n' + e.stack;
}
} finally {
process.exit = originalProcessExit;
log.error = originalLogError;
log.warn = originalLogWarn;
}

if (errorExpected !== !!errors.length || exitedOrErrorThrown !== errorExpected) {
throw new Error(errors.length ? errors.join('\n') : 'Expected error');
// Validate that the correct number of errors and warnings were thrown
const messages = [];

if (errorExpected !== !!errors.length) {
if (errors.length) {
messages.push(...errors);
} else {
messages.push('Expected an error');
}
}

if (warningExpected !== !!warns.length) {
throw new Error(warns.length ? warns.join('\n') : 'Expected warning');
if (warns.length) {
messages.push(...warns);
} else {
messages.push('Expected a warning');
}
}

if (!errorExpected && exited) {
messages.push('Process exited');
}

if (!errorExpected && !!errorThrown && !exited) {
messages.push('Error thrown: ' + errorThrown);
}

if (messages.length) {
throw new Error(messages.join('\n'));
}
}