Skip to content

Commit

Permalink
[FIX] XMLTemplateAnalyzer: Handle empty XML view/fragment (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 authored Jun 29, 2020
1 parent 70656fb commit 7488d5f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
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

0 comments on commit 7488d5f

Please sign in to comment.