Skip to content

Commit 4643a0c

Browse files
committed
fix: fix tests now that the generator is running correctly
1 parent 37f862c commit 4643a0c

File tree

9 files changed

+212
-127
lines changed

9 files changed

+212
-127
lines changed

example/lib/main.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ void main() {
66
}
77

88
@Openapi(
9-
additionalProperties:
10-
DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
11-
inputSpecFile: 'openapi-spec.yaml',
12-
typeMappings: {'Pet': 'ExamplePet'},
13-
generatorName: Generator.dio,
14-
runSourceGenOnOutput: true,
15-
alwaysRun: true,
16-
outputDirectory: 'api/petstore_api')
9+
additionalProperties:
10+
DioProperties(pubName: 'petstore_api', pubAuthor: 'Johnny dep..'),
11+
inputSpecFile: 'openapi-spec.yaml',
12+
typeMappings: {'Pet': 'ExamplePet'},
13+
generatorName: Generator.dio,
14+
runSourceGenOnOutput: true,
15+
alwaysRun: true,
16+
outputDirectory: 'api/petstore_api',
17+
)
1718
class MyApp extends StatelessWidget {
1819
const MyApp({Key? key}) : super(key: key);
1920

openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class Openapi {
107107
/// Use a custom pubspec when running the generator.
108108
final String? projectPubspecPath;
109109

110+
/// Include in depth logging output from run commands.
111+
final bool debugLogging;
112+
110113
const Openapi({
111114
this.additionalProperties,
112115
@deprecated this.overwriteExistingFiles,
@@ -127,6 +130,7 @@ class Openapi {
127130
this.cachePath,
128131
this.useNextGen = false,
129132
this.projectPubspecPath,
133+
this.debugLogging = false,
130134
});
131135
}
132136

openapi-generator-annotations/test/openapi_generator_annotations_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void main() {
2323
expect(props.cachePath, isNull);
2424
expect(props.useNextGen, false);
2525
expect(props.projectPubspecPath, isNull);
26+
expect(props.debugLogging, isFalse);
2627
});
2728
group('NextGen', () {
2829
test('Sets cachePath', () {
@@ -44,6 +45,13 @@ void main() {
4445
projectPubspecPath: 'test');
4546
expect(api.projectPubspecPath, 'test');
4647
});
48+
test('Set debug logging', () {
49+
final api = Openapi(
50+
inputSpecFile: '',
51+
generatorName: Generator.dart,
52+
debugLogging: true);
53+
expect(api.debugLogging, isTrue);
54+
});
4755
});
4856
});
4957
}

openapi-generator/lib/src/models/generator_arguments.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class GeneratorArguments {
4444
///
4545
/// The default location is: .dart_tool/openapi-generator-cache.json
4646
final String cachePath;
47+
final bool isDebug;
4748

4849
/// Use a custom pubspec file when generating.
4950
///
@@ -125,6 +126,7 @@ class GeneratorArguments {
125126
bool useNextGen = false,
126127
String? cachePath,
127128
String? pubspecPath,
129+
bool isDebug = false,
128130
}) : alwaysRun = annotations.readPropertyOrDefault('alwaysRun', alwaysRun),
129131
_inputFile =
130132
annotations.readPropertyOrDefault('inputSpecFile', inputSpecFile),
@@ -159,7 +161,8 @@ class GeneratorArguments {
159161
pubspecPath = annotations.readPropertyOrDefault<String>(
160162
'projectPubspecPath',
161163
pubspecPath ??
162-
'${Directory.current.path}${Platform.pathSeparator}pubspec.yaml');
164+
'${Directory.current.path}${Platform.pathSeparator}pubspec.yaml'),
165+
isDebug = annotations.readPropertyOrDefault('debugLogging', isDebug);
163166

164167
/// The stringified name of the [Generator].
165168
String get generatorName => generator == Generator.dart

openapi-generator/lib/src/openapi_generator_runner.dart

Lines changed: 109 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
2525
FutureOr<String> generateForAnnotatedElement(
2626
Element element, ConstantReader annotations, BuildStep buildStep) async {
2727
logOutputMessage(
28-
log: log,
29-
communication: OutputMessage(
30-
message: [
28+
log: log,
29+
communication: OutputMessage(
30+
message: [
3131
'',
3232
' - :::::::::::::::::::::::::::::::::::::::::::',
3333
' - :: Openapi generator for dart ::',
3434
' - :::::::::::::::::::::::::::::::::::::::::::',
35-
].join('\n')));
35+
].join('\n'),
36+
),
37+
);
3638

