Skip to content

Commit

Permalink
fix: Correct most of the tests while using the newest verison of the …
Browse files Browse the repository at this point in the history
…source gen changes
  • Loading branch information
Nexushunter committed Sep 23, 2023
1 parent 37af696 commit 4156d9a
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ void main() {
}
});
test('uses the provided environment', () async {
print(Directory.current.path);
final result = Process.runSync(
'dart',
[
Expand Down
47 changes: 25 additions & 22 deletions openapi-generator/lib/src/models/generator_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> typeMapping = const {},
Map<String, String> importMapping = const {},
Map<String, String> reservedWordsMapping = const {},
Map<String, String> inlineSchemaNameMapping = const {},
Map<String, String>? typeMapping,
Map<String, String>? importMapping,
Map<String, String>? reservedWordsMapping,
Map<String, String>? 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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion openapi-generator/lib/src/openapi_generator_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
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');
Expand Down
19 changes: 5 additions & 14 deletions openapi-generator/test/generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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<AssertionError>());
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions openapi-generator/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenapiGenerator>(),
MockSpec<ConstantReader>(),
MockSpec<BuildStep>(),
MockSpec<MethodElement>(),
MockSpec<ClassElement>(),
Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions openapi-generator/test/specs/next_gen_builder_test_aws_config.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

0 comments on commit 4156d9a

Please sign in to comment.