Skip to content

Commit

Permalink
Add CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
atjn committed Jul 20, 2024
1 parent 405ae70 commit 19e4ddc
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { exec as execCallback } from "node:child_process";
import { join as joinPaths, dirname as pathDirname } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import tap from "tap";

const exec = promisify(execCallback);

/**
* @file
* This test checks that basic CLI functionality works.
*/

/**
* TODO: replace with `import.meta.dirname` when it becomes stable:
* https://nodejs.org/api/esm.html#importmeta
*/
const dirname = pathDirname(fileURLToPath(import.meta.url));

const cwd = joinPaths(dirname, "..");
const sizeOfFixtureFolder = "0.01 MB";

for (const folderArg of ["--folder=", "-f=", ""]) {
const args = `${folderArg}test/fixture`;

tap.test(`get folder size with args: ${args}`, async () => {
const result = await exec(`bin/get-folder-size.js ${args}`, {
cwd,
});

tap.ok(
result.stdout.startsWith(sizeOfFixtureFolder),
"should return the size of the folder",
);
});

for (const ignoreArg of ["--ignore=", "-i="]) {
for (const flipArgs of [false, true]) {
const arg1 = `${folderArg}test/fixture`;
const arg2 = `${ignoreArg}.*txt`;
const args = flipArgs ? `${arg2} ${arg1}` : `${arg1} ${arg2}`;

tap.test(`get folder size with args: ${args}`, async () => {
const result = await exec(`bin/get-folder-size.js ${args}`, {
cwd,
});

tap.ok(
result.stdout.startsWith("0.00 MB"),
"should return zero as size",
);
});
}
}
}

tap.test("get folder size with missing args", async () => {
tap.rejects(async () => {
await exec("bin/get-folder-size.js", { cwd });
}, "should reject since no folder path is provided");
});

0 comments on commit 19e4ddc

Please sign in to comment.