| 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 'test_pub.dart'; | 8 import 'test_pub.dart'; |
| 9 import '../../pub/pubspec.dart'; | 9 import '../../pub/pubspec.dart'; |
| 10 import '../../pub/source.dart'; | 10 import '../../pub/source.dart'; |
| 11 import '../../pub/source_registry.dart'; | 11 import '../../pub/source_registry.dart'; |
| 12 import '../../pub/utils.dart'; | 12 import '../../pub/utils.dart'; |
| 13 import '../../pub/version.dart'; | 13 import '../../pub/version.dart'; |
| 14 | 14 |
| 15 class MockSource extends Source { | 15 class MockSource extends Source { |
| 16 final String name = "mock"; | 16 final String name = "mock"; |
| 17 final bool shouldCache = false; | 17 final bool shouldCache = false; |
| 18 dynamic parseDescription(String filePath, description, | 18 dynamic parseDescription(String filePath, description, |
| 19 {bool fromLockFile: false}) { | 19 {bool fromLockFile: false}) { |
| 20 if (description != 'ok') throw new FormatException('Bad'); | 20 if (description != 'ok') throw new FormatException('Bad'); |
| 21 return description; | 21 return description; |
| 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('Pubspec', () { | 28 group('parse()', () { |
| 29 group('parse()', () { | 29 var sources = new SourceRegistry(); |
| 30 var sources = new SourceRegistry(); | 30 sources.register(new MockSource()); |
| 31 sources.register(new MockSource()); | |
| 32 | 31 |
| 33 expectFormatError(String pubspec) { | 32 expectFormatError(String pubspec) { |
| 34 expect(() => new Pubspec.parse(null, pubspec, sources), | 33 expect(() => new Pubspec.parse(null, pubspec, sources), |
| 35 throwsFormatException); | 34 throwsFormatException); |
| 36 } | 35 } |
| 37 | 36 |
| 38 test("allows a version constraint for dependencies", () { | 37 test("allows a version constraint for dependencies", () { |
| 39 var pubspec = new Pubspec.parse(null, ''' | 38 var pubspec = new Pubspec.parse(null, ''' |
| 40 dependencies: | 39 dependencies: |
| 41 foo: | 40 foo: |
| 42 mock: ok | 41 mock: ok |
| 43 version: ">=1.2.3 <3.4.5" | 42 version: ">=1.2.3 <3.4.5" |
| 44 ''', sources); | 43 ''', sources); |
| 45 | 44 |
| 46 var foo = pubspec.dependencies[0]; | 45 var foo = pubspec.dependencies[0]; |
| 47 expect(foo.name, equals('foo')); | 46 expect(foo.name, equals('foo')); |
| 48 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); | 47 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); |
| 49 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); | 48 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); |
| 50 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); | 49 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); |
| 51 }); | 50 }); |
| 52 | 51 |
| 53 test("allows an empty dependencies map", () { | 52 test("allows an empty dependencies map", () { |
| 54 var pubspec = new Pubspec.parse(null, ''' | 53 var pubspec = new Pubspec.parse(null, ''' |
| 55 dependencies: | 54 dependencies: |
| 56 ''', sources); | 55 ''', sources); |
| 57 | 56 |
| 58 expect(pubspec.dependencies, isEmpty); | 57 expect(pubspec.dependencies, isEmpty); |
| 59 }); | 58 }); |
| 60 | 59 |
| 61 test("throws if the description isn't valid", () { | 60 test("allows a version constraint for dev dependencies", () { |
| 62 expectFormatError(''' | 61 var pubspec = new Pubspec.parse(null, ''' |
| 62 dev_dependencies: |
| 63 foo: |
| 64 mock: ok |
| 65 version: ">=1.2.3 <3.4.5" |
| 66 ''', sources); |
| 67 |
| 68 var foo = pubspec.devDependencies[0]; |
| 69 expect(foo.name, equals('foo')); |
| 70 expect(foo.constraint.allows(new Version(1, 2, 3)), isTrue); |
| 71 expect(foo.constraint.allows(new Version(1, 2, 5)), isTrue); |
| 72 expect(foo.constraint.allows(new Version(3, 4, 5)), isFalse); |
| 73 }); |
| 74 |
| 75 test("allows an empty dev dependencies map", () { |
| 76 var pubspec = new Pubspec.parse(null, ''' |
| 77 dev_dependencies: |
| 78 ''', sources); |
| 79 |
| 80 expect(pubspec.devDependencies, isEmpty); |
| 81 }); |
| 82 |
| 83 test("throws if a package is in dependencies and dev_dependencies", () { |
| 84 expectFormatError(''' |
| 85 dependencies: |
| 86 foo: |
| 87 mock: ok |
| 88 dev_dependencies: |
| 89 foo: |
| 90 mock: ok |
| 91 '''); |
| 92 }); |
| 93 |
| 94 test("throws if the description isn't valid", () { |
| 95 expectFormatError(''' |
| 63 dependencies: | 96 dependencies: |
| 64 foo: | 97 foo: |
| 65 mock: bad | 98 mock: bad |
| 66 '''); | 99 '''); |
| 67 }); | 100 }); |
| 68 | 101 |
| 69 test("throws if 'name' is not a string", () { | 102 test("throws if 'name' is not a string", () { |
| 70 expectFormatError('name: [not, a, string]'); | 103 expectFormatError('name: [not, a, string]'); |
| 71 }); | 104 }); |
| 72 | 105 |
| 73 test("throws if 'homepage' is not a string", () { | 106 test("throws if 'homepage' is not a string", () { |
| 74 expectFormatError('homepage:'); | 107 expectFormatError('homepage:'); |
| 75 expectFormatError('homepage: [not, a, string]'); | 108 expectFormatError('homepage: [not, a, string]'); |
| 76 }); | 109 }); |
| 77 | 110 |
| 78 test("throws if 'homepage' doesn't have an HTTP scheme", () { | 111 test("throws if 'homepage' doesn't have an HTTP scheme", () { |
| 79 new Pubspec.parse(null, 'homepage: http://ok.com', sources); | 112 new Pubspec.parse(null, 'homepage: http://ok.com', sources); |
| 80 new Pubspec.parse(null, 'homepage: https://also-ok.com', sources); | 113 new Pubspec.parse(null, 'homepage: https://also-ok.com', sources); |
| 81 | 114 |
| 82 expectFormatError('homepage: ftp://badscheme.com'); | 115 expectFormatError('homepage: ftp://badscheme.com'); |
| 83 expectFormatError('homepage: javascript:alert("!!!")'); | 116 expectFormatError('homepage: javascript:alert("!!!")'); |
| 84 expectFormatError('homepage: data:image/png;base64,somedata'); | 117 expectFormatError('homepage: data:image/png;base64,somedata'); |
| 85 expectFormatError('homepage: no-scheme.com'); | 118 expectFormatError('homepage: no-scheme.com'); |
| 86 }); | 119 }); |
| 87 | 120 |
| 88 test("throws if 'documentation' is not a string", () { | 121 test("throws if 'documentation' is not a string", () { |
| 89 expectFormatError('documentation:'); | 122 expectFormatError('documentation:'); |
| 90 expectFormatError('documentation: [not, a, string]'); | 123 expectFormatError('documentation: [not, a, string]'); |
| 91 }); | 124 }); |
| 92 | 125 |
| 93 test("throws if 'documentation' doesn't have an HTTP scheme", () { | 126 test("throws if 'documentation' doesn't have an HTTP scheme", () { |
| 94 new Pubspec.parse(null, 'documentation: http://ok.com', sources); | 127 new Pubspec.parse(null, 'documentation: http://ok.com', sources); |
| 95 new Pubspec.parse(null, 'documentation: https://also-ok.com', sources); | 128 new Pubspec.parse(null, 'documentation: https://also-ok.com', sources); |
| 96 | 129 |
| 97 expectFormatError('documentation: ftp://badscheme.com'); | 130 expectFormatError('documentation: ftp://badscheme.com'); |
| 98 expectFormatError('documentation: javascript:alert("!!!")'); | 131 expectFormatError('documentation: javascript:alert("!!!")'); |
| 99 expectFormatError('documentation: data:image/png;base64,somedata'); | 132 expectFormatError('documentation: data:image/png;base64,somedata'); |
| 100 expectFormatError('documentation: no-scheme.com'); | 133 expectFormatError('documentation: no-scheme.com'); |
| 101 }); | 134 }); |
| 102 | 135 |
| 103 test("throws if 'authors' is not a string or a list of strings", () { | 136 test("throws if 'authors' is not a string or a list of strings", () { |
| 104 new Pubspec.parse(null, 'authors: ok fine', sources); | 137 new Pubspec.parse(null, 'authors: ok fine', sources); |
| 105 new Pubspec.parse(null, 'authors: [also, ok, fine]', sources); | 138 new Pubspec.parse(null, 'authors: [also, ok, fine]', sources); |
| 106 | 139 |
| 107 expectFormatError('authors: 123'); | 140 expectFormatError('authors: 123'); |
| 108 expectFormatError('authors: {not: {a: string}}'); | 141 expectFormatError('authors: {not: {a: string}}'); |
| 109 expectFormatError('authors: [ok, {not: ok}]'); | 142 expectFormatError('authors: [ok, {not: ok}]'); |
| 110 }); | 143 }); |
| 111 | 144 |
| 112 test("throws if 'author' is not a string", () { | 145 test("throws if 'author' is not a string", () { |
| 113 new Pubspec.parse(null, 'author: ok fine', sources); | 146 new Pubspec.parse(null, 'author: ok fine', sources); |
| 114 | 147 |
| 115 expectFormatError('author: 123'); | 148 expectFormatError('author: 123'); |
| 116 expectFormatError('author: {not: {a: string}}'); | 149 expectFormatError('author: {not: {a: string}}'); |
| 117 expectFormatError('author: [not, ok]'); | 150 expectFormatError('author: [not, ok]'); |
| 118 }); | 151 }); |
| 119 | 152 |
| 120 test("throws if both 'author' and 'authors' are present", () { | 153 test("throws if both 'author' and 'authors' are present", () { |
| 121 expectFormatError('{author: abe, authors: ted}'); | 154 expectFormatError('{author: abe, authors: ted}'); |
| 122 }); | 155 }); |
| 123 | 156 |
| 124 test("allows comment-only files", () { | 157 test("allows comment-only files", () { |
| 125 var pubspec = new Pubspec.parse(null, ''' | 158 var pubspec = new Pubspec.parse(null, ''' |
| 126 # No external dependencies yet | 159 # No external dependencies yet |
| 127 # Including for completeness | 160 # Including for completeness |
| 128 # ...and hoping the spec expands to include details about author, version, etc | 161 # ...and hoping the spec expands to include details about author, version, etc |
| 129 # See http://www.dartlang.org/docs/pub-package-manager/ for details | 162 # See http://www.dartlang.org/docs/pub-package-manager/ for details |
| 130 ''', sources); | 163 ''', sources); |
| 131 expect(pubspec.version, equals(Version.none)); | 164 expect(pubspec.version, equals(Version.none)); |
| 132 expect(pubspec.dependencies, isEmpty); | 165 expect(pubspec.dependencies, isEmpty); |
| 166 }); |
| 167 |
| 168 group("environment", () { |
| 169 test("defaults to any SDK constraint if environment is omitted", () { |
| 170 var pubspec = new Pubspec.parse(null, '', sources); |
| 171 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any)); |
| 133 }); | 172 }); |
| 134 | 173 |
| 135 group("environment", () { | 174 test("allows an empty environment map", () { |
| 136 test("defaults to any SDK constraint if environment is omitted", () { | 175 var pubspec = new Pubspec.parse(null, ''' |
| 137 var pubspec = new Pubspec.parse(null, '', sources); | |
| 138 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any)); | |
| 139 }); | |
| 140 | |
| 141 test("allows an empty environment map", () { | |
| 142 var pubspec = new Pubspec.parse(null, ''' | |
| 143 environment: | 176 environment: |
| 144 ''', sources); | 177 ''', sources); |
| 145 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any)); | 178 expect(pubspec.environment.sdkVersion, equals(VersionConstraint.any)); |
| 146 }); | 179 }); |
| 147 | 180 |
| 148 test("throws if the environment value isn't a map", () { | 181 test("throws if the environment value isn't a map", () { |
| 149 expectFormatError(''' | 182 expectFormatError(''' |
| 150 environment: [] | 183 environment: [] |
| 151 '''); | 184 '''); |
| 152 }); | 185 }); |
| 153 | 186 |
| 154 test("allows a version constraint for the sdk", () { | 187 test("allows a version constraint for the sdk", () { |
| 155 var pubspec = new Pubspec.parse(null, ''' | 188 var pubspec = new Pubspec.parse(null, ''' |
| 156 environment: | 189 environment: |
| 157 sdk: ">=1.2.3 <2.3.4" | 190 sdk: ">=1.2.3 <2.3.4" |
| 158 ''', sources); | 191 ''', sources); |
| 159 expect(pubspec.environment.sdkVersion, | 192 expect(pubspec.environment.sdkVersion, |
| 160 equals(new VersionConstraint.parse(">=1.2.3 <2.3.4"))); | 193 equals(new VersionConstraint.parse(">=1.2.3 <2.3.4"))); |
| 161 }); | 194 }); |
| 162 | 195 |
| 163 test("throws if the sdk isn't a string", () { | 196 test("throws if the sdk isn't a string", () { |
| 164 expectFormatError(''' | 197 expectFormatError(''' |
| 165 environment: | 198 environment: |
| 166 sdk: [] | 199 sdk: [] |
| 167 '''); | 200 '''); |
| 168 }); | 201 }); |
| 169 | 202 |
| 170 test("throws if the sdk isn't a valid version constraint", () { | 203 test("throws if the sdk isn't a valid version constraint", () { |
| 171 expectFormatError(''' | 204 expectFormatError(''' |
| 172 environment: | 205 environment: |
| 173 sdk: "oopies" | 206 sdk: "oopies" |
| 174 '''); | 207 '''); |
| 175 }); | |
| 176 }); | 208 }); |
| 177 }); | 209 }); |
| 178 }); | 210 }); |
| 179 } | 211 } |
| OLD | NEW |