OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pubspec_test; | 5 library pubspec_test; |
6 | 6 |
7 import '../../../pkg/unittest/lib/unittest.dart'; | 7 import '../../../pkg/unittest/lib/unittest.dart'; |
8 import '../../pub/pubspec.dart'; | 8 import '../../pub/pubspec.dart'; |
9 import '../../pub/source.dart'; | 9 import '../../pub/source.dart'; |
10 import '../../pub/source_registry.dart'; | 10 import '../../pub/source_registry.dart'; |
11 import '../../pub/utils.dart'; | 11 import '../../pub/utils.dart'; |
12 import '../../pub/version.dart'; | 12 import '../../pub/version.dart'; |
13 | 13 |
14 class MockSource extends Source { | 14 class MockSource extends Source { |
15 final String name = "mock"; | 15 final String name = "mock"; |
16 final bool shouldCache = false; | 16 final bool shouldCache = false; |
17 void validateDescription(description, {bool fromLockFile: false}) { | 17 void validateDescription(description, {bool fromLockFile: false}) { |
18 if (description != 'ok') throw new FormatException('Bad'); | 18 if (description != 'ok') throw new FormatException('Bad'); |
19 } | 19 } |
20 String packageName(description) => 'foo'; | 20 String packageName(description) => 'foo'; |
21 } | 21 } |
22 | 22 |
23 main() { | 23 main() { |
24 group('Pubspec', () { | 24 group('Pubspec', () { |
25 group('parse()', () { | 25 group('parse()', () { |
26 var sources = new SourceRegistry(); | 26 var sources = new SourceRegistry(); |
27 sources.register(new MockSource()); | 27 sources.register(new MockSource()); |
28 | 28 |
| 29 expectFormatError(String pubspec) { |
| 30 expect(() => new Pubspec.parse(pubspec, sources), |
| 31 throwsFormatException); |
| 32 } |
| 33 |
29 test("allows a version constraint for dependencies", () { | 34 test("allows a version constraint for dependencies", () { |
30 var pubspec = new Pubspec.parse(''' | 35 var pubspec = new Pubspec.parse(''' |
31 dependencies: | 36 dependencies: |
32 foo: | 37 foo: |
33 mock: ok | 38 mock: ok |
34 version: ">=1.2.3 <3.4.5" | 39 version: ">=1.2.3 <3.4.5" |
35 ''', sources); | 40 ''', sources); |
36 | 41 |
37 var foo = pubspec.dependencies[0]; | 42 var foo = pubspec.dependencies[0]; |
38 expect(foo.name, equals('foo')); | 43 expect(foo.name, equals('foo')); |
39 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); | 44 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); |
40 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); | 45 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); |
41 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); | 46 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); |
42 }); | 47 }); |
43 | 48 |
44 test("allows an empty dependencies map", () { | 49 test("allows an empty dependencies map", () { |
45 var pubspec = new Pubspec.parse(''' | 50 var pubspec = new Pubspec.parse(''' |
46 dependencies: | 51 dependencies: |
47 ''', sources); | 52 ''', sources); |
48 | 53 |
49 expect(pubspec.dependencies, isEmpty); | 54 expect(pubspec.dependencies, isEmpty); |
50 }); | 55 }); |
51 | 56 |
52 test("throws if the description isn't valid", () { | 57 test("throws if the description isn't valid", () { |
53 expect(() { | 58 expectFormatError(''' |
54 new Pubspec.parse(''' | |
55 dependencies: | 59 dependencies: |
56 foo: | 60 foo: |
57 mock: bad | 61 mock: bad |
58 ''', sources); | 62 '''); |
59 }, throwsFormatException); | |
60 }); | 63 }); |
61 | 64 |
62 test("throws if 'name' is not a string", () { | 65 test("throws if 'name' is not a string", () { |
63 expect(() => new Pubspec.parse('name: [not, a, string]', sources), | 66 expectFormatError('name: [not, a, string]'); |
64 throwsFormatException); | |
65 }); | 67 }); |
66 | 68 |
67 test("throws if 'homepage' is not a string", () { | 69 test("throws if 'homepage' is not a string", () { |
68 expect(() => new Pubspec.parse('homepage: [not, a, string]', sources), | 70 expectFormatError('homepage:'); |
69 throwsFormatException); | 71 expectFormatError('homepage: [not, a, string]'); |
| 72 }); |
| 73 |
| 74 test("throws if 'homepage' doesn't have an HTTP scheme", () { |
| 75 new Pubspec.parse('homepage: http://ok.com', sources); |
| 76 new Pubspec.parse('homepage: https://also-ok.com', sources); |
| 77 |
| 78 expectFormatError('ftp://badscheme.com'); |
| 79 expectFormatError('javascript:alert("!!!")'); |
| 80 expectFormatError('data:image/png;base64,somedata'); |
| 81 expectFormatError('homepage: no-scheme.com'); |
70 }); | 82 }); |
71 | 83 |
72 test("throws if 'authors' is not a string or a list of strings", () { | 84 test("throws if 'authors' is not a string or a list of strings", () { |
73 new Pubspec.parse('authors: ok fine', sources); | 85 new Pubspec.parse('authors: ok fine', sources); |
74 new Pubspec.parse('authors: [also, ok, fine]', sources); | 86 new Pubspec.parse('authors: [also, ok, fine]', sources); |
75 | 87 |
76 expect(() => new Pubspec.parse('authors: 123', sources), | 88 expectFormatError('authors: 123'); |
77 throwsFormatException); | 89 expectFormatError('authors: {not: {a: string}}'); |
78 | 90 expectFormatError('authors: [ok, {not: ok}]'); |
79 expect(() => new Pubspec.parse('authors: {not: {a: string}}', sources), | |
80 throwsFormatException); | |
81 | |
82 expect(() => new Pubspec.parse('authors: [ok, {not: ok}]', sources), | |
83 throwsFormatException); | |
84 }); | 91 }); |
85 | 92 |
86 test("throws if 'author' is not a string", () { | 93 test("throws if 'author' is not a string", () { |
87 new Pubspec.parse('author: ok fine', sources); | 94 new Pubspec.parse('author: ok fine', sources); |
88 | 95 |
89 expect(() => new Pubspec.parse('author: 123', sources), | 96 expectFormatError('author: 123'); |
90 throwsFormatException); | 97 expectFormatError('author: {not: {a: string}}'); |
91 | 98 expectFormatError('author: [not, ok]'); |
92 expect(() => new Pubspec.parse('author: {not: {a: string}}', sources), | |
93 throwsFormatException); | |
94 | |
95 expect(() => new Pubspec.parse('author: [not, ok]', sources), | |
96 throwsFormatException); | |
97 }); | 99 }); |
98 | 100 |
99 test("throws if both 'author' and 'authors' are present", () { | 101 test("throws if both 'author' and 'authors' are present", () { |
100 expect(() => new Pubspec.parse('{author: abe, authors: ted}', sources), | 102 expectFormatError('{author: abe, authors: ted}'); |
101 throwsFormatException); | |
102 }); | 103 }); |
103 | 104 |
104 test("allows comment-only files", () { | 105 test("allows comment-only files", () { |
105 var pubspec = new Pubspec.parse(''' | 106 var pubspec = new Pubspec.parse(''' |
106 # No external dependencies yet | 107 # No external dependencies yet |
107 # Including for completeness | 108 # Including for completeness |
108 # ...and hoping the spec expands to include details about author, version, etc | 109 # ...and hoping the spec expands to include details about author, version, etc |
109 # See http://www.dartlang.org/docs/pub-package-manager/ for details | 110 # See http://www.dartlang.org/docs/pub-package-manager/ for details |
110 ''', sources); | 111 ''', sources); |
111 expect(pubspec.version, equals(Version.none)); | 112 expect(pubspec.version, equals(Version.none)); |
112 expect(pubspec.dependencies, isEmpty); | 113 expect(pubspec.dependencies, isEmpty); |
113 }); | 114 }); |
114 }); | 115 }); |
115 }); | 116 }); |
116 } | 117 } |
OLD | NEW |