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

Side by Side Diff: utils/tests/pub/validator_test.dart

Issue 11635060: Add a validator for dependency version constraints. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years 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 validator_test; 5 library validator_test;
6 6
7 import 'dart:io'; 7 import 'dart:io';
8 import 'dart:json'; 8 import 'dart:json';
9 9
10 import 'test_pub.dart'; 10 import 'test_pub.dart';
11 import '../../../pkg/unittest/lib/unittest.dart'; 11 import '../../../pkg/unittest/lib/unittest.dart';
12 import '../../../pkg/http/lib/http.dart' as http;
13 import '../../../pkg/http/lib/testing.dart';
12 import '../../pub/entrypoint.dart'; 14 import '../../pub/entrypoint.dart';
13 import '../../pub/io.dart'; 15 import '../../pub/io.dart';
14 import '../../pub/validator.dart'; 16 import '../../pub/validator.dart';
17 import '../../pub/validator/dependency.dart';
15 import '../../pub/validator/lib.dart'; 18 import '../../pub/validator/lib.dart';
16 import '../../pub/validator/license.dart'; 19 import '../../pub/validator/license.dart';
17 import '../../pub/validator/name.dart'; 20 import '../../pub/validator/name.dart';
18 import '../../pub/validator/pubspec_field.dart'; 21 import '../../pub/validator/pubspec_field.dart';
19 22
20 void expectNoValidationError(ValidatorCreator fn) { 23 void expectNoValidationError(ValidatorCreator fn) {
21 expectLater(schedulePackageValidation(fn), pairOf(isEmpty, isEmpty)); 24 expectLater(schedulePackageValidation(fn), pairOf(isEmpty, isEmpty));
22 } 25 }
23 26
24 void expectValidationError(ValidatorCreator fn) { 27 void expectValidationError(ValidatorCreator fn) {
25 expectLater(schedulePackageValidation(fn), pairOf(isNot(isEmpty), anything)); 28 expectLater(schedulePackageValidation(fn), pairOf(isNot(isEmpty), anything));
26 } 29 }
27 30
28 void expectValidationWarning(ValidatorCreator fn) { 31 void expectValidationWarning(ValidatorCreator fn) {
29 expectLater(schedulePackageValidation(fn), pairOf(isEmpty, isNot(isEmpty))); 32 expectLater(schedulePackageValidation(fn), pairOf(isEmpty, isNot(isEmpty)));
30 } 33 }
31 34
35 Validator dependency(Entrypoint entrypoint) =>
36 new DependencyValidator(entrypoint);
37
32 Validator lib(Entrypoint entrypoint) => new LibValidator(entrypoint); 38 Validator lib(Entrypoint entrypoint) => new LibValidator(entrypoint);
33 39
34 Validator license(Entrypoint entrypoint) => new LicenseValidator(entrypoint); 40 Validator license(Entrypoint entrypoint) => new LicenseValidator(entrypoint);
35 41
36 Validator name(Entrypoint entrypoint) => new NameValidator(entrypoint); 42 Validator name(Entrypoint entrypoint) => new NameValidator(entrypoint);
37 43
38 Validator pubspecField(Entrypoint entrypoint) => 44 Validator pubspecField(Entrypoint entrypoint) =>
39 new PubspecFieldValidator(entrypoint); 45 new PubspecFieldValidator(entrypoint);
40 46
41 void scheduleNormalPackage() => normalPackage.scheduleCreate(); 47 void scheduleNormalPackage() => normalPackage.scheduleCreate();
42 48
43 main() { 49 main() {
44 group('should consider a package valid if it', () { 50 group('should consider a package valid if it', () {
45 setUp(scheduleNormalPackage); 51 setUp(scheduleNormalPackage);
46 52
47 test('looks normal', () { 53 test('looks normal', () {
48 dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate(); 54 dir(appPath, [libPubspec("test_pkg", "1.0.0")]).scheduleCreate();
55 expectNoValidationError(dependency);
56 expectNoValidationError(lib);
49 expectNoValidationError(license); 57 expectNoValidationError(license);
58 expectNoValidationError(name);
50 expectNoValidationError(pubspecField); 59 expectNoValidationError(pubspecField);
51 run(); 60 run();
52 }); 61 });
53 62
54 test('has a COPYING file', () { 63 test('has a COPYING file', () {
55 file(join(appPath, 'LICENSE'), '').scheduleDelete(); 64 file(join(appPath, 'LICENSE'), '').scheduleDelete();
56 file(join(appPath, 'COPYING'), '').scheduleCreate(); 65 file(join(appPath, 'COPYING'), '').scheduleCreate();
57 expectNoValidationError(license); 66 expectNoValidationError(license);
58 run(); 67 run();
59 }); 68 });
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 test('has a non-Dart file in lib', () { 104 test('has a non-Dart file in lib', () {
96 dir(appPath, [ 105 dir(appPath, [
97 libPubspec("test_pkg", "1.0.0"), 106 libPubspec("test_pkg", "1.0.0"),
98 dir("lib", [ 107 dir("lib", [
99 file("thing.txt", "woo hoo") 108 file("thing.txt", "woo hoo")
100 ]) 109 ])
101 ]).scheduleCreate(); 110 ]).scheduleCreate();
102 expectNoValidationError(lib); 111 expectNoValidationError(lib);
103 run(); 112 run();
104 }); 113 });
114
115 test('has an unconstrainted dependency on "unittest"', () {
Bob Nystrom 2012/12/21 00:11:46 "unconstrainted" -> "unconstrained"
nweiz 2012/12/21 01:01:50 Done.
116 dir(appPath, [
117 libPubspec("test_pkg", "1.0.0", [
118 {'hosted': 'unittest'}
119 ])
120 ]).scheduleCreate();
121 expectNoValidationError(dependency);
122 run();
123 });
105 }); 124 });
106 125
107 group('should consider a package invalid if it', () { 126 group('should consider a package invalid if it', () {
108 setUp(scheduleNormalPackage); 127 setUp(scheduleNormalPackage);
109 128
110 test('is missing the "homepage" field', () { 129 test('is missing the "homepage" field', () {
111 var package = package("test_pkg", "1.0.0"); 130 var package = package("test_pkg", "1.0.0");
112 package.remove("homepage"); 131 package.remove("homepage");
113 dir(appPath, [pubspec(package)]).scheduleCreate(); 132 dir(appPath, [pubspec(package)]).scheduleCreate();
114 133
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 test('has a lib directory containing only src', () { 295 test('has a lib directory containing only src', () {
277 file(join(appPath, "lib", "test_pkg.dart"), '').scheduleDelete(); 296 file(join(appPath, "lib", "test_pkg.dart"), '').scheduleDelete();
278 dir(appPath, [ 297 dir(appPath, [
279 dir("lib", [ 298 dir("lib", [
280 dir("src", [file("test_pkg.dart", "int i = 0;")]) 299 dir("src", [file("test_pkg.dart", "int i = 0;")])
281 ]) 300 ])
282 ]).scheduleCreate(); 301 ]).scheduleCreate();
283 expectValidationError(lib); 302 expectValidationError(lib);
284 run(); 303 run();
285 }); 304 });
305
306 group('has a dependency with a non-hosted source', () {
307 group('where a hosted version of that dependency exists', () {
308 test("and should suggest the hosted package's primary version", () {
309 useMockClient(new MockClient((request) {
310 expect(request.method, equals("GET"));
311 expect(request.url.path, equals("/packages/foo.json"));
312
313 return new Future.immediate(new http.Response(JSON.stringify({
314 "name": "foo",
315 "uploaders": ["nweiz@google.com"],
316 "versions": ["3.0.0-pre", "2.0.0", "1.0.0"]
317 }), 200));
318 }));
319
320 dir(appPath, [
321 libPubspec("test_pkg", "1.0.0", [
322 {'git': 'git://github.com/dart-lang/foo'}
323 ])
324 ]).scheduleCreate();
325
326 expectLater(schedulePackageValidation(dependency),
327 pairOf(isEmpty, someElement(contains(
328 ' foo: ">=2.0.0 <3.0.0"'))));
329
330 run();
331 });
332
333 test("and should suggest the hosted package's prerelease version if it's "
Bob Nystrom 2012/12/21 00:11:46 Long line.
nweiz 2012/12/21 01:01:50 Done.
334 "the only version available", () {
335 useMockClient(new MockClient((request) {
336 expect(request.method, equals("GET"));
337 expect(request.url.path, equals("/packages/foo.json"));
338
339 return new Future.immediate(new http.Response(JSON.stringify({
340 "name": "foo",
341 "uploaders": ["nweiz@google.com"],
342 "versions": ["3.0.0-pre", "2.0.0-pre"]
343 }), 200));
344 }));
345
346 dir(appPath, [
347 libPubspec("test_pkg", "1.0.0", [
348 {'git': 'git://github.com/dart-lang/foo'}
349 ])
350 ]).scheduleCreate();
351
352 expectLater(schedulePackageValidation(dependency),
353 pairOf(isEmpty, someElement(contains(
354 ' foo: ">=3.0.0-pre <4.0.0"'))));
355
356 run();
357 });
358
359 test("and should suggest a single-version dependency if the primary "
360 "version is pre-1.0.0", () {
361 useMockClient(new MockClient((request) {
362 expect(request.method, equals("GET"));
363 expect(request.url.path, equals("/packages/foo.json"));
364
365 return new Future.immediate(new http.Response(JSON.stringify({
366 "name": "foo",
367 "uploaders": ["nweiz@google.com"],
368 "versions": ["0.0.1", "0.0.2"]
369 }), 200));
370 }));
371
372 dir(appPath, [
373 libPubspec("test_pkg", "1.0.0", [
374 {'git': 'git://github.com/dart-lang/foo'}
375 ])
376 ]).scheduleCreate();
377
378 expectLater(schedulePackageValidation(dependency),
379 pairOf(isEmpty, someElement(contains(' foo: 0.0.2'))));
380
381 run();
382 });
383 });
384
385 group('where no hosted version of that dependency exists', () {
386 test("and should use the other source's version", () {
387 useMockClient(new MockClient((request) {
388 expect(request.method, equals("GET"));
389 expect(request.url.path, equals("/packages/foo.json"));
390
391 return new Future.immediate(new http.Response("not found", 404));
392 }));
393
394 dir(appPath, [
395 libPubspec("test_pkg", "1.0.0", [
396 {
397 'git': {'url': 'git://github.com/dart-lang/foo'},
398 'version': '>=1.0.0 <2.0.0'
399 }
400 ])
401 ]).scheduleCreate();
402
403 expectLater(schedulePackageValidation(dependency),
404 pairOf(isEmpty, someElement(contains(
405 ' foo: ">=1.0.0 <2.0.0"'))));
406
407 run();
408 });
409
410 test("and should use the other source's unquoted version if it's "
411 "concrete", () {
412 useMockClient(new MockClient((request) {
413 expect(request.method, equals("GET"));
414 expect(request.url.path, equals("/packages/foo.json"));
415
416 return new Future.immediate(new http.Response("not found", 404));
417 }));
418
419 dir(appPath, [
420 libPubspec("test_pkg", "1.0.0", [
421 {
422 'git': {'url': 'git://github.com/dart-lang/foo'},
423 'version': '0.2.3'
424 }
425 ])
426 ]).scheduleCreate();
427
428 expectLater(schedulePackageValidation(dependency),
429 pairOf(isEmpty, someElement(contains(' foo: 0.2.3'))));
430
431 run();
432 });
433 });
434 });
435
436 group('has an unconstrained dependency', () {
437 group('and it should not suggest a version', () {
438 test("if there's no lockfile", () {
439 dir(appPath, [
440 libPubspec("test_pkg", "1.0.0", [
441 {'hosted': 'foo'}
442 ])
443 ]).scheduleCreate();
444
445 expectLater(schedulePackageValidation(dependency),
446 pairOf(isEmpty, everyElement(isNot(contains("\n foo:")))));
447
448 run();
449 });
450
451 test("if the lockfile doesn't have an entry for the dependency", () {
452 dir(appPath, [
453 libPubspec("test_pkg", "1.0.0", [
454 {'hosted': 'foo'}
455 ]),
456 file("pubspec.lock", JSON.stringify({
457 'packages': {
458 'bar': {
459 'version': '1.2.3',
460 'source': 'hosted',
461 'description': {
462 'name': 'bar',
463 'url': 'http://pub.dartlang.org'
464 }
465 }
466 }
467 }))
468 ]).scheduleCreate();
469
470 expectLater(schedulePackageValidation(dependency),
471 pairOf(isEmpty, everyElement(isNot(contains("\n foo:")))));
472
473 run();
474 });
475 });
476
477 group('with a lockfile', () {
478 test('and it should suggest a constraint based on the locked '
479 'version', () {
480 dir(appPath, [
481 libPubspec("test_pkg", "1.0.0", [
482 {'hosted': 'foo'}
483 ]),
484 file("pubspec.lock", JSON.stringify({
485 'packages': {
486 'foo': {
487 'version': '1.2.3',
488 'source': 'hosted',
489 'description': {
490 'name': 'foo',
491 'url': 'http://pub.dartlang.org'
492 }
493 }
494 }
495 }))
496 ]).scheduleCreate();
497
498 expectLater(schedulePackageValidation(dependency),
499 pairOf(isEmpty, someElement(contains(
500 ' foo: ">=1.2.3 <2.0.0"'))));
501
502 run();
503 });
504
505 test('and it should suggest a concrete constraint if the locked '
506 'version is pre-1.0.0', () {
507 dir(appPath, [
508 libPubspec("test_pkg", "1.0.0", [
509 {'hosted': 'foo'}
510 ]),
511 file("pubspec.lock", JSON.stringify({
512 'packages': {
513 'foo': {
514 'version': '0.1.2',
515 'source': 'hosted',
516 'description': {
517 'name': 'foo',
518 'url': 'http://pub.dartlang.org'
519 }
520 }
521 }
522 }))
523 ]).scheduleCreate();
524
525 expectLater(schedulePackageValidation(dependency),
526 pairOf(isEmpty, someElement(contains(' foo: 0.1.2'))));
527
528 run();
529 });
530 });
531 });
286 }); 532 });
287 } 533 }
OLDNEW
« utils/pub/validator/dependency.dart ('K') | « utils/tests/pub/test_pub.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698