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.
This commit is contained in:
Donovan Glover 2024-03-30 08:27:53 -04:00
parent f1a5aa8520
commit 771a97ef39
No known key found for this signature in database
GPG Key ID: EA7408A77AE1BE65
2 changed files with 28 additions and 2 deletions

View File

@ -31,11 +31,25 @@ async function getImportsInFile(file: string): Promise<string[]> {
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));
}
}

View File

@ -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));
});
}