Skip to content
Closed
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
38 changes: 38 additions & 0 deletions src/parsers/installrdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import * as messages from 'messages';
import log from 'logger';
import { singleLineString } from 'utils';

const TARGET_APPLICATION_FIELDS = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move to const and add something to the name to reflect this is to do with RDF?

'id', 'minVersion', 'maxVersion',
];

export default class InstallRdfParser {

Expand All @@ -21,6 +24,7 @@ export default class InstallRdfParser {
type: this._getAddonType(),
version: this._getVersion(),
restartless: this._getIsBootstrapped(),
application: this._getApplication(),
});
}

Expand Down Expand Up @@ -105,6 +109,40 @@ export default class InstallRdfParser {
this._getTopLevelNodeByTag('em:bootstrap')) === 'true';
}

_getApplication() {
var targetApplications = this._getTopLevelNodesByTag(
'em:targetApplication');
var applications = [];

for (let targetApplication of targetApplications) {
let application = {};
let descriptionNodes = Array.prototype.slice.call(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anywhere you're doing this you can use this._makeArray I see there's a couple of other places that could be used in this file too.

targetApplication.childNodes)
.filter((node) => node.nodeName === 'Description');

if (descriptionNodes.length !== 1) {
throw new Error(singleLineString`<em:targetApplication> node should only
have a single descendant <Description>`);
}

for (var i in descriptionNodes[0].childNodes) {
let childNode = descriptionNodes[0].childNodes[i];
if (TARGET_APPLICATION_FIELDS.indexOf(childNode.localName) !== -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application[childNode.localName] = this._getNodeValue(childNode);
}
}

if (application.id && application.maxVersion && application.minVersion) {
applications.push(application);
} else {
throw new Error(singleLineString`targetApplication must contain an id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be an error message pushed onto the collector?

Also I think doing something a little more granular will help developers e.g. flag each field that's invalid separately so that it's clearer when just one thing is broken.

minVersion and maxVersion fields to be valid.`);
}
}

return applications;
}

_getGUID() {
// Install.rdf only.
var guid = this._getNodeValue(this._getTopLevelNodeByTag('em:id'));
Expand Down
5 changes: 5 additions & 0 deletions src/parsers/manifestjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ export default class ManifestJSONParser {
type: this._getType(),
version: this._getVersion(),
restartless: true, // All web extensions are restartless
application: this._getApplication(),
});
}

_getApplication() {
return this.parsedJSON.applications;
}

_getType() {
return PACKAGE_EXTENSION;
}
Expand Down
108 changes: 108 additions & 0 deletions tests/parsers/test.installrdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,114 @@ describe('InstallRdfParser._getIsBootstrapped()', () => {
});
});

describe('InstallRdfParser._getApplication()', () => {
it('should retrieve the application info', () => {
var id = '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}';
var minVersion = '1.5';
var maxVersion = '3.0.*';
var rdf = validRDF(`<em:targetApplication>
<Description>
<em:id>${id}</em:id> <!--Firefox-->
<em:minVersion>${minVersion}</em:minVersion>
<em:maxVersion>${maxVersion}</em:maxVersion>
</Description>
</em:targetApplication>`);
var rdfScanner = new RDFScanner(rdf, INSTALL_RDF);
return rdfScanner.getContents()
.then((xmlDoc) => {
var installRdfParser = new InstallRdfParser(xmlDoc);
return installRdfParser._getApplication();
})
.then((applications) => {
assert.equal(applications[0].id, id);
assert.equal(applications[0].minVersion, minVersion);
assert.equal(applications[0].maxVersion, maxVersion);
});
});

it('should retrieve the application info', () => {
var expectedApplications = [{
id: '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}',
minVersion: '1.5',
maxVersion: '3.0.*',
}, {
id: '{abcdefgh-c20a-464f-9b0e-13a3a9e97384}',
minVersion: '2.5',
maxVersion: '4.0.*',
},
];
var rdf = validRDF(`<em:targetApplication>
<Description>
<em:id>${expectedApplications[0].id}</em:id> <!--Firefox-->
<em:minVersion>${expectedApplications[0].minVersion}</em:minVersion>
<em:maxVersion>${expectedApplications[0].maxVersion}</em:maxVersion>
</Description>
</em:targetApplication>
<em:targetApplication>
<Description>
<em:id>${expectedApplications[1].id}</em:id> <!--Firefox-->
<em:minVersion>${expectedApplications[1].minVersion}</em:minVersion>
<em:maxVersion>${expectedApplications[1].maxVersion}</em:maxVersion>
</Description>
</em:targetApplication>`);
var rdfScanner = new RDFScanner(rdf, INSTALL_RDF);
return rdfScanner.getContents()
.then((xmlDoc) => {
var installRdfParser = new InstallRdfParser(xmlDoc);
return installRdfParser._getApplication();
})
.then((applications) => {
for (var i = 0; i < applications.length; i++) {
assert.equal(applications[i].id, expectedApplications[i].id);
assert.equal(applications[i].minVersion,
expectedApplications[i].minVersion);
assert.equal(applications[i].maxVersion,
expectedApplications[i].maxVersion);
}
});
});

it('should reject on missing Description node', () => {
var rdf = validRDF(
`<em:targetApplication><foo></foo></em:targetApplication>`);
var rdfScanner = new RDFScanner(rdf, INSTALL_RDF);
return rdfScanner.getContents()
.then((xmlDoc) => {
var installRdfParser = new InstallRdfParser(xmlDoc);
return installRdfParser._getApplication();
})
.then(unexpectedSuccess)
.catch((err) => {
assert.equal(err.message,
singleLineString`<em:targetApplication> node should only have a
single descendant <Description>`);
});
});

it('should reject on missing id node', () => {
var minVersion = '1.5';
var maxVersion = '3.0.*';
var rdf = validRDF(`<em:targetApplication>
<Description>
<em:minVersion>${minVersion}</em:minVersion>
<em:maxVersion>${maxVersion}</em:maxVersion>
</Description>
</em:targetApplication>`);
var rdfScanner = new RDFScanner(rdf, INSTALL_RDF);
return rdfScanner.getContents()
.then((xmlDoc) => {
var installRdfParser = new InstallRdfParser(xmlDoc);
return installRdfParser._getApplication();
})
.then(unexpectedSuccess)
.catch((err) => {
assert.equal(err.message,
singleLineString`targetApplication must contain an id,
minVersion and maxVersion fields to be valid.`);
});
});
});

describe('InstallRdfParser._getDescriptionNode()', function() {

it('should reject on missing RDF node', () => {
Expand Down