Skip to content

Commit

Permalink
refactor infrastructure log functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed Feb 14, 2022
1 parent 26a1916 commit 1e7acc5
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 244 deletions.
62 changes: 28 additions & 34 deletions test/ConfigCacheTestCases.longtest.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
const { describeCases, logErrors } = require("./ConfigTestCases.template");
const { describeCases } = 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]
wasm: {
// Can not compile wasm module
["missing-wasm-experiment"]:
/^Pack got invalid because of write to: Compilation\/modules.+wasm.wasm$/
},
["process-assets"]: {
["html-plugin"]:
/^Pack got invalid because of write to: RealContentHashPlugin|analyse|index.html$/
},
parsing: {
// Module parse failed
context:
/^Pack got invalid because of write to: Compilation\/modules|.+dump-file\.txt/
},
loaders: {
// Error in loader
options:
/^Pack got invalid because of write to: Compilation\/modules.+loaders\/options\/error1\.js$/
},
assets: {
["delete-asset"]:
/^Pack got invalid because of write to: TerserWebpackPlugin|bundle0.js$/
},
["asset-modules"]: {
["http-url"]:
/^Pack got invalid because of write to: webpack\.HttpUriPlugin|https:\/\/raw.githubusercontent.com\/\/webpack\/\/webpack\/\/main\/CODE_OF_CONDUCT\.md$/
}
},
cache: {
type: "filesystem",
Expand Down
127 changes: 39 additions & 88 deletions test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@ const prepareOptions = require("./helpers/prepareOptions");
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 createInfrastructureLogErrorsChecker = require("./helpers/infrastructureLogErrors");

const casesPath = path.join(__dirname, "configCases");
const categories = fs.readdirSync(casesPath).map(cat => {
Expand Down Expand Up @@ -59,34 +49,7 @@ const createLogger = appendTarget => {
};
};

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 @@ -101,11 +64,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);
};
const infrastructureLogChecker = config.infrastructureLogErrors
? createInfrastructureLogErrorsChecker(
config.infrastructureLogErrors
)
: undefined;
// eslint-disable-next-line no-loop-func
describe(testName, function () {
const testDirectory = path.join(casesPath, category.name, testName);
Expand Down Expand Up @@ -248,20 +211,17 @@ 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 (infrastructureLogChecker) {
const error = infrastructureLogChecker.check(
category.name,
testName,
infraStructureLog,
{
run: 1,
options
}
);
if (error) return done(error);
}
if (err) return handleFatalError(err, done);
done();
Expand Down Expand Up @@ -312,20 +272,17 @@ 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);
}
if (infrastructureLogChecker) {
const error = infrastructureLogChecker.check(
category.name,
testName,
infraStructureLog,
{
run: 2,
options
}
);
if (error) return done(error);
}
done();
});
Expand Down Expand Up @@ -398,20 +355,17 @@ 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);
}
if (infrastructureLogChecker) {
const error = infrastructureLogChecker.check(
category.name,
testName,
infraStructureLog,
{
run: 1,
options
}
);
if (error) return done(error);
}

let filesCount = 0;
Expand Down Expand Up @@ -738,6 +692,3 @@ const describeCases = config => {
};

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

0 comments on commit 1e7acc5

Please sign in to comment.