From 4156d9a18bf83337e608219315d19abbe08f8bd8 Mon Sep 17 00:00:00 2001 From: Nexushunter Date: Fri, 22 Sep 2023 20:54:04 -0400 Subject: [PATCH] fix: Correct most of the tests while using the newest verison of the source gen changes --- .../openapi_generator_annotations_test.dart | 1 + .../lib/src/models/generator_arguments.dart | 47 ++++++++++--------- .../lib/src/openapi_generator_runner.dart | 2 +- openapi-generator/test/generator_test.dart | 19 ++------ openapi-generator/test/mocks.dart | 2 - .../next_gen_builder_local_test_config.dart | 16 ------- .../next_gen_builder_test_aws_config.dart | 21 --------- ...next_gen_builder_test_invalid_config.dart} | 4 +- 8 files changed, 34 insertions(+), 78 deletions(-) delete mode 100644 openapi-generator/test/specs/next_gen_builder_local_test_config.dart delete mode 100644 openapi-generator/test/specs/next_gen_builder_test_aws_config.dart rename openapi-generator/test/specs/{next_gen_builder_dio_alt_test_config.dart => next_gen_builder_test_invalid_config.dart} (89%) diff --git a/openapi-generator-annotations/test/openapi_generator_annotations_test.dart b/openapi-generator-annotations/test/openapi_generator_annotations_test.dart index 26ab99b..3011960 100644 --- a/openapi-generator-annotations/test/openapi_generator_annotations_test.dart +++ b/openapi-generator-annotations/test/openapi_generator_annotations_test.dart @@ -176,6 +176,7 @@ void main() { } }); test('uses the provided environment', () async { + print(Directory.current.path); final result = Process.runSync( 'dart', [ diff --git a/openapi-generator/lib/src/models/generator_arguments.dart b/openapi-generator/lib/src/models/generator_arguments.dart index ee5420b..5ec1246 100644 --- a/openapi-generator/lib/src/models/generator_arguments.dart +++ b/openapi-generator/lib/src/models/generator_arguments.dart @@ -114,44 +114,47 @@ class GeneratorArguments { GeneratorArguments({ required Openapi annotation, - bool alwaysRun = false, + bool? alwaysRun, String? inputSpecFile, - InputSpec inputSpec = const InputSpec.empty(), - String templateDirectory = '', + InputSpec? inputSpec, + String? templateDirectory, Generator? generator, - Map typeMapping = const {}, - Map importMapping = const {}, - Map reservedWordsMapping = const {}, - Map inlineSchemaNameMapping = const {}, + Map? typeMapping, + Map? importMapping, + Map? reservedWordsMapping, + Map? inlineSchemaNameMapping, AdditionalProperties? additionalProperties, InlineSchemaOptions? inlineSchemaOptions, - bool skipValidation = false, - bool runSourceGen = true, + bool? skipValidation, + bool? runSourceGen, String? outputDirectory, - bool fetchDependencies = true, + bool? fetchDependencies, bool? useNextGen, String? cachePath, String? pubspecPath, - bool isDebug = false, - }) : alwaysRun = annotation.alwaysRun ?? alwaysRun, + bool? isDebug, + }) : alwaysRun = alwaysRun ?? annotation.alwaysRun ?? true, _inputFile = inputSpecFile ?? annotation.inputSpecFile, - templateDirectory = annotation.templateDirectory ?? templateDirectory, + templateDirectory = + templateDirectory ?? annotation.templateDirectory ?? '', generator = generator ?? annotation.generatorName, - typeMappings = annotation.typeMappings ?? typeMapping, - importMappings = annotation.importMappings ?? importMapping, + typeMappings = typeMapping ?? annotation.typeMappings ?? {}, + importMappings = importMapping ?? annotation.importMappings ?? {}, reservedWordsMappings = - annotation.reservedWordsMappings ?? reservedWordsMapping, - inlineSchemaNameMappings = - annotation.inlineSchemaNameMappings ?? inlineSchemaNameMapping, + reservedWordsMapping ?? annotation.reservedWordsMappings ?? {}, + inlineSchemaNameMappings = inlineSchemaNameMapping ?? + annotation.inlineSchemaNameMappings ?? + {}, additionalProperties = additionalProperties ?? annotation.additionalProperties, inlineSchemaOptions = inlineSchemaOptions, // ?? annotations.readPropertyOrDefault( // 'inlineSchemaOptions', inlineSchemaOptions), - skipValidation = annotation.skipSpecValidation ?? skipValidation, - runSourceGen = annotation.runSourceGenOnOutput ?? runSourceGen, + skipValidation = + skipValidation ?? annotation.skipSpecValidation ?? false, + runSourceGen = runSourceGen ?? annotation.runSourceGenOnOutput ?? true, shouldFetchDependencies = - annotation.fetchDependencies ?? fetchDependencies, + fetchDependencies ?? annotation.fetchDependencies ?? true, outputDirectory = annotation.outputDirectory ?? outputDirectory ?? Directory.current.path, @@ -161,7 +164,7 @@ class GeneratorArguments { pubspecPath ?? '${Directory.current.path}${Platform.pathSeparator}pubspec.yaml', isDebug = annotation.debugLogging, - inputSpec = annotation.inputSpec ?? inputSpec; + inputSpec = inputSpec ?? annotation.inputSpec ?? InputSpec.empty(); /// The stringified name of the [Generator]. String get generatorName => generator == Generator.dart diff --git a/openapi-generator/lib/src/openapi_generator_runner.dart b/openapi-generator/lib/src/openapi_generator_runner.dart index 6e687e2..4f182d5 100755 --- a/openapi-generator/lib/src/openapi_generator_runner.dart +++ b/openapi-generator/lib/src/openapi_generator_runner.dart @@ -49,7 +49,7 @@ class OpenapiGenerator extends GeneratorForAnnotation { todo: 'Remove the [Openapi] annotation from `$friendlyName`.', ); } else { - final apiAnnotation = Reviver(annotations) as annots.Openapi; + final apiAnnotation = Reviver(annotations).toInstance() as annots.Openapi; if (!apiAnnotation.useNextGen && apiAnnotation.cachePath != null) { throw AssertionError('useNextGen must be set when using cachePath'); diff --git a/openapi-generator/test/generator_test.dart b/openapi-generator/test/generator_test.dart index 136d9b7..efa9937 100644 --- a/openapi-generator/test/generator_test.dart +++ b/openapi-generator/test/generator_test.dart @@ -18,7 +18,6 @@ import 'utils.dart'; void main() { group('OpenApiGenerator', () { group('NextGen', () { - late MockConstantReader mockedAnnotations; late src_gen.ConstantReader defaultAnnotations; late Openapi annotation; late GeneratorArguments realArguments; @@ -29,7 +28,6 @@ void main() { resetMockitoState(); mockedArgs = MockGeneratorArguments(); mockRunner = MockCommandRunner(); - mockedAnnotations = MockConstantReader(); defaultAnnotations = await loadAnnoation('next_gen_builder_test_config.dart'); annotation = src_gen.Reviver(defaultAnnotations).toInstance(); @@ -71,19 +69,11 @@ void main() { test('throws AssertionError when useCache is set but useNextGen is not', () async { - final mockedUseNextGen = MockConstantReader(); - when(mockedUseNextGen.literalValue).thenReturn(false); - - final mockedUseCachePath = MockConstantReader(); - when(mockedUseCachePath.literalValue).thenReturn('something'); - - when(mockedAnnotations.read('useNextGen')).thenReturn(mockedUseNextGen); - when(mockedAnnotations.read('cachePath')) - .thenReturn(mockedUseCachePath); - try { await OpenapiGenerator().generateForAnnotatedElement( - MockClassElement(), mockedAnnotations, MockBuildStep()); + MockClassElement(), + await loadAnnoation('next_gen_builder_test_invalid_config.dart'), + MockBuildStep()); fail('Should throw when useNextGen is false and cache path is set.'); } catch (e, _) { expect(e, isA()); @@ -188,7 +178,8 @@ void main() { cachedSpec: anyNamed('cachedSpec'), loadedSpec: anyNamed('loadedSpec'))) .thenAnswer((_) async => true); - final args = GeneratorArguments(annotation: annotation); + final args = GeneratorArguments( + annotation: annotation, generator: Generator.dart); await OpenapiGenerator(logger: logger, runner: mockRunner) .generatorV2( args: args, diff --git a/openapi-generator/test/mocks.dart b/openapi-generator/test/mocks.dart index 301fd6f..d167d09 100644 --- a/openapi-generator/test/mocks.dart +++ b/openapi-generator/test/mocks.dart @@ -7,11 +7,9 @@ import 'package:openapi_generator/src/models/command.dart'; import 'package:openapi_generator/src/models/generator_arguments.dart'; import 'package:openapi_generator/src/openapi_generator_runner.dart'; import 'package:openapi_generator_annotations/openapi_generator_annotations.dart'; -import 'package:source_gen/source_gen.dart'; @GenerateNiceMocks([ MockSpec(), - MockSpec(), MockSpec(), MockSpec(), MockSpec(), diff --git a/openapi-generator/test/specs/next_gen_builder_local_test_config.dart b/openapi-generator/test/specs/next_gen_builder_local_test_config.dart deleted file mode 100644 index ec5da7e..0000000 --- a/openapi-generator/test/specs/next_gen_builder_local_test_config.dart +++ /dev/null @@ -1,16 +0,0 @@ -library test_lib; - -import 'package:openapi_generator_annotations/openapi_generator_annotations.dart'; - -@Openapi( - inputSpecFile: - 'https://raw.githubusercontent.com/Nexushunter/tagmine-api/main/openapi.yaml', - inputSpec: InputSpec( - path: './test/specs/openapi.test.yaml', - ), - generatorName: Generator.dart, - fetchDependencies: true, - useNextGen: true, - cachePath: './test/specs/output-nextgen/expected-args/cache.json', - outputDirectory: './test/specs/output-nextgen/expected-args') -class TestClassConfig {} diff --git a/openapi-generator/test/specs/next_gen_builder_test_aws_config.dart b/openapi-generator/test/specs/next_gen_builder_test_aws_config.dart deleted file mode 100644 index d29e3b7..0000000 --- a/openapi-generator/test/specs/next_gen_builder_test_aws_config.dart +++ /dev/null @@ -1,21 +0,0 @@ -library test_lib; - -import 'package:openapi_generator_annotations/openapi_generator_annotations.dart'; - -@Openapi( - inputSpecFile: - 'https://raw.githubusercontent.com/Nexushunter/tagmine-api/main/openapi.yaml', - inputSpec: RemoteSpec( - path: - 'http://bucket.s3.us-east-1.localhost.localstack.cloud:4566/openapi.yaml', - headerDelegate: AWSRemoteSpecHeaderDelegate( - bucket: 'bucket', - accessKeyId: 'test', - secretAccessKey: 'test', - ), - ), - generatorName: Generator.dio, - useNextGen: true, - cachePath: './test/specs/output-nextgen/expected-args/cache.json', - outputDirectory: './test/specs/output-nextgen/expected-args') -class TestClassConfig {} diff --git a/openapi-generator/test/specs/next_gen_builder_dio_alt_test_config.dart b/openapi-generator/test/specs/next_gen_builder_test_invalid_config.dart similarity index 89% rename from openapi-generator/test/specs/next_gen_builder_dio_alt_test_config.dart rename to openapi-generator/test/specs/next_gen_builder_test_invalid_config.dart index e147d6a..a19474d 100644 --- a/openapi-generator/test/specs/next_gen_builder_dio_alt_test_config.dart +++ b/openapi-generator/test/specs/next_gen_builder_test_invalid_config.dart @@ -9,8 +9,8 @@ import 'package:openapi_generator_annotations/openapi_generator_annotations.dart path: 'https://raw.githubusercontent.com/Nexushunter/tagmine-api/main/openapi.yaml', ), - generatorName: Generator.dioAlt, - useNextGen: true, + generatorName: Generator.dio, + useNextGen: false, cachePath: './test/specs/output-nextgen/expected-args/cache.json', outputDirectory: './test/specs/output-nextgen/expected-args') class TestClassConfig {}