|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:path/path.dart' as path; |
| 4 | +import 'package:test/expect.dart'; |
| 5 | +import 'package:test/scaffolding.dart'; |
| 6 | + |
| 7 | +import 'utils.dart'; |
| 8 | + |
| 9 | +/// We test the build runner by mocking the specs and then checking the output |
| 10 | +/// content for the expected generate command. |
| 11 | +void main() { |
| 12 | + group('Github Issues', () { |
| 13 | + // setUpAll(() { |
| 14 | + // if (!f.existsSync()) { |
| 15 | + // f.createSync(recursive: true); |
| 16 | + // } |
| 17 | + // f.writeAsStringSync('{}'); |
| 18 | + // }); |
| 19 | + // tearDown(() { |
| 20 | + // if (f.existsSync()) { |
| 21 | + // f.deleteSync(); |
| 22 | + // } |
| 23 | + // }); |
| 24 | + group('#137', () { |
| 25 | + var parentFolder = path.join(testSpecPath, 'issue', '137'); |
| 26 | + var workingDirectory = path.join(parentFolder, 'output'); |
| 27 | + setUpAll( |
| 28 | + () { |
| 29 | + var workingDirectory = path.join(parentFolder, 'output'); |
| 30 | + cleanup(workingDirectory); |
| 31 | + }, |
| 32 | + ); |
| 33 | + test('Test that valid model generated via List in additional properties', |
| 34 | + () async { |
| 35 | + var annotatedFile = |
| 36 | + File('$parentFolder/github_issue_137_test_config.dart'); |
| 37 | + var annotatedFileContents = annotatedFile.readAsStringSync(); |
| 38 | + var inputSpecFile = File('$parentFolder/github_issue_#137.yaml'); |
| 39 | + // final annotations = (await resolveSource( |
| 40 | + // annotatedFileContents, |
| 41 | + // (resolver) async => |
| 42 | + // (await resolver.findLibraryByName('test_lib'))!)) |
| 43 | + // .getClass('TestClassConfig')! |
| 44 | + // .metadata |
| 45 | + // .map((e) => ConstantReader(e.computeConstantValue()!)) |
| 46 | + // .first; |
| 47 | + // final args = GeneratorArguments(annotations: annotations); |
| 48 | + var generatedOutput = await generateForSource(annotatedFile.path, |
| 49 | + openapiSpecFilePath: inputSpecFile.path); |
| 50 | + |
| 51 | + var workingDirectory = path.join(parentFolder, 'output'); |
| 52 | + var analyzeResult = await Process.run( |
| 53 | + 'dart', |
| 54 | + ['analyze', '--fatal-warnings'], |
| 55 | + workingDirectory: workingDirectory, |
| 56 | + ); |
| 57 | + expect(analyzeResult.exitCode, 0, reason: '${analyzeResult.stdout}'); |
| 58 | + cleanup(workingDirectory); |
| 59 | + }, skip: true); |
| 60 | + }); |
| 61 | + |
| 62 | + group('#135', () { |
| 63 | + var parentFolder = path.join(testSpecPath, 'issue', '135'); |
| 64 | + var workingDirectory = path.join(parentFolder, 'output'); |
| 65 | + setUpAll( |
| 66 | + () { |
| 67 | + var workingDirectory = path.join(parentFolder, 'output'); |
| 68 | + cleanup(workingDirectory); |
| 69 | + }, |
| 70 | + ); |
| 71 | + test( |
| 72 | + 'Test that code generation succeeds on OpenAPI 3.1.0 API definition with dart generator', |
| 73 | + () async { |
| 74 | + var annotatedFile = |
| 75 | + File('$parentFolder/github_issue_135_dart_test_config.dart'); |
| 76 | + var annotatedFileContents = annotatedFile.readAsStringSync(); |
| 77 | + var inputSpecFile = File('$parentFolder/github_issue_#135.json'); |
| 78 | + |
| 79 | + var generatedOutput = await generateForSource(annotatedFile.path, |
| 80 | + openapiSpecFilePath: inputSpecFile.path); |
| 81 | + |
| 82 | + expect(generatedOutput, |
| 83 | + contains('Skipping source gen because generator does not need it.'), |
| 84 | + reason: generatedOutput); |
| 85 | + expect(generatedOutput, contains('Successfully formatted code.'), |
| 86 | + reason: generatedOutput); |
| 87 | + var analyzeResult = await Process.run( |
| 88 | + 'dart', |
| 89 | + ['analyze', '--no-fatal-warnings'], |
| 90 | + workingDirectory: workingDirectory, |
| 91 | + ); |
| 92 | + expect(analyzeResult.exitCode, 0, |
| 93 | + reason: '${analyzeResult.stdout}\n\n${analyzeResult.stderr}'); |
| 94 | + cleanup(workingDirectory); |
| 95 | + }, skip: true); |
| 96 | + test( |
| 97 | + 'Test that code generation succeeds on OpenAPI 3.1.0 API definition with dio generator', |
| 98 | + () async { |
| 99 | + var annotatedFile = |
| 100 | + File('$parentFolder/github_issue_135_dio_test_config.dart'); |
| 101 | + var annotatedFileContents = annotatedFile.readAsStringSync(); |
| 102 | + var inputSpecFile = File('$parentFolder/github_issue_#135.json'); |
| 103 | + |
| 104 | + var generatedOutput = await generateForSource(annotatedFile.path, |
| 105 | + openapiSpecFilePath: inputSpecFile.path); |
| 106 | + |
| 107 | + // expect(generatedOutput, contains('Skipping source gen because generator does not need it.'),reason:generatedOutput); |
| 108 | + expect(generatedOutput, contains('Successfully formatted code.'), |
| 109 | + reason: generatedOutput); |
| 110 | + var workingDirectory = path.join(parentFolder, 'output'); |
| 111 | + var analyzeResult = await Process.run( |
| 112 | + 'dart', |
| 113 | + ['analyze', '--no-fatal-warnings'], |
| 114 | + workingDirectory: workingDirectory, |
| 115 | + ); |
| 116 | + expect(analyzeResult.exitCode, 0, |
| 117 | + reason: '${analyzeResult.stdout}\n\n ${analyzeResult.stderr}'); |
| 118 | + cleanup(workingDirectory); |
| 119 | + }, |
| 120 | + ); |
| 121 | + }); |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +void cleanup(String path) async { |
| 126 | + final directory = Directory(path); |
| 127 | + |
| 128 | + if (await directory.exists()) { |
| 129 | + await directory.delete(recursive: true); |
| 130 | + print('Folder deleted successfully.'); |
| 131 | + } else { |
| 132 | + print('Folder does not exist.'); |
| 133 | + } |
| 134 | +} |
0 commit comments