From 771a97ef391472afbb7182b0666e893fe2f6dffc Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Sat, 30 Mar 2024 08:27:53 -0400 Subject: [PATCH] tests: Add option to exclude files from imports Note that these tests were originally made to prevent dead code from being in the repository, although a proper coverage solution would likely be more useful long-term. --- tests/lib.ts | 16 +++++++++++++++- tests/main.ts | 14 +++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/lib.ts b/tests/lib.ts index 9e43415d..ce33da6b 100644 --- a/tests/lib.ts +++ b/tests/lib.ts @@ -31,11 +31,25 @@ async function getImportsInFile(file: string): Promise { return imports; } -export async function assertAllModulesInDirectory(directory: string) { +/** Assets that all files in a given directory are imported in default.nix. + * + * @param directory The directory to assert. + * @param excludes Files to exclude from assertion. Useful for files imported elsewhere. + */ +export async function assertAllModulesInDirectory( + directory: string, + excludes?: string[], +) { const files = await getFilesInDirectory(`./${directory}`); const imports = await getImportsInFile(`./${directory}/default.nix`); for (const file of files) { + const basename = file.split(`${directory}/`)[1]; + + if (excludes && excludes.includes(basename)) { + continue; + } + assert(imports.includes(file)); } } diff --git a/tests/main.ts b/tests/main.ts index 817a3db4..0bd71f13 100644 --- a/tests/main.ts +++ b/tests/main.ts @@ -9,8 +9,20 @@ const dirs = [ "specializations", ]; +/** A helper function to return excluded files. + * + * @param directory The directory to get excludes for. + * @returns An array of excluded files for the given directory or undefined. + */ +function getExcludes(directory: string): string[] | undefined { + switch (directory) { + case "packages": + return ["hycov.nix"]; + } +} + for (const dir of dirs) { Deno.test(`imports all modules in ./${dir}`, async () => { - await assertAllModulesInDirectory(dir); + await assertAllModulesInDirectory(dir, getExcludes(dir)); }); }