keep old test case too

This commit is contained in:
Tobias Koppers 2021-10-18 23:14:50 +02:00
parent 8f2c266966
commit 9b08be6d10
13 changed files with 82 additions and 49 deletions

View File

@ -0,0 +1 @@
"./a.json"

View File

@ -0,0 +1 @@
"./b.json"

View File

@ -0,0 +1 @@
"./a.json"

View File

@ -0,0 +1 @@
"./b.json"

View File

@ -0,0 +1 @@
"./a.json"

View File

@ -0,0 +1 @@
"./c.json"

View File

@ -0,0 +1,31 @@
it("should error loadModule when a cycle with 2 modules is requested", () => {
expect(require("./loader!./2/a")).toEqual([
["./b.json", [
["./a.json", "err: There is a circular build dependency, which makes it impossible to create this module"]
]]
]);
});
it("should error loadModule when a cycle with 3 modules is requested", () => {
expect(require("./loader!./3/a")).toEqual([
["./b.json", [
["./c.json", [
["./a.json", "err: There is a circular build dependency, which makes it impossible to create this module"]
]]
]]
]);
});
it("should error loadModule when requesting itself", () => {
expect(require("./loader!./1/a")).toEqual([
["./a.json", "err: There is a circular build dependency, which makes it impossible to create this module"]
]);
});
it("should not report a cycle when loadModule is used twice (https://github.com/webpack/webpack/issues/14379)", () => {
expect(require("./loader!./4/a")).toEqual([
["./b.json", [
["./c.json", []]
]],
["./b.json", [
["./c.json", []]
]]
]);
});

View File

@ -0,0 +1,30 @@
const { promisify } = require("util");
/** @type {import("../../../../").LoaderDefinitionFunction} */
exports.default = function (source) {
const content = JSON.parse(source);
// content is one reference or an array of references
const refs = Array.isArray(content) ? content : [content];
const callback = this.async();
const loadModulePromise = promisify(this.loadModule.bind(this));
async function loadReferencedModules() {
// Modules are loaded sequentially as the false-positive circular reference
// bug from https://github.com/webpack/webpack/issues/14379 doesn't occur if
// they are loaded in parallel.
const loadedRefs = []
for(const ref of refs) {
try {
const source = await loadModulePromise("../loader!" + ref);
loadedRefs.push([ref, JSON.parse(source)]);
} catch(err) {
loadedRefs.push([ref, `err: ${err && err.message}`]);
}
}
return loadedRefs;
}
loadReferencedModules().then((loadResults) => {
callback(null, JSON.stringify(loadResults));
});
};

View File

@ -1,31 +1,15 @@
it("should error loadModule when a cycle with 2 modules is requested", () => {
expect(require("./loader!./2/a")).toEqual([
["./b.json", [
["./a.json", "err: There is a circular build dependency, which makes it impossible to create this module"]
]]
]);
expect(require("./loader!./2/a")).toMatch(
/^source: err: There is a circular build dependency/
);
});
it("should error loadModule when a cycle with 3 modules is requested", () => {
expect(require("./loader!./3/a")).toEqual([
["./b.json", [
["./c.json", [
["./a.json", "err: There is a circular build dependency, which makes it impossible to create this module"]
]]
]]
]);
expect(require("./loader!./3/a")).toMatch(
/^source: source: err: There is a circular build dependency/
);
});
it("should error loadModule when requesting itself", () => {
expect(require("./loader!./1/a")).toEqual([
["./a.json", "err: There is a circular build dependency, which makes it impossible to create this module"]
]);
});
it("should not report a cycle when loadModule is used twice (https://github.com/webpack/webpack/issues/14379)", () => {
expect(require("./loader!./4/a")).toEqual([
["./b.json", [
["./c.json", []]
]],
["./b.json", [
["./c.json", []]
]]
]);
expect(require("./loader!./1/a")).toMatch(
/^err: There is a circular build dependency/
);
});

View File

@ -1,30 +1,12 @@
const { promisify } = require("util");
/** @type {import("../../../../").LoaderDefinitionFunction} */
exports.default = function (source) {
const content = JSON.parse(source);
// content is one reference or an array of references
const refs = Array.isArray(content) ? content : [content];
const ref = JSON.parse(source);
const callback = this.async();
const loadModulePromise = promisify(this.loadModule.bind(this));
async function loadReferencedModules() {
// Modules are loaded sequentially as the false-positive circular reference
// bug from https://github.com/webpack/webpack/issues/14379 doesn't occur if
// they are loaded in parallel.
const loadedRefs = []
for(const ref of refs) {
try {
const source = await loadModulePromise("../loader!" + ref);
loadedRefs.push([ref, JSON.parse(source)]);
} catch(err) {
loadedRefs.push([ref, `err: ${err && err.message}`]);
}
this.loadModule("../loader!" + ref, (err, source, sourceMap, module) => {
if (err) {
callback(null, JSON.stringify(`err: ${err && err.message}`));
} else {
callback(null, JSON.stringify(`source: ${JSON.parse(source)}`));
}
return loadedRefs;
}
loadReferencedModules().then((loadResults) => {
callback(null, JSON.stringify(loadResults));
});
};