Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: sdk/lib/_internal/pub/test/pubspec_test.dart

Issue 24246002: Lazily throw pubspec parsing exceptions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 'package:unittest/unittest.dart'; 7 import 'package:unittest/unittest.dart';
8 8
9 import '../lib/src/pubspec.dart'; 9 import '../lib/src/pubspec.dart';
10 import '../lib/src/source.dart'; 10 import '../lib/src/source.dart';
(...skipping 11 matching lines...) Expand all
22 } 22 }
23 String packageName(description) => 'foo'; 23 String packageName(description) => 'foo';
24 } 24 }
25 25
26 main() { 26 main() {
27 initConfig(); 27 initConfig();
28 group('parse()', () { 28 group('parse()', () {
29 var sources = new SourceRegistry(); 29 var sources = new SourceRegistry();
30 sources.register(new MockSource()); 30 sources.register(new MockSource());
31 31
32 expectFormatError(String pubspec) { 32 var throwsPubspecException =
33 expect(() => new Pubspec.parse(null, pubspec, sources), 33 throwsA(new isInstanceOf<PubspecException>('PubspecException'));
34 throwsFormatException); 34
35 expectPubspecException(String contents, fn(Pubspec pubspec)) {
36 var pubspec = new Pubspec.parse(contents, sources);
37 expect(() => fn(pubspec), throwsPubspecException);
35 } 38 }
36 39
40 test("doesn't eagerly throw an error for an invalid field", () {
41 // Shouldn't throw an error.
42 new Pubspec.parse('version: not a semver', sources);
43 });
44
45 test("eagerly throws an error if the pubspec name doesn't match the "
46 "expected name", () {
47 expect(() => new Pubspec.parse("name: foo", sources, expectedName: 'bar'),
48 throwsPubspecException);
49 });
50
51 test("eagerly throws an error if the pubspec doesn't have a name and an "
52 "expected name is passed", () {
53 expect(() => new Pubspec.parse("{}", sources, expectedName: 'bar'),
54 throwsPubspecException);
55 });
56
37 test("allows a version constraint for dependencies", () { 57 test("allows a version constraint for dependencies", () {
38 var pubspec = new Pubspec.parse(null, ''' 58 var pubspec = new Pubspec.parse('''
39 dependencies: 59 dependencies:
40 foo: 60 foo:
41 mock: ok 61 mock: ok
42 version: ">=1.2.3 <3.4.5" 62 version: ">=1.2.3 <3.4.5"
43 ''', sources); 63 ''', sources);
44 64
45 var foo = pubspec.dependencies[0]; 65 var foo = pubspec.dependencies[0];
46 expect(foo.name, equals('foo')); 66 expect(foo.name, equals('foo'));
47 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); 67 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue);
48 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); 68 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue);
49 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); 69 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse);
50 }); 70 });
51 71
52 test("allows an empty dependencies map", () { 72 test("allows an empty dependencies map", () {
53 var pubspec = new Pubspec.parse(null, ''' 73 var pubspec = new Pubspec.parse('''
54 dependencies: 74 dependencies:
55 ''', sources); 75 ''', sources);
56 76
57 expect(pubspec.dependencies, isEmpty); 77 expect(pubspec.dependencies, isEmpty);
58 }); 78 });
59 79
60 test("allows a version constraint for dev dependencies", () { 80 test("allows a version constraint for dev dependencies", () {
61 var pubspec = new Pubspec.parse(null, ''' 81 var pubspec = new Pubspec.parse('''
62 dev_dependencies: 82 dev_dependencies:
63 foo: 83 foo:
64 mock: ok 84 mock: ok
65 version: ">=1.2.3 <3.4.5" 85 version: ">=1.2.3 <3.4.5"
66 ''', sources); 86 ''', sources);
67 87
68 var foo = pubspec.devDependencies[0]; 88 var foo = pubspec.devDependencies[0];
69 expect(foo.name, equals('foo')); 89 expect(foo.name, equals('foo'));
70 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); 90 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue);
71 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); 91 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue);
72 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); 92 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse);
73 }); 93 });
74 94
75 test("allows an empty dev dependencies map", () { 95 test("allows an empty dev dependencies map", () {
76 var pubspec = new Pubspec.parse(null, ''' 96 var pubspec = new Pubspec.parse('''
77 dev_dependencies: 97 dev_dependencies:
78 ''', sources); 98 ''', sources);
79 99
80 expect(pubspec.devDependencies, isEmpty); 100 expect(pubspec.devDependencies, isEmpty);
81 }); 101 });
82 102
83 test("allows an unknown source", () { 103 test("allows an unknown source", () {
84 var pubspec = new Pubspec.parse(null, ''' 104 var pubspec = new Pubspec.parse('''
85 dependencies: 105 dependencies:
86 foo: 106 foo:
87 unknown: blah 107 unknown: blah
88 ''', sources); 108 ''', sources);
89 109
90 var foo = pubspec.dependencies[0]; 110 var foo = pubspec.dependencies[0];
91 expect(foo.name, equals('foo')); 111 expect(foo.name, equals('foo'));
92 expect(foo.source, equals('unknown')); 112 expect(foo.source, equals('unknown'));
93 }); 113 });
94 114
95 test("throws if a package is in dependencies and dev_dependencies", () { 115 test("throws if a package is in dependencies and dev_dependencies", () {
96 expectFormatError(''' 116 var contents = '''
97 dependencies: 117 dependencies:
98 foo: 118 foo:
99 mock: ok 119 mock: ok
100 dev_dependencies: 120 dev_dependencies:
101 foo: 121 foo:
102 mock: ok 122 mock: ok
103 '''); 123 ''';
124 expectPubspecException(contents, (pubspec) => pubspec.dependencies);
125 expectPubspecException(contents, (pubspec) => pubspec.devDependencies);
104 }); 126 });
105 127
106 test("throws if it dependes on itself", () { 128 test("throws if it dependes on itself", () {
107 expectFormatError(''' 129 expectPubspecException('''
108 name: myapp 130 name: myapp
109 dependencies: 131 dependencies:
110 myapp: 132 myapp:
111 mock: ok 133 mock: ok
112 '''); 134 ''', (pubspec) => pubspec.dependencies);
113 }); 135 });
114 136
115 test("throws if it has a dev dependency on itself", () { 137 test("throws if it has a dev dependency on itself", () {
116 expectFormatError(''' 138 expectPubspecException('''
117 name: myapp 139 name: myapp
118 dev_dependencies: 140 dev_dependencies:
119 myapp: 141 myapp:
120 mock: ok 142 mock: ok
121 '''); 143 ''', (pubspec) => pubspec.devDependencies);
122 }); 144 });
123 145
124 test("throws if the description isn't valid", () { 146 test("throws if the description isn't valid", () {
125 expectFormatError(''' 147 expectPubspecException('''
126 dependencies: 148 dependencies:
127 foo: 149 foo:
128 mock: bad 150 mock: bad
129 '''); 151 ''', (pubspec) => pubspec.dependencies);
130 }); 152 });
131 153
132 test("throws if dependency version is not a string", () { 154 test("throws if dependency version is not a string", () {
133 expectFormatError(''' 155 expectPubspecException('''
134 dependencies: 156 dependencies:
135 foo: 157 foo:
136 mock: ok 158 mock: ok
137 version: 1.2 159 version: 1.2
138 '''); 160 ''', (pubspec) => pubspec.dependencies);
139 }); 161 });
140 162
141 test("throws if version is not a version constraint", () { 163 test("throws if version is not a version constraint", () {
142 expectFormatError(''' 164 expectPubspecException('''
143 dependencies: 165 dependencies:
144 foo: 166 foo:
145 mock: ok 167 mock: ok
146 version: not constraint 168 version: not constraint
147 '''); 169 ''', (pubspec) => pubspec.dependencies);
148 }); 170 });
149 171
150 test("throws if 'name' is not a string", () { 172 test("throws if 'name' is not a string", () {
151 expectFormatError('name: [not, a, string]'); 173 expectPubspecException('name: [not, a, string]',
174 (pubspec) => pubspec.name);
152 }); 175 });
153 176
154 test("throws if version is not a string", () { 177 test("throws if version is not a string", () {
155 expectFormatError(''' 178 expectPubspecException('version: 1.0', (pubspec) => pubspec.version);
156 version: 1.0
157 ''');
158 }); 179 });
159 180
160 test("throws if version is not a version", () { 181 test("throws if version is not a version", () {
161 expectFormatError(''' 182 expectPubspecException('version: not version',
162 version: not version 183 (pubspec) => pubspec.version);
163 ''');
164 });
165
166 test("throws if 'homepage' is not a string", () {
167 expectFormatError('homepage:');
168 expectFormatError('homepage: [not, a, string]');
169 });
170
171 test("throws if 'homepage' doesn't have an HTTP scheme", () {
172 new Pubspec.parse(null, 'homepage: http://ok.com', sources);
173 new Pubspec.parse(null, 'homepage: https://also-ok.com', sources);
174
175 expectFormatError('homepage: ftp://badscheme.com');
176 expectFormatError('homepage: javascript:alert("!!!")');
177 expectFormatError('homepage: data:image/png;base64,somedata');
178 expectFormatError('homepage: no-scheme.com');
179 });
180
181 test("throws if 'documentation' is not a string", () {
182 expectFormatError('documentation:');
183 expectFormatError('documentation: [not, a, string]');
184 });
185
186 test("throws if 'documentation' doesn't have an HTTP scheme", () {
187 new Pubspec.parse(null, 'documentation: http://ok.com', sources);
188 new Pubspec.parse(null, 'documentation: https://also-ok.com', sources);
189
190 expectFormatError('documentation: ftp://badscheme.com');
191 expectFormatError('documentation: javascript:alert("!!!")');
192 expectFormatError('documentation: data:image/png;base64,somedata');
193 expectFormatError('documentation: no-scheme.com');
194 });
195
196 test("throws if 'authors' is not a string or a list of strings", () {
197 new Pubspec.parse(null, 'authors: ok fine', sources);
198 new Pubspec.parse(null, 'authors: [also, ok, fine]', sources);
199
200 expectFormatError('authors: 123');
201 expectFormatError('authors: {not: {a: string}}');
202 expectFormatError('authors: [ok, {not: ok}]');
203 });
204
205 test("throws if 'author' is not a string", () {
206 new Pubspec.parse(null, 'author: ok fine', sources);
207
208 expectFormatError('author: 123');
209 expectFormatError('author: {not: {a: string}}');
210 expectFormatError('author: [not, ok]');
211 });
212
213 test("throws if both 'author' and 'authors' are present", () {
214 expectFormatError('{author: abe, authors: ted}');
Bob Nystrom 2013/09/23 17:11:22 Can you move all of these tests over to validator?
nweiz 2013/09/23 22:06:10 Done.
215 }); 184 });
216 185
217 test("throws if a transformer isn't a string or map", () { 186 test("throws if a transformer isn't a string or map", () {
218 expectFormatError('{transformers: 12}'); 187 expectPubspecException('transformers: 12',
219 expectFormatError('{transformers: [12]}'); 188 (pubspec) => pubspec.transformers);
189 expectPubspecException('transformers: [12]',
190 (pubspec) => pubspec.transformers);
220 }); 191 });
221 192
222 test("throws if a transformer's configuration isn't a map", () { 193 test("throws if a transformer's configuration isn't a map", () {
223 expectFormatError('{transformers: {pkg: 12}}'); 194 expectPubspecException('transformers: {pkg: 12}',
195 (pubspec) => pubspec.transformers);
224 }); 196 });
225 197
226 test("allows comment-only files", () { 198 test("allows comment-only files", () {
227 var pubspec = new Pubspec.parse(null, ''' 199 var pubspec = new Pubspec.parse('''
228 # No external dependencies yet 200 # No external dependencies yet
229 # Including for completeness 201 # Including for completeness
230 # ...and hoping the spec expands to include details about author, version, etc 202 # ...and hoping the spec expands to include details about author, version, etc
231 # See http://www.dartlang.org/docs/pub-package-manager/ for details 203 # See http://www.dartlang.org/docs/pub-package-manager/ for details
232 ''', sources); 204 ''', sources);
233 expect(pubspec.version, equals(Version.none)); 205 expect(pubspec.version, equals(Version.none));
234 expect(pubspec.dependencies, isEmpty); 206 expect(pubspec.dependencies, isEmpty);
235 }); 207 });
236 208
237 group("environment", () { 209 group("environment", () {
238 test("defaults to any SDK constraint if environment is omitted", () { 210 test("defaults to any SDK constraint if environment is omitted", () {
239 var pubspec = new Pubspec.parse(null, '', sources); 211 var pubspec = new Pubspec.parse('', sources);
240 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any)); 212 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any));
241 }); 213 });
242 214
243 test("allows an empty environment map", () { 215 test("allows an empty environment map", () {
244 var pubspec = new Pubspec.parse(null, ''' 216 var pubspec = new Pubspec.parse('''
245 environment: 217 environment:
246 ''', sources); 218 ''', sources);
247 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any)); 219 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any));
248 }); 220 });
249 221
250 test("throws if the environment value isn't a map", () { 222 test("throws if the environment value isn't a map", () {
251 expectFormatError(''' 223 expectPubspecException('environment: []',
252 environment: [] 224 (pubspec) => pubspec.environment);
253 ''');
254 }); 225 });
255 226
256 test("allows a version constraint for the sdk", () { 227 test("allows a version constraint for the sdk", () {
257 var pubspec = new Pubspec.parse(null, ''' 228 var pubspec = new Pubspec.parse('''
258 environment: 229 environment:
259 sdk: ">=1.2.3 <2.3.4" 230 sdk: ">=1.2.3 <2.3.4"
260 ''', sources); 231 ''', sources);
261 expect(pubspec.environment.sdkVersion, 232 expect(pubspec.environment.sdkVersion,
262 equals(new VersionConstraint.parse(">=1.2.3 <2.3.4"))); 233 equals(new VersionConstraint.parse(">=1.2.3 <2.3.4")));
263 }); 234 });
264 235
265 test("throws if the sdk isn't a string", () { 236 test("throws if the sdk isn't a string", () {
266 expectFormatError(''' 237 expectPubspecException('environment: {sdk: []}',
267 environment: 238 (pubspec) => pubspec.environment);
268 sdk: [] 239 expectPubspecException('environment: {sdk: 1.0}',
269 '''); 240 (pubspec) => pubspec.environment);
270 }); 241 });
271 242
272 test("throws if the sdk is not a string", () {
273 expectFormatError('''
274 environment:
275 sdk: 1.0
276 ''');
277 });
278
279 test("throws if the sdk isn't a valid version constraint", () { 243 test("throws if the sdk isn't a valid version constraint", () {
280 expectFormatError(''' 244 expectPubspecException('environment: {sdk: "oopies"}',
281 environment: 245 (pubspec) => pubspec.environment);
282 sdk: "oopies"
283 ''');
284 }); 246 });
285 }); 247 });
286 }); 248 });
287 } 249 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698