Skip to content

Feature/validate #34

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

Merged
merged 11 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
working solution test runner
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
  • Loading branch information
ShMcK committed Jun 13, 2020
commit 16e42f125505545830b2806532a3262d063ce1eb
62 changes: 39 additions & 23 deletions src/utils/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import { promisify } from "util";
const asyncExec = promisify(cpExec);

export function createExec(cwd: string) {
return function exec(
return async function exec(
command: string
): Promise<{ stdout: string; stderr: string }> | never {
return asyncExec(command, { cwd });
): Promise<{ stdout: string | null; stderr: string }> {
try {
const result = await asyncExec(command, { cwd });
return result;
} catch (e) {
return { stdout: null, stderr: e.message };
}
};
}

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

export function createCommandRunner(cwd: string) {
return async function runCommands(commands: string[], dir?: string) {
return async function runCommands(
commands: string[],
dir?: string
): Promise<boolean> {
let errors = [];
for (const command of commands) {
try {
console.log(`> ${command}`);
let cwdDir = cwd;
if (dir) {
cwdDir = path.join(cwd, dir);
}
await createExec(cwdDir)(command);
const { stdout, stderr } = await createExec(cwdDir)(command);

console.warn(stderr);
console.log(stdout);
} catch (e) {
console.log(`Setup command failed: "${command}"`);
console.log(e.message);
console.error(`Command failed: "${command}"`);
console.warn(e.message);
errors.push(e.message);
}
}
return !!errors.length;
};
}

function isAbsolute(p: string) {
return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
}
// function isAbsolute(p: string) {
// return path.normalize(p + "/") === path.normalize(path.resolve(p) + "/");
// }

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

const commandIsAbsolute = isAbsolute(command);
// const commandIsAbsolute = isAbsolute(command);

let runnerPath;
if (commandIsAbsolute) {
// absolute path
runnerPath = command;
} else {
// relative path
runnerPath = path.join(cwd, directory || "", command);
let wd = cwd;
if (directory) {
wd = path.join(cwd, directory);
}

const commandWithArgs = `${runnerPath} ${args.tap}`;
const commandWithArgs = `${command} ${args.tap}`;

return async function runTest() {
const { stdout, stderr } = await createExec(cwd)(commandWithArgs);
console.log(stdout);
console.warn(stderr);
return async function runTest(): Promise<{
stdout: string | null;
stderr: string | null;
}> {
try {
// console.log(await createExec(wd)("ls -a node_modules/.bin"));
return await createExec(wd)(commandWithArgs);
} catch (e) {
return Promise.resolve({ stdout: null, stderr: e.message });
}
};
}
4 changes: 1 addition & 3 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ async function validate(args: string[]) {
const runCommands = createCommandRunner(tmpDir);
const runTest = createTestRunner(tmpDir, skeleton.config.testRunner);

// VALIDATE TUTORIAL TESTS

// setup
if (commits.INIT) {
// load commits
Expand Down Expand Up @@ -131,7 +129,7 @@ async function validate(args: string[]) {
// run test
console.info("Running solution test");
// expect pass
// await runTest();
await runTest();
}
}
}
Expand Down