Skip to content

Commit b9988c1

Browse files
authored
👷 Facts generate utils (#682)
## What does this change? The PR provides a handy util to generate `actual_data` once for all. Tests are also updated accordingly.
1 parent c6b0ec9 commit b9988c1

File tree

49 files changed

+1539
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1539
-229
lines changed

‎.github/workflows/build.yml

+10-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ jobs:
3737
dart pub global activate melos
3838
melos bootstrap
3939
40+
- name: Generate test data and see if changes
41+
run: |
42+
melos gen:actual_data
43+
git --no-pager diff --exit-code packages/core/test_resources/actual_data
44+
4045
- name: Run tests for Dart packages
4146
if: startsWith(matrix.os, 'ubuntu')
4247
run: melos test:dart --no-select
@@ -54,12 +59,12 @@ jobs:
5459
token: ${{ secrets.CODECOV_TOKEN }}
5560
files: ./packages/core/coverage.lcov
5661

57-
- name: Generate example
62+
- name: Generate example then check formats and changes
5863
run: |
5964
melos run gen:examples:command --no-select
6065
melos run gen:examples:build_runner --no-select
66+
dart format --set-exit-if-changed examples
67+
git --no-pager diff --exit-code examples
6168
62-
- name: Check for any formatting and statically analyze the code.
63-
run: |
64-
melos run format
65-
melos analyze
69+
- name: Statically analyze the code
70+
run: melos analyze

‎analysis_options.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ include: package:lints/recommended.yaml
33

44
analyzer:
55
exclude:
6-
- 'example'
76
- '**.g.dart'
87
- 'test_resources/**'
98
errors:

‎melos.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ scripts:
6767
- example_resources
6868
description: flutter pub run build_runner build --delete-conflicting-outputs
6969

70+
gen:actual_data:
71+
run: dart packages/core/scripts/generate_facts.dart
72+
description: Generate actual data for tests.
73+
7074
test:
7175
run: |
7276
melos test:dart --no-select

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"prepare": "husky install"
88
},
99
"lint-staged": {
10-
"*.dart": [
10+
"!(packages/core/test_resources/actual_data/**/*)*.dart": [
1111
"melos format"
1212
]
1313
},
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
include: ../../analysis_options.yaml
2+
3+
analyzer:
4+
exclude:
5+
- 'example/**'
+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
include: ../../analysis_options.yaml
2+
3+
analyzer:
4+
exclude:
5+
- 'example/**'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'dart:io';
2+
3+
import 'package:dartx/dartx_io.dart';
4+
import 'package:flutter_gen_core/flutter_generator.dart';
5+
import 'package:path/path.dart' as p;
6+
7+
late final Directory dir;
8+
9+
void main() async {
10+
dir = File.fromUri(Platform.script).parent.parent.directory('test_resources');
11+
final configFiles =
12+
dir.listSync().whereType<File>().where((e) => e.extension == '.yaml');
13+
for (final file in configFiles) {
14+
final File pubspecFile;
15+
final File? buildFile;
16+
final String namePrefix;
17+
if (file.name.startsWith('build_')) {
18+
pubspecFile = File(p.join(dir.path, 'pubspec_assets.yaml'));
19+
buildFile = file;
20+
namePrefix = 'build_';
21+
} else {
22+
pubspecFile = file;
23+
buildFile = null;
24+
namePrefix = '';
25+
}
26+
final name = file.nameWithoutExtension.removePrefix('pubspec_');
27+
final generator = FlutterGenerator(
28+
pubspecFile,
29+
buildFile: buildFile,
30+
assetsName: '${namePrefix}assets_$name.gen.dart',
31+
colorsName: '${namePrefix}colors_$name.gen.dart',
32+
fontsName: '${namePrefix}fonts_$name.gen.dart',
33+
overrideOutputPath: p.join(dir.path, 'actual_data'),
34+
);
35+
await generator.build().catchError((e, s) {
36+
print('$e\n$s');
37+
});
38+
}
39+
}

‎packages/core/test/assets_gen_integrations_test.dart

+5-29
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,12 @@ void main() {
3434

3535
test('Assets with No integrations on pubspec.yaml', () async {
3636
const pubspec = 'test_resources/pubspec_assets_no_integrations.yaml';
37-
const fact = 'test_resources/actual_data/assets_no_integrations.gen.dart';
38-
const generated =
39-
'test_resources/lib/gen/assets_no_integrations.gen.dart';
40-
41-
await expectedAssetsGen(pubspec, generated, fact);
37+
await expectedAssetsGen(pubspec);
4238
});
4339

4440
test('Assets with no image integration', () async {
4541
const pubspec = 'test_resources/pubspec_assets_no_image_integration.yaml';
46-
const fact =
47-
'test_resources/actual_data/assets_no_image_integration.gen.dart';
48-
const generated =
49-
'test_resources/lib/gen/assets_no_image_integration.gen.dart';
50-
51-
await expectedAssetsGen(pubspec, generated, fact);
42+
await expectedAssetsGen(pubspec);
5243
});
5344

5445
test('Integration.classInstantiate', () {
@@ -62,12 +53,7 @@ void main() {
6253

6354
test('Assets with Svg integrations on pubspec.yaml', () async {
6455
const pubspec = 'test_resources/pubspec_assets_svg_integrations.yaml';
65-
const fact =
66-
'test_resources/actual_data/assets_svg_integrations.gen.dart';
67-
const generated =
68-
'test_resources/lib/gen/assets_svg_integrations.gen.dart';
69-
70-
await expectedAssetsGen(pubspec, generated, fact);
56+
await expectedAssetsGen(pubspec);
7157

7258
final integration = SvgIntegration('');
7359
expect(integration.className, 'SvgGenImage');
@@ -151,12 +137,7 @@ void main() {
151137

152138
test('Assets with Rive integrations on pubspec.yaml', () async {
153139
const pubspec = 'test_resources/pubspec_assets_rive_integrations.yaml';
154-
const fact =
155-
'test_resources/actual_data/assets_rive_integrations.gen.dart';
156-
const generated =
157-
'test_resources/lib/gen/assets_rive_integrations.gen.dart';
158-
159-
await expectedAssetsGen(pubspec, generated, fact);
140+
await expectedAssetsGen(pubspec);
160141

161142
final integration = RiveIntegration('');
162143
expect(integration.className, 'RiveGenImage');
@@ -203,12 +184,7 @@ void main() {
203184

204185
test('Assets with Lottie integrations on pubspec.yaml', () async {
205186
const pubspec = 'test_resources/pubspec_assets_lottie_integrations.yaml';
206-
const fact =
207-
'test_resources/actual_data/assets_lottie_integrations.gen.dart';
208-
const generated =
209-
'test_resources/lib/gen/assets_lottie_integrations.gen.dart';
210-
211-
await expectedAssetsGen(pubspec, generated, fact);
187+
await expectedAssetsGen(pubspec);
212188

213189
final integration = LottieIntegration('');
214190
expect(integration.className, 'LottieGenImage');

‎packages/core/test/assets_gen_test.dart

+17-79
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,27 @@ void main() {
1616
group('Test Assets generator', () {
1717
test('Assets on pubspec.yaml', () async {
1818
const pubspec = 'test_resources/pubspec_assets.yaml';
19-
const fact = 'test_resources/actual_data/assets.gen.dart';
20-
const generated = 'test_resources/lib/gen/assets.gen.dart';
21-
22-
await expectedAssetsGen(pubspec, generated, fact);
19+
await expectedAssetsGen(pubspec);
2320
});
2421

2522
test('Assets snake-case style on pubspec.yaml', () async {
2623
const pubspec = 'test_resources/pubspec_assets_snake_case.yaml';
27-
const fact = 'test_resources/actual_data/assets_snake_case.gen.dart';
28-
const generated = 'test_resources/lib/gen/assets_snake_case.gen.dart';
29-
30-
await expectedAssetsGen(pubspec, generated, fact);
24+
await expectedAssetsGen(pubspec);
3125
});
3226

3327
test('Assets camel-case style on pubspec.yaml', () async {
3428
const pubspec = 'test_resources/pubspec_assets_camel_case.yaml';
35-
const fact = 'test_resources/actual_data/assets_camel_case.gen.dart';
36-
const generated = 'test_resources/lib/gen/assets_camel_case.gen.dart';
37-
38-
await expectedAssetsGen(pubspec, generated, fact);
29+
await expectedAssetsGen(pubspec);
3930
});
4031

4132
test('Assets with Unknown mime type on pubspec.yaml', () async {
4233
const pubspec = 'test_resources/pubspec_unknown_mime_type.yaml';
43-
const fact =
44-
'test_resources/actual_data/assets_unknown_mime_type.gen.dart';
45-
const generated =
46-
'test_resources/lib/gen/assets_unknown_mime_type.gen.dart';
47-
48-
await expectedAssetsGen(pubspec, generated, fact);
34+
await expectedAssetsGen(pubspec);
4935
});
5036

5137
test('Assets with ignore files on pubspec.yaml', () async {
5238
const pubspec = 'test_resources/pubspec_ignore_files.yaml';
53-
const fact = 'test_resources/actual_data/assets_ignore_files.gen.dart';
54-
const generated = 'test_resources/lib/gen/assets_ignore_files.gen.dart';
55-
56-
await expectedAssetsGen(pubspec, generated, fact);
39+
await expectedAssetsGen(pubspec);
5740
});
5841

5942
test('Assets with No lists on pubspec.yaml', () async {
@@ -72,15 +55,9 @@ void main() {
7255

7356
test('Assets with package parameter enabled', () async {
7457
const pubspec = 'test_resources/pubspec_assets_package_parameter.yaml';
75-
const fact =
76-
'test_resources/actual_data/assets_package_parameter.gen.dart';
77-
const generated =
78-
'test_resources/lib/gen/assets_package_parameter.gen.dart';
79-
80-
await expectedAssetsGen(pubspec, generated, fact);
58+
final (content, _) = await expectedAssetsGen(pubspec);
8159

8260
// The generated classes have `package` fields.
83-
final content = await File(generated).readAsString();
8461
expect(content, contains("static const String package = 'test';"));
8562
expect(
8663
content,
@@ -96,67 +73,40 @@ void main() {
9673

9774
test('Assets with directory path enabled', () async {
9875
const pubspec = 'test_resources/pubspec_assets_directory_path.yaml';
99-
const fact = 'test_resources/actual_data/assets_directory_path.gen.dart';
100-
const generated = 'test_resources/lib/gen/assets_directory_path.gen.dart';
101-
await expectedAssetsGen(pubspec, generated, fact);
76+
await expectedAssetsGen(pubspec);
10277
});
10378

10479
test('Assets with directory path and package parameter enabled', () async {
10580
const pubspec =
10681
'test_resources/pubspec_assets_directory_path_with_package_parameter.yaml';
107-
const fact =
108-
'test_resources/actual_data/assets_directory_path_with_package_parameter.gen.dart';
109-
const generated =
110-
'test_resources/lib/gen/assets_directory_path_with_package_parameter.gen.dart';
111-
await expectedAssetsGen(pubspec, generated, fact);
82+
await expectedAssetsGen(pubspec);
11283
});
11384

11485
test('Assets with excluded files and directories', () async {
11586
const pubspec = 'test_resources/pubspec_assets_exclude_files.yaml';
116-
const fact =
117-
'test_resources/actual_data/assets_package_exclude_files.gen.dart';
118-
const generated =
119-
'test_resources/lib/gen/assets_package_exclude_files.gen.dart';
120-
121-
await expectedAssetsGen(pubspec, generated, fact);
87+
await expectedAssetsGen(pubspec);
12288
});
12389

12490
test('Assets with change the class name', () async {
12591
const pubspec = 'test_resources/pubspec_assets_change_class_name.yaml';
126-
const fact =
127-
'test_resources/actual_data/assets_change_class_name.gen.dart';
128-
const generated =
129-
'test_resources/lib/gen/assets_change_class_name.gen.dart';
130-
131-
await expectedAssetsGen(pubspec, generated, fact);
92+
await expectedAssetsGen(pubspec);
13293
});
13394

13495
test('Assets with parse metadata enabled', () async {
13596
const pubspec = 'test_resources/pubspec_assets_parse_metadata.yaml';
136-
const fact = 'test_resources/actual_data/assets_parse_metadata.gen.dart';
137-
const generated = 'test_resources/lib/gen/assets_parse_metadata.gen.dart';
138-
139-
await expectedAssetsGen(pubspec, generated, fact);
97+
await expectedAssetsGen(pubspec);
14098
});
14199

142100
test('Assets with flavored assets', () async {
143101
const pubspec = 'test_resources/pubspec_assets_flavored.yaml';
144-
const fact = 'test_resources/actual_data/assets_flavored.gen.dart';
145-
const generated = 'test_resources/lib/gen/assets_flavored.gen.dart';
146-
147-
await expectedAssetsGen(pubspec, generated, fact);
102+
await expectedAssetsGen(pubspec);
148103
});
149104

150105
test('Assets with duplicate flavoring entries', () async {
151106
const pubspec =
152107
'test_resources/pubspec_assets_flavored_duplicate_entry.yaml';
153-
const fact =
154-
'test_resources/actual_data/assets_flavored_duplicate_entry.gen.dart';
155-
const generated =
156-
'test_resources/lib/gen/assets_flavored_duplicate_entry.gen.dart';
157-
158108
await expectLater(
159-
() => runAssetsGen(pubspec, generated, fact),
109+
() => expectedAssetsGen(pubspec),
160110
throwsA(isA<StateError>()),
161111
);
162112
});
@@ -232,10 +182,7 @@ void main() {
232182
() async {
233183
const pubspec = 'test_resources/pubspec_assets.yaml';
234184
const build = 'test_resources/build_assets.yaml';
235-
const fact = 'test_resources/actual_data/build_assets.gen.dart';
236-
const generated = 'test_resources/lib/build_gen/assets.gen.dart';
237-
238-
await expectedAssetsGen(pubspec, generated, fact, build: build);
185+
await expectedAssetsGen(pubspec, build: build);
239186
},
240187
);
241188

@@ -244,10 +191,7 @@ void main() {
244191
() async {
245192
const pubspec = 'test_resources/pubspec_assets.yaml';
246193
const build = 'test_resources/build_runner_assets.yaml';
247-
const fact = 'test_resources/actual_data/build_runner_assets.gen.dart';
248-
const generated = 'test_resources/lib/build_gen/assets.gen.dart';
249-
250-
await expectedAssetsGen(pubspec, generated, fact, build: build);
194+
await expectedAssetsGen(pubspec, build: build);
251195
},
252196
);
253197

@@ -256,24 +200,18 @@ void main() {
256200
() async {
257201
const pubspec = 'test_resources/pubspec_assets.yaml';
258202
const build = 'test_resources/build_empty.yaml';
259-
const fact = 'test_resources/actual_data/build_empty.gen.dart';
260-
const generated = 'test_resources/lib/build_gen/assets.gen.dart';
261-
262-
await expectedAssetsGen(pubspec, generated, fact, build: build);
203+
await expectedAssetsGen(pubspec, build: build);
263204
},
264205
);
265206

266207
test('fallback to build.yaml if valid', () async {
267208
const pubspec = 'test_resources/pubspec_assets.yaml';
268-
const fact = 'test_resources/actual_data/build_assets.gen.dart';
269-
const generated = 'test_resources/lib/build_gen/assets.gen.dart';
270-
271209
final buildFile = File('build.yaml');
272210
final originalBuildContent = buildFile.readAsStringSync();
273211
buildFile.writeAsStringSync(
274212
File('test_resources/build_assets.yaml').readAsStringSync(),
275213
);
276-
await expectedAssetsGen(pubspec, generated, fact).whenComplete(() {
214+
await expectedAssetsGen(pubspec).whenComplete(() {
277215
buildFile.writeAsStringSync(originalBuildContent);
278216
});
279217
});

‎packages/core/test/colors_gen_test.dart

+2-10
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ void main() {
1313
group('Test Color generator', () {
1414
test('Colors on pubspec.yaml', () async {
1515
const pubspec = 'test_resources/pubspec_colors.yaml';
16-
const fact = 'test_resources/actual_data/colors.gen.dart';
17-
const generated = 'test_resources/lib/gen/colors.gen.dart';
18-
19-
await expectedColorsGen(pubspec, generated, fact);
16+
await expectedColorsGen(pubspec);
2017
});
2118

2219
test('Wrong colors settings on pubspec.yaml', () async {
@@ -62,12 +59,7 @@ void main() {
6259

6360
test('Change the class name', () async {
6461
const pubspec = 'test_resources/pubspec_colors_change_class_name.yaml';
65-
const fact =
66-
'test_resources/actual_data/colors_change_class_name.gen.dart';
67-
const generated =
68-
'test_resources/lib/gen/colors_change_class_name.gen.dart';
69-
70-
await expectedColorsGen(pubspec, generated, fact);
62+
await expectedColorsGen(pubspec);
7163
});
7264
});
7365
}

0 commit comments

Comments
 (0)