1
1
import 'dart:io' ;
2
2
3
3
import 'package:analyzer/dart/element/type.dart' ;
4
- import 'package:build_test/build_test.dart' ;
5
4
import 'package:logging/logging.dart' ;
6
5
import 'package:mockito/mockito.dart' ;
7
6
import 'package:openapi_generator/src/models/command.dart' ;
@@ -30,15 +29,8 @@ void main() {
30
29
mockedArgs = MockGeneratorArguments ();
31
30
mockRunner = MockCommandRunner ();
32
31
mockedAnnotations = MockConstantReader ();
33
- defaultAnnotations = (await resolveSource (
34
- File ('$testSpecPath /next_gen_builder_test_config.dart' )
35
- .readAsStringSync (),
36
- (resolver) async =>
37
- (await resolver.findLibraryByName ('test_lib' ))! ))
38
- .getClass ('TestClassConfig' )!
39
- .metadata
40
- .map ((e) => ConstantReader (e.computeConstantValue ()! ))
41
- .first;
32
+ defaultAnnotations =
33
+ await loadAnnoation ('next_gen_builder_test_config.dart' );
42
34
realArguments = GeneratorArguments (annotations: defaultAnnotations);
43
35
});
44
36
@@ -117,16 +109,8 @@ void main() {
117
109
test ('flutter when wrapper is fvm' , () async {
118
110
final logs = < LogRecord > [];
119
111
logger.onRecord.listen (logs.add);
120
- final annotations = (await resolveSource (
121
- File ('$testSpecPath /next_gen_builder_fvm_test_config.dart' )
122
- .readAsStringSync (),
123
- (resolver) async =>
124
- (await resolver.findLibraryByName ('test_lib' ))! ))
125
- .getClass ('TestClassConfig' )!
126
- .metadata
127
- .map ((e) => ConstantReader (e.computeConstantValue ()! ))
128
- .first;
129
-
112
+ final annotations =
113
+ await loadAnnoation ('next_gen_builder_fvm_test_config.dart' );
130
114
when (mockRunner.checkForFlutterEnvironemt (
131
115
wrapper: argThat (
132
116
TypeMatcher <Wrapper >()
@@ -145,15 +129,8 @@ void main() {
145
129
test ('flutter when wrapper is ./flutter' , () async {
146
130
final logs = < LogRecord > [];
147
131
logger.onRecord.listen (logs.add);
148
- final annotations = (await resolveSource (
149
- File ('$testSpecPath /next_gen_builder_flutterw_test_config.dart' )
150
- .readAsStringSync (),
151
- (resolver) async =>
152
- (await resolver.findLibraryByName ('test_lib' ))! ))
153
- .getClass ('TestClassConfig' )!
154
- .metadata
155
- .map ((e) => ConstantReader (e.computeConstantValue ()! ))
156
- .first;
132
+ final annotations =
133
+ await loadAnnoation ('next_gen_builder_flutterw_test_config.dart' );
157
134
158
135
when (mockRunner.checkForFlutterEnvironemt (
159
136
wrapper: argThat (
@@ -173,15 +150,8 @@ void main() {
173
150
test ('when defined in pubspec' , () async {
174
151
final logs = < LogRecord > [];
175
152
logger.onRecord.listen (logs.add);
176
- final annotations = (await resolveSource (
177
- File ('$testSpecPath /next_gen_builder_flutter_test_config.dart' )
178
- .readAsStringSync (),
179
- (resolver) async =>
180
- (await resolver.findLibraryByName ('test_lib' ))! ))
181
- .getClass ('TestClassConfig' )!
182
- .metadata
183
- .map ((e) => ConstantReader (e.computeConstantValue ()! ))
184
- .first;
153
+ final annotations =
154
+ await loadAnnoation ('next_gen_builder_flutter_test_config.dart' );
185
155
186
156
when (
187
157
mockRunner.checkForFlutterEnvironemt (
@@ -201,6 +171,109 @@ void main() {
201
171
});
202
172
});
203
173
174
+ group ('uses correct generator' , () {
175
+ test ('dart' , () async {
176
+ final logs = < LogRecord > [];
177
+ logger.onRecord.listen (logs.add);
178
+
179
+ final annotations =
180
+ await loadAnnoation ('next_gen_builder_local_test_config.dart' );
181
+
182
+ when (mockRunner.runCommand (
183
+ command: anyNamed ('command' ),
184
+ workingDirectory: anyNamed ('workingDirectory' )))
185
+ .thenAnswer (
186
+ (_) async => ProcessResult (999 , 0 , 'stdout' , 'stderr' ));
187
+
188
+ when (mockRunner.isSpecFileDirty (
189
+ cachedSpec: anyNamed ('cachedSpec' ),
190
+ loadedSpec: anyNamed ('loadedSpec' )))
191
+ .thenAnswer ((_) async => true );
192
+ final args = GeneratorArguments (annotations: annotations);
193
+ await OpenapiGenerator (logger: logger, runner: mockRunner)
194
+ .generatorV2 (
195
+ args: args,
196
+ baseCommand: 'dart' ,
197
+ annotatedPath: 'annotatedPath' );
198
+ final generationLogIndex = logs.indexWhere ((element) =>
199
+ element.message.contains (
200
+ 'Running following command to generate openapi client - ' ));
201
+ logs.forEach ((element) {
202
+ print (element.message);
203
+ });
204
+ final log = logs[generationLogIndex];
205
+ expect (log.message, contains ('-g=dart' ));
206
+ final sourceGenLogIndex = logs
207
+ .indexWhere ((element) => element.message.contains ('source gen' ));
208
+ expect (logs[sourceGenLogIndex].message,
209
+ 'Skipping source gen because generator does not need it.' );
210
+ });
211
+ test ('dio' , () async {
212
+ final logs = < LogRecord > [];
213
+ logger.onRecord.listen (logs.add);
214
+
215
+ final annotations =
216
+ await loadAnnoation ('next_gen_builder_test_config.dart' );
217
+
218
+ when (mockRunner.runCommand (
219
+ command: anyNamed ('command' ),
220
+ workingDirectory: anyNamed ('workingDirectory' )))
221
+ .thenAnswer (
222
+ (_) async => ProcessResult (999 , 0 , 'stdout' , 'stderr' ));
223
+
224
+ when (mockRunner.isSpecFileDirty (
225
+ cachedSpec: anyNamed ('cachedSpec' ),
226
+ loadedSpec: anyNamed ('loadedSpec' )))
227
+ .thenAnswer ((_) async => true );
228
+ final args = GeneratorArguments (annotations: annotations);
229
+ await OpenapiGenerator (logger: logger, runner: mockRunner)
230
+ .generatorV2 (
231
+ args: args,
232
+ baseCommand: 'dart' ,
233
+ annotatedPath: 'annotatedPath' );
234
+ final generationLogIndex = logs.indexWhere ((element) =>
235
+ element.message.contains (
236
+ 'Running following command to generate openapi client - ' ));
237
+ logs.forEach ((element) {
238
+ print (element.message);
239
+ });
240
+ final log = logs[generationLogIndex];
241
+ expect (log.message, contains ('-g=dart-dio' ));
242
+ });
243
+ test ('dioAlt' , () async {
244
+ final logs = < LogRecord > [];
245
+ logger.onRecord.listen (logs.add);
246
+
247
+ final annotations =
248
+ await loadAnnoation ('next_gen_builder_dio_alt_test_config.dart' );
249
+
250
+ when (mockRunner.runCommand (
251
+ command: anyNamed ('command' ),
252
+ workingDirectory: anyNamed ('workingDirectory' )))
253
+ .thenAnswer (
254
+ (_) async => ProcessResult (999 , 0 , 'stdout' , 'stderr' ));
255
+
256
+ when (mockRunner.isSpecFileDirty (
257
+ cachedSpec: anyNamed ('cachedSpec' ),
258
+ loadedSpec: anyNamed ('loadedSpec' )))
259
+ .thenAnswer ((_) async => true );
260
+ final args = GeneratorArguments (annotations: annotations);
261
+ await OpenapiGenerator (logger: logger, runner: mockRunner)
262
+ .generatorV2 (
263
+ args: args,
264
+ baseCommand: 'dart' ,
265
+ annotatedPath: 'annotatedPath' );
266
+ final generationLogIndex = logs.indexWhere ((element) =>
267
+ element.message.contains (
268
+ 'Running following command to generate openapi client - ' ));
269
+ logs.forEach ((element) {
270
+ print (element.message);
271
+ });
272
+ final log = logs[generationLogIndex];
273
+ expect (log.message, contains ('-g=dart2-api' ));
274
+ });
275
+ });
276
+
204
277
group ('generatorV2' , () {
205
278
group ('completes successfully' , () {
206
279
late OpenapiGenerator generator;
0 commit comments