Skip to content

Commit ee93955

Browse files
committed
test(annotations): fixed failing test
1 parent 4152aab commit ee93955

15 files changed

+246
-60
lines changed

melos_openapi_generator_dart.iml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
<excludeFolder url="file://$MODULE_DIR$/example/api/petstore_api/.dart_tool" />
1111
<excludeFolder url="file://$MODULE_DIR$/example/api/petstore_api/.pub" />
1212
<excludeFolder url="file://$MODULE_DIR$/example/api/petstore_api/build" />
13+
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
14+
<excludeFolder url="file://$MODULE_DIR$/.pub" />
15+
<excludeFolder url="file://$MODULE_DIR$/build" />
1316
</content>
1417
<orderEntry type="sourceFolder" forTests="false" />
1518
<orderEntry type="library" name="Dart SDK" level="project" />
1619
<orderEntry type="library" name="Dart Packages" level="project" />
1720
</component>
18-
</module>
21+
</module>

openapi-generator-cli/bin/main.dart

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,12 @@ import 'dart:convert';
33
import 'dart:io';
44

55
import 'package:http/http.dart' as http;
6+
import 'package:openapi_generator_cli/src/models.dart';
67
import 'package:path/path.dart' as p;
78

89
const baseDownloadUrl =
910
'https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli';
1011

11-
/// Default configuration values
12-
class ConfigDefaults {
13-
static const openapiGeneratorVersion = '7.9.0';
14-
static const additionalCommands = '';
15-
static const downloadUrlOverride = null;
16-
static const jarCacheDir = '.dart_tool/openapi_generator_cache';
17-
static const customGeneratorUrls = <String>[
18-
'https://repo1.maven.org/maven2/com/bluetrainsoftware/maven/openapi-dart-generator/7.2/openapi-dart-generator-7.2.jar'
19-
];
20-
}
21-
22-
/// Configuration keys as static constants
23-
class ConfigKeys {
24-
static const openapiGeneratorVersion = 'openapiGeneratorVersion';
25-
static const additionalCommands = 'additionalCommands';
26-
static const downloadUrlOverride = 'downloadUrlOverride';
27-
static const jarCachePath = 'jarCacheDir';
28-
static const customGeneratorUrls = 'customGeneratorUrls';
29-
}
30-
3112
/// Resolves a given path to an absolute path, handling both relative and absolute inputs
3213
String resolvePath(String path) {
3314
return p.isAbsolute(path) ? path : p.absolute(Directory.current.path, path);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import 'package:args/command_runner.dart';
2+
import 'package:openapi_generator_cli/src/models.dart';
3+
4+
class GenerateCommand extends Command {
5+
// The [name] and [description] properties must be defined by every
6+
// subclass.
7+
final name = "generate";
8+
final description = "Record changes to the repository.";
9+
10+
CommitCommand() {
11+
// Add options based on ConfigDefaults and ConfigKeys
12+
argParser.addOption(ConfigKeys.openapiGeneratorVersion,
13+
help: 'The version of the OpenAPI generator to use.',
14+
defaultsTo: ConfigDefaults.openapiGeneratorVersion);
15+
16+
argParser.addOption(ConfigKeys.additionalCommands,
17+
help:
18+
'Additional commands to pass to the generator. This command will be appended at the end of the commands pas',
19+
defaultsTo: ConfigDefaults.additionalCommands);
20+
21+
argParser.addOption(ConfigKeys.downloadUrlOverride,
22+
help: 'A custom URL to override the default download location.',
23+
defaultsTo: ConfigDefaults.downloadUrlOverride);
24+
25+
argParser.addOption(ConfigKeys.jarCachePath,
26+
help: 'The directory where the JAR cache will be stored.',
27+
defaultsTo: ConfigDefaults.jarCacheDir);
28+
29+
argParser.addMultiOption(ConfigKeys.customGeneratorUrls,
30+
help:
31+
'Urls for the jars of additional OpenAPI generators to combine with the official one.',
32+
defaultsTo: ConfigDefaults.customGeneratorUrls);
33+
}
34+
35+
// [run] may also return a Future.
36+
void run() {
37+
// [argResults] is set before [run()] is called and contains the flags/options
38+
// passed to this command.
39+
print(argResults?.option('all'));
40+
}
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// Default configuration values
2+
class ConfigDefaults {
3+
static const openapiGeneratorVersion = '7.9.0';
4+
static const additionalCommands = '';
5+
static const downloadUrlOverride = null;
6+
static const jarCacheDir = '.dart_tool/openapi_generator_cache';
7+
static const customGeneratorUrls = <String>[
8+
'https://repo1.maven.org/maven2/com/bluetrainsoftware/maven/openapi-dart-generator/7.2/openapi-dart-generator-7.2.jar'
9+
];
10+
}
11+
12+
/// Configuration keys as static constants
13+
class ConfigKeys {
14+
static const openapiGeneratorVersion = 'openapiGeneratorVersion';
15+
static const additionalCommands = 'additionalCommands';
16+
static const downloadUrlOverride = 'downloadUrlOverride';
17+
static const jarCachePath = 'jarCacheDir';
18+
static const customGeneratorUrls = 'customGeneratorUrls';
19+
}

openapi-generator-cli/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ environment:
99
dependencies:
1010
http: '>=1.2.2 <=2.0.0'
1111
path: '>=1.9.0 <=2.0.0'
12+
args: '>=2.6.0 <3.0.0'
13+
cli_launcher: ^0.3.1
1214

1315
dev_dependencies:
1416
pedantic:

openapi-generator-cli/test/openapi_generator_cli_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:io';
22

33
import 'package:http/http.dart' as http;
44
import 'package:http/testing.dart';
5+
import 'package:openapi_generator_cli/src/models.dart';
56
import 'package:path/path.dart' as p;
67
import 'package:test/test.dart';
78

openapi-generator/lib/src/openapi_generator_runner.dart

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:openapi_generator/src/process_runner.dart';
1212
import 'package:openapi_generator/src/utils.dart';
1313
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart'
1414
as annots;
15+
import 'package:path/path.dart' as path;
1516
import 'package:source_gen/source_gen.dart';
1617

1718
import 'models/command.dart';
@@ -131,7 +132,21 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
131132
workingDirectory: Directory.current.path,
132133
runInShell: Platform.isWindows,
133134
);
134-
135+
var outputDir = path.isRelative(arguments.outputDirectory!)
136+
? path.normalize(
137+
path.join(Directory.current.path, arguments.outputDirectory!))
138+
: path.normalize(arguments.outputDirectory!);
139+
var outputFolderExists = Directory(outputDir).existsSync();
140+
if (!outputFolderExists) {
141+
logOutputMessage(
142+
log: log,
143+
communication: OutputMessage(
144+
message: [
145+
'\n::::::::::::::::::::::::\n:: Seems the code may not have been generated. If you do not find more info in the logs, set debugLogging to true on your annotation and try again.\n::::::::::::::::::::::::',
146+
].join('\n'),
147+
),
148+
);
149+
}
135150
if (result.exitCode != 0) {
136151
return Future.error(
137152
OutputMessage(

openapi-generator/melos_openapi_generator.iml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<excludeFolder url="file://$MODULE_DIR$/test/specs/issue/115/output/.dart_tool" />
2424
<excludeFolder url="file://$MODULE_DIR$/test/specs/issue/115/output/.pub" />
2525
<excludeFolder url="file://$MODULE_DIR$/test/specs/issue/115/output/build" />
26+
<excludeFolder url="file://$MODULE_DIR$/test/specs/issue/167/output/.dart_tool" />
27+
<excludeFolder url="file://$MODULE_DIR$/test/specs/issue/167/output/.pub" />
28+
<excludeFolder url="file://$MODULE_DIR$/test/specs/issue/167/output/build" />
2629
</content>
2730
<orderEntry type="sourceFolder" forTests="false" />
2831
<orderEntry type="library" name="Dart SDK" level="project" />

openapi-generator/test/github_issues_test.dart

Lines changed: 103 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import 'utils.dart';
1010

1111
/// We test the build runner by mocking the specs and then checking the output
1212
/// content for the expected generate command.
13+
///
14+
/// we do not use mock process runner for github issues because we want to test
15+
/// that generated code compiles.
16+
/// If you do not want to generate the actual code, then you can initialise [MockProcessRunner] in the test
1317
void main() {
14-
// we do not use mock process runner for github issues because we want to test
15-
// that generated code compiles
1618
var processRunner = ProcessRunner();
1719
group('Github Issues', () {
1820
// setUpAll(() {
@@ -58,11 +60,8 @@ void main() {
5860
process: processRunner,
5961
);
6062

61-
expect(generatedOutput,
62-
contains('Skipping source gen because generator does not need it.'),
63-
reason: generatedOutput);
64-
expect(generatedOutput, contains('Successfully formatted code.'),
65-
reason: generatedOutput);
63+
expectSourceGenSkipped(generatedOutput);
64+
expectCodeFormattedSuccessfully(generatedOutput);
6665
var analyzeResult = await Process.run(
6766
'dart',
6867
['analyze'],
@@ -101,11 +100,9 @@ void main() {
101100
annotatedFileContent.replaceAll('{{issueNumber}}', issueNumber),
102101
);
103102

104-
expect(generatedOutput,
105-
contains('Skipping source gen because generator does not need it.'),
106-
reason: generatedOutput);
107-
expect(generatedOutput, contains('Successfully formatted code.'),
108-
reason: generatedOutput);
103+
expectSourceGenSkipped(generatedOutput);
104+
expectCodeFormattedSuccessfully(generatedOutput);
105+
109106
var analyzeResult = await Process.run(
110107
'dart',
111108
['analyze', '--no-fatal-warnings'],
@@ -133,13 +130,8 @@ void main() {
133130
annotatedFileContent.replaceAll('{{issueNumber}}', issueNumber),
134131
);
135132

136-
expect(
137-
generatedOutput,
138-
contains(
139-
'pub run build_runner build --delete-conflicting-outputs'),
140-
reason: generatedOutput);
141-
expect(generatedOutput, contains('Successfully formatted code.'),
142-
reason: generatedOutput);
133+
expectSourceGenRun(generatedOutput);
134+
expectCodeFormattedSuccessfully(generatedOutput);
143135
var workingDirectory = path.join(parentFolder, 'output');
144136
var analyzeResult = await Process.run(
145137
'dart',
@@ -180,11 +172,8 @@ void main() {
180172
annotatedFileContent.replaceAll('{{issueNumber}}', issueNumber),
181173
);
182174

183-
expect(generatedOutput,
184-
contains('Skipping source gen because generator does not need it.'),
185-
reason: generatedOutput);
186-
expect(generatedOutput, contains('Successfully formatted code.'),
187-
reason: generatedOutput);
175+
expectSourceGenSkipped(generatedOutput);
176+
expectCodeFormattedSuccessfully(generatedOutput);
188177
var analyzeResult = await Process.run(
189178
'dart',
190179
['analyze', '--fatal-warnings'],
@@ -212,13 +201,8 @@ void main() {
212201
annotatedFileContent.replaceAll('{{issueNumber}}', issueNumber),
213202
);
214203

215-
expect(
216-
generatedOutput,
217-
contains(
218-
'pub run build_runner build --delete-conflicting-outputs'),
219-
reason: generatedOutput);
220-
expect(generatedOutput, contains('Successfully formatted code.'),
221-
reason: generatedOutput);
204+
expectSourceGenRun(generatedOutput);
205+
expectCodeFormattedSuccessfully(generatedOutput);
222206
var workingDirectory = path.join(parentFolder, 'output');
223207
await Process.run(
224208
'dart',
@@ -257,11 +241,8 @@ void main() {
257241
var generatedOutput = await generateFromPath(annotatedFile.path,
258242
process: processRunner, openapiSpecFilePath: inputSpecFile.path);
259243

260-
expect(generatedOutput,
261-
contains('Skipping source gen because generator does not need it.'),
262-
reason: generatedOutput);
263-
expect(generatedOutput, contains('Successfully formatted code.'),
264-
reason: generatedOutput);
244+
expectSourceGenSkipped(generatedOutput);
245+
expectCodeFormattedSuccessfully(generatedOutput);
265246
var analyzeResult = await Process.run(
266247
'dart',
267248
['analyze', '--no-fatal-warnings'],
@@ -282,8 +263,7 @@ void main() {
282263
var generatedOutput = await generateFromPath(annotatedFile.path,
283264
process: processRunner, openapiSpecFilePath: inputSpecFile.path);
284265

285-
expect(generatedOutput, contains('Successfully formatted code.'),
286-
reason: generatedOutput);
266+
expectCodeFormattedSuccessfully(generatedOutput);
287267
var workingDirectory = path.join(parentFolder, 'output');
288268
var analyzeResult = await Process.run(
289269
'dart',
@@ -333,5 +313,90 @@ void main() {
333313
cleanup(workingDirectory);
334314
}, skip: true);
335315
});
316+
317+
group('#164', () {
318+
var issueNumber = '164';
319+
var parentFolder = path.join(testSpecPath, 'issue', issueNumber);
320+
var workingDirectory = path.join(parentFolder, 'output');
321+
setUpAll(
322+
() {
323+
var workingDirectory = path.join(parentFolder, 'output');
324+
cleanup(workingDirectory);
325+
},
326+
);
327+
test('[dio] Test that generation does not fail', () async {
328+
var generatedOutput = await generateFromAnnotation(
329+
Openapi(
330+
additionalProperties: DioProperties(
331+
pubName: 'petstore_api', pubAuthor: 'Johnny_dep'),
332+
inputSpec: RemoteSpec(
333+
path: 'https://petstore3.swagger.io/api/v3/openapi.json'),
334+
typeMappings: {'Pet': 'ExamplePet'},
335+
generatorName: Generator.dio,
336+
runSourceGenOnOutput: true,
337+
skipIfSpecIsUnchanged: false,
338+
cleanSubOutputDirectory: [
339+
'./test/specs/issue/$issueNumber/output'
340+
],
341+
outputDirectory: './test/specs/issue/$issueNumber/output'),
342+
process: processRunner,
343+
);
344+
345+
expectSourceGenRun(generatedOutput);
346+
expectCodeFormattedSuccessfully(generatedOutput);
347+
var analyzeResult = await Process.run(
348+
'dart',
349+
['analyze', '--no-fatal-warnings'],
350+
workingDirectory: workingDirectory,
351+
);
352+
expect(analyzeResult.exitCode, 0,
353+
reason: '${analyzeResult.stdout}\n\n${analyzeResult.stderr}');
354+
cleanup(workingDirectory);
355+
});
356+
});
357+
358+
group('#167', () {
359+
var issueNumber = '167';
360+
var parentFolder = path.join(testSpecPath, 'issue', issueNumber);
361+
var workingDirectory = path.join(parentFolder, 'output');
362+
setUpAll(
363+
() {
364+
var workingDirectory = path.join(parentFolder, 'output');
365+
cleanup(workingDirectory);
366+
},
367+
);
368+
test('[dio] Test that generation does not fail', () async {
369+
var generatedOutput = await generateFromAnnotation(
370+
Openapi(
371+
additionalProperties: DioAltProperties(
372+
pubName: 'issue_api',
373+
),
374+
inputSpec: InputSpec(
375+
path:
376+
'./test/specs/issue/$issueNumber/github_issue_#167.yaml'),
377+
generatorName: Generator.dio,
378+
runSourceGenOnOutput: true,
379+
typeMappings: {'Pet': 'ExamplePet', 'Test': 'ExampleTest'},
380+
skipIfSpecIsUnchanged: false,
381+
cleanSubOutputDirectory: [
382+
'./test/specs/issue/$issueNumber/output'
383+
],
384+
outputDirectory: './test/specs/issue/$issueNumber/output'),
385+
process: processRunner,
386+
);
387+
388+
expectSourceGenRun(generatedOutput);
389+
expectCodeFormattedSuccessfully(generatedOutput);
390+
var analyzeResult = await Process.run(
391+
'dart',
392+
['analyze', '--no-fatal-warnings'],
393+
workingDirectory: workingDirectory,
394+
);
395+
expect(analyzeResult.exitCode, 0,
396+
reason: '${analyzeResult.stdout}\n\n${analyzeResult.stderr}');
397+
398+
cleanup(workingDirectory);
399+
});
400+
});
336401
});
337402
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type: object
2+
properties:
3+
optionA1:
4+
type: string
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type: object
2+
properties:
3+
optionB1:
4+
type: string
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
openapi: 3.0.3
2+
info:
3+
version: '1.0'
4+
title: Test
5+
paths:
6+
/test:
7+
get:
8+
operationId: test
9+
responses:
10+
'200':
11+
description: response
12+
content:
13+
application/json:
14+
schema:
15+
$ref: "./test.yml"

0 commit comments

Comments
 (0)