Skip to content

Commit 115ad76

Browse files
authored
Merge pull request #239 from matthewpwilson/xmlparser
Change XML parser dependency
2 parents a2622c5 + 65b8cab commit 115ad76

File tree

8 files changed

+24
-30
lines changed

8 files changed

+24
-30
lines changed

__tests__/api/BundleContent/BundleMocked.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
import { Bundle } from "../../../src/api/BundleContent/Bundle";
1313
import * as fs from "fs";
14-
14+
import * as parser from "fast-xml-parser";
1515

1616
// Note, the following tests mock the file-system. Snapshot based tests are unlikely to
1717
// work as the jest implementation will itself need to interact with the filesystem.
1818
describe("MockedFilesystemTests", () => {
1919
afterEach(() => {
2020
jest.restoreAllMocks();
2121
});
22-
beforeAll(() => {
23-
// Allow xml2json to initialise itself before we start messing with the filesystem below it.
24-
const parser = require("xml2json");
25-
});
2622

2723
it("should tolerate META-INF directory not existing", () => {
2824
// Mocks for the manifest - META-INF exists
@@ -552,7 +548,7 @@ describe("MockedFilesystemTests", () => {
552548

553549
it("should complain if exceptions are thrown during manifest parsing", () => {
554550

555-
jest.spyOn(JSON, "parse").mockImplementationOnce(() => { throw new Error("Wibble"); });
551+
jest.spyOn(parser, "parse").mockImplementationOnce(() => { throw new Error("Wibble"); });
556552

557553
let err: Error;
558554
try {

__tests__/api/BundleContent/Manifest.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe("Manifest01", () => {
169169
}
170170

171171
// Check the output as JSON
172-
expect(err.message).toContain("Existing CICS Manifest file found with unparsable content.");
172+
expect(err.message).toContain("Existing CICS Manifest file found with unparsable content:");
173173
});
174174
it("Parse a manifest with bad namespace", () => {
175175

@@ -197,6 +197,6 @@ describe("Manifest01", () => {
197197
}
198198

199199
// Check the output as JSON
200-
expect(err.message).toContain("Existing CICS Manifest file found with unparsable content.");
200+
expect(err.message).toContain("Existing CICS Manifest file found with unparsable content:");
201201
});
202202
});

__tests__/api/BundleContent/__snapshots__/AutoBundler.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ exports[`AutoBundler01 should not merge an existing bundle 1`] = `"{\\"manifest\
1212

1313
exports[`AutoBundler01 should read an existing bundle 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"bundleVersion\\":\\"1\\",\\"bundleRelease\\":\\"2\\",\\"id\\":\\"ThisIsAnId\\",\\"bundleMajorVer\\":\\"10\\",\\"bundleMinorVer\\":\\"11\\",\\"bundleMicroVer\\":\\"12\\",\\"define\\":[{\\"name\\":\\"name1\\",\\"type\\":\\"type1\\",\\"path\\":\\"path1\\"},{\\"name\\":\\"name2\\",\\"type\\":\\"type2\\",\\"path\\":\\"path2\\"},{\\"name\\":\\"name3\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/Test.nodejsapp\\"}]}}"`;
1414

15-
exports[`AutoBundler01 should receive default values from package.json 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"id\\":\\"testBundleName\\",\\"bundleMajorVer\\":1,\\"bundleMinorVer\\":0,\\"bundleMicroVer\\":0,\\"define\\":[{\\"name\\":\\"testBundleName\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/testBundleName.nodejsapp\\"}]}}"`;
15+
exports[`AutoBundler01 should receive default values from package.json 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"id\\":\\"testBundleName\\",\\"bundleMajorVer\\":1,\\"bundleMinorVer\\":0,\\"bundleMicroVer\\":0,\\"define\\":[{\\"name\\":\\"testBundleName\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/testBundleName.nodejsapp\\"}]}}"`;
1616

17-
exports[`AutoBundler01 should set a nodejsapp name 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"id\\":\\"testBundleName\\",\\"bundleMajorVer\\":1,\\"bundleMinorVer\\":0,\\"bundleMicroVer\\":0,\\"define\\":[{\\"name\\":\\"MyExampleOverride\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/MyExampleOverride.nodejsapp\\"}]}}"`;
17+
exports[`AutoBundler01 should set a nodejsapp name 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"id\\":\\"testBundleName\\",\\"bundleMajorVer\\":1,\\"bundleMinorVer\\":0,\\"bundleMicroVer\\":0,\\"define\\":[{\\"name\\":\\"MyExampleOverride\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/MyExampleOverride.nodejsapp\\"}]}}"`;
1818

1919
exports[`AutoBundler01 should set a port number 1`] = `"Supplied Port is outside the range of 1-65535: -27"`;
2020

21-
exports[`AutoBundler01 should set the bundle version 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"bundleMajorVer\\":3,\\"bundleMinorVer\\":4,\\"bundleMicroVer\\":5}}"`;
21+
exports[`AutoBundler01 should set the bundle version 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"bundleMajorVer\\":3,\\"bundleMinorVer\\":4,\\"bundleMicroVer\\":5}}"`;
2222

23-
exports[`AutoBundler01 should set the bundleid 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"id\\":\\"test\\"}}"`;
23+
exports[`AutoBundler01 should set the bundleid 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"id\\":\\"test\\"}}"`;
2424

25-
exports[`AutoBundler01 should support main script from package.json 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"id\\":\\"testBundleName\\",\\"bundleMajorVer\\":1,\\"bundleMinorVer\\":0,\\"bundleMicroVer\\":0,\\"define\\":[{\\"name\\":\\"testBundleName\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/testBundleName.nodejsapp\\"}]}}"`;
25+
exports[`AutoBundler01 should support main script from package.json 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"id\\":\\"testBundleName\\",\\"bundleMajorVer\\":1,\\"bundleMinorVer\\":0,\\"bundleMicroVer\\":0,\\"define\\":[{\\"name\\":\\"testBundleName\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/testBundleName.nodejsapp\\"}]}}"`;
2626

2727
exports[`AutoBundler01 should tolerate an almost empty package.json 1`] = `"No startscript value set for NODEJSAPP \\"almostEmpty\\""`;
2828

__tests__/api/BundleContent/__snapshots__/BundleSimple.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Bundle01 add a NODEJSAPP 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"define\\":[{\\"name\\":\\"NodeName\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/NodeName.nodejsapp\\"}]}}"`;
3+
exports[`Bundle01 add a NODEJSAPP 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"define\\":[{\\"name\\":\\"NodeName\\",\\"type\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\",\\"path\\":\\"nodejsapps/NodeName.nodejsapp\\"}]}}"`;
44

55
exports[`Bundle01 add a NODEJSAPP 2`] = `
66
"<manifest xmlns=\\"http://www.ibm.com/xmlns/prod/cics/bundle\\"><define name=\\"NodeName\\" type=\\"http://www.ibm.com/xmlns/prod/cics/bundle/NODEJSAPP\\" path=\\"nodejsapps/NodeName.nodejsapp\\"></define></manifest>
@@ -25,4 +25,4 @@ exports[`Bundle01 should read an existing bundle 1`] = `"{\\"manifest\\":{\\"xml
2525
2626
exports[`Bundle01 should warn that an existing bundle cant be overwritten 1`] = `"A bundle manifest file already exists. Specify --overwrite to replace it, or --merge to merge changes into it."`;
2727
28-
exports[`Bundle01 tolerate an existing almost empty manifest 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"$t\\":\\"\\",\\"define\\":[{\\"name\\":\\"name1\\",\\"type\\":\\"type1\\",\\"path\\":\\"Artefact1\\"}]}}"`;
28+
exports[`Bundle01 tolerate an existing almost empty manifest 1`] = `"{\\"manifest\\":{\\"xmlns\\":\\"http://www.ibm.com/xmlns/prod/cics/bundle\\",\\"define\\":[{\\"name\\":\\"name1\\",\\"type\\":\\"type1\\",\\"path\\":\\"Artefact1\\"}]}}"`;

__tests__/api/BundlePush/BundlePusher.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ let zosmfProfile = {};
7474
let sshProfile = {};
7575
let cicsProfile = {};
7676

77-
// Initialise xml2json before mocking anything
78-
const parser = require("xml2json");
79-
8077
let zosMFSpy = jest.spyOn(ZosmfSession, "createBasicZosmfSession").mockImplementation(() => ({}));
8178
let sshSpy = jest.spyOn(SshSession, "createBasicSshSession").mockImplementation(() => ({}));
8279
let createSpy = jest.spyOn(Create, "uss").mockImplementation(() => ({}));
@@ -185,7 +182,7 @@ describe("BundlePusher01", () => {
185182
existsSpy.mockReturnValue(true);
186183
readSpy.mockImplementation((data: string) => ("wibble"));
187184
await runPushTestWithError("__tests__/__resources__/BadManifestBundle01", false,
188-
"Existing CICS Manifest file found with unparsable content.");
185+
"Existing CICS Manifest file found with unparsable content:");
189186
});
190187
it("should complain with missing manifest file", async () => {
191188
readSpy.mockImplementationOnce(() => {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
"configurationModule": "lib/imperative.js"
3232
},
3333
"dependencies": {
34-
"xml2json": "^0.11.2",
34+
"fast-xml-parser": "^3.12.16",
3535
"@zowe/cics": "^2.0.1"
3636
},
3737
"devDependencies": {
3838
"@zowe/cli": "^5.5.0",
3939
"@zowe/imperative": "^4.1.2",
40-
"@zowe/cics": "^2.0.1",
4140
"@types/fs-extra": "^5.0.5",
4241
"@types/jest": "^22.2.3",
4342
"@types/node": "^8.0.28",

src/api/BundleContent/Manifest.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import { BundlePart, IBundlePartDataType } from "./BundlePart";
1515
import { IHandlerParameters } from "@zowe/imperative";
1616

17+
import * as parser from "fast-xml-parser";
18+
19+
const serialiser = new parser.j2xParser({ignoreAttributes: false, attributeNamePrefix: ""});
20+
1721
/**
1822
* Interface to represent the manifest data for a CICS Bundle.
1923
*
@@ -221,8 +225,7 @@ export class Manifest {
221225
* @memberof Manifest
222226
*/
223227
public getXML(): string {
224-
const parser = require("xml2json");
225-
return parser.toXml(JSON.stringify(this.manifestAsJson)) + "\n";
228+
return serialiser.parse(this.manifestAsJson) + "\n";
226229
}
227230

228231
/**
@@ -385,19 +388,16 @@ export class Manifest {
385388

386389
try {
387390
// Reading the file worked, so convert the contents into a JSON Object
388-
const parser = require("xml2json");
389-
this.manifestAsJson = JSON.parse( parser.toJson( xmltext, { reversible: true } ) );
391+
this.manifestAsJson = parser.parse(xmltext, {ignoreAttributes: false, attributeNamePrefix: "", trimValues: true});
390392
}
391393
catch (exception)
392394
{
393395
throw new Error("Parsing error occurred reading a CICS manifest file: " + exception.message);
394396
}
395397

396-
// Note, the XML parser has no support for namespaces.
397-
// If the manifest is namespace prefixed then it wont be found. Yuck.
398398
// Validate that the manifest we've read is usable.
399399
if (this.manifestAsJson.manifest === undefined) {
400-
throw new Error("Existing CICS Manifest file found with unparsable content.");
400+
throw new Error("Existing CICS Manifest file found with unparsable content: " + JSON.stringify(this.manifestAsJson));
401401
}
402402

403403
// Now check the namespace

src/api/BundleContent/NodejsappBundlePart.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import { BundlePart } from "./BundlePart";
1515
import { TemplateNodejsappProfile } from "./TemplateNodejsappProfile";
1616
import { IHandlerParameters } from "@zowe/imperative";
17+
import * as parser from "fast-xml-parser";
18+
19+
const serialiser = new parser.j2xParser({ignoreAttributes: false, attributeNamePrefix: ""});
1720

1821
/**
1922
* Interface to represent the manifest data for a NODEJSAPP BundlePart.
@@ -132,8 +135,7 @@ export class NodejsappBundlePart extends BundlePart {
132135
* @memberof NodejsappBundlePart
133136
*/
134137
public getPartXML(): string {
135-
const parser = require("xml2json");
136-
return parser.toXml(JSON.stringify(this.partXML)) + "\n";
138+
return serialiser.parse(this.partXML) + "\n";
137139
}
138140

139141
/**

0 commit comments

Comments
 (0)