Skip to content

Commit 16e42f1

Browse files
committed
working solution test runner
Signed-off-by: shmck <[email protected]>
1 parent 514b2ad commit 16e42f1

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/utils/exec.ts

+39-23
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { promisify } from "util";
66
const asyncExec = promisify(cpExec);
77

88
export function createExec(cwd: string) {
9-
return function exec(
9+
return async function exec(
1010
command: string
11-
): Promise<{ stdout: string; stderr: string }> | never {
12-
return asyncExec(command, { cwd });
11+
): Promise<{ stdout: string | null; stderr: string }> {
12+
try {
13+
const result = await asyncExec(command, { cwd });
14+
return result;
15+
} catch (e) {
16+
return { stdout: null, stderr: e.message };
17+
}
1318
};
1419
}
1520

@@ -31,46 +36,57 @@ export function createCherryPick(cwd: string) {
3136
}
3237

3338
export function createCommandRunner(cwd: string) {
34-
return async function runCommands(commands: string[], dir?: string) {
39+
return async function runCommands(
40+
commands: string[],
41+
dir?: string
42+
): Promise<boolean> {
43+
let errors = [];
3544
for (const command of commands) {
3645
try {
3746
console.log(`> ${command}`);
3847
let cwdDir = cwd;
3948
if (dir) {
4049
cwdDir = path.join(cwd, dir);
4150
}
42-
await createExec(cwdDir)(command);
51+
const { stdout, stderr } = await createExec(cwdDir)(command);
52+
53+
console.warn(stderr);
54+
console.log(stdout);
4355
} catch (e) {
44-
console.log(`Setup command failed: "${command}"`);
45-
console.log(e.message);
56+
console.error(`Command failed: "${command}"`);
57+
console.warn(e.message);
58+
errors.push(e.message);
4659
}
4760
}
61+
return !!errors.length;
4862
};
4963
}
5064

51-
function isAbsolute(p: string) {
52-
return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
53-
}
65+
// function isAbsolute(p: string) {
66+
// return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
67+
// }
5468

5569
export function createTestRunner(cwd: string, config: T.TestRunnerConfig) {
5670
const { command, args, directory } = config;
5771

58-
const commandIsAbsolute = isAbsolute(command);
72+
// const commandIsAbsolute = isAbsolute(command);
5973

60-
let runnerPath;
61-
if (commandIsAbsolute) {
62-
// absolute path
63-
runnerPath = command;
64-
} else {
65-
// relative path
66-
runnerPath = path.join(cwd, directory || "", command);
74+
let wd = cwd;
75+
if (directory) {
76+
wd = path.join(cwd, directory);
6777
}
6878

69-
const commandWithArgs = `${runnerPath} ${args.tap}`;
79+
const commandWithArgs = `${command} ${args.tap}`;
7080

71-
return async function runTest() {
72-
const { stdout, stderr } = await createExec(cwd)(commandWithArgs);
73-
console.log(stdout);
74-
console.warn(stderr);
81+
return async function runTest(): Promise<{
82+
stdout: string | null;
83+
stderr: string | null;
84+
}> {
85+
try {
86+
// console.log(await createExec(wd)("ls -a node_modules/.bin"));
87+
return await createExec(wd)(commandWithArgs);
88+
} catch (e) {
89+
return Promise.resolve({ stdout: null, stderr: e.message });
90+
}
7591
};
7692
}

src/validate.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ async function validate(args: string[]) {
5757
const runCommands = createCommandRunner(tmpDir);
5858
const runTest = createTestRunner(tmpDir, skeleton.config.testRunner);
5959

60-
// VALIDATE TUTORIAL TESTS
61-
6260
// setup
6361
if (commits.INIT) {
6462
// load commits
@@ -131,7 +129,7 @@ async function validate(args: string[]) {
131129
// run test
132130
console.info("Running solution test");
133131
// expect pass
134-
// await runTest();
132+
await runTest();
135133
}
136134
}
137135
}

0 commit comments

Comments
 (0)