Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Feb 10, 2022
1 parent 403057c commit 2ec870f
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 1 deletion.
37 changes: 36 additions & 1 deletion test/ConfigCacheTestCases.longtest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
const { describeCases } = require("./ConfigTestCases.template");
const { describeCases, logErrors } = require("./ConfigTestCases.template");

describeCases({
name: "ConfigCacheTestCases",
infrastructureLogErrors: {
allowList: [
{
// Pack got invalid because of write to: Compilation/modules|/home/runner/work/webpack/webpack/test/configCases/wasm/missing-wasm-experiment/wasm.wasm
category: "wasm",
test: "missing-wasm-experiment"
},
{
// Pack got invalid because of write to: RealContentHashPlugin|analyse|index.html
category: "process-assets",
test: "html-plugin"
},
{
// Pack got invalid because of write to: Compilation/modules|/home/runner/work/webpack/webpack/test/cases/parsing/context/templates/dump-file.txt
category: "parsing",
test: "context"
},
{
// Pack got invalid because of write to: Compilation/modules|/home/runner/work/webpack/webpack/test/configCases/loaders/options/loader-1.js??ruleSet[1].rules[9]!/home/runner/work/webpack/webpack/test/configCases/loaders/options/error1.js
category: "loaders",
test: "options"
},
{
// Pack got invalid because of write to: TerserWebpackPlugin|bundle0.js
category: "assets",
test: "delete-asset"
},
{
// Pack got invalid because of write to: webpack.HttpUriPlugin|https://raw.githubusercontent.com//webpack//webpack//main/CODE_OF_CONDUCT.md
category: "asset-modules",
test: "http-url"
}
],
filter: [logErrors.PERSISTENCE_CACHE_INVALIDATE_ERROR]
},
cache: {
type: "filesystem",
buildDependencies: {
Expand Down
118 changes: 118 additions & 0 deletions test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ const { parseResource } = require("../lib/util/identifier");
const captureStdio = require("./helpers/captureStdio");
const asModule = require("./helpers/asModule");

const PERSISTENCE_CACHE_INVALIDATE_ERROR = (log, config) => {
if (config.run < 2) return;
const match =
/^\[webpack\.cache\.PackFileCacheStrategy\] Pack got invalid because of write to:(.+)$/.exec(
log
);
if (match) {
return `Pack got invalid because of write to: ${match[1].trim()}`;
}
};

const casesPath = path.join(__dirname, "configCases");
const categories = fs.readdirSync(casesPath).map(cat => {
return {
Expand All @@ -29,7 +40,53 @@ const categories = fs.readdirSync(casesPath).map(cat => {
};
});

const createLogger = appendTarget => {
return {
log: l => appendTarget.push(l),
debug: l => appendTarget.push(l),
trace: l => appendTarget.push(l),
info: l => appendTarget.push(l),
warn: console.warn.bind(console),
error: console.error.bind(console),
logTime: () => {},
group: () => {},
groupCollapsed: () => {},
groupEnd: () => {},
profile: () => {},
profileEnd: () => {},
clear: () => {},
status: () => {}
};
};

const returnLogError = (logs, errorsFilter, config) => {
for (const log of logs) {
for (const filter of errorsFilter) {
const result = filter(log, config);
if (result) {
return new Error(result);
}
}
}
};

const describeCases = config => {
let allowErrorsMap;
if (config.infrastructureLogErrors) {
allowErrorsMap = new Map();
if (config.infrastructureLogErrors.allowList) {
for (const { category, test } of config.infrastructureLogErrors
.allowList) {
let byCategory = allowErrorsMap.get(category);
if (!byCategory) {
byCategory = new Set();
allowErrorsMap.set(category, byCategory);
}
byCategory.add(test);
}
}
}

describe(config.name, () => {
let stderr;
beforeEach(() => {
Expand All @@ -44,6 +101,11 @@ const describeCases = config => {
// eslint-disable-next-line no-loop-func
describe(category.name, () => {
for (const testName of category.tests) {
const inAllowErrorsList = () => {
const byCategory = allowErrorsMap.get(category.name);
if (!byCategory) return false;
return byCategory.has(testName);
};
// eslint-disable-next-line no-loop-func
describe(testName, function () {
const testDirectory = path.join(casesPath, category.name, testName);
Expand All @@ -54,6 +116,7 @@ const describeCases = config => {
});
return;
}
const infraStructureLog = [];
const outBaseDir = path.join(__dirname, "js");
const testSubPath = path.join(config.name, category.name, testName);
const outputDirectory = path.join(outBaseDir, testSubPath);
Expand Down Expand Up @@ -97,6 +160,10 @@ const describeCases = config => {
name: `config-${idx}`,
...config.cache
};
options.infrastructureLogging = {
debug: true,
console: createLogger(infraStructureLog)
};
}
if (!options.snapshot) options.snapshot = {};
if (!options.snapshot.managedPaths) {
Expand Down Expand Up @@ -168,6 +235,7 @@ const describeCases = config => {
it(`${testName} should pre-compile to fill disk cache (1st)`, done => {
rimraf.sync(outputDirectory);
fs.mkdirSync(outputDirectory, { recursive: true });
infraStructureLog.length = 0;
const deprecationTracker = deprecationTracking.start();
require("..")(options, err => {
deprecationTracker();
Expand All @@ -180,13 +248,29 @@ const describeCases = config => {
)
);
}
if (config.infrastructureLogErrors) {
if (!inAllowErrorsList()) {
const error = returnLogError(
infraStructureLog,
Array.isArray(config.infrastructureLogErrors.filter)
? config.infrastructureLogErrors.filter
: [config.infrastructureLogErrors.filter],
{
run: 1,
options
}
);
if (error) return done(error);
}
}
if (err) return handleFatalError(err, done);
done();
});
}, 60000);
it(`${testName} should pre-compile to fill disk cache (2nd)`, done => {
rimraf.sync(outputDirectory);
fs.mkdirSync(outputDirectory, { recursive: true });
infraStructureLog.length = 0;
const deprecationTracker = deprecationTracking.start();
require("..")(options, (err, stats) => {
deprecationTracker();
Expand Down Expand Up @@ -228,13 +312,29 @@ const describeCases = config => {
);
}
}
if (config.infrastructureLogErrors) {
if (!inAllowErrorsList()) {
const error = returnLogError(
infraStructureLog,
Array.isArray(config.infrastructureLogErrors.filter)
? config.infrastructureLogErrors.filter
: [config.infrastructureLogErrors.filter],
{
run: 2,
options
}
);
if (error) return done(error);
}
}
done();
});
}, 40000);
}
it(`${testName} should compile`, done => {
rimraf.sync(outputDirectory);
fs.mkdirSync(outputDirectory, { recursive: true });
infraStructureLog.length = 0;
const deprecationTracker = deprecationTracking.start();
const onCompiled = (err, stats) => {
const deprecations = deprecationTracker();
Expand Down Expand Up @@ -298,6 +398,21 @@ const describeCases = config => {
) {
return;
}
if (config.infrastructureLogErrors) {
if (!inAllowErrorsList()) {
const error = returnLogError(
infraStructureLog,
Array.isArray(config.infrastructureLogErrors.filter)
? config.infrastructureLogErrors.filter
: [config.infrastructureLogErrors.filter],
{
run: 3,
options
}
);
if (error) return done(error);
}
}

let filesCount = 0;

Expand Down Expand Up @@ -623,3 +738,6 @@ const describeCases = config => {
};

exports.describeCases = describeCases;
exports.logErrors = {
PERSISTENCE_CACHE_INVALIDATE_ERROR
};

0 comments on commit 2ec870f

Please sign in to comment.