3739
try {
3840
if (element is! ClassElement) {
@@ -85,35 +87,37 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
8587
return generatorV2(args: args, baseCommand: baseCommand);
8688
}
8789

88-
await runOpenApiJar(arguments: args.jarArgs);
89-
await generateSources(baseCommand: baseCommand, args: args);
90+
await runOpenApiJar(arguments: args);
9091
await fetchDependencies(baseCommand: baseCommand, args: args);
92+
await generateSources(baseCommand: baseCommand, args: args);
9193
} catch (e, st) {
9294
late OutputMessage communication;
9395
if (e is! OutputMessage) {
9496
communication = OutputMessage(
95-
message: '- :: There was an error generating the spec. ::',
96-
level: Level.SEVERE,
97-
additionalContext: e,
98-
stackTrace: st);
97+
message: '- :: There was an error generating the spec. ::',
98+
level: Level.SEVERE,
99+
additionalContext: e,
100+
stackTrace: st,
101+
);
99102
} else {
100103
communication = e;
101104
}
102105

103106
logOutputMessage(log: log, communication: communication);
104107
} finally {
105108
logOutputMessage(
106-
log: log,
107-
communication: OutputMessage(
108-
message: ' - :::::::::::::::::::::::::::::::::::::::::::'));
109+
log: log,
110+
communication: OutputMessage(
111+
message: ' - :::::::::::::::::::::::::::::::::::::::::::',
112+
),
113+
);
109114
}
110115
return '';
111116
}
112117

