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