Skip to content

Commit

Permalink
add missing changes from #3
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Jul 17, 2023
1 parent 4f2a91a commit 5e0571b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@types/node": "^20.4.2",
"ava": "^5.3.1",
"c8": "^8.0.0",
"ci-info": "^3.8.0",
"get-bin-path": "^10.0.0",
"is-executable": "^2.0.1",
"replace-in-files-cli": "^2.2.0",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Command-line task lists made pretty.

Gracefully handles and displays failures, including if a given command is not found.

If used in a CI environment, command output is outputted as is.

## Install

```sh
Expand Down
8 changes: 7 additions & 1 deletion src/tasks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Listr, type ListrTask } from "listr2";
import type { ExecaReturnValue, $ } from "execa";
import { $, type ExecaReturnValue } from "execa";
import { isCI } from "ci-info";
import { parseCommand, trimIfNeeded } from "./helpers.js";

/**
Expand All @@ -26,6 +27,10 @@ export const getTasks = ({ commands, exitOnError, showTimer }: TaskContext) => {
const tasks = commands.map(command => ({
title: parseCommand(command),
task: async ({ $$ }, task) => {
if(isCI) {
return $({ shell: true, stdio: "inherit" })`${command}`;
}

const [commandName, args] = parseCommand(command, { getArgs: true });

task.title = `Running "${command}"...`;
Expand Down Expand Up @@ -62,5 +67,6 @@ export const getTasks = ({ commands, exitOnError, showTimer }: TaskContext) => {
formatOutput: "wrap",
removeEmptyLines: false,
},
rendererSilent: isCI,
});
};
29 changes: 22 additions & 7 deletions test/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from "node:process";
import anyTest, { type TestFn } from "ava";
import { execa } from "execa";
import { getBinPath } from "get-bin-path";
Expand All @@ -16,16 +17,25 @@ test.before("setup context", async t => {
t.true(await isExecutable(t.context.binPath), "Source binary not executable!");
});

// eslint-disable-next-line no-return-assign
test.before("disable CI check", () => process.env.CI = "false");

const trim = (stdout: string) => stdout.trim().split("\n").map(line => stripAnsi(line).trim());

const verifyCli = (shouldPass: boolean) => test.macro(async (t, commands: string | string[], expectedLines: string[]) => {
const args = commands ? [commands].flat() : undefined;
const { exitCode, stdout } = await execa(t.context.binPath, args, { reject: false });
const receivedLines = trim(stdout);
const verifyCli = (shouldPass: boolean, setup = async () => "", teardown = async () => "") => (
test.macro(async (t, commands: string | string[], expectedLines: string[]) => {
await setup();

t.is(exitCode, shouldPass ? 0 : 1, "CLI exited with the wrong exit code!");
t.deepEqual(receivedLines, expectedLines, "CLI output different than expectations!");
});
const args = commands ? [commands].flat() : undefined;
const { exitCode, stdout } = await execa(t.context.binPath, args, { reject: false });
const receivedLines = trim(stdout);

t.deepEqual(receivedLines, expectedLines, "CLI output different than expectations!");
t.is(exitCode, shouldPass ? 0 : 1, "CLI exited with the wrong exit code!");

await teardown();
})
);

const cliPasses = verifyCli(true);
const cliFails = verifyCli(false);
Expand Down Expand Up @@ -160,6 +170,11 @@ test("outputs stdout and stderr", cliPasses, "node -e 'console.log(true); consol
"[SUCCESS] node",
]);

// eslint-disable-next-line no-return-assign
const cliPassesCi = verifyCli(true, async () => process.env.CI = "true", async () => process.env.CI = "false");

test.serial("uses silent renderer in CI", cliPassesCi, "node -e 'console.log(true)'", ["true"]);

const helpText = [
"Usage",
"$ listr <command> […]",
Expand Down

0 comments on commit 5e0571b

Please sign in to comment.