Skip to content

Commit 45870e8

Browse files
authored
Merge pull request #108 from Nexushunter/refactor-testing
Refactor testing
2 parents a95675f + bfb0ec4 commit 45870e8

30 files changed

+1605
-1378
lines changed

.github/workflows/code_quality.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ jobs:
2525
steps:
2626
- name: Checkout
2727
uses: actions/checkout@v3
28-
- name: Start Docker compose containers
29-
if: ${{ matrix.work_dir == 'openapi-generator' }}
30-
run: docker-compose -f "docker-compose.yaml" up -d --build
3128
- name: Setup Dart
3229
uses: dart-lang/setup-dart@v1.5.0
3330
with:
@@ -36,6 +33,9 @@ jobs:
3633
run: dart pub get
3734
- name: Validate formatting
3835
run: dart format ./ --set-exit-if-changed
36+
- name: Generate Mocks
37+
if: ${{ matrix.work_dir == 'openapi-generator' }}
38+
run: dart run build_runner build --delete-conflicting-outputs
3939
- name: Run analyzer
4040
run: dart analyze --fatal-warnings
4141
- name: Run tests
@@ -51,9 +51,6 @@ jobs:
5151
verbose: true
5252
flags: ${{ matrix.work_dir }}
5353
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
54-
- name: Stop Docker Container
55-
if: ${{ matrix.work_dir == 'openapi-generator' && always() }}
56-
run: docker-compose -f "docker-compose.yaml" down
5754

5855
build:
5956
name: Build example project 🛠️

example/pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ dependency_overrides:
2727
path: ../openapi-generator
2828
openapi_generator_cli:
2929
path: ../openapi-generator-cli
30-
30+
source_gen:
31+
git:
32+
ref: reviver
33+
url: https://github.com/Nexushunter/source_gen
34+
path: source_gen
3135

3236
# Dependencies specify other packages that your package needs in order to work.
3337
# To automatically upgrade your package dependencies to the latest versions

openapi-generator-annotations/lib/src/openapi_generator_annotations_base.dart

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
library openapi_generator_annotations;
2+
13
import 'dart:convert';
24
import 'dart:io';
35

46
import 'package:crypto/crypto.dart';
57
import 'package:meta/meta.dart';
8+
import 'package:source_gen/source_gen.dart';
69

710
/// Config base class
811
/// Your annotated class must extend this config class
@@ -226,11 +229,6 @@ class RemoteSpec extends InputSpec {
226229
const RemoteSpec.empty() : this(path: 'http://localhost:8080/');
227230

228231
Uri get url => Uri.parse(path);
229-
230-
RemoteSpec.fromMap(Map<String, dynamic> map)
231-
: headerDelegate =
232-
map['headerDelegate'] ?? const RemoteSpecHeaderDelegate(),
233-
super.fromMap(map);
234232
}
235233

236234
/// Default [RemoteSpecHeaderDelegate] used when retrieving a remote OAS spec.
@@ -266,12 +264,6 @@ class AWSRemoteSpecHeaderDelegate extends RemoteSpecHeaderDelegate {
266264
this.accessKeyId = null,
267265
}) : super();
268266

269-
AWSRemoteSpecHeaderDelegate.fromMap(Map<String, dynamic> map)
270-
: bucket = map['bucket'],
271-
accessKeyId = map['accessKeyId'],
272-
secretAccessKey = map['secretAccessKey'],
273-
super.fromMap(map);
274-
275267
/// Generates the [header] map used within the GET request.
276268
///
277269
/// Assumes that the user's auth AWS credentials

openapi-generator-annotations/pubspec.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ environment:
1010
dependencies:
1111
crypto: '>=3.0.0 <4.0.0'
1212
meta: '>=1.3.0 <2.0.0'
13+
source_gen:
1314

1415
dev_dependencies:
1516
test:
1617
source_gen_test:
1718
lint:
19+
20+
dependency_overrides:
21+
source_gen:
22+
git:
23+
ref: reviver
24+
url: https://github.com/Nexushunter/source_gen.git
25+
path: source_gen

