Skip to content

Commit

Permalink
chore: test directory (#6408)
Browse files Browse the repository at this point in the history
* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory

* chore: adjust test directory
  • Loading branch information
LingyuCoder authored Apr 29, 2024
1 parent af80a3f commit 7066027
Show file tree
Hide file tree
Showing 676 changed files with 418 additions and 1,090 deletions.
26 changes: 19 additions & 7 deletions packages/rspack-test-tools/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function compareModules(modules: string[], sourceModules: Map<string, str
export function createBuiltinCase(name: string, src: string, dist: string): void;

// @public (undocumented)
export function createCompilerCase(name: string, src: string, dist: string, root: string): void;
export function createCompilerCase(name: string, src: string, dist: string, testConfig: string): void;

// @public (undocumented)
export function createConfigCase(name: string, src: string, dist: string): void;
Expand All @@ -93,7 +93,7 @@ export function createDiagnosticCase(name: string, src: string, dist: string): v
export function createDiffCase(name: string, src: string, dist: string): void;

// @public (undocumented)
export function createErrorCase(name: string, src: string, dist: string, root: string): void;
export function createErrorCase(name: string, src: string, dist: string, testConfig: string): void;

// @public (undocumented)
export function createHashCase(name: string, src: string, dist: string): void;
Expand All @@ -108,10 +108,10 @@ export function createHotStepCase(name: string, src: string, dist: string, targe
export function createNormalCase(name: string, src: string, dist: string): void;

// @public (undocumented)
export function createStatsAPICase(name: string, src: string, dist: string, root: string): void;
export function createStatsAPICase(name: string, src: string, dist: string, testConfig: string): void;

// @public (undocumented)
export function createStatsCase(name: string, src: string, dist: string): void;
export function createStatsOutputCase(name: string, src: string, dist: string): void;

// @public (undocumented)
export function createTreeShakingCase(name: string, src: string, dist: string): void;
Expand Down Expand Up @@ -149,9 +149,12 @@ export class DefaultsConfigTaskProcessor extends SimpleTaskProcessor<ECompilerTy
}

// @public (undocumented)
export function describeByWalk(name: string, sourceBase: string, distBase: string, createCase: (name: string, src: string, dist: string) => void, whitelist?: {
cat?: RegExp;
case?: RegExp;
export function describeByWalk(testFile: string, createCase: (name: string, src: string, dist: string) => void, options?: {
type?: "file" | "directory";
level?: number;
source?: string;
dist?: string;
absoluteDist?: boolean;
}): void;

// @public (undocumented)
Expand Down Expand Up @@ -247,6 +250,12 @@ export class ErrorTaskProcessor<T extends ECompilerType> extends SimpleTaskProce
run(env: ITestEnv, context: ITestContext): Promise<void>;
}

// @public (undocumented)
export function escapeEOL(str: string): string;

// @public (undocumented)
export function escapeSep(str: string): string;

// @public (undocumented)
export function formatCode(name: string, raw: string, options: IFormatCodeOptions): string;

Expand Down Expand Up @@ -549,6 +558,9 @@ export interface IRspackWatchStepProcessorOptions {
// @public (undocumented)
export const isDirectory: (p: string) => boolean;

// @public (undocumented)
export const isFile: (p: string) => boolean;

// @public (undocumented)
export interface ISimpleProcessorOptions<T extends ECompilerType = ECompilerType.Rspack> {
// (undocumented)
Expand Down
10 changes: 5 additions & 5 deletions packages/rspack-test-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"build:viewer": "rspack build",
"dev:viewer": "rspack serve",
"dev": "tsc -b -w",
"test": "run-s \"test:*\"",
"testu": "run-s \"test:* -- -u\"",
"test:base": "cross-env NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --config ./jest.config.js",
"test:hot": "cross-env RSPACK_HOT_TEST=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --config ./jest.config.hot.js",
"test:diff": "cross-env RSPACK_DIFF=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --config ./jest.config.diff.js",
"test": "sh -c 'run-s \"test:* -- $*\"' sh",
"testu": "sh -c 'run-s \"test:* -- $*\"' sh -u",
"test:base": "cross-env NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --config ./jest.config.js --passWithNoTests",
"test:hot": "cross-env RSPACK_HOT_TEST=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --config ./jest.config.hot.js --passWithNoTests",
"test:diff": "cross-env RSPACK_DIFF=true NO_COLOR=1 node --expose-gc --max-old-space-size=8192 --experimental-vm-modules ../../node_modules/jest-cli/bin/jest --logHeapUsage --config ./jest.config.diff.js --passWithNoTests",
"api-extractor": "api-extractor run --verbose",
"api-extractor:ci": "api-extractor run --verbose || diff temp/api.md etc/api.md"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/src/case/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const FILTERS: Record<

const creator = new BasicCaseCreator({
clean: true,
describe: true,
describe: false,
description(name) {
return `${name} should match snapshot`;
},
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack-test-tools/src/case/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ export function createCompilerCase(
name: string,
src: string,
dist: string,
root: string
testConfig: string
) {
const caseConfig = require(path.join(root, name));
const caseConfig = require(testConfig);

const runner = getSimpleProcessorRunner(src, dist, {
it,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/src/case/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BasicCaseCreator } from "../test/creator";

const creator = new BasicCaseCreator({
clean: true,
describe: true,
describe: false,
steps: ({ name }) => [
new RspackConfigProcessor({
name,
Expand Down
3 changes: 1 addition & 2 deletions packages/rspack-test-tools/src/case/diagnostic.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from "path";
import { RspackDiagnosticProcessor } from "../processor";
import { BasicCaseCreator } from "../test/creator";

const creator = new BasicCaseCreator({
clean: true,
describe: true,
describe: false,
steps: ({ name }) => [
new RspackDiagnosticProcessor({
name
Expand Down
5 changes: 2 additions & 3 deletions packages/rspack-test-tools/src/case/error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ECompilerType } from "../type";
import path from "path";
import { getSimpleProcessorRunner } from "../test/simple";
import { ErrorTaskProcessor } from "../processor";

Expand All @@ -9,13 +8,13 @@ export function createErrorCase(
name: string,
src: string,
dist: string,
root: string
testConfig: string
) {
if (!addedSerializer) {
ErrorTaskProcessor.addSnapshotSerializer();
addedSerializer = true;
}
const caseConfig = require(path.join(root, name));
const caseConfig = require(testConfig);
const runner = getSimpleProcessorRunner(src, dist, {
it,
beforeEach,
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/src/case/hot-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getCreator(target: TTarget) {
target,
new BasicCaseCreator({
clean: true,
describe: true,
describe: false,
target,
steps: ({ name, target }) => [
new RspackHotStepProcessor({
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/src/case/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from "./diff";
export * from "./hash";
export * from "./hot";
export * from "./normal";
export * from "./stats";
export * from "./stats-output";
export * from "./watch";
export * from "./treeshaking";
export * from "./defaults";
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack-test-tools/src/case/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { NormalRunnerFactory } from "../runner";

const creator = new BasicCaseCreator({
clean: true,
describe: true,
describe: false,
steps: ({ name }) => [
new RspackNormalProcessor({
name,
root: path.resolve(__dirname, "../../tests/cases"),
root: path.resolve(__dirname, "../../tests/normalCases"),
compilerOptions: {}, // do not used in rspack
runable: true
})
Expand Down
5 changes: 2 additions & 3 deletions packages/rspack-test-tools/src/case/stats-api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ECompilerType } from "../type";
import path from "path";
import { getSimpleProcessorRunner } from "../test/simple";
import { StatsAPITaskProcessor } from "../processor";

Expand All @@ -9,13 +8,13 @@ export function createStatsAPICase(
name: string,
src: string,
dist: string,
root: string
testConfig: string
) {
if (!addedSerializer) {
StatsAPITaskProcessor.addSnapshotSerializer();
addedSerializer = true;
}
const caseConfig = require(path.join(root, name));
const caseConfig = require(testConfig);
const runner = getSimpleProcessorRunner(src, dist, {
it,
beforeEach,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ const creator = new BasicCaseCreator({
description: name => `should print correct stats for ${name}`
});

export function createStatsCase(name: string, src: string, dist: string) {
export function createStatsOutputCase(name: string, src: string, dist: string) {
creator.create(name, src, dist);
}
2 changes: 1 addition & 1 deletion packages/rspack-test-tools/src/case/treeshaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BasicCaseCreator } from "../test/creator";

const creator = new BasicCaseCreator({
clean: true,
describe: true,
describe: false,
description(name, step) {
if (step === 0) {
return `${name} with builtin.treeShaking should match snapshot`;
Expand Down
93 changes: 61 additions & 32 deletions packages/rspack-test-tools/src/helper/directory.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,75 @@
import fs from "fs";
import path from "path";
import { escapeSep } from ".";

export const isDirectory = (p: string) => fs.lstatSync(p).isDirectory();
export const isFile = (p: string) => fs.lstatSync(p).isFile();
export const isValidCaseDirectory = (name: string) =>
!name.startsWith("_") && !name.startsWith(".");
!name.startsWith("_") && !name.startsWith(".") && name !== "node_modules";

export function describeByWalk(
name: string,
sourceBase: string,
distBase: string,
testFile: string,
createCase: (name: string, src: string, dist: string) => void,
whitelist: {
cat?: RegExp;
case?: RegExp;
options: {
type?: "file" | "directory";
level?: number;
source?: string;
dist?: string;
absoluteDist?: boolean;
} = {}
) {
const categories = fs
.readdirSync(sourceBase)
.filter(isValidCaseDirectory)
.filter(folder => isDirectory(path.join(sourceBase, folder)))
.filter(i => (whitelist?.cat ? whitelist.cat.test(i) : true))
.map(cat => {
return {
name: cat,
tests: fs
.readdirSync(path.join(sourceBase, cat))
.filter(isValidCaseDirectory)
.filter(folder => isDirectory(path.join(sourceBase, cat, folder)))
.filter(i => (whitelist?.case ? whitelist.case.test(i) : true))
.sort()
};
});
describe(name, () => {
for (let { name: catName, tests } of categories) {
if (tests.length === 0) continue;
describe(catName, () => {
for (const testName of tests) {
const src = path.join(sourceBase, catName, testName);
const dist = path.join(distBase, catName, testName);
createCase(testName, src, dist);
const testBasename = path
.basename(testFile)
.replace(/\.(diff|hot)?test\.js/, "");
const testId = testBasename.charAt(0).toLowerCase() + testBasename.slice(1);
const sourceBase =
options.source || path.join(path.dirname(testFile), `${testId}Cases`);
const distBase =
options.dist || path.join(path.dirname(testFile), "js", testId);
const level = options.level || 2;
const type = options.type || "directory";
const absoluteDist = options.absoluteDist ?? true;
function describeDirectory(dirname: string, currentLevel: number) {
fs.readdirSync(path.join(sourceBase, dirname))
.filter(isValidCaseDirectory)
.filter(folder => {
const p = path.join(sourceBase, dirname, folder);
if (type === "file" && currentLevel === 1) {
return isFile(p);
} else if (type === "directory" || currentLevel > 1) {
return isDirectory(p);
} else {
return false;
}
})
.map(folder => {
const caseName = path.join(dirname, folder);
if (currentLevel > 1) {
describeDirectory(caseName, currentLevel - 1);
} else {
const name = escapeSep(
path.join(testId, caseName).split(".").shift()!
);
describe(name, () => {
let source = path.join(sourceBase, caseName);
let dist = "";
if (absoluteDist) {
dist = path.join(distBase, caseName);
} else {
const relativeDist = options.dist || "dist";
if (path.isAbsolute(relativeDist)) {
dist = path.join(relativeDist, caseName);
} else {
dist = path.join(sourceBase, caseName, relativeDist);
}
}
createCase(folder, source, dist);
});
}
});
}
}

describe(testId, () => {
describeDirectory("", level);
});
}
1 change: 1 addition & 0 deletions packages/rspack-test-tools/src/helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./read-config-file";
export * from "./parse-modules";
export * from "./directory";
export * from "./win";
9 changes: 9 additions & 0 deletions packages/rspack-test-tools/src/helper/win.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from "path";

export function escapeSep(str: string) {
return str.split(path.win32.sep).join(path.posix.sep);
}

export function escapeEOL(str: string) {
return str.split("\r\n").join("\n").trim();
}
19 changes: 9 additions & 10 deletions packages/rspack-test-tools/src/processor/diagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BasicTaskProcessor } from "./basic";
import assert from "assert";
import path from "path";
import fs from "fs";
import { escapeEOL } from "../helper";
const serializer = require("jest-serializer-path");
const normalizePaths = serializer.normalizePaths;
const rspackPath = path.resolve(__dirname, "../../../rspack");
Expand Down Expand Up @@ -51,20 +52,18 @@ export class RspackDiagnosticProcessor extends BasicTaskProcessor<ECompilerType.
})
);
// TODO: change to stats.errorStack
if (context.getSource().includes("module-build-failed")) {
// Replace potential loader stack
output = output
.replaceAll("│", "")
.split(/\r?\n/)
.map((s: string) => s.trim())
.join("");
}
output = output
.replaceAll("│", "")
.split(/\r?\n/)
.map((s: string) => s.trim())
.join("");

const errorOutputPath = path.resolve(context.getSource(), `./stats.err`);
if (!fs.existsSync(errorOutputPath) || global.updateSnapshot) {
fs.writeFileSync(errorOutputPath, output);
fs.writeFileSync(errorOutputPath, escapeEOL(output));
} else {
expect(output).toBe(fs.readFileSync(errorOutputPath, "utf-8"));
const expectContent = fs.readFileSync(errorOutputPath, "utf-8");
expect(escapeEOL(output)).toBe(escapeEOL(expectContent));
}
}

Expand Down
Loading

2 comments on commit 7066027

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-04-29 ec3c0df) Current Change
10000_development-mode + exec 2.8 s ± 43 ms 2.82 s ± 50 ms +0.93 %
10000_development-mode_hmr + exec 690 ms ± 16 ms 699 ms ± 3.9 ms +1.26 %
10000_production-mode + exec 2.49 s ± 18 ms 2.6 s ± 23 ms +4.41 %
arco-pro_development-mode + exec 2.54 s ± 39 ms 2.62 s ± 52 ms +2.83 %
arco-pro_development-mode_hmr + exec 431 ms ± 2.6 ms 429 ms ± 2.2 ms -0.43 %
arco-pro_development-mode_hmr_intercept-plugin + exec 443 ms ± 2.5 ms 440 ms ± 3.1 ms -0.57 %
arco-pro_development-mode_intercept-plugin + exec 3.25 s ± 72 ms 3.44 s ± 124 ms +5.94 %
arco-pro_production-mode + exec 3.99 s ± 103 ms 4.17 s ± 55 ms +4.54 %
arco-pro_production-mode_intercept-plugin + exec 4.75 s ± 102 ms 4.96 s ± 100 ms +4.49 %
threejs_development-mode_10x + exec 2.08 s ± 23 ms 2.1 s ± 8.7 ms +1.18 %
threejs_development-mode_10x_hmr + exec 771 ms ± 32 ms 754 ms ± 15 ms -2.20 %
threejs_production-mode_10x + exec 5.26 s ± 54 ms 5.3 s ± 40 ms +0.78 %

Threshold exceeded: ["arco-pro_development-mode_intercept-plugin + exec"]

Please sign in to comment.