Skip to content

Commit

Permalink
[FIX] generateResourcesJson: Don't list resources omitted from build …
Browse files Browse the repository at this point in the history
…result

Resources that are tagged to be omitted from the build result should not
be listed in the resources.json as it should represent a listing of
files that are part of the build output.
Resources of dependencies should also not be used for dependency lookups
if they are omitted from the results as they also won't be listed in the
resources.json of that dependency.
  • Loading branch information
matz3 committed Jan 21, 2022
1 parent 1689f13 commit 9608c51
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/tasks/generateResourcesJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ function getCreatorOptions(projectName) {
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/fs.AbstractReader} [parameters.dependencies] Reader or Collection to read dependency files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @param {string} parameters.options.projectName Project name
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = async function({workspace, dependencies, options: {projectName}}) {
const resources = await workspace.byGlob(["/resources/**/*"].concat(DEFAULT_EXCLUDES));
module.exports = async function({workspace, dependencies, taskUtil, options: {projectName}}) {
let resources = await workspace.byGlob(["/resources/**/*"].concat(DEFAULT_EXCLUDES));

// TODO 3.0: Make dependencies parameter mandatory
let dependencyResources;
Expand All @@ -111,6 +112,18 @@ module.exports = async function({workspace, dependencies, options: {projectName}
await dependencies.byGlob("/resources/**/*.{js,json,xml,html,properties,library}");
}

if (taskUtil) {
// Filter out resources that will be omitted from the build results
resources = resources.filter((resource) => {
return !taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
});
if (dependencyResources) {
dependencyResources = dependencyResources.filter((resource) => {
return !taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.OmitFromBuildResult);
});
}
}

const resourceLists = await resourceListCreator({
resources,
dependencyResources,
Expand Down
1 change: 1 addition & 0 deletions lib/types/application/ApplicationBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class ApplicationBuilder extends AbstractBuilder {
return getTask("generateResourcesJson").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/library/LibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class LibraryBuilder extends AbstractBuilder {
return getTask("generateResourcesJson").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/themeLibrary/ThemeLibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ThemeLibraryBuilder extends AbstractBuilder {
return getTask("generateResourcesJson").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name
}
Expand Down
67 changes: 67 additions & 0 deletions test/lib/tasks/generateResourcesJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ test.serial("empty resources (sap.ui.core)", async (t) => {
});
t.deepEqual(result, undefined, "no resources returned");
t.is(resourceListCreatorStub.callCount, 1);
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].resources, [], "no resources are passed");
const expectedOptions = {
externalResources: {
"sap/ui/core": [
Expand All @@ -75,6 +76,7 @@ test.serial("empty resources (my.lib)", async (t) => {
});
t.deepEqual(result, undefined, "no resources returned");
t.is(t.context.resourceListCreatorStub.callCount, 1);
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].resources, [], "no resources are passed");
const expectedOptions = {};
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].options, expectedOptions, "options match");
});
Expand All @@ -96,8 +98,73 @@ test.serial("empty resources (my.lib with dependencies)", async (t) => {
});
t.deepEqual(result, undefined, "no resources returned");
t.is(t.context.resourceListCreatorStub.callCount, 1);
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].resources, [], "no resources are passed");
const expectedOptions = {};
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].options, expectedOptions, "options match");
t.is(t.context.resourceListCreatorStub.getCall(0).args[0].dependencyResources, dependencyResources,
"dependencyResources reference should be passed to resourceListCreator");
});

test.serial("Resources omitted from build result should be ignored", async (t) => {
const generateResourcesJson = require("../../../lib/tasks/generateResourcesJson");

const resource1 = {};
const resource2 = {};
const resource3 = {};

const workspace = createWorkspace();
workspace.byGlob = sinon.stub().resolves([
resource1,
resource2,
resource3,
]);

const dependencyResource1 = {};
const dependencyResource2 = {};
const dependencies = {
byGlob: sinon.stub().resolves([dependencyResource1, dependencyResource2])
};

const taskUtil = {
getTag: sinon.stub(),
STANDARD_TAGS: {
OmitFromBuildResult: "TEST-OmitFromBuildResult-TEST"
}
};

// resources
taskUtil.getTag
.onCall(0).returns(false)
.onCall(1).returns(true) // second resource should be filtered out
.onCall(2).returns(false);

// dependencyResources
taskUtil.getTag
.onCall(3).returns(true) // first dependencyResource should be filtered out
.onCall(4).returns(false);

const result = await generateResourcesJson({
workspace,
dependencies,
taskUtil,
options: {
projectName: "my.lib"
}
});

t.is(taskUtil.getTag.callCount, 5);
t.deepEqual(taskUtil.getTag.getCall(0).args, [resource1, "TEST-OmitFromBuildResult-TEST"]);
t.deepEqual(taskUtil.getTag.getCall(1).args, [resource2, "TEST-OmitFromBuildResult-TEST"]);
t.deepEqual(taskUtil.getTag.getCall(2).args, [resource3, "TEST-OmitFromBuildResult-TEST"]);
t.deepEqual(taskUtil.getTag.getCall(3).args, [dependencyResource1, "TEST-OmitFromBuildResult-TEST"]);
t.deepEqual(taskUtil.getTag.getCall(4).args, [dependencyResource2, "TEST-OmitFromBuildResult-TEST"]);

t.deepEqual(result, undefined, "no resources returned");
t.is(t.context.resourceListCreatorStub.callCount, 1);
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].resources,
[resource1, resource3], "only resources 1 and 3 are passed");
const expectedOptions = {};
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].options, expectedOptions, "options match");
t.deepEqual(t.context.resourceListCreatorStub.getCall(0).args[0].dependencyResources, [dependencyResource2],
"dependencyResources reference should be passed to resourceListCreator");
});

0 comments on commit 9608c51

Please sign in to comment.