openapi-generator-annotations/test/openapi_generator_annotations_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void main() {
9191
expect(remote.path, 'http://localhost:8080/');
9292
expect(remote.headerDelegate, isA<RemoteSpecHeaderDelegate>());
9393
});
94+
9495
test('uses path', () {
9596
final remote = RemoteSpec(path: 'https://example.com/path');
9697
expect(remote.path, 'https://example.com/path');
@@ -112,7 +113,6 @@ void main() {
112113
accessKeyId: 'test',
113114
secretAccessKey: 'test',
114115
);
115-
116116
test('signs the url correctly', () {
117117
final now = DateTime.now();
118118
final actual = delegate.authHeaderContent(
@@ -176,6 +176,7 @@ void main() {
176176
}
177177
});
178178
test('uses the provided environment', () async {
179+
print(Directory.current.path);
179180
final result = Process.runSync(
180181
'dart',
181182
[

openapi-generator/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,6 @@ example/.dart_tool
169169

170170
# Generated test output
171171
test/specs/test-cached.json
172-
test/specs/localstack/**
172+
test/specs/commands-cache.json
173+
test/specs/localstack
174+
test/mocks.mocks.dart

openapi-generator/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: gen
2+
3+
gen:
4+
dart run build_runner build --delete-conflicting-outputs

openapi-generator/docker-compose.yaml

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import 'dart:mirrors';
22

33
import 'package:analyzer/dart/element/type.dart';
4-
import 'package:openapi_generator/src/utils.dart';
5-
import 'package:openapi_generator_annotations/openapi_generator_annotations.dart';
64
import 'package:source_gen/source_gen.dart' show ConstantReader, TypeChecker;
75

86
/// Extension adding the type methods to `ConstantReader`.
@@ -73,100 +71,3 @@ extension TypeMethods on ConstantReader {
7371
return values[enumIndex];
7472
}
7573
}
76-
77-
extension ReadProperty on ConstantReader {
78-
T readPropertyOrDefault<T>(String name, T defaultValue) {
79-
final v = peek(name);
80-
if (v == null) {
81-
return defaultValue;
82-
}
83-
84-
if (isA(v, InputSpec)) {
85-
final revived = v.revive();
86-
87-
if (isA(v, RemoteSpec)) {
88-
final map = revived.namedArguments;
89-
final delegate = map['headerDelegate'];
90-
final mapped = <String, dynamic>{
91-
'path': convertToPropertyValue(map['path']!),
92-
};
93-
if (delegate?.isNull ?? true) {
94-
return RemoteSpec.fromMap(mapped) as T;
95-
} else {
96-
final delegateReader = ConstantReader(delegate);
97-
if (isA(delegateReader, AWSRemoteSpecHeaderDelegate)) {
98-
mapped['headerDelegate'] = AWSRemoteSpecHeaderDelegate.fromMap(
99-
delegateReader.revive().namedArguments.map(
100-
(key, value) => MapEntry(
101-
key,
102-
convertToPropertyValue(value),
103-
),
104-
),
105-
);
106-
}
107-
return RemoteSpec.fromMap(mapped) as T;
108-
}
109-
} else {
110-
final map = revived.namedArguments.map(
111-
(key, value) => MapEntry(
112-
key,
113-
convertToPropertyValue(value),
114-
),
115-
);
116-
return InputSpec.fromMap(map) as T;
117-
}
118-
}
119-
120-
if (isA(v, AdditionalProperties)) {
121-
final map = v.revive().namedArguments.map(
122-
(key, value) => MapEntry(
123-
key,
124-
convertToPropertyValue(value),
125-
),
126-
);
127-
if (isA(v, DioProperties)) {
128-
return DioProperties.fromMap(map) as T;
129-
} else if (isA(v, DioAltProperties)) {
130-
return DioAltProperties.fromMap(map) as T;
131-
} else {
132-
return AdditionalProperties.fromMap(map) as T;
133-
}
134-
}
135-
136-
if (isA(v, InlineSchemaOptions)) {
137-
return InlineSchemaOptions.fromMap(
138-
v.revive().namedArguments.map(
139-
(key, value) => MapEntry(
140-
key,
141-
convertToPropertyValue(value),
142-
),
143-
),
144-
) as T;
145-
}
146-
147-
if (isA(v, Map<String, String>)) {
148-
return v.mapValue.map((key, value) => MapEntry(
149-
convertToPropertyValue(key!) as String,
150-
convertToPropertyValue(value!) as String)) as T;
151-
} else if (isA(v, bool)) {
152-
return v.boolValue as T;
153-
} else if (isA(v, double)) {
154-
return v.doubleValue as T;
155-
} else if (isA(v, int)) {
156-
return v.intValue as T;
157-
} else if (isA(v, String)) {
158-
return v.stringValue as T;
159-
} else if (isA(v, Set)) {
160-
return v.setValue.map(convertToPropertyValue) as T;
161-
} else if (isA(v, List)) {
162-
return v.listValue.map(convertToPropertyValue) as T;
163-
} else if (isA(v, Enum)) {
164-
return v.enumValue();
165-
} else {
166-
return defaultValue;
167-
}
168-
}
169-
}
170-
171-
bool isA(ConstantReader? v, Type t) =>
172-
v?.instanceOf(TypeChecker.fromRuntime(t)) ?? false;

openapi-generator/lib/src/gen_on_spec_changes.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@ FutureOr<Map<String, dynamic>> loadSpec(
5959
}
6060
} else {
6161
Map<String, String>? headers;
62-
if (specConfig.headerDelegate is AWSRemoteSpecHeaderDelegate) {
63-
try {
64-
headers = (specConfig.headerDelegate as AWSRemoteSpecHeaderDelegate)
65-
.header(path: specConfig.url.path);
66-
} catch (e, st) {
67-
return Future.error(
68-
OutputMessage(
69-
message: 'failed to generate AWS headers',
70-
additionalContext: e,
71-
stackTrace: st,
72-
level: Level.SEVERE,
73-
),
74-
);
75-
}
76-
} else {
77-
headers = specConfig.headerDelegate.header();
78-
}
62+
// if (specConfig.headerDelegate is AWSRemoteSpecHeaderDelegate) {
63+
// try {
64+
// headers = (specConfig.headerDelegate as AWSRemoteSpecHeaderDelegate)
65+
// .header(path: specConfig.url.path);
66+
// } catch (e, st) {
67+
// return Future.error(
68+
// OutputMessage(
69+
// message: 'failed to generate AWS headers',
70+
// additionalContext: e,
71+
// stackTrace: st,
72+
// level: Level.SEVERE,
73+
// ),
74+
// );
75+
// }
76+
// } else {
77+
// headers = specConfig.headerDelegate.header();
78+
// }
7979

8080
final resp = await http.get(specConfig.url, headers: headers);
8181
if (resp.statusCode == 200) {

0 commit comments

Comments
 (0)