Skip to content

Commit 839095f

Browse files
committed
Fix #20
1 parent ef645bd commit 839095f

11 files changed

Lines changed: 163 additions & 51 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.3.0] - Add bdd_options.yaml
2+
3+
* Add possibility to store config outside of `build.yaml`. Now we may re-use the same config (for example `external_steps`) across different packages.
4+
15
## [1.2.1] - Data Tables
26

37
* Add data tables support

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,23 @@ targets:
254254
- package:<your_package>/<your_step>.dart
255255
```
256256

257+
If you have many packages you might want to reuse the whole list of external steps. For that you'll have to create a `bdd_options.yaml` file in the root folder of your project with the following content:
258+
```yaml
259+
include: package:bdd_widget_test/bdd_options.yaml # if you want to reuse default steps as well
260+
externalSteps:
261+
- package:<your_package>/<your_step>.dart
262+
```
263+
264+
Alternatively, ff you need just to include an external config, use the `include` option in the `build.yaml` config:
265+
```yaml
266+
targets:
267+
$default:
268+
builders:
269+
bdd_widget_test|featureBuilder:
270+
options:
271+
include: package:<your_package>/bdd_options.yaml
272+
```
273+
257274
### How to group steps in a single project?
258275

259276
You may create sub-folders (like `common`, `login`, `home`, etc.) in the `step` folder and move generated steps there. The plugin is smart enough to find them (see the `example` folder).

example/bdd_options.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include: package:bdd_widget_test/bdd_options.yaml
2+
externalSteps:
3+
- package:bdd_widget_test/step/i_see_text.dart

example/build.yaml

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,26 @@ targets:
1111
# stepFolderName: bdd_steps
1212
stepFolderName: ../test/step # this trick is required to share steps between widget and integration tests
1313
# testMethodName: customTestMethodName
14-
externalSteps:
14+
include: package:bdd_widget_test/bdd_options.yaml # you may add defaul external steps with this line
15+
externalSteps: # or list only steps that you need
1516
- package:bdd_widget_test/step/i_see_text.dart
16-
- package:bdd_widget_test/step/i_dont_see_text.dart
17-
- package:bdd_widget_test/step/i_see_multiple_texts.dart
18-
- package:bdd_widget_test/step/i_tap_text.dart
19-
- package:bdd_widget_test/step/i_see_icon.dart
20-
- package:bdd_widget_test/step/i_dont_see_icon.dart
21-
- package:bdd_widget_test/step/i_tap_icon.dart
22-
- package:bdd_widget_test/step/i_see_rich_text.dart
23-
- package:bdd_widget_test/step/i_dont_see_rich_text.dart
24-
- package:bdd_widget_test/step/i_see_widget.dart
25-
- package:bdd_widget_test/step/i_dont_see_widget.dart
26-
- package:bdd_widget_test/step/i_see_exactly_widgets.dart
27-
- package:bdd_widget_test/step/i_see_multiple_widgets.dart
28-
- package:bdd_widget_test/step/i_enter_into_input_field.dart
29-
- package:bdd_widget_test/step/i_see_disabled_elevated_button.dart
30-
- package:bdd_widget_test/step/i_see_enabled_elevated_button.dart
31-
- package:bdd_widget_test/step/i_wait.dart
32-
- package:bdd_widget_test/step/i_dismiss_the_page.dart
17+
# - package:bdd_widget_test/step/i_dont_see_text.dart
18+
# - package:bdd_widget_test/step/i_see_multiple_texts.dart
19+
# - package:bdd_widget_test/step/i_tap_text.dart
20+
# - package:bdd_widget_test/step/i_see_icon.dart
21+
# - package:bdd_widget_test/step/i_dont_see_icon.dart
22+
# - package:bdd_widget_test/step/i_tap_icon.dart
23+
# - package:bdd_widget_test/step/i_see_rich_text.dart
24+
# - package:bdd_widget_test/step/i_dont_see_rich_text.dart
25+
# - package:bdd_widget_test/step/i_see_widget.dart
26+
# - package:bdd_widget_test/step/i_dont_see_widget.dart
27+
# - package:bdd_widget_test/step/i_see_exactly_widgets.dart
28+
# - package:bdd_widget_test/step/i_see_multiple_widgets.dart
29+
# - package:bdd_widget_test/step/i_enter_into_input_field.dart
30+
# - package:bdd_widget_test/step/i_see_disabled_elevated_button.dart
31+
# - package:bdd_widget_test/step/i_see_enabled_elevated_button.dart
32+
# - package:bdd_widget_test/step/i_wait.dart
33+
# - package:bdd_widget_test/step/i_dismiss_the_page.dart
3334
generate_for:
3435
- test/*.feature
3536
- integration_test/*.feature

example/pubspec.lock

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ packages:
2121
name: archive
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "3.1.2"
24+
version: "3.1.6"
2525
args:
2626
dependency: transitive
2727
description:
@@ -35,14 +35,14 @@ packages:
3535
name: async
3636
url: "https://pub.dartlang.org"
3737
source: hosted
38-
version: "2.8.1"
38+
version: "2.8.2"
3939
bdd_widget_test:
4040
dependency: "direct dev"
4141
description:
4242
path: ".."
4343
relative: true
4444
source: path
45-
version: "1.2.1"
45+
version: "1.3.0"
4646
boolean_selector:
4747
dependency: transitive
4848
description:
@@ -56,7 +56,7 @@ packages:
5656
name: build
5757
url: "https://pub.dartlang.org"
5858
source: hosted
59-
version: "2.1.0"
59+
version: "2.2.1"
6060
build_config:
6161
dependency: transitive
6262
description:
@@ -84,14 +84,14 @@ packages:
8484
name: build_runner
8585
url: "https://pub.dartlang.org"
8686
source: hosted
87-
version: "2.1.2"
87+
version: "2.1.7"
8888
build_runner_core:
8989
dependency: transitive
9090
description:
9191
name: build_runner_core
9292
url: "https://pub.dartlang.org"
9393
source: hosted
94-
version: "7.1.0"
94+
version: "7.2.2"
9595
built_collection:
9696
dependency: transitive
9797
description:
@@ -112,7 +112,7 @@ packages:
112112
name: characters
113113
url: "https://pub.dartlang.org"
114114
source: hosted
115-
version: "1.1.0"
115+
version: "1.2.0"
116116
charcode:
117117
dependency: transitive
118118
description:
@@ -291,7 +291,7 @@ packages:
291291
name: matcher
292292
url: "https://pub.dartlang.org"
293293
source: hosted
294-
version: "0.12.10"
294+
version: "0.12.11"
295295
meta:
296296
dependency: transitive
297297
description:
@@ -333,7 +333,7 @@ packages:
333333
name: platform
334334
url: "https://pub.dartlang.org"
335335
source: hosted
336-
version: "3.0.0"
336+
version: "3.0.2"
337337
pool:
338338
dependency: transitive
339339
description:
@@ -347,7 +347,7 @@ packages:
347347
name: process
348348
url: "https://pub.dartlang.org"
349349
source: hosted
350-
version: "4.2.3"
350+
version: "4.2.4"
351351
pub_semver:
352352
dependency: transitive
353353
description:
@@ -436,7 +436,7 @@ packages:
436436
name: test_api
437437
url: "https://pub.dartlang.org"
438438
source: hosted
439-
version: "0.4.2"
439+
version: "0.4.3"
440440
timing:
441441
dependency: transitive
442442
description:
@@ -457,14 +457,14 @@ packages:
457457
name: vector_math
458458
url: "https://pub.dartlang.org"
459459
source: hosted
460-
version: "2.1.0"
460+
version: "2.1.1"
461461
vm_service:
462462
dependency: transitive
463463
description:
464464
name: vm_service
465465
url: "https://pub.dartlang.org"
466466
source: hosted
467-
version: "7.1.1"
467+
version: "7.3.0"
468468
watcher:
469469
dependency: transitive
470470
description:

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
dev_dependencies:
1111
bdd_widget_test:
1212
path: ../
13-
build_runner: ^2.0.1
13+
build_runner: ^2.1.7
1414
integration_test:
1515
sdk: flutter
1616
flutter_test:

lib/bdd_options.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
externalSteps:
2+
- package:bdd_widget_test/step/i_see_text.dart
3+
- package:bdd_widget_test/step/i_dont_see_text.dart
4+
- package:bdd_widget_test/step/i_see_multiple_texts.dart
5+
- package:bdd_widget_test/step/i_tap_text.dart
6+
- package:bdd_widget_test/step/i_see_icon.dart
7+
- package:bdd_widget_test/step/i_dont_see_icon.dart
8+
- package:bdd_widget_test/step/i_tap_icon.dart
9+
- package:bdd_widget_test/step/i_see_rich_text.dart
10+
- package:bdd_widget_test/step/i_dont_see_rich_text.dart
11+
- package:bdd_widget_test/step/i_see_widget.dart
12+
- package:bdd_widget_test/step/i_dont_see_widget.dart
13+
- package:bdd_widget_test/step/i_see_exactly_widgets.dart
14+
- package:bdd_widget_test/step/i_see_multiple_widgets.dart
15+
- package:bdd_widget_test/step/i_enter_into_input_field.dart
16+
- package:bdd_widget_test/step/i_see_disabled_elevated_button.dart
17+
- package:bdd_widget_test/step/i_see_enabled_elevated_button.dart
18+
- package:bdd_widget_test/step/i_wait.dart
19+
- package:bdd_widget_test/step/i_dismiss_the_page.dart

lib/bdd_widget_test.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class FeatureBuilder implements Builder {
1919

2020
@override
2121
Future<void> build(BuildStep buildStep) async {
22+
final options = await prepareOptions();
23+
2224
final inputId = buildStep.inputId;
2325
final contents = await buildStep.readAsString(inputId);
2426

@@ -27,10 +29,9 @@ class FeatureBuilder implements Builder {
2729
featureDir: featureDir,
2830
package: inputId.package,
2931
isIntegrationTest: inputId.pathSegments.contains('integration_test'),
30-
existingSteps:
31-
getExistingStepSubfolders(featureDir, generatorOptions.stepFolder),
32+
existingSteps: getExistingStepSubfolders(featureDir, options.stepFolder),
3233
input: contents,
33-
generatorOptions: generatorOptions,
34+
generatorOptions: options,
3435
);
3536

3637
final featureDart = inputId.changeExtension('_test.dart');
@@ -43,6 +44,17 @@ class FeatureBuilder implements Builder {
4344
await Future.wait(steps);
4445
}
4546

47+
Future<GeneratorOptions> prepareOptions() async {
48+
final fileOptions = File('bdd_options.yaml').existsSync()
49+
? readFromUri(Uri.file('bdd_options.yaml'))
50+
: null;
51+
final mergedOptions = fileOptions != null
52+
? merge(generatorOptions, fileOptions)
53+
: generatorOptions;
54+
final options = await flattenOptions(mergedOptions);
55+
return options;
56+
}
57+
4658
Future<void> createFileRecursively(String filename, String content) async {
4759
final f = File(filename);
4860
if (f.existsSync()) {

lib/src/generator_options.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import 'dart:io';
2+
import 'dart:isolate';
3+
4+
import 'package:yaml/yaml.dart';
5+
16
const _defaultTestName = 'testWidgets';
27
const _stepFolderName = 'step';
38

@@ -6,6 +11,7 @@ class GeneratorOptions {
611
String? testMethodName,
712
List<String>? externalSteps,
813
String? stepFolderName,
14+
this.include,
915
}) : stepFolder = stepFolderName ?? _stepFolderName,
1016
testMethodName = testMethodName ?? _defaultTestName,
1117
externalSteps = externalSteps ?? const [];
@@ -15,9 +21,51 @@ class GeneratorOptions {
1521
testMethodName: json['testMethodName'] as String?,
1622
externalSteps: (json['externalSteps'] as List?)?.cast<String>(),
1723
stepFolderName: json['stepFolderName'] as String?,
24+
include: json['include'] as String?,
1825
);
1926

2027
final String stepFolder;
2128
final String testMethodName;
29+
final String? include;
2230
final List<String> externalSteps;
2331
}
32+
33+
Future<GeneratorOptions> flattenOptions(GeneratorOptions options) async {
34+
if (options.include == null) {
35+
return options;
36+
}
37+
final includedOptions = await readFromPackage(options.include!);
38+
final newOptions = merge(options, includedOptions);
39+
return flattenOptions(newOptions);
40+
}
41+
42+
Future<GeneratorOptions> readFromPackage(String packageUri) async {
43+
final uri = await Isolate.resolvePackageUri(
44+
Uri.parse(packageUri),
45+
);
46+
if (uri == null) {
47+
throw Exception('Could not read $packageUri');
48+
}
49+
return readFromUri(uri);
50+
}
51+
52+
GeneratorOptions readFromUri(Uri uri) {
53+
final doc = loadYamlNode(File.fromUri(uri).readAsStringSync()) as YamlMap;
54+
return GeneratorOptions(
55+
testMethodName: doc['testMethodName'] as String?,
56+
externalSteps: (doc['externalSteps'] as List?)?.cast<String>(),
57+
stepFolderName: doc['stepFolderName'] as String?,
58+
include: doc['include'] as String?,
59+
);
60+
}
61+
62+
GeneratorOptions merge(GeneratorOptions a, GeneratorOptions b) =>
63+
GeneratorOptions(
64+
testMethodName: a.testMethodName != _defaultTestName
65+
? a.testMethodName
66+
: b.testMethodName,
67+
stepFolderName:
68+
a.stepFolder != _stepFolderName ? a.stepFolder : b.stepFolder,
69+
externalSteps: [...a.externalSteps, ...b.externalSteps],
70+
include: b.include,
71+
);

0 commit comments

Comments
 (0)