Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(cli): Spinner tests #4713

Merged
merged 8 commits into from
May 13, 2024
Merged
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
1 change: 1 addition & 0 deletions _tools/check_licence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { globToRegExp } from "../path/glob_to_regexp.ts";

const EXTENSIONS = [".mjs", ".js", ".ts", ".rs"];
const EXCLUDED_DIRS = [
"**/cli/testdata",
"**/dotenv/testdata",
"**/fs/testdata",
"**/http/testdata",
Expand Down
187 changes: 187 additions & 0 deletions cli/spinner_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals, assertGreater, assertLess } from "@std/assert";
import { Spinner } from "./spinner.ts";

async function spawnDeno(args: string[], opts?: Deno.CommandOptions) {
const cmd = new Deno.Command(Deno.execPath(), {
args: ["run", ...args],
stdout: "piped",
stderr: "piped",
...opts,
});
const output = await cmd.output();
return decoder.decode(output.stdout);
}

const normalizeString = (s: string) =>
// deno-lint-ignore no-control-regex
s.replace(/\r\n|\r|\n|\u001b\[[0-9;]*[a-zA-Z]/g, "").trim();

const COLOR_RESET = "\u001b[0m";
const LINE_CLEAR = "\r\u001b[K";
const decoder = new TextDecoder();

Deno.test("Spinner constructor accepts spinner", async () => {
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_spinner.ts",
]);
const actual = normalizeString(text);

assert(actual.startsWith("0 1 2 3 4 5 6 7 8 9"));
});

Deno.test("Spinner constructor accepts message", async () => {
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_message.ts",
]);
const actual = normalizeString(text);

assert(actual.startsWith("⠋ Spinning with Deno 🦕"));
});

Deno.test("Spinner constructor accepts interval", async () => {
const text1 = await spawnDeno([
"cli/testdata/spinner_cases/custom_interval_750.ts",
]);
const actual1 = normalizeString(text1);

// means it only ran once
assertEquals(actual1, "⠋");

const text2 = await spawnDeno([
"cli/testdata/spinner_cases/custom_interval_10.ts",
]);
const actual2 = normalizeString(text2);

// give setInterval a good buffer to avoid needlessly failing
assertGreater(actual2.length, 100);
assertLess(actual2.length, 300);
});

Deno.test("Spinner constructor accepts each color", async () => {
{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_black.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[30m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_red.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[31m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_green.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[32m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_yellow.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[33m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_blue.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[34m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_magenta.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[35m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_cyan.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[36m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_white.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[37m⠋${COLOR_RESET} `);
}

{
const text = await spawnDeno([
"cli/testdata/spinner_cases/custom_color_gray.ts",
]);
assertEquals(text, `${LINE_CLEAR}\u001b[90m⠋${COLOR_RESET} `);
}
});

Deno.test("Spinner.color can set each color", async () => {
const text = await spawnDeno(["cli/testdata/spinner_cases/set_color.ts"]);

const expectedStr = `${LINE_CLEAR}\u001b[30m⠋${COLOR_RESET} ` + // Black
`${LINE_CLEAR}\u001b[31m⠙${COLOR_RESET} ` + // Red
`${LINE_CLEAR}\u001b[32m⠹${COLOR_RESET} `; // Green

assertEquals(text, expectedStr);
});

Deno.test("Spinner.color can get each color", () => {
const spinner = new Spinner();

spinner.color = "black";
assertEquals(spinner.color, "\u001b[30m");

spinner.color = "red";
assertEquals(spinner.color, "\u001b[31m");

spinner.color = "green";
assertEquals(spinner.color, "\u001b[32m");

spinner.color = "yellow";
assertEquals(spinner.color, "\u001b[33m");

spinner.color = "blue";
assertEquals(spinner.color, "\u001b[34m");

spinner.color = "magenta";
assertEquals(spinner.color, "\u001b[35m");

spinner.color = "cyan";
assertEquals(spinner.color, "\u001b[36m");

spinner.color = "white";
assertEquals(spinner.color, "\u001b[37m");

spinner.color = "gray";
assertEquals(spinner.color, "\u001b[90m");
});

Deno.test("Spinner.start() begins the sequence", async () => {
const text = await spawnDeno(["cli/testdata/spinner_cases/start.ts"]);
assertEquals(text, `${LINE_CLEAR}⠋${COLOR_RESET} `);
});

Deno.test("Spinner.stop() terminates the sequence", async () => {
const text = await spawnDeno(["cli/testdata/spinner_cases/stop.ts"]);
assertEquals(
text,
`${LINE_CLEAR}⠋${COLOR_RESET} ${LINE_CLEAR}⠙${COLOR_RESET} `,
);
});

Deno.test("Spinner.message can be updated", async () => {
const text = await spawnDeno([
"cli/testdata/spinner_cases/change_message.ts",
]);
const actual = normalizeString(text);
const expected = "⠋ ⠙ One dino 🦕⠹ Two dinos 🦕🦕⠸ Three dinos 🦕🦕🦕";
assertEquals(actual, expected);
});
11 changes: 11 additions & 0 deletions cli/testdata/spinner_cases/change_message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner();

spinner.start();

setTimeout(() => (spinner.message = "One dino 🦕"), 125); // 150
setTimeout(() => (spinner.message = "Two dinos 🦕🦕"), 200); // 225
setTimeout(() => (spinner.message = "Three dinos 🦕🦕🦕"), 275); // 300

setTimeout(spinner.stop, 350);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_black.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "black" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_blue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "blue" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_cyan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "cyan" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_gray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "gray" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_green.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "green" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_magenta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "magenta" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_red.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "red" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_white.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "white" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_color_yellow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ color: "yellow" });

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_interval_10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ interval: 10 });

spinner.start();

setTimeout(spinner.stop, 1000);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_interval_750.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ interval: 750 });

spinner.start();

setTimeout(spinner.stop, 1000);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/custom_message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({ message: "Spinning with Deno 🦕" });

spinner.start();

setTimeout(spinner.stop, 100);
9 changes: 9 additions & 0 deletions cli/testdata/spinner_cases/custom_spinner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner({
spinner: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
});

spinner.start();

setTimeout(spinner.stop, 1000);
11 changes: 11 additions & 0 deletions cli/testdata/spinner_cases/set_color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner();

spinner.start();

spinner.color = "black";
setTimeout(() => spinner.color = "red", 125); // 150
setTimeout(() => spinner.color = "green", 200); // 225

setTimeout(spinner.stop, 250);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner();

spinner.start();

setTimeout(spinner.stop, 100);
7 changes: 7 additions & 0 deletions cli/testdata/spinner_cases/stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Spinner } from "../../spinner.ts";

const spinner = new Spinner();

spinner.start();

setTimeout(spinner.stop, 200);