mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-02-28 21:20:28 +00:00
test: 💍 完善 Rax 出码的测试用例
This commit is contained in:
parent
084c37c40b
commit
d834653b20
@ -37,6 +37,9 @@
|
||||
"ava": {
|
||||
"compileEnhancements": false,
|
||||
"snapshotDir": "test/fixtures/__snapshots__",
|
||||
"files": [
|
||||
"test/*.test.ts"
|
||||
],
|
||||
"extensions": [
|
||||
"ts"
|
||||
],
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
// TODO: 引入默认全局样式
|
||||
|
||||
body {
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
import { createElement, Component } from 'rax';
|
||||
|
||||
import Page from 'rax-view';
|
||||
|
||||
import Text from 'rax-text';
|
||||
|
||||
import './index.css';
|
||||
|
||||
@ -41,7 +41,7 @@
|
||||
targetRootID: 'root',
|
||||
},
|
||||
meta: {
|
||||
name: '示例应用',
|
||||
name: 'Rax App Demo',
|
||||
git_group: 'demo-group',
|
||||
project_name: 'demo-project',
|
||||
description: '这是一个示例应用',
|
||||
|
||||
@ -4,6 +4,7 @@ import fs from 'fs';
|
||||
import glob from 'glob';
|
||||
import JSON from 'json5';
|
||||
import path from 'path';
|
||||
import chalk from 'chalk';
|
||||
|
||||
import CodeGenerator from '../src';
|
||||
import createRaxAppBuilder from '../src/solutions/rax-app';
|
||||
@ -25,30 +26,30 @@ async function exportProject(schemaJson: IProjectSchema, targetPath: string, pro
|
||||
|
||||
const defineTest = (caseDirName: string) => {
|
||||
test(`rax-app ${caseDirName} should works`, async (t) => {
|
||||
const caseFullDir = path.join(TEST_CASES_DIR, caseDirName);
|
||||
const schema = JSON.parse(fs.readFileSync(path.join(caseFullDir, 'schema.json5'), 'utf-8'));
|
||||
const actualDir = path.join(caseFullDir, 'actual');
|
||||
try {
|
||||
const caseFullDir = path.join(TEST_CASES_DIR, caseDirName);
|
||||
const schema = JSON.parse(fs.readFileSync(path.join(caseFullDir, 'schema.json5'), 'utf-8'));
|
||||
const actualDir = path.join(caseFullDir, 'actual');
|
||||
|
||||
await exportProject(schema, actualDir, 'demo-project');
|
||||
removeActualDirRecursiveSync(actualDir, caseFullDir);
|
||||
|
||||
const actualFiles = glob.sync('**/*.{js,jsx,json,ts,tsx,less,css,scss,sass}', { cwd: actualDir });
|
||||
await exportProject(schema, actualDir, 'demo-project');
|
||||
|
||||
t.true(actualFiles.length > 0)
|
||||
const actualFiles = glob.sync('**/*.{js,jsx,json,ts,tsx,less,css,scss,sass}', { cwd: actualDir });
|
||||
|
||||
spawnSync('npx', ['prettier', '--write', ...actualFiles], {
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
cwd: actualDir,
|
||||
});
|
||||
t.true(actualFiles.length > 0)
|
||||
|
||||
const diffRes = spawnSync('diff', ['-bur', 'actual', 'expected'], {
|
||||
stdio: 'pipe',
|
||||
shell: true,
|
||||
cwd: caseFullDir,
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
runPrettierSync(actualFiles, actualDir);
|
||||
|
||||
t.is(diffRes.stdout, '')
|
||||
const diffRes = diffActualAndExpectedSync(caseFullDir);
|
||||
if (diffRes) {
|
||||
t.fail(diffRes);
|
||||
} else {
|
||||
t.pass();
|
||||
}
|
||||
} catch(e){
|
||||
throw e; // just for debugger
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -58,3 +59,48 @@ test('simple truth should pass', async (t) => {
|
||||
|
||||
fs.readdirSync(TEST_CASES_DIR).forEach(defineTest);
|
||||
|
||||
function removeActualDirRecursiveSync(actualDir: string, caseFullDir: string) {
|
||||
spawnSync('rm', ['-rf', actualDir], {
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
cwd: caseFullDir
|
||||
});
|
||||
}
|
||||
|
||||
function runPrettierSync(files: string[], cwd: string) {
|
||||
spawnSync('npx', ['prettier', '--write', ...files], {
|
||||
stdio: 'inherit',
|
||||
shell: true,
|
||||
cwd,
|
||||
});
|
||||
}
|
||||
|
||||
function diffActualAndExpectedSync(caseFullDir: string): string {
|
||||
const res = spawnSync('diff', ['-wur', 'expected', 'actual'], {
|
||||
stdio: 'pipe',
|
||||
shell: true,
|
||||
cwd: caseFullDir,
|
||||
encoding: 'utf-8',
|
||||
});
|
||||
|
||||
return colorizeDiffOutput(res.stdout);
|
||||
}
|
||||
|
||||
function colorizeDiffOutput(output: string): string {
|
||||
if (!output){
|
||||
return output;
|
||||
}
|
||||
|
||||
return output.split('\n').map(line => {
|
||||
if (/^Only/i.test(line)){
|
||||
return chalk.red(line);
|
||||
} else if (line[0] === '+'){
|
||||
return chalk.yellow(line);
|
||||
} else if (line[0] === '-'){
|
||||
return chalk.red(line);
|
||||
} else {
|
||||
return line;
|
||||
}
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user