113118
/// Runs the OpenAPI compiler with the given [args].
114-
Future<void> runOpenApiJar(
115-
{required FutureOr<List<String>> arguments}) async {
116-
final args = await arguments;
119+
Future<void> runOpenApiJar({required GeneratorArguments arguments}) async {
120+
final args = await arguments.jarArgs;
117121
logOutputMessage(
118122
log: log,
119123
communication: OutputMessage(
@@ -128,17 +132,22 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
128132
// Include java environment variables in openApiCliCommand
129133
var javaOpts = Platform.environment['JAVA_OPTS'] ?? '';
130134

131-
final result = await Process.run(
132-
'java',
133-
[
134-
if (javaOpts.isNotEmpty) javaOpts,
135-
'-jar',
136-
binPath,
137-
...args,
138-
],
139-
workingDirectory: Directory.current.path,
140-
runInShell: Platform.isWindows,
141-
);
135+
ProcessResult result;
136+
if (!testMode) {
137+
result = await Process.run(
138+
'java',
139+
[
140+
if (javaOpts.isNotEmpty) javaOpts,
141+
'-jar',
142+
binPath,
143+
...args,
144+
],
145+
workingDirectory: Directory.current.path,
146+
runInShell: Platform.isWindows,
147+
);
148+
} else {
149+
result = ProcessResult(999999, 0, null, null);
150+
}
142151

143152
if (result.exitCode != 0) {
144153
return Future.error(
@@ -154,7 +163,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
154163
log: log,
155164
communication: OutputMessage(
156165
message: [
157-
// result.stdout,
166+
if (arguments.isDebug) result.stdout,
158167
' - :: Codegen completed successfully. ::',
159168
].join('\n'),
160169
),
@@ -172,35 +181,42 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
172181
{required GeneratorArguments args, required String baseCommand}) async {
173182
if (args.isRemote) {
174183
logOutputMessage(
175-
log: log,
176-
communication: OutputMessage(
177-
message:
178-
' - :: Using a remote specification, a cache will still be create but may be outdated. ::',
179-
level: Level.WARNING));
184+
log: log,
185+
communication: OutputMessage(
186+
message:
187+
' - :: Using a remote specification, a cache will still be create but may be outdated. ::',
188+
level: Level.WARNING,
189+
),
190+
);
180191
}
181192
try {
182193
if (await hasDiff(
183194
cachedPath: args.cachePath,
184195
loadPath: await args.inputFileOrFetch,
185196
)) {
186197
logOutputMessage(
187-
log: log,
188-
communication: OutputMessage(
189-
message: ' - :: Dirty Spec found. Running generation. ::'));
190-
await runOpenApiJar(arguments: args.jarArgs);
191-
await generateSources(baseCommand: baseCommand, args: args);
198+
log: log,
199+
communication: OutputMessage(
200+
message: ' - :: Dirty Spec found. Running generation. ::',
201+
),
202+
);
203+
await runOpenApiJar(arguments: args);
192204
await fetchDependencies(baseCommand: baseCommand, args: args);
205+
await generateSources(baseCommand: baseCommand, args: args);
193206
if (!args.hasLocalCache) {
194207
logOutputMessage(
195-
log: log,
196-
communication: OutputMessage(
197-
message: ' - :: No local cache found. Creating one. ::'));
208+
log: log,
209+
communication: OutputMessage(
210+
message: ' - :: No local cache found. Creating one. ::',
211+
),
212+
);
198213
} else {
199214
logOutputMessage(
200-
log: log,
201-
communication: OutputMessage(
202-
message:
203-
' - :: Local cache found. Overwriting existing one. ::'));
215+
log: log,
216+
communication: OutputMessage(
217+
message: ' - :: Local cache found. Overwriting existing one. ::',
218+
),
219+
);
204220
}
205221
await cacheSpec(
206222
outputLocation: args.cachePath,
@@ -253,21 +269,25 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
253269
logOutputMessage(
254270
log: log,
255271
communication: OutputMessage(
256-
message: ' - :: Skipping source gen step due to flag being set. ::',
257-
level: Level.WARNING),
272+
message: ' - :: Skipping source gen step due to flag being set. ::',
273+
level: Level.WARNING,
274+
),
258275
);
259276
} else if (!args.shouldGenerateSources) {
260277
logOutputMessage(
261-
log: log,
262-
communication: OutputMessage(
263-
message:
264-
' - :: Skipping source gen because generator does not need it. ::'));
278+
log: log,
279+
communication: OutputMessage(
280+
message:
281+
' - :: Skipping source gen because generator does not need it. ::',
282+
),
283+
);
265284
} else {
266-
return runSourceGen(baseCommand: baseCommand, args: args).then(
285+
return await runSourceGen(baseCommand: baseCommand, args: args).then(
267286
(_) => logOutputMessage(
268287
log: log,
269288
communication: OutputMessage(
270-
message: ' - :: Sources generated successfully. ::'),
289+
message: ' - :: Sources generated successfully. ::',
290+
),
271291
),
272292
onError: (e, st) => Future.error(
273293
OutputMessage(
@@ -285,9 +305,11 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
285305
Future<void> runSourceGen(
286306
{required String baseCommand, required GeneratorArguments args}) async {
287307
logOutputMessage(
288-
log: log,
289-
communication:
290-
OutputMessage(message: ' - :: Running source code generation. ::'));
308+
log: log,
309+
communication: OutputMessage(
310+
message: ' - :: Running source code generation. ::',
311+
),
312+
);
291313
final command = Command(
292314
executable: baseCommand,
293315
arguments: 'pub run build_runner build --delete-conflicting-outputs'
@@ -303,12 +325,17 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
303325
),
304326
);
305327

306-
final results = await Process.run(
307-
command.executable,
308-
command.arguments,
309-
runInShell: Platform.isWindows,
310-
workingDirectory: args.outputDirectory,
311-
);
328+
ProcessResult results;
329+
if (!testMode) {
330+
results = await Process.run(
331+
command.executable,
332+
command.arguments,
333+
runInShell: Platform.isWindows,
334+
workingDirectory: args.outputDirectory,
335+
);
336+
} else {
337+
results = ProcessResult(99999, 0, null, null);
338+
}
312339

313340
if (results.exitCode != 0) {
314341
return Future.error(
@@ -335,10 +362,12 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
335362
{required String baseCommand, required GeneratorArguments args}) async {
336363
if (!args.shouldFetchDependencies) {
337364
logOutputMessage(
338-
log: log,
339-
communication: OutputMessage(
340-
message: ' - :: Skipping install step because flag was set. ::',
341-
level: Level.WARNING));
365+
log: log,
366+
communication: OutputMessage(
367+
message: ' - :: Skipping install step because flag was set. ::',
368+
level: Level.WARNING,
369+
),
370+
);
342371
} else {
343372
final command = Command(
344373
executable: baseCommand,
@@ -354,15 +383,24 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
354383
),
355384
);
356385

357-
final result = await Process.run(command.executable, command.arguments,
386+
ProcessResult results;
387+
if (!testMode) {
388+
results = await Process.run(
389+
command.executable,
390+
command.arguments,
358391
runInShell: Platform.isWindows,
359-
workingDirectory: args.outputDirectory);
360-
if (result.exitCode != 0) {
392+
workingDirectory: args.outputDirectory,
393+
);
394+
} else {
395+
results = ProcessResult(999999, 0, null, null);
396+
}
397+
398+
if (results.exitCode != 0) {
361399
return Future.error(
362400
OutputMessage(
363401
message: ' - :: Install within generated sources failed. ::',
364402
level: Level.SEVERE,
365-
additionalContext: result.stderr,
403+
additionalContext: results.stderr,
366404
stackTrace: StackTrace.current,
367405
),
368406
);
@@ -371,7 +409,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
371409
log: log,
372410
communication: OutputMessage(
373411
message: [
374-
result.stdout,
412+
if (args.isDebug) results.stdout,
375413
' - :: Install completed successfully. ::',
376414
].join('\n'),
377415
),

0 commit comments

Comments
 (0)