Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] XMLTemplateAnalyzer: Handle empty XML view/fragment #471

Merged
merged 3 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/lbt/analyzer/XMLTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ class XMLTemplateAnalyzer {
return;
}

if ( !result ) {
// Handle empty xml views/fragments
reject(new Error("Invalid empty XML document: " + info.name));
return;
}

// console.log(result);
// clear();
if ( isFragment ) {
Expand Down
58 changes: 22 additions & 36 deletions test/lib/lbt/analyzer/XMLTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const XMLTemplateAnalyzer = require("../../../../lib/lbt/analyzer/XMLTemplateAna
const ModuleInfo = require("../../../../lib/lbt/resources/ModuleInfo");
const sinon = require("sinon");

const fakeMockPool = {
findResource: () => Promise.resolve()
};

test("integration: Analysis of an xml view", async (t) => {
const xml = `<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns:m="sap.m" xmlns:l="sap.ui.layout"
controllerName="myController">
Expand All @@ -13,15 +17,10 @@ test("integration: Analysis of an xml view", async (t) => {
<m:Button />
</l:HorizontalLayout>
</mvc:View>`;
const mockPool = {async findResource(name) {
return {
buffer: () => name.endsWith(".xml") ? JSON.stringify(xml): "test"
};
}};

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(mockPool);
const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeView(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
Expand All @@ -39,15 +38,10 @@ test("integration: Analysis of an xml view with data binding in properties", asy
controllerName="myController">
<core:ComponentContainer async="true" name="{/component}" />
</mvc:View>`;
const mockPool = {async findResource(name) {
return {
buffer: () => name.endsWith(".xml") ? JSON.stringify(xml): "test"
};
}};

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(mockPool);
const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeView(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
Expand All @@ -70,15 +64,10 @@ test("integration: Analysis of an xml view with core:require", async (t) => {
<Button core:require="{Toast:'sap/m/MessageToast'}" text="Show Toast" press="Toast.show(\${$source>text})"/>

</mvc:View>`;
const mockPool = {async findResource(name) {
return {
buffer: () => name.endsWith(".xml") ? JSON.stringify(xml): "test"
};
}};

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(mockPool);
const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeView(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
Expand All @@ -104,15 +93,10 @@ test("integration: Analysis of an xml view with core:require (invalid module nam
<Button core:require="{ Toast: '' }" text="Show Toast" press="Toast.show(\${$source>text})"/>

</mvc:View>`;
const mockPool = {async findResource(name) {
return {
buffer: () => name.endsWith(".xml") ? JSON.stringify(xml): "test"
};
}};

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(mockPool);
const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeView(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
Expand All @@ -135,15 +119,10 @@ test("integration: Analysis of an xml view with core:require (parsing error)", a
<Button core:require="this can't be parsed" text="Show Toast" press="Toast.show(\${$source>text})"/>

</mvc:View>`;
const mockPool = {async findResource(name) {
return {
buffer: () => name.endsWith(".xml") ? JSON.stringify(xml): "test"
};
}};

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(mockPool);
const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeView(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
Expand All @@ -165,15 +144,10 @@ test("integration: Analysis of an xml fragment", async (t) => {
</l:HorizontalLayout>
</items>
</HBox>`;
const mockPool = {async findResource(name) {
return {
buffer: () => name.endsWith(".xml") ? JSON.stringify(xml): "test"
};
}};

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(mockPool);
const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeFragment(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
Expand All @@ -185,6 +159,18 @@ test("integration: Analysis of an xml fragment", async (t) => {
"Implicit dependency should be added since a fragment is analyzed");
});

test("integration: Analysis of an empty xml view", async (t) => {
const xml = "";

const moduleInfo = new ModuleInfo("empty.xml");

const analyzer = new XMLTemplateAnalyzer(fakeMockPool);

await t.throwsAsync(analyzer.analyzeView(xml, moduleInfo), {
message: "Invalid empty XML document: empty.xml"
}, "Should throw an error for empty XML views");
});

test("_addDependency: self reference", (t) => {
const moduleInfo = {
addDependency: function() {},
Expand Down