test: 💍 重构执行命令的部分, 让单元测试代码更清晰

This commit is contained in:
牧毅 2020-08-12 10:33:44 +08:00
parent cac29d8818
commit 63d5b6e4b0

View File

@ -12,19 +12,9 @@ import { IProjectSchema } from '../src/types/schema';
const TEST_CASES_DIR = path.join(__dirname, '../test-cases/rax-app'); const TEST_CASES_DIR = path.join(__dirname, '../test-cases/rax-app');
async function exportProject(schemaJson: IProjectSchema, targetPath: string, projectName: string) { fs.readdirSync(TEST_CASES_DIR).forEach(defineTest);
const raxAppBuilder = createRaxAppBuilder();
const result = await raxAppBuilder.generateProject(schemaJson);
const publisher = CodeGenerator.publishers.disk();
await publisher.publish({
project: result,
outputPath: targetPath,
projectSlug: projectName,
createProjectFolder: true,
});
}
const defineTest = (caseDirName: string) => { function defineTest(caseDirName: string) {
test(`rax-app ${caseDirName} should works`, async (t) => { test(`rax-app ${caseDirName} should works`, async (t) => {
try { try {
const caseFullDir = path.join(TEST_CASES_DIR, caseDirName); const caseFullDir = path.join(TEST_CASES_DIR, caseDirName);
@ -37,7 +27,7 @@ const defineTest = (caseDirName: string) => {
const actualFiles = glob.sync('**/*.{js,jsx,json,ts,tsx,less,css,scss,sass}', { cwd: actualDir }); const actualFiles = glob.sync('**/*.{js,jsx,json,ts,tsx,less,css,scss,sass}', { cwd: actualDir });
t.true(actualFiles.length > 0) t.true(actualFiles.length > 0);
runPrettierSync(actualFiles, actualDir); runPrettierSync(actualFiles, actualDir);
@ -47,60 +37,76 @@ const defineTest = (caseDirName: string) => {
} else { } else {
t.pass(); t.pass();
} }
} catch(e){ } catch (e) {
throw e; // just for debugger throw e; // just for debugger
} }
}); });
}; }
test('simple truth should pass', async (t) => { async function exportProject(schemaJson: IProjectSchema, targetPath: string, projectName: string) {
t.is(0, 0); const raxAppBuilder = createRaxAppBuilder();
}); const result = await raxAppBuilder.generateProject(schemaJson);
const publisher = CodeGenerator.publishers.disk();
fs.readdirSync(TEST_CASES_DIR).forEach(defineTest); await publisher.publish({
project: result,
outputPath: targetPath,
projectSlug: projectName,
createProjectFolder: true,
});
}
function removeActualDirRecursiveSync(actualDir: string, caseFullDir: string) { function removeActualDirRecursiveSync(actualDir: string, caseFullDir: string) {
spawnSync('rm', ['-rf', actualDir], { ensureShellExec('rm', ['-rf', actualDir], { cwd: caseFullDir });
stdio: 'inherit',
shell: true,
cwd: caseFullDir
});
} }
function runPrettierSync(files: string[], cwd: string) { function runPrettierSync(files: string[], cwd: string) {
spawnSync('npx', ['prettier', '--write', ...files], { ensureShellExec('npx', ['prettier', '--write', ...files], { cwd });
stdio: 'inherit',
shell: true,
cwd,
});
} }
function diffActualAndExpectedSync(caseFullDir: string): string { function diffActualAndExpectedSync(caseFullDir: string): string {
const res = spawnSync('diff', ['-wur', 'expected', 'actual'], { const res = ensureShellExec('diff', ['-wur', 'expected', 'actual'], {
stdio: 'pipe',
shell: true,
cwd: caseFullDir, cwd: caseFullDir,
encoding: 'utf-8',
}); });
return colorizeDiffOutput(res.stdout); return colorizeDiffOutput(res.stdout);
} }
function ensureShellExec(
shellCmd: string,
args: string[],
{ cwd = process.cwd() }: { cwd?: string } = {},
): { stdout: string; stderr: string } {
const res = spawnSync(shellCmd, args, {
stdio: 'pipe',
shell: true,
cwd,
encoding: 'utf-8',
});
if (res.status !== 0) {
throw new Error(`Shell command "${shellCmd}" failed with status: ${res.status}`);
}
return res;
}
function colorizeDiffOutput(output: string): string { function colorizeDiffOutput(output: string): string {
if (!output){ if (!output) {
return output; return output;
} }
return output.split('\n').map(line => { return output
if (/^Only/i.test(line)){ .split('\n')
.map((line) => {
if (/^Only/i.test(line)) {
return chalk.red(line); return chalk.red(line);
} else if (line[0] === '+'){ } else if (line[0] === '+') {
return chalk.yellow(line); return chalk.yellow(line);
} else if (line[0] === '-'){ } else if (line[0] === '-') {
return chalk.red(line); return chalk.red(line);
} else { } else {
return line; return line;
} }
}).join('\n'); })
.join('\n');
} }