Skip to content

Commit 9df45b1

Browse files
committed
test runner progress
Signed-off-by: shmck <[email protected]>
1 parent 37b3d50 commit 9df45b1

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/utils/exec.ts

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import * as T from "../../typings/tutorial";
12
import { exec as cpExec } from "child_process";
3+
import * as path from "path";
24
import { promisify } from "util";
35

46
const asyncExec = promisify(cpExec);
@@ -29,15 +31,46 @@ export function createCherryPick(cwd: string) {
2931
}
3032

3133
export function createCommandRunner(cwd: string) {
32-
return async function runCommands(commands: string[]) {
34+
return async function runCommands(commands: string[], dir?: string) {
3335
for (const command of commands) {
3436
try {
3537
console.log(`> ${command}`);
36-
await createExec(cwd)(command);
38+
let cwdDir = cwd;
39+
if (dir) {
40+
cwdDir = path.join(cwd, dir);
41+
}
42+
await createExec(cwdDir)(command);
3743
} catch (e) {
3844
console.log(`Setup command failed: "${command}"`);
3945
console.log(e.message);
4046
}
4147
}
4248
};
4349
}
50+
51+
function isAbsolute(p: string) {
52+
return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
53+
}
54+
55+
export function createTestRunner(cwd: string, config: T.TestRunnerConfig) {
56+
const { command, args, directory } = config;
57+
58+
const commandIsAbsolute = isAbsolute(command);
59+
60+
let runnerPath;
61+
if (commandIsAbsolute) {
62+
// absolute path
63+
runnerPath = command;
64+
} else {
65+
// relative path
66+
runnerPath = path.join(cwd, directory || "", command);
67+
}
68+
69+
const commandWithArgs = `${runnerPath} ${args.tap}`;
70+
71+
return async function runTest() {
72+
const { stdout, stderr } = await createExec(cwd)(commandWithArgs);
73+
console.log(stdout);
74+
console.warn(stderr);
75+
};
76+
}

src/validate.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import * as fs from "fs-extra";
33
import * as yamlParser from "js-yaml";
44
import { getArg } from "./utils/args";
55
import gitP, { SimpleGit } from "simple-git/promise";
6-
import { createCommandRunner, createCherryPick } from "./utils/exec";
6+
import {
7+
createCommandRunner,
8+
createCherryPick,
9+
createTestRunner,
10+
} from "./utils/exec";
711
import { getCommits, CommitLogObject } from "./utils/commits";
812

913
async function validate(args: string[]) {
@@ -51,6 +55,7 @@ async function validate(args: string[]) {
5155
// no js cherry pick implementation
5256
const cherryPick = createCherryPick(tmpDir);
5357
const runCommands = createCommandRunner(tmpDir);
58+
const runTest = createTestRunner(tmpDir, skeleton.config.testRunner);
5459

5560
// VALIDATE TUTORIAL TESTS
5661

@@ -63,11 +68,15 @@ async function validate(args: string[]) {
6368
// run commands
6469
if (skeleton.config?.testRunner?.setup?.commands) {
6570
console.info("Running setup commands...");
66-
await runCommands(skeleton.config?.testRunner?.setup?.commands);
71+
72+
await runCommands(
73+
skeleton.config?.testRunner?.setup?.commands,
74+
// add optional setup directory
75+
skeleton.config?.testRunner?.directory
76+
);
6777
}
6878
}
6979

70-
console.log(skeleton.levels);
7180
for (const level of skeleton.levels) {
7281
if (level.setup) {
7382
// load commits
@@ -84,6 +93,7 @@ async function validate(args: string[]) {
8493
// steps
8594
if (level.steps) {
8695
for (const step of level.steps) {
96+
console.log(step);
8797
// load commits
8898
if (step.setup.commits) {
8999
console.log(`Loading ${step.id} commits...`);
@@ -94,6 +104,10 @@ async function validate(args: string[]) {
94104
console.log(`Running ${step.id} commands...`);
95105
await runCommands(step.setup.commands);
96106
}
107+
108+
// run test
109+
console.info("Running test");
110+
await runTest();
97111
}
98112
}
99113
}

typings/tutorial.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export interface TestRunnerArgs {
6262

6363
export interface TestRunnerConfig {
6464
command: string;
65-
args?: TestRunnerArgs;
65+
args: TestRunnerArgs;
6666
directory?: string;
6767
setup?: StepActions;
6868
}

0 commit comments

Comments
 (0)