From 824b710eb90375603a0f4772c8ddd308e3dd43c6 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Thu, 31 Aug 2023 11:50:03 -0400 Subject: [PATCH] tests: Abstract logic into separate file --- tests/lib.ts | 38 ++++++++++++++++++++++++++++++++++++++ tests/main.ts | 39 +-------------------------------------- 2 files changed, 39 insertions(+), 38 deletions(-) create mode 100644 tests/lib.ts diff --git a/tests/lib.ts b/tests/lib.ts new file mode 100644 index 00000000..8bf2bae5 --- /dev/null +++ b/tests/lib.ts @@ -0,0 +1,38 @@ +import { assert } from "https://deno.land/std@0.200.0/assert/mod.ts"; +import { walk } from "https://deno.land/std@0.200.0/fs/walk.ts"; + +async function getFilesInDirectory(directory: string): Promise { + const files = []; + + for await (const walkEntry of walk(directory)) { + if (walkEntry.isFile) { + if (walkEntry.path.includes("default.nix")) continue + files.push(walkEntry.path) + } + } + + return files +} + +async function getImportsInFile(file: string): Promise { + const text = await Deno.readTextFile(file); + const lines = text.split("\n") + const imports = []; + + for (let i = 0; i < lines.length; i++) { + if (lines[i].includes("./")) { + imports.push(file.split("./")[1].split("/")[0] + "/" + lines[i].split("./")[1].split(" ")[0]) + } + } + + return imports +} + +export async function assertAllModulesInDirectory(directory: string) { + const files = await getFilesInDirectory(`./${directory}`) + const imports = await getImportsInFile(`./${directory}/default.nix`) + + for (const file of files) { + assert(imports.includes(file)) + } +} diff --git a/tests/main.ts b/tests/main.ts index 5cd06fa5..c1b91bfe 100644 --- a/tests/main.ts +++ b/tests/main.ts @@ -1,41 +1,4 @@ -import { assert } from "https://deno.land/std@0.200.0/assert/mod.ts"; -import { walk } from "https://deno.land/std@0.200.0/fs/walk.ts"; - -async function getFilesInDirectory(directory: string): Promise { - const files = []; - - for await (const walkEntry of walk(directory)) { - if (walkEntry.isFile) { - if (walkEntry.path.includes("default.nix")) continue - files.push(walkEntry.path) - } - } - - return files -} - -async function getImportsInFile(file: string): Promise { - const text = await Deno.readTextFile(file); - const lines = text.split("\n") - const imports = []; - - for (let i = 0; i < lines.length; i++) { - if (lines[i].includes("./")) { - imports.push(file.split("./")[1].split("/")[0] + "/" + lines[i].split("./")[1].split(" ")[0]) - } - } - - return imports -} - -async function assertAllModulesInDirectory(directory: string) { - const files = await getFilesInDirectory(`./${directory}`) - const imports = await getImportsInFile(`./${directory}/default.nix`) - - for (const file of files) { - assert(imports.includes(file)) - } -} +import { assertAllModulesInDirectory } from "./lib.ts"; const dirs = ["containers", "home", "modules", "overlays", "packages", "specializations"]