Skip to content

Commit

Permalink
Merge pull request #1562 from andymckay/check-files
Browse files Browse the repository at this point in the history
check that files in the background script actually exist
  • Loading branch information
Andy McKay committed Sep 28, 2017
2 parents 8555b9d + 78e96e1 commit 6bae32b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/messages/manifestjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ export function manifestIconMissing(path) {
};
}

export const MANIFEST_BACKGROUND_FILE_NOT_FOUND = 'MANIFEST_BACKGROUND_FILE_NOT_FOUND';
export function manifestBackgroundMissing(path, type) {
return {
code: MANIFEST_BACKGROUND_FILE_NOT_FOUND,
legacyCode: null,
message: type === 'script' ?
'A background script defined in the manifest could not be found.' :
'A background page defined in the manifest could not be found.',
description:
sprintf(
type === 'script' ?
_('Background script could not be found at "%(path)s".') :
_('Background page could not be found at "%(path)s".'),
{ path }
),
file: MANIFEST_JSON,
};
}

export const PROP_NAME_MISSING = manifestPropMissing('name');
export const PROP_VERSION_MISSING = manifestPropMissing('version');

Expand Down
20 changes: 20 additions & 0 deletions src/parsers/manifestjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ export default class ManifestJSONParser extends JSONParser {
this.validateIcons();
}

if (this.parsedJSON.background) {
if (this.parsedJSON.background.scripts) {
this.parsedJSON.background.scripts.forEach((script) => {
this.validateFileExistsInPackage(script, 'script');
});
}
if (this.parsedJSON.background.page) {
this.validateFileExistsInPackage(this.parsedJSON.background.page, 'page');
}
}

if (!this.selfHosted && this.parsedJSON.applications &&
this.parsedJSON.applications.gecko &&
this.parsedJSON.applications.gecko.update_url) {
Expand Down Expand Up @@ -182,6 +193,15 @@ export default class ManifestJSONParser extends JSONParser {
});
}

validateFileExistsInPackage(filePath, type) {
const _path = normalizePath(filePath);
if (!Object.prototype.hasOwnProperty.call(this.io.files, _path)) {
this.collector.addError(messages.manifestBackgroundMissing(
_path, type));
this.isValid = false;
}
}

validateCspPolicy(policy) {
const directives = parseCspPolicy(policy);

Expand Down
54 changes: 54 additions & 0 deletions tests/parsers/test.manifestjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,4 +763,58 @@ describe('ManifestJSONParser', () => {
expect(warnings[1].code).toEqual(messages.WRONG_ICON_EXTENSION.code);
});
});

describe('background', () => {
it('does not add errors if the script exists', () => {
const linter = new Linter({ _: ['bar'] });
const json = validManifestJSON({
background: { scripts: ['foo.js'] },
});
const manifestJSONParser = new ManifestJSONParser(
json, linter.collector, { io: { files: { 'foo.js': '' } } });
expect(manifestJSONParser.isValid).toBeTruthy();
});

it('does error if the script does not exist', () => {
const linter = new Linter({ _: ['bar'] });
const json = validManifestJSON({
background: { scripts: ['foo.js'] },
});
const manifestJSONParser = new ManifestJSONParser(
json, linter.collector, { io: { files: {} } });
expect(manifestJSONParser.isValid).toBeFalsy();
assertHasMatchingError(linter.collector.errors, {
code: messages.MANIFEST_BACKGROUND_FILE_NOT_FOUND,
message:
'A background script defined in the manifest could not be found.',
description: 'Background script could not be found at "foo.js".',
});
});

it('does not add errors if the page exists', () => {
const linter = new Linter({ _: ['bar'] });
const json = validManifestJSON({
background: { page: 'foo.html' },
});
const manifestJSONParser = new ManifestJSONParser(
json, linter.collector, { io: { files: { 'foo.html': '' } } });
expect(manifestJSONParser.isValid).toBeTruthy();
});

it('does error if the page does not exist', () => {
const linter = new Linter({ _: ['bar'] });
const json = validManifestJSON({
background: { page: 'foo.html' },
});
const manifestJSONParser = new ManifestJSONParser(
json, linter.collector, { io: { files: {} } });
expect(manifestJSONParser.isValid).toBeFalsy();
assertHasMatchingError(linter.collector.errors, {
code: messages.MANIFEST_BACKGROUND_FILE_NOT_FOUND,
message:
'A background page defined in the manifest could not be found.',
description: 'Background page could not be found at "foo.html".',
});
});
});
});

0 comments on commit 6bae32b

Please sign in to comment.