Skip to content

Commit 8673e8c

Browse files
andrewbreyc4spar
authored andcommitted
test(*): add a basic set of test cases
1 parent 1e57841 commit 8673e8c

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

.github/workflows/lint-and-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ jobs:
2222
run: deno lint --unstable
2323

2424
- name: Run tests
25-
run: deno test --unstable --allow-read --allow-env --allow-run
25+
run: deno test --unstable --shuffle --allow-read --allow-env --allow-run --allow-write

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2-
.vscode
2+
.vscode
3+
.tmp/

src/runtime/mod.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { cd } from "./cd.ts";
33
import { exec } from "./exec.ts";
44
import { quote } from "./quote.ts";
55

6+
export { ProcessError } from "./process_error.ts";
7+
export { ProcessOutput } from "./process_output.ts";
8+
69
export type $Global = typeof exec & typeof colors & {
710
shell: string;
811
prefix: string;

test.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
11
/// <reference path="./types.d.ts" />
22

3-
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
3+
import {
4+
assert,
5+
assertEquals,
6+
assertThrowsAsync,
7+
} from "https://deno.land/[email protected]/testing/asserts.ts";
48

5-
import { $ } from "./mod.ts";
9+
import { $, cd, path, ProcessError } from "./mod.ts";
610

711
Deno.test("$ works", async () => {
812
const result = await $`echo hello`;
913

1014
assertEquals(result.stdout, "hello\n");
1115
});
16+
17+
Deno.test("passing environment variables to the child process", async () => {
18+
Deno.env.set("IS_THIS_THING_ON", "yes");
19+
const result = await $`echo $IS_THIS_THING_ON`;
20+
21+
assertEquals(result.stdout, "yes\n");
22+
});
23+
24+
Deno.test("escape and quote arguments", async () => {
25+
const complexArg = 'bar"";baz!$#^$\'&*~*%)({}||\\/';
26+
const result = (await $`echo ${complexArg}`);
27+
28+
assertEquals(result.stdout.trim(), complexArg);
29+
});
30+
31+
Deno.test("create a directory with a space in the name", async () => {
32+
const now = Date.now();
33+
const path = `./.tmp/test_${now}/foo bar`;
34+
35+
try {
36+
await $`mkdir -p ${path}`;
37+
assert((await Deno.stat(path)).isDirectory);
38+
} finally {
39+
await Deno.remove(`./.tmp`, { recursive: true });
40+
}
41+
});
42+
43+
Deno.test("subprocess command failure throws a ProcessError", async () => {
44+
await assertThrowsAsync(async () => await $`somefakebinary`, ProcessError);
45+
});
46+
47+
Deno.test("cwd of the parent process is always the starting point for calls to cd", async () => {
48+
const parentPwd = Deno.cwd();
49+
50+
try {
51+
const testStartingDir = path.dirname(path.fromFileUrl(import.meta.url));
52+
Deno.chdir(testStartingDir);
53+
54+
cd("src");
55+
const pwd1 = await $`pwd`;
56+
assertEquals(pwd1.stdout.trim(), path.join(testStartingDir, "src"));
57+
58+
cd(".github");
59+
const pwd2 = await $`pwd`;
60+
assertEquals(pwd2.stdout.trim(), path.join(testStartingDir, ".github"));
61+
} finally {
62+
Deno.chdir(parentPwd);
63+
}
64+
});

0 commit comments

Comments
 (0)