@@ -10,6 +10,7 @@ library;
1010import 'dart:io' ;
1111
1212import 'package:json_serializable/src/check_dependencies.dart' ;
13+ import 'package:json_serializable/src/constants.dart' ;
1314import 'package:path/path.dart' as p;
1415import 'package:pub_semver/pub_semver.dart' ;
1516import 'package:pubspec_parse/pubspec_parse.dart' ;
@@ -20,7 +21,7 @@ import 'package:test_process/test_process.dart';
2021import 'test_utils.dart' ;
2122
2223void main () {
23- test ('validate pubspec constraint' , () {
24+ test ('validate pubspec constraint' , () async {
2425 final annotationConstraint =
2526 _jsonSerialPubspec.dependencies['json_annotation' ] as HostedDependency ;
2627 final versionRange = annotationConstraint.version as VersionRange ;
@@ -29,10 +30,35 @@ void main() {
2930 expect (versionRange.min, requiredJsonAnnotationMinVersion);
3031 });
3132
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+
3259 test (
3360 'missing dependency in production code' ,
3461 () => _structurePackage (
35- sourceDirectory: 'lib' ,
3662 message: _missingProductionDep,
3763 ),
3864 );
@@ -50,7 +76,6 @@ void main() {
5076 test (
5177 'dev dependency with a production usage' ,
5278 () => _structurePackage (
53- sourceDirectory: 'lib' ,
5479 devDependencies: {'json_annotation' : _annotationLowerBound},
5580 message: _missingProductionDep,
5681 ),
@@ -59,7 +84,6 @@ void main() {
5984 test (
6085 'dependency with `null` constraint' ,
6186 () => _structurePackage (
62- sourceDirectory: 'lib' ,
6387 dependencies: {'json_annotation' : null },
6488 message:
6589 'The version constraint "any" on json_annotation allows versions '
@@ -70,7 +94,6 @@ void main() {
7094 test (
7195 'dependency with "any" constraint' ,
7296 () => _structurePackage (
73- sourceDirectory: 'lib' ,
7497 dependencies: {'json_annotation' : 'any' },
7598 message:
7699 'The version constraint "any" on json_annotation allows versions '
@@ -81,7 +104,6 @@ void main() {
81104 test (
82105 'dependency with too low version range' ,
83106 () => _structurePackage (
84- sourceDirectory: 'lib' ,
85107 dependencies: {'json_annotation' : '^4.0.0' },
86108 message:
87109 'The version constraint "^4.0.0" on json_annotation allows versions '
@@ -114,16 +136,19 @@ final _missingProductionDep =
114136 '"dependencies" section of your pubspec with a lower bound of at least '
115137 '"$_annotationLowerBound ".' ;
116138
139+ const _testPkgName = '_test_pkg' ;
140+
117141Future <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},
120145 Map <String , dynamic > dependencies = const {},
121146 Map <String , dynamic > devDependencies = const {},
122147}) async {
123148 final pubspec = loudEncode (
124149 {
125- 'name' : '_test_pkg' ,
126- 'environment' : { 'sdk' : '>=2.14.0 <3.0.0' } ,
150+ 'name' : _testPkgName ,
151+ 'environment' : environment ,
127152 'dependencies' : dependencies,
128153 'dev_dependencies' : {
129154 ...devDependencies,
@@ -162,9 +187,8 @@ class SomeClass{}
162187 )
163188 ],
164189 ).create ();
165-
166190 final proc = await TestProcess .start (
167- 'dart' ,
191+ Platform .resolvedExecutable ,
168192 ['run' , 'build_runner' , 'build' ],
169193 workingDirectory: d.sandbox,
170194 );
@@ -175,9 +199,22 @@ class SomeClass{}
175199 print (line);
176200 }
177201
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 ('''
179215[WARNING] json_serializable on $sourceDirectory /sample.dart:
180216$message ''' ));
217+ }
181218
182219 await proc.shouldExit (0 );
183220}
0 commit comments