@@ -10,6 +10,7 @@ library;
10
10
import 'dart:io' ;
11
11
12
12
import 'package:json_serializable/src/check_dependencies.dart' ;
13
+ import 'package:json_serializable/src/constants.dart' ;
13
14
import 'package:path/path.dart' as p;
14
15
import 'package:pub_semver/pub_semver.dart' ;
15
16
import 'package:pubspec_parse/pubspec_parse.dart' ;
@@ -20,7 +21,7 @@ import 'package:test_process/test_process.dart';
20
21
import 'test_utils.dart' ;
21
22
22
23
void main () {
23
- test ('validate pubspec constraint' , () {
24
+ test ('validate pubspec constraint' , () async {
24
25
final annotationConstraint =
25
26
_jsonSerialPubspec.dependencies['json_annotation' ] as HostedDependency ;
26
27
final versionRange = annotationConstraint.version as VersionRange ;
@@ -29,10 +30,35 @@ void main() {
29
30
expect (versionRange.min, requiredJsonAnnotationMinVersion);
30
31
});
31
32
33
+ group ('language version' , () {
34
+ test ('is less than required' , () async {
35
+ const sdkLowerBound = '2.12.0' ;
36
+ await _structurePackage (
37
+ environment: const {'sdk' : '^$sdkLowerBound ' },
38
+ dependencies: {'json_annotation' : _annotationLowerBound},
39
+ message: '''
40
+ The language version ($sdkLowerBound ) of this package ($_testPkgName ) does not match the required range `$supportedLanguageConstraint `.
41
+
42
+ Edit pubspec.yaml to include an SDK constraint of at least $supportedLanguageConstraint .
43
+
44
+ environment:
45
+ sdk: $supportedLanguageConstraint
46
+ ''' ,
47
+ );
48
+ });
49
+
50
+ test (
51
+ 'is at least the required `$supportedLanguageConstraint `' ,
52
+ () async => await _structurePackage (
53
+ dependencies: {'json_annotation' : _annotationLowerBound},
54
+ message: null ,
55
+ ),
56
+ );
57
+ });
58
+
32
59
test (
33
60
'missing dependency in production code' ,
34
61
() => _structurePackage (
35
- sourceDirectory: 'lib' ,
36
62
message: _missingProductionDep,
37
63
),
38
64
);
@@ -50,7 +76,6 @@ void main() {
50
76
test (
51
77
'dev dependency with a production usage' ,
52
78
() => _structurePackage (
53
- sourceDirectory: 'lib' ,
54
79
devDependencies: {'json_annotation' : _annotationLowerBound},
55
80
message: _missingProductionDep,
56
81
),
@@ -59,7 +84,6 @@ void main() {
59
84
test (
60
85
'dependency with `null` constraint' ,
61
86
() => _structurePackage (
62
- sourceDirectory: 'lib' ,
63
87
dependencies: {'json_annotation' : null },
64
88
message:
65
89
'The version constraint "any" on json_annotation allows versions '
@@ -70,7 +94,6 @@ void main() {
70
94
test (
71
95
'dependency with "any" constraint' ,
72
96
() => _structurePackage (
73
- sourceDirectory: 'lib' ,
74
97
dependencies: {'json_annotation' : 'any' },
75
98
message:
76
99
'The version constraint "any" on json_annotation allows versions '
@@ -81,7 +104,6 @@ void main() {
81
104
test (
82
105
'dependency with too low version range' ,
83
106
() => _structurePackage (
84
- sourceDirectory: 'lib' ,
85
107
dependencies: {'json_annotation' : '^4.0.0' },
86
108
message:
87
109
'The version constraint "^4.0.0" on json_annotation allows versions '
@@ -114,16 +136,19 @@ final _missingProductionDep =
114
136
'"dependencies" section of your pubspec with a lower bound of at least '
115
137
'"$_annotationLowerBound ".' ;
116
138
139
+ const _testPkgName = '_test_pkg' ;
140
+
117
141
Future <void > _structurePackage ({
118
- required String sourceDirectory,
119
- required String message,
142
+ String sourceDirectory = 'lib' ,
143
+ required String ? message,
144
+ Map <String , dynamic > environment = const {'sdk' : supportedLanguageConstraint},
120
145
Map <String , dynamic > dependencies = const {},
121
146
Map <String , dynamic > devDependencies = const {},
122
147
}) async {
123
148
final pubspec = loudEncode (
124
149
{
125
- 'name' : '_test_pkg' ,
126
- 'environment' : { 'sdk' : '>=2.14.0 <3.0.0' } ,
150
+ 'name' : _testPkgName ,
151
+ 'environment' : environment ,
127
152
'dependencies' : dependencies,
128
153
'dev_dependencies' : {
129
154
...devDependencies,
@@ -162,9 +187,8 @@ class SomeClass{}
162
187
)
163
188
],
164
189
).create ();
165
-
166
190
final proc = await TestProcess .start (
167
- 'dart' ,
191
+ Platform .resolvedExecutable ,
168
192
['run' , 'build_runner' , 'build' ],
169
193
workingDirectory: d.sandbox,
170
194
);
@@ -175,9 +199,22 @@ class SomeClass{}
175
199
print (line);
176
200
}
177
201
178
- expect (lines.toString (), contains ('''
202
+ final output = lines.toString ();
203
+ final expectedWarningCount = message == null ? 0 : 1 ;
204
+ final warningCount = '[WARNING]' .allMatches (output).length;
205
+ expect (
206
+ warningCount,
207
+ expectedWarningCount,
208
+ reason:
209
+ 'Expected the number of output warnings ($warningCount ) to match the '
210
+ 'number of expected warnings ($expectedWarningCount ).' ,
211
+ );
212
+
213
+ if (message != null ) {
214
+ expect (output, contains ('''
179
215
[WARNING] json_serializable on $sourceDirectory /sample.dart:
180
216
$message ''' ));
217
+ }
181
218
182
219
await proc.shouldExit (0 );
183
220
}
0 commit comments