Skip to content

Commit

Permalink
[FIX] SmartTemplateAnalyzer: Detect dependencies from "pages" object
Browse files Browse the repository at this point in the history
The "pages" property can be either an array or an object.
  • Loading branch information
matz3 committed May 2, 2019
1 parent e6c6d03 commit 2d400c2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 26 deletions.
23 changes: 13 additions & 10 deletions lib/lbt/analyzer/SmartTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,20 @@ class TemplateComponentAnalyzer {
const promises = [];
const that = this;
const st = (manifest && manifest["sap.ui.generic.app"]) || {};
function recursePage(page) {
if ( page.component && page.component.name ) {
const module = ModuleName.fromUI5LegacyName( page.component.name + ".Component" );
log.verbose("template app: add dependency to template component %s", module);
info.addDependency(module);
promises.push( that._analyzeTemplateComponent(module, page, info) );
}
recurse(page);
}
function recurse(ctx) {
if ( ctx.pages ) {
ctx.pages.forEach((page) => {
if ( page.component && page.component.name ) {
const module = ModuleName.fromUI5LegacyName( page.component.name + ".Component" );
log.verbose("template app: add dependency to template component %s", module);
info.addDependency(module);
promises.push( that._analyzeTemplateComponent(module, page, info) );
}
recurse(page);
});
if ( Array.isArray(ctx.pages) ) {
ctx.pages.forEach(recursePage);
} else if (typeof ctx.pages === "object") {
Object.values(ctx.pages).forEach(recursePage);
}
}
recurse(st);
Expand Down
105 changes: 89 additions & 16 deletions test/lib/lbt/analyzer/SmartTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test("_analyzeManifest: without manifest", async (t) => {
});


test("_analyzeManifest: with manifest with recursive pages", async (t) => {
test("_analyzeManifest: with manifest with recursive pages (as array)", async (t) => {
const moduleInfo = {
addDependency: function() {}
};
Expand All @@ -58,14 +58,14 @@ test("_analyzeManifest: with manifest with recursive pages", async (t) => {
"sap.ui.generic.app": {
"pages": [{
"component": {
"name": "mycomp.js",
"name": "test.mycomp",
"settings": {
"templateName": "myTemplate"
}
},
"pages": [{
"component": {
"name": "mycomp2.js",
"name": "test.mycomp2",
"settings": {
"templateName": "myTemplate"
}
Expand All @@ -81,41 +81,114 @@ test("_analyzeManifest: with manifest with recursive pages", async (t) => {
await analyzer._analyzeManifest(manifest, moduleInfo);

t.is(stubAnalyzeTemplateComponent.callCount, 2, "_analyzeTemplateComponent was called twice");
t.deepEqual(stubAnalyzeTemplateComponent.getCall(0).args[0], "mycomp/js/Component.js",
t.deepEqual(stubAnalyzeTemplateComponent.getCall(0).args[0], "test/mycomp/Component.js",
"_analyzeTemplateComponent should be called with the component");
t.deepEqual(stubAnalyzeTemplateComponent.getCall(0).args[1], {
"component": {
"name": "mycomp.js",
"name": "test.mycomp",
"settings": {
"templateName": "myTemplate"
}
},
"pages": [{
"component": {
"name": "mycomp2.js",
"name": "test.mycomp2",
"settings": {
"templateName": "myTemplate"
}
},
}]
}, "_analyzeTemplateComponent should be called with the page");

t.deepEqual(stubAnalyzeTemplateComponent.getCall(1).args[0], "mycomp2/js/Component.js",
t.deepEqual(stubAnalyzeTemplateComponent.getCall(1).args[0], "test/mycomp2/Component.js",
"_analyzeTemplateComponent should be called with the component");
t.deepEqual(stubAnalyzeTemplateComponent.getCall(1).args[1], {
"component": {
"name": "mycomp2.js",
"name": "test.mycomp2",
"settings": {
"templateName": "myTemplate"
}
}
}, "_analyzeTemplateComponent should be called with the page");

t.is(stubAddDependency.callCount, 2, "addDependency was called twice");
t.deepEqual(stubAddDependency.getCall(0).args[0], "mycomp/js/Component.js",
t.deepEqual(stubAddDependency.getCall(0).args[0], "test/mycomp/Component.js",
"addDependency should be called with the dependency name");
});

test("_analyzeManifest: with manifest with recursive pages (as object)", async (t) => {
const moduleInfo = {
addDependency: function() {}
};
const stubAddDependency = sinon.spy(moduleInfo, "addDependency");

const manifest = {
"sap.ui.generic.app": {
"pages": {
"MyPage1": {
"component": {
"name": "test.mycomp",
"settings": {
"templateName": "myTemplate"
}
},
"pages": {
"MyPage2": {
"component": {
"name": "test.mycomp2",
"settings": {
"templateName": "myTemplate"
}
}
}
}
}
}
}
};

const analyzer = new SmartTemplateAnalyzer();
const stubAnalyzeTemplateComponent = sinon.stub(analyzer, "_analyzeTemplateComponent").resolves();

await analyzer._analyzeManifest(manifest, moduleInfo);

t.is(stubAnalyzeTemplateComponent.callCount, 2, "_analyzeTemplateComponent was called twice");
t.deepEqual(stubAnalyzeTemplateComponent.getCall(0).args[0], "test/mycomp/Component.js",
"_analyzeTemplateComponent should be called with the component");
t.deepEqual(stubAnalyzeTemplateComponent.getCall(0).args[1], {
"component": {
"name": "test.mycomp",
"settings": {
"templateName": "myTemplate"
}
},
"pages": {
"MyPage2": {
"component": {
"name": "test.mycomp2",
"settings": {
"templateName": "myTemplate"
}
}
}
}
}, "_analyzeTemplateComponent should be called with the page");

t.deepEqual(stubAnalyzeTemplateComponent.getCall(1).args[0], "test/mycomp2/Component.js",
"_analyzeTemplateComponent should be called with the component");
t.deepEqual(stubAnalyzeTemplateComponent.getCall(1).args[1], {
"component": {
"name": "test.mycomp2",
"settings": {
"templateName": "myTemplate"
}
}
}, "_analyzeTemplateComponent should be called with the page");

t.is(stubAddDependency.callCount, 2, "addDependency was called twice");
t.deepEqual(stubAddDependency.getCall(0).args[0], "test/mycomp/Component.js",
"addDependency should be called with the dependency name");
});

test.serial("_analyzeTemplateComponent: Manifest with TemplateAssembler code", async (t) => {
const moduleInfo = {
Expand Down Expand Up @@ -217,8 +290,8 @@ test.serial("_analyzeTemplateComponent: with template name from pageConfig", asy
});

test("_analyzeAST: get template name from ast", async (t) => {
const code = `sap.ui.define(["a", "sap/suite/ui/generic/template/lib/TemplateAssembler"],
function(a, TemplateAssembler){
const code = `sap.ui.define(["a", "sap/suite/ui/generic/template/lib/TemplateAssembler"],
function(a, TemplateAssembler){
return TemplateAssembler.getTemplateComponent(getMethods,
"sap.fe.templates.Page.Component", {
metadata: {
Expand Down Expand Up @@ -248,8 +321,8 @@ test("_analyzeAST: get template name from ast", async (t) => {
});

test("_analyzeAST: no template name from ast", async (t) => {
const code = `sap.ui.define(["a", "sap/suite/ui/generic/template/lib/TemplateAssembler"],
function(a, TemplateAssembler){
const code = `sap.ui.define(["a", "sap/suite/ui/generic/template/lib/TemplateAssembler"],
function(a, TemplateAssembler){
return TemplateAssembler.getTemplateComponent(getMethods,
"sap.fe.templates.Page.Component", {
metadata: {
Expand Down Expand Up @@ -283,7 +356,7 @@ test("Analysis of Manifest and TemplateAssembler code", async (t) => {
"sap.ui.generic.app": {
"pages": [{
"component": {
"name": "mycomp.js",
"name": "test.mycomp",
"settings": {
"templateName": "myTemplate"
}
Expand All @@ -292,7 +365,7 @@ test("Analysis of Manifest and TemplateAssembler code", async (t) => {
}
};

const code = `sap.ui.define(["a", "sap/suite/ui/generic/template/lib/TemplateAssembler"],
const code = `sap.ui.define(["a", "sap/suite/ui/generic/template/lib/TemplateAssembler"],
function(a, TemplateAssembler){ return TemplateAssembler.getTemplateComponent(getMethods,
"sap.suite.ui.generic.templates.Page.Component", {
metadata: {
Expand All @@ -319,7 +392,7 @@ test("Analysis of Manifest and TemplateAssembler code", async (t) => {
const analyzer = new SmartTemplateAnalyzer(mockPool);
const name = "MyComponent.js";
await analyzer.analyze({name}, moduleInfo);
t.deepEqual(moduleInfo.dependencies, ["mycomp/js/Component.js", "myTemplate.view.xml"],
t.deepEqual(moduleInfo.dependencies, ["test/mycomp/Component.js", "myTemplate.view.xml"],
"Resulting dependencies should come from manifest and code");
});

Expand Down

0 comments on commit 2d400c2

Please sign in to comment.