Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Binary file modified .gitignore
Binary file not shown.
32 changes: 0 additions & 32 deletions plan.md

This file was deleted.

33 changes: 33 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as process from "process";

function main() {
const args = process.argv.slice(2);

for (const arg of args) {
if (arg.trim() === "") {
console.error("Error: Argument cannot be empty or whitespace-only.");
process.exit(1);
}
}

if (args.length > 0) {
const firstArg = args[0];
if (firstArg === "--version" || firstArg === "-v") {
console.log("fleet-e2e-toy v1.0.0");
process.exit(0);
} else if (firstArg === "help" || firstArg === "--help" || firstArg === "-h") {
console.log("Usage: fleet-e2e-toy [command] [options]\n\n" +
"Commands:\n" +
" help Show this help message\n\n" +
"Options:\n" +
" --version, -v Show version information\n" +
" --help, -h Show this help message");
process.exit(0);
} else {
console.error(`Unknown command or flag: ${firstArg}`);
process.exit(1);
}
}
}

main();
106 changes: 106 additions & 0 deletions tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { exec } from "child_process";
import * as path from "path";

jest.setTimeout(20000);

const cliScript = path.resolve(__dirname, "../src/cli.ts");

function runCli(args: string[]): Promise<{ code: number; stdout: string; stderr: string }> {
return new Promise((resolve) => {
// For executing in terminal, quote each argument.
// Replace double quotes inside the arg with escaped double quotes.
const escapedArgs = args.map(arg => {
const escaped = arg.replace(/"/g, '\\"');
return `"${escaped}"`;
}).join(" ");

const cmd = `npx ts-node "${cliScript}" ${escapedArgs}`;

exec(cmd, (error, stdout, stderr) => {
resolve({
code: error ? (error.code ?? 1) : 0,
stdout: stdout.trim(),
stderr: stderr.trim(),
});
});
});
}

describe("CLI Tool", () => {
describe("Version Flags", () => {
it("should print version and exit 0 for --version", async () => {
const result = await runCli(["--version"]);
expect(result.code).toBe(0);
expect(result.stdout).toBe("fleet-e2e-toy v1.0.0");
expect(result.stderr).toBe("");
});

it("should print version and exit 0 for -v", async () => {
const result = await runCli(["-v"]);
expect(result.code).toBe(0);
expect(result.stdout).toBe("fleet-e2e-toy v1.0.0");
expect(result.stderr).toBe("");
});
});

describe("Help Commands", () => {
const expectedHelpText =
"Usage: fleet-e2e-toy [command] [options]\n\n" +
"Commands:\n" +
" help Show this help message\n\n" +
"Options:\n" +
" --version, -v Show version information\n" +
" --help, -h Show this help message";

it("should print help and exit 0 for help command", async () => {
const result = await runCli(["help"]);
expect(result.code).toBe(0);
expect(result.stdout).toBe(expectedHelpText);
expect(result.stderr).toBe("");
});

it("should print help and exit 0 for --help flag", async () => {
const result = await runCli(["--help"]);
expect(result.code).toBe(0);
expect(result.stdout).toBe(expectedHelpText);
expect(result.stderr).toBe("");
});

it("should print help and exit 0 for -h flag", async () => {
const result = await runCli(["-h"]);
expect(result.code).toBe(0);
expect(result.stdout).toBe(expectedHelpText);
expect(result.stderr).toBe("");
});
});

describe("Input Validation", () => {
it("should fail and exit 1 for empty string argument", async () => {
const result = await runCli([""]);
expect(result.code).toBe(1);
expect(result.stderr).toContain("Error: Argument cannot be empty or whitespace-only.");
expect(result.stdout).toBe("");
});

it("should fail and exit 1 for whitespace-only argument", async () => {
const result = await runCli([" "]);
expect(result.code).toBe(1);
expect(result.stderr).toContain("Error: Argument cannot be empty or whitespace-only.");
expect(result.stdout).toBe("");
});

it("should fail and exit 1 for unknown command", async () => {
const result = await runCli(["unknown-command"]);
expect(result.code).toBe(1);
expect(result.stderr).toContain("Unknown command or flag: unknown-command");
expect(result.stdout).toBe("");
});

it("should fail and exit 1 for unknown flag", async () => {
const result = await runCli(["--unknown-flag"]);
expect(result.code).toBe(1);
expect(result.stderr).toContain("Unknown command or flag: --unknown-flag");
expect(result.stdout).toBe("");
});
});
});
4 changes: 4 additions & 0 deletions tool
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
# Determine the directory of this script to handle execution from other folders
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
npx ts-node "$DIR/src/cli.ts" "$@"
2 changes: 2 additions & 0 deletions tool.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
npx ts-node "%~dp0src\cli.ts" %*
Loading