Skip to content

Commit

Permalink
chore(generator): added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gibahjoe committed Nov 4, 2024
1 parent 8131e8e commit 4a8c843
Show file tree
Hide file tree
Showing 11 changed files with 682 additions and 23 deletions.
12 changes: 12 additions & 0 deletions .github/ISSUE_TEMPLATE/1-bug-report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ body:
description: Explain how a maintainer can reliably reproduce the bug.
validations:
required: true
- type: textarea
attributes:
label: Minimal openapi specification
description: Please provide a minimal specification that reproduces the error.
validations:
required: true
- type: textarea
attributes:
label: Annotation used
description: Please provide the @Openapi annotation you used that reproduces the error.
validations:
required: true
- type: textarea
attributes:
label: Expected behavior
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ doc/api/

pubspec_overrides.yaml
!pubspec.lock

.iml
openapi_generator_config.json
47 changes: 47 additions & 0 deletions example/github_issue_#137.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"openapi": "3.0.1",
"info": {
"title": "Test API",
"version": "1.0"
},
"paths": {
"/Test": {
"get": {
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestDto"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"TestDto": {
"required": [
"values"
],
"type": "object",
"properties": {
"values": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
}
}
}
39 changes: 21 additions & 18 deletions openapi-generator/lib/src/openapi_generator_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,28 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
arguments.cleanSubOutputDirectory?.forEach((directory) {
final childDirectory =
Directory('${arguments.outputDirectory}/${directory.toString()}');
try {
final outputDirectory = Directory('${arguments.outputDirectory}')
.resolveSymbolicLinksSync();
// Make sure is sub directory, avoid ../../
if (childDirectory
.resolveSymbolicLinksSync()
.startsWith(outputDirectory)) {
childDirectory.delete(recursive: true);
if (childDirectory.existsSync() &&
childDirectory.listSync().isNotEmpty) {
try {
final outputDirectory = Directory('${arguments.outputDirectory}')
.resolveSymbolicLinksSync();
// Make sure is sub directory, avoid ../../
if (childDirectory
.resolveSymbolicLinksSync()
.startsWith(outputDirectory)) {
childDirectory.delete(recursive: true);
}
} catch (e, st) {
logOutputMessage(
log: log,
communication: OutputMessage(
message: 'Output directory already empty',
additionalContext: e,
stackTrace: st,
level: Level.WARNING,
),
);
}
} catch (e, st) {
logOutputMessage(
log: log,
communication: OutputMessage(
message: 'Output directory already empty',
additionalContext: e,
stackTrace: st,
level: Level.WARNING,
),
);
}
});
}
Expand Down
134 changes: 134 additions & 0 deletions openapi-generator/test/github_issues_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import 'dart:io';

import 'package:path/path.dart' as path;
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

import 'utils.dart';

/// We test the build runner by mocking the specs and then checking the output
/// content for the expected generate command.
void main() {
group('Github Issues', () {
// setUpAll(() {
// if (!f.existsSync()) {
// f.createSync(recursive: true);
// }
// f.writeAsStringSync('{}');
// });
// tearDown(() {
// if (f.existsSync()) {
// f.deleteSync();
// }
// });
group('#137', () {
var parentFolder = path.join(testSpecPath, 'issue', '137');
var workingDirectory = path.join(parentFolder, 'output');
setUpAll(
() {
var workingDirectory = path.join(parentFolder, 'output');
cleanup(workingDirectory);
},
);
test('Test that valid model generated via List in additional properties',
() async {
var annotatedFile =
File('$parentFolder/github_issue_137_test_config.dart');
var annotatedFileContents = annotatedFile.readAsStringSync();
var inputSpecFile = File('$parentFolder/github_issue_#137.yaml');
// final annotations = (await resolveSource(
// annotatedFileContents,
// (resolver) async =>
// (await resolver.findLibraryByName('test_lib'))!))
// .getClass('TestClassConfig')!
// .metadata
// .map((e) => ConstantReader(e.computeConstantValue()!))
// .first;
// final args = GeneratorArguments(annotations: annotations);
var generatedOutput = await generateForSource(annotatedFile.path,
openapiSpecFilePath: inputSpecFile.path);

var workingDirectory = path.join(parentFolder, 'output');
var analyzeResult = await Process.run(
'dart',
['analyze', '--fatal-warnings'],
workingDirectory: workingDirectory,
);
expect(analyzeResult.exitCode, 0, reason: '${analyzeResult.stdout}');
cleanup(workingDirectory);
}, skip: true);
});

group('#135', () {
var parentFolder = path.join(testSpecPath, 'issue', '135');
var workingDirectory = path.join(parentFolder, 'output');
setUpAll(
() {
var workingDirectory = path.join(parentFolder, 'output');
cleanup(workingDirectory);
},
);
test(
'Test that code generation succeeds on OpenAPI 3.1.0 API definition with dart generator',
() async {
var annotatedFile =
File('$parentFolder/github_issue_135_dart_test_config.dart');
var annotatedFileContents = annotatedFile.readAsStringSync();
var inputSpecFile = File('$parentFolder/github_issue_#135.json');

var generatedOutput = await generateForSource(annotatedFile.path,
openapiSpecFilePath: inputSpecFile.path);

expect(generatedOutput,
contains('Skipping source gen because generator does not need it.'),
reason: generatedOutput);
expect(generatedOutput, contains('Successfully formatted code.'),
reason: generatedOutput);
var analyzeResult = await Process.run(
'dart',
['analyze', '--no-fatal-warnings'],
workingDirectory: workingDirectory,
);
expect(analyzeResult.exitCode, 0,
reason: '${analyzeResult.stdout}\n\n${analyzeResult.stderr}');
cleanup(workingDirectory);
}, skip: true);
test(
'Test that code generation succeeds on OpenAPI 3.1.0 API definition with dio generator',
() async {
var annotatedFile =
File('$parentFolder/github_issue_135_dio_test_config.dart');
var annotatedFileContents = annotatedFile.readAsStringSync();
var inputSpecFile = File('$parentFolder/github_issue_#135.json');

var generatedOutput = await generateForSource(annotatedFile.path,
openapiSpecFilePath: inputSpecFile.path);

// expect(generatedOutput, contains('Skipping source gen because generator does not need it.'),reason:generatedOutput);
expect(generatedOutput, contains('Successfully formatted code.'),
reason: generatedOutput);
var workingDirectory = path.join(parentFolder, 'output');
var analyzeResult = await Process.run(
'dart',
['analyze', '--no-fatal-warnings'],
workingDirectory: workingDirectory,
);
expect(analyzeResult.exitCode, 0,
reason: '${analyzeResult.stdout}\n\n ${analyzeResult.stderr}');
cleanup(workingDirectory);
},
);
});
});
}

void cleanup(String path) async {
final directory = Directory(path);

if (await directory.exists()) {
await directory.delete(recursive: true);
print('Folder deleted successfully.');
} else {
print('Folder does not exist.');
}
}
Loading

0 comments on commit 4a8c843

Please sign in to comment.