Skip to content
Snippets Groups Projects
Commit 442b1ae5 authored by rnystrom@google.com's avatar rnystrom@google.com
Browse files

Validate that the homepage is using an approved scheme.

Also cleaned up the pubspec tests a bit.

Review URL: https://codereview.chromium.org//11412017

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge@14984 260f80e4-7a28-3924-810f-c04153c831b5
parent 63d82812
No related branches found
No related tags found
No related merge requests found
......@@ -76,12 +76,28 @@ class Pubspec {
// Even though the pub app itself doesn't use these fields, we validate
// them here so that users find errors early before they try to upload to
// the server:
// TODO(rnystrom): We should split this validation into separate layers:
// 1. Stuff that is required in any pubspec to perform any command. Things
// like "must have a name". That should go here.
// 2. Stuff that is required to upload a package. Things like "homepage
// must use a valid scheme". That should go elsewhere. pub upload should
// call it, and we should provide a separate command to show the user,
// and also expose it to the editor in some way.
if (parsedPubspec.containsKey('homepage')) {
var homepage = parsedPubspec['homepage'];
if (homepage is! String) {
throw new FormatException(
'The "homepage" field should be a string, but was "$homepage".');
}
if (parsedPubspec.containsKey('homepage') &&
parsedPubspec['homepage'] is! String) {
throw new FormatException(
'The "homepage" field should be a string, but was '
'${parsedPubspec["homepage"]}.');
var goodScheme = new RegExp(r'^https?:');
if (!goodScheme.hasMatch(homepage)) {
throw new FormatException(
'The "homepage" field should be an "http:" or "https:" URL, but '
'was "$homepage".');
}
}
if (parsedPubspec.containsKey('author') &&
......
......@@ -26,6 +26,11 @@ main() {
var sources = new SourceRegistry();
sources.register(new MockSource());
expectFormatError(String pubspec) {
expect(() => new Pubspec.parse(pubspec, sources),
throwsFormatException);
}
test("allows a version constraint for dependencies", () {
var pubspec = new Pubspec.parse('''
dependencies:
......@@ -50,55 +55,51 @@ dependencies:
});
test("throws if the description isn't valid", () {
expect(() {
new Pubspec.parse('''
expectFormatError('''
dependencies:
foo:
mock: bad
''', sources);
}, throwsFormatException);
''');
});
test("throws if 'name' is not a string", () {
expect(() => new Pubspec.parse('name: [not, a, string]', sources),
throwsFormatException);
expectFormatError('name: [not, a, string]');
});
test("throws if 'homepage' is not a string", () {
expect(() => new Pubspec.parse('homepage: [not, a, string]', sources),
throwsFormatException);
expectFormatError('homepage:');
expectFormatError('homepage: [not, a, string]');
});
test("throws if 'homepage' doesn't have an HTTP scheme", () {
new Pubspec.parse('homepage: http://ok.com', sources);
new Pubspec.parse('homepage: https://also-ok.com', sources);
expectFormatError('ftp://badscheme.com');
expectFormatError('javascript:alert("!!!")');
expectFormatError('data:image/png;base64,somedata');
expectFormatError('homepage: no-scheme.com');
});
test("throws if 'authors' is not a string or a list of strings", () {
new Pubspec.parse('authors: ok fine', sources);
new Pubspec.parse('authors: [also, ok, fine]', sources);
expect(() => new Pubspec.parse('authors: 123', sources),
throwsFormatException);
expect(() => new Pubspec.parse('authors: {not: {a: string}}', sources),
throwsFormatException);
expect(() => new Pubspec.parse('authors: [ok, {not: ok}]', sources),
throwsFormatException);
expectFormatError('authors: 123');
expectFormatError('authors: {not: {a: string}}');
expectFormatError('authors: [ok, {not: ok}]');
});
test("throws if 'author' is not a string", () {
new Pubspec.parse('author: ok fine', sources);
expect(() => new Pubspec.parse('author: 123', sources),
throwsFormatException);
expect(() => new Pubspec.parse('author: {not: {a: string}}', sources),
throwsFormatException);
expect(() => new Pubspec.parse('author: [not, ok]', sources),
throwsFormatException);
expectFormatError('author: 123');
expectFormatError('author: {not: {a: string}}');
expectFormatError('author: [not, ok]');
});
test("throws if both 'author' and 'authors' are present", () {
expect(() => new Pubspec.parse('{author: abe, authors: ted}', sources),
throwsFormatException);
expectFormatError('{author: abe, authors: ted}');
});
test("allows comment-only files", () {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment