|
4 | 4 | import 'package:aft/aft.dart';
|
5 | 5 | import 'package:aft/src/constraints_checker.dart';
|
6 | 6 | import 'package:pub_semver/pub_semver.dart';
|
7 |
| -import 'package:pubspec_parse/pubspec_parse.dart'; |
8 | 7 | import 'package:test/test.dart';
|
9 |
| -import 'package:yaml/yaml.dart'; |
10 | 8 | import 'package:yaml_edit/yaml_edit.dart';
|
11 | 9 |
|
| 10 | +import 'common.dart'; |
| 11 | + |
12 | 12 | void main() {
|
| 13 | + group('GlobalConstraintsChecker', () { |
| 14 | + for (final action in ConstraintsAction.values) { |
| 15 | + test( |
| 16 | + 'handles SDK constraints for preview Dart versions (${action.name})', |
| 17 | + () { |
| 18 | + final preReleaseConstraint = VersionConstraint.compatibleWith( |
| 19 | + Version(3, 2, 0, pre: '0'), |
| 20 | + ); |
| 21 | + final actions = dummyPackage( |
| 22 | + 'actions', |
| 23 | + publishable: false, |
| 24 | + sdkConstraint: preReleaseConstraint, |
| 25 | + ); |
| 26 | + final amplifyFlutter = dummyPackage( |
| 27 | + 'amplify_flutter', |
| 28 | + sdkConstraint: preReleaseConstraint, |
| 29 | + ); |
| 30 | + final checker = GlobalConstraintChecker( |
| 31 | + action, |
| 32 | + const {}, |
| 33 | + Environment( |
| 34 | + sdk: VersionConstraint.compatibleWith(Version(3, 0, 0)), |
| 35 | + ), |
| 36 | + ); |
| 37 | + |
| 38 | + { |
| 39 | + expect( |
| 40 | + checker.checkConstraints(actions.key), |
| 41 | + isTrue, |
| 42 | + reason: |
| 43 | + 'Package is not publishable and can take a prerelease constraint ' |
| 44 | + 'to leverage new Dart features', |
| 45 | + ); |
| 46 | + expect(checker.mismatchedDependencies, isEmpty); |
| 47 | + expect(actions.key.pubspecInfo.pubspecYamlEditor.edits, isEmpty); |
| 48 | + } |
| 49 | + |
| 50 | + { |
| 51 | + switch (action) { |
| 52 | + case ConstraintsAction.apply || ConstraintsAction.update: |
| 53 | + expect( |
| 54 | + checker.checkConstraints(amplifyFlutter.key), |
| 55 | + isTrue, |
| 56 | + ); |
| 57 | + expect( |
| 58 | + amplifyFlutter.key.pubspecInfo.pubspecYamlEditor.edits.single, |
| 59 | + isA<SourceEdit>().having( |
| 60 | + (edit) => edit.replacement.trim(), |
| 61 | + 'replacement', |
| 62 | + '^3.0.0', |
| 63 | + ), |
| 64 | + ); |
| 65 | + expect(checker.mismatchedDependencies, isEmpty); |
| 66 | + case ConstraintsAction.check: |
| 67 | + expect( |
| 68 | + checker.checkConstraints(amplifyFlutter.key), |
| 69 | + isFalse, |
| 70 | + reason: |
| 71 | + 'Package is publishable and must match the global SDK constraint', |
| 72 | + ); |
| 73 | + expect( |
| 74 | + checker.mismatchedDependencies.single, |
| 75 | + isA<MismatchedDependency>() |
| 76 | + .having( |
| 77 | + (err) => err.package.name, |
| 78 | + 'packageName', |
| 79 | + 'amplify_flutter', |
| 80 | + ) |
| 81 | + .having( |
| 82 | + (err) => err.dependencyName, |
| 83 | + 'dependencyName', |
| 84 | + 'sdk', |
| 85 | + ), |
| 86 | + ); |
| 87 | + expect( |
| 88 | + amplifyFlutter.key.pubspecInfo.pubspecYamlEditor.edits, |
| 89 | + isEmpty, |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + }, |
| 94 | + ); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
13 | 98 | group('PublishConstraintsChecker', () {
|
14 | 99 | for (final action in ConstraintsAction.values) {
|
15 | 100 | final result = switch (action) {
|
@@ -119,62 +204,3 @@ void main() {
|
119 | 204 | }
|
120 | 205 | });
|
121 | 206 | }
|
122 |
| - |
123 |
| -MapEntry<PackageInfo, List<PackageInfo>> dummyPackage( |
124 |
| - String name, { |
125 |
| - Version? version, |
126 |
| - bool publishable = true, |
127 |
| - Map<PackageInfo, VersionConstraint> deps = const {}, |
128 |
| - Map<PackageInfo, VersionConstraint> devDeps = const {}, |
129 |
| -}) { |
130 |
| - final path = 'packages/$name'; |
131 |
| - |
132 |
| - final pubspecEditor = YamlEditor(''' |
133 |
| -name: $name |
134 |
| -
|
135 |
| -environment: |
136 |
| - sdk: ^3.0.0 |
137 |
| -
|
138 |
| -dependencies: {} |
139 |
| -
|
140 |
| -dev_dependencies: {} |
141 |
| -'''); |
142 |
| - |
143 |
| - if (version != null) { |
144 |
| - pubspecEditor.update(['version'], version.toString()); |
145 |
| - } |
146 |
| - |
147 |
| - void addConstraints( |
148 |
| - Map<PackageInfo, VersionConstraint> constraints, |
149 |
| - DependencyType type, |
150 |
| - ) { |
151 |
| - for (final MapEntry(key: dep, value: constraint) in constraints.entries) { |
152 |
| - final path = <String>[type.key, dep.name]; |
153 |
| - pubspecEditor.update(path, constraint.toString()); |
154 |
| - } |
155 |
| - } |
156 |
| - |
157 |
| - addConstraints(deps, DependencyType.dependency); |
158 |
| - addConstraints(devDeps, DependencyType.devDependency); |
159 |
| - |
160 |
| - if (!publishable) { |
161 |
| - pubspecEditor.update(['publish_to'], 'none'); |
162 |
| - } |
163 |
| - |
164 |
| - final pubspecYaml = pubspecEditor.toString(); |
165 |
| - final pubspec = Pubspec.parse(pubspecYaml); |
166 |
| - final pubspecMap = loadYamlNode(pubspecYaml) as YamlMap; |
167 |
| - |
168 |
| - final package = PackageInfo( |
169 |
| - name: name, |
170 |
| - path: path, |
171 |
| - pubspecInfo: PubspecInfo( |
172 |
| - pubspec: pubspec, |
173 |
| - pubspecYaml: pubspecYaml, |
174 |
| - pubspecMap: pubspecMap, |
175 |
| - uri: Uri.base.resolve(path), |
176 |
| - ), |
177 |
| - flavor: PackageFlavor.dart, |
178 |
| - ); |
179 |
| - return MapEntry(package, [...deps.keys, ...devDeps.keys]); |
180 |
| -} |
|
0 commit comments