From a4d93436c805050095dfe0f26d40d41f74db871a Mon Sep 17 00:00:00 2001 From: Eike Bartels Date: Wed, 14 Feb 2024 10:05:32 +0100 Subject: [PATCH 1/6] feat: add option to disable ensureInitialized --- CHANGELOG.md | 4 ++++ README.md | 3 +++ example/build.yaml | 4 +++- lib/bdd_widget_test.dart | 1 + lib/src/feature_file.dart | 18 +++++++++++------- lib/src/feature_generator.dart | 31 +++++++++++++++++++++++-------- lib/src/generator_options.dart | 8 +++++++- test/integration_test.dart | 30 ++++++++++++++++++++++++++++++ 8 files changed, 82 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd77c62..e10a48f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.6.5] - Option to disable IntegrationTestBinding inclusion + +* Add includeIntegrationTestBinding option to disable/ enable IntegrationTestWidgetsFlutterBinding.ensureInitialized(); (by @eikebartels) + ## [1.6.4] - Gherkin compliance * Add hooks (by @daniel-deboeverie-lemon) diff --git a/README.md b/README.md index 5777936..e1284ff 100644 --- a/README.md +++ b/README.md @@ -424,8 +424,11 @@ targets: testMethodName: patrolTest testerName: $ testerType: PatrolIntegrationTester + includeIntegrationTestBinding: false ``` +Since Patrol version 3.0.0, `IntegrationTestWidgetsFlutterBinding.ensureInitialized` must not be called. Set `includeIntegrationTestBinding` to `false`. + ## Contributing If you find a bug or would like to request a new feature, just [open an issue](https://github.com/olexale/bdd_widget_test/issues/new). Your contributions are always welcome! diff --git a/example/build.yaml b/example/build.yaml index 9875cf7..f5d29e0 100644 --- a/example/build.yaml +++ b/example/build.yaml @@ -8,6 +8,7 @@ targets: builders: bdd_widget_test|featureBuilder: options: + includeIntegrationTestBinding: false # if false, integration test will not include binding; default is true stepFolderName: step # this trick is required to share steps between widget and integration tests # testMethodName: customTestMethodName include: package:bdd_widget_test/bdd_options.yaml # you may add defaul external steps with this line @@ -32,4 +33,5 @@ targets: # - package:bdd_widget_test/step/i_dismiss_the_page.dart generate_for: - test/** - - integration_test/** \ No newline at end of file + - integration_test/** + \ No newline at end of file diff --git a/lib/bdd_widget_test.dart b/lib/bdd_widget_test.dart index 5bb26bf..44cc132 100644 --- a/lib/bdd_widget_test.dart +++ b/lib/bdd_widget_test.dart @@ -31,6 +31,7 @@ class FeatureBuilder implements Builder { existingSteps: getExistingStepSubfolders(featureDir, options.stepFolder), input: contents, generatorOptions: options, + includeIntegrationTestBinding: generatorOptions.includeIntegrationTestBinding, ); final featureDart = inputId.changeExtension('_test.dart'); diff --git a/lib/src/feature_file.dart b/lib/src/feature_file.dart index 4f0a2e7..e371253 100644 --- a/lib/src/feature_file.dart +++ b/lib/src/feature_file.dart @@ -15,6 +15,7 @@ class FeatureFile { required this.package, required String input, this.isIntegrationTest = false, + this.includeIntegrationTestBinding = true, this.existingSteps = const {}, this.generatorOptions = const GeneratorOptions(), }) : _lines = _prepareLines( @@ -65,19 +66,22 @@ class FeatureFile { final String featureDir; final String package; final bool isIntegrationTest; + /// Whether to include the integration test binding in the generated test for integration tests. Defaults to true. + final bool includeIntegrationTestBinding; final List _lines; final Map existingSteps; final GeneratorOptions generatorOptions; final HookFile? hookFile; String get dartContent => generateFeatureDart( - _lines, - getStepFiles(), - generatorOptions.testMethodName, - _testerType, - _testerName, - isIntegrationTest, - hookFile, + lines: _lines, + steps: getStepFiles(), + testMethodName: generatorOptions.testMethodName, + testerType: _testerType, + testerName: _testerName, + isIntegrationTest: isIntegrationTest, + includeIntegrationTestBinding: includeIntegrationTestBinding, + hookFile: hookFile, ); List getStepFiles() => _stepFiles; diff --git a/lib/src/feature_generator.dart b/lib/src/feature_generator.dart index 34ec167..51e32ab 100644 --- a/lib/src/feature_generator.dart +++ b/lib/src/feature_generator.dart @@ -7,14 +7,29 @@ import 'package:bdd_widget_test/src/step_generator.dart'; import 'package:bdd_widget_test/src/util/common.dart'; import 'package:bdd_widget_test/src/util/constants.dart'; -String generateFeatureDart( - List lines, - List steps, - String testMethodName, - String testerType, - String testerName, - bool isIntegrationTest, + +/// Generates a Dart code for a feature based on the provided parameters. +/// +/// The [lines] parameter is a list of [BddLine] objects representing the lines of the feature file. +/// The [steps] parameter is a list of [StepFile] objects representing the step definitions. +/// The [testMethodName] parameter is a string representing the name of the test method. +/// The [testerType] parameter is a string representing the type of the tester. +/// The [testerName] parameter is a string representing the name of the tester. +/// The [isIntegrationTest] parameter is a boolean indicating whether the test is an integration test. +/// The [includeIntegrationTestBinding] parameter is a boolean indicating whether to include the integration test binding. +/// The [hookFile] parameter is an optional [HookFile] object representing the hook definitions. +/// +/// Returns a string containing the generated Dart code for the feature. +String generateFeatureDart({ + required List lines, + required List steps, + required String testMethodName, + required String testerType, + required String testerName, + required bool isIntegrationTest, + required bool includeIntegrationTestBinding, HookFile? hookFile, + } ) { final sb = StringBuffer(); sb.writeln('// GENERATED CODE - DO NOT MODIFY BY HAND'); @@ -70,7 +85,7 @@ String generateFeatureDart( sb.writeln(); sb.writeln('void main() {'); - if (isIntegrationTest) { + if (isIntegrationTest && includeIntegrationTestBinding) { sb.writeln(' IntegrationTestWidgetsFlutterBinding.ensureInitialized();'); sb.writeln(); } diff --git a/lib/src/generator_options.dart b/lib/src/generator_options.dart index ddbe09e..05076ab 100644 --- a/lib/src/generator_options.dart +++ b/lib/src/generator_options.dart @@ -18,13 +18,15 @@ class GeneratorOptions { bool? addHooks, String? hookFolderName, this.include, + bool? includeIntegrationTestBinding, }) : stepFolder = stepFolderName ?? _stepFolderName, testMethodName = testMethodName ?? _defaultTestMethodName, testerType = testerType ?? _defaultTesterType, testerName = testerName ?? _defaultTesterName, addHooks = addHooks ?? false, hookFolderName = hookFolderName ?? _hookFolderName, - externalSteps = externalSteps ?? const []; + externalSteps = externalSteps ?? const [], + includeIntegrationTestBinding = includeIntegrationTestBinding ?? true; factory GeneratorOptions.fromMap(Map json) => GeneratorOptions( @@ -38,6 +40,7 @@ class GeneratorOptions { include: json['include'] is String ? [(json['include'] as String)] : (json['include'] as List?)?.cast(), + includeIntegrationTestBinding: json['includeIntegrationTestBinding'] as bool?, ); final String stepFolder; @@ -48,6 +51,7 @@ class GeneratorOptions { final String hookFolderName; final List? include; final List externalSteps; + final bool includeIntegrationTestBinding; } Future flattenOptions(GeneratorOptions options) async { @@ -88,6 +92,7 @@ GeneratorOptions readFromUri(Uri uri) { include: doc['include'] is String ? [(doc['include'] as String)] : (doc['include'] as YamlList?)?.value.cast(), + includeIntegrationTestBinding: doc['includeIntegrationTestBinding'] as bool?, ); } @@ -108,4 +113,5 @@ GeneratorOptions merge(GeneratorOptions a, GeneratorOptions b) => ? a.hookFolderName : b.hookFolderName, include: b.include, + includeIntegrationTestBinding: a.includeIntegrationTestBinding, ); diff --git a/test/integration_test.dart b/test/integration_test.dart index 61270ad..115e27a 100644 --- a/test/integration_test.dart +++ b/test/integration_test.dart @@ -34,4 +34,34 @@ void main() { ); expect(feature.dartContent, expectedFeatureDart); }); + + test('integration-related lines are not added if includeIntegrationTestBinding is false', () { + const expectedFeatureDart = ''' +// GENERATED CODE - DO NOT MODIFY BY HAND +// ignore_for_file: unused_import, directives_ordering + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:integration_test/integration_test.dart'; + +import './step/the_app_is_running.dart'; + +void main() { + group(\'\'\'Testing feature\'\'\', () { + testWidgets(\'\'\'Testing scenario\'\'\', (tester) async { + await theAppIsRunning(tester); + }); + }); +} +'''; + + final feature = FeatureFile( + featureDir: 'test.feature', + package: 'test', + input: minimalFeatureFile, + isIntegrationTest: true, + includeIntegrationTestBinding: false, + ); + expect(feature.dartContent, expectedFeatureDart); + }); } From e9c18ea71270593d2295bd9b6a5e4d3f08762b7c Mon Sep 17 00:00:00 2001 From: Oleksandr Leushchenko Date: Sat, 17 Feb 2024 13:55:18 +0200 Subject: [PATCH 2/6] Fix includeIntegrationTestBinding parameter merge --- lib/src/generator_options.dart | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/src/generator_options.dart b/lib/src/generator_options.dart index 05076ab..6bf4501 100644 --- a/lib/src/generator_options.dart +++ b/lib/src/generator_options.dart @@ -25,7 +25,7 @@ class GeneratorOptions { testerName = testerName ?? _defaultTesterName, addHooks = addHooks ?? false, hookFolderName = hookFolderName ?? _hookFolderName, - externalSteps = externalSteps ?? const [], + externalSteps = externalSteps ?? const [], includeIntegrationTestBinding = includeIntegrationTestBinding ?? true; factory GeneratorOptions.fromMap(Map json) => @@ -40,7 +40,8 @@ class GeneratorOptions { include: json['include'] is String ? [(json['include'] as String)] : (json['include'] as List?)?.cast(), - includeIntegrationTestBinding: json['includeIntegrationTestBinding'] as bool?, + includeIntegrationTestBinding: + json['includeIntegrationTestBinding'] as bool?, ); final String stepFolder; @@ -92,7 +93,8 @@ GeneratorOptions readFromUri(Uri uri) { include: doc['include'] is String ? [(doc['include'] as String)] : (doc['include'] as YamlList?)?.value.cast(), - includeIntegrationTestBinding: doc['includeIntegrationTestBinding'] as bool?, + includeIntegrationTestBinding: + doc['includeIntegrationTestBinding'] as bool?, ); } @@ -113,5 +115,6 @@ GeneratorOptions merge(GeneratorOptions a, GeneratorOptions b) => ? a.hookFolderName : b.hookFolderName, include: b.include, - includeIntegrationTestBinding: a.includeIntegrationTestBinding, + includeIntegrationTestBinding: + a.includeIntegrationTestBinding || b.includeIntegrationTestBinding, ); From 3b1b0810fd3682955ce57e9bb95ddbf5cf53fb9f Mon Sep 17 00:00:00 2001 From: Oleksandr Leushchenko Date: Sat, 17 Feb 2024 13:57:48 +0200 Subject: [PATCH 3/6] Remove redundant comments --- lib/src/feature_file.dart | 18 +++++++++--------- lib/src/feature_generator.dart | 30 ++++++++---------------------- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/lib/src/feature_file.dart b/lib/src/feature_file.dart index e371253..c7493cc 100644 --- a/lib/src/feature_file.dart +++ b/lib/src/feature_file.dart @@ -66,7 +66,7 @@ class FeatureFile { final String featureDir; final String package; final bool isIntegrationTest; - /// Whether to include the integration test binding in the generated test for integration tests. Defaults to true. + final bool includeIntegrationTestBinding; final List _lines; final Map existingSteps; @@ -74,14 +74,14 @@ class FeatureFile { final HookFile? hookFile; String get dartContent => generateFeatureDart( - lines: _lines, - steps: getStepFiles(), - testMethodName: generatorOptions.testMethodName, - testerType: _testerType, - testerName: _testerName, - isIntegrationTest: isIntegrationTest, - includeIntegrationTestBinding: includeIntegrationTestBinding, - hookFile: hookFile, + _lines, + getStepFiles(), + generatorOptions.testMethodName, + _testerType, + _testerName, + isIntegrationTest, + includeIntegrationTestBinding, + hookFile, ); List getStepFiles() => _stepFiles; diff --git a/lib/src/feature_generator.dart b/lib/src/feature_generator.dart index 51e32ab..4d98886 100644 --- a/lib/src/feature_generator.dart +++ b/lib/src/feature_generator.dart @@ -7,29 +7,15 @@ import 'package:bdd_widget_test/src/step_generator.dart'; import 'package:bdd_widget_test/src/util/common.dart'; import 'package:bdd_widget_test/src/util/constants.dart'; - -/// Generates a Dart code for a feature based on the provided parameters. -/// -/// The [lines] parameter is a list of [BddLine] objects representing the lines of the feature file. -/// The [steps] parameter is a list of [StepFile] objects representing the step definitions. -/// The [testMethodName] parameter is a string representing the name of the test method. -/// The [testerType] parameter is a string representing the type of the tester. -/// The [testerName] parameter is a string representing the name of the tester. -/// The [isIntegrationTest] parameter is a boolean indicating whether the test is an integration test. -/// The [includeIntegrationTestBinding] parameter is a boolean indicating whether to include the integration test binding. -/// The [hookFile] parameter is an optional [HookFile] object representing the hook definitions. -/// -/// Returns a string containing the generated Dart code for the feature. -String generateFeatureDart({ - required List lines, - required List steps, - required String testMethodName, - required String testerType, - required String testerName, - required bool isIntegrationTest, - required bool includeIntegrationTestBinding, +String generateFeatureDart( + List lines, + List steps, + String testMethodName, + String testerType, + String testerName, + bool isIntegrationTest, + bool includeIntegrationTestBinding, HookFile? hookFile, - } ) { final sb = StringBuffer(); sb.writeln('// GENERATED CODE - DO NOT MODIFY BY HAND'); From 3b56bf248c752f3e7678b6f479b9d84ae840d15a Mon Sep 17 00:00:00 2001 From: Oleksandr Leushchenko Date: Sat, 17 Feb 2024 14:03:09 +0200 Subject: [PATCH 4/6] Make GeneratorOptions.readFromUri use fromMap --- lib/src/generator_options.dart | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/lib/src/generator_options.dart b/lib/src/generator_options.dart index 6bf4501..a6486de 100644 --- a/lib/src/generator_options.dart +++ b/lib/src/generator_options.dart @@ -82,20 +82,7 @@ Future _readFromPackage(String packageUri) async { GeneratorOptions readFromUri(Uri uri) { final rawYaml = fs.file(uri).readAsStringSync(); final doc = loadYamlNode(rawYaml) as YamlMap; - return GeneratorOptions( - testMethodName: doc['testMethodName'] as String?, - testerType: doc['testerType'] as String?, - testerName: doc['testerName'] as String?, - externalSteps: (doc['externalSteps'] as List?)?.cast(), - stepFolderName: doc['stepFolderName'] as String?, - addHooks: doc['addHooks'] as bool?, - hookFolderName: doc['hookFolderName'] as String?, - include: doc['include'] is String - ? [(doc['include'] as String)] - : (doc['include'] as YamlList?)?.value.cast(), - includeIntegrationTestBinding: - doc['includeIntegrationTestBinding'] as bool?, - ); + return GeneratorOptions.fromMap(doc.value.cast()); } GeneratorOptions merge(GeneratorOptions a, GeneratorOptions b) => From bbd0e89d67fce23f577cf570fafde1d3f5d86270 Mon Sep 17 00:00:00 2001 From: Oleksandr Leushchenko Date: Sat, 17 Feb 2024 14:05:59 +0200 Subject: [PATCH 5/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e10a48f..4851bba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ ## [1.6.5] - Option to disable IntegrationTestBinding inclusion -* Add includeIntegrationTestBinding option to disable/ enable IntegrationTestWidgetsFlutterBinding.ensureInitialized(); (by @eikebartels) +* Add includeIntegrationTestBinding option to control `IntegrationTestWidgetsFlutterBinding.ensureInitialized();` (by @eikebartels) ## [1.6.4] - Gherkin compliance From aee1618fe7833839dd75cfa44744ffc1fadd9fb9 Mon Sep 17 00:00:00 2001 From: Oleksandr Leushchenko Date: Sat, 17 Feb 2024 14:10:50 +0200 Subject: [PATCH 6/6] Fix formatting --- lib/bdd_widget_test.dart | 3 ++- test/integration_test.dart | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/bdd_widget_test.dart b/lib/bdd_widget_test.dart index 44cc132..8a662d2 100644 --- a/lib/bdd_widget_test.dart +++ b/lib/bdd_widget_test.dart @@ -31,7 +31,8 @@ class FeatureBuilder implements Builder { existingSteps: getExistingStepSubfolders(featureDir, options.stepFolder), input: contents, generatorOptions: options, - includeIntegrationTestBinding: generatorOptions.includeIntegrationTestBinding, + includeIntegrationTestBinding: + generatorOptions.includeIntegrationTestBinding, ); final featureDart = inputId.changeExtension('_test.dart'); diff --git a/test/integration_test.dart b/test/integration_test.dart index 115e27a..6ee6dbb 100644 --- a/test/integration_test.dart +++ b/test/integration_test.dart @@ -35,7 +35,9 @@ void main() { expect(feature.dartContent, expectedFeatureDart); }); - test('integration-related lines are not added if includeIntegrationTestBinding is false', () { + test( + 'integration-related lines are not added if includeIntegrationTestBinding is false', + () { const expectedFeatureDart = ''' // GENERATED CODE - DO NOT MODIFY BY HAND // ignore_for_file: unused_import, directives_ordering