diff --git a/.github/workflows/dart_ci.yaml b/.github/workflows/dart_ci.yaml index f22689a..d54db87 100644 --- a/.github/workflows/dart_ci.yaml +++ b/.github/workflows/dart_ci.yaml @@ -18,14 +18,15 @@ jobs: - uses: actions/checkout@v4 - uses: dart-lang/setup-dart@v1 with: - sdk: 2.19.6 + sdk: 3.11.0 + - run: dart pub upgrade - run: dart format --set-exit-if-changed -o none . test: runs-on: ubuntu-latest strategy: fail-fast: false matrix: - sdk: [ 2.19.6, stable, beta ] + sdk: [ stable ] steps: - uses: actions/checkout@v4 - uses: dart-lang/setup-dart@v1 @@ -46,7 +47,7 @@ jobs: strategy: fail-fast: false matrix: - sdk: [ 2.19.6, stable, beta ] + sdk: [ stable ] steps: - uses: actions/checkout@v4 - uses: dart-lang/setup-dart@v1 diff --git a/analysis_options.yaml b/analysis_options.yaml index bccca15..66e4bb4 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -3,6 +3,6 @@ analyzer: exclude: - example/** - lib/**.g.dart - language: - strict-inference: true - strict-raw-types: true + +formatter: + trailing_commas: preserve diff --git a/bin/browser_aggregate_tests.dart b/bin/browser_aggregate_tests.dart index bae3ed1..2b53d84 100644 --- a/bin/browser_aggregate_tests.dart +++ b/bin/browser_aggregate_tests.dart @@ -8,22 +8,30 @@ const testPreset = '--preset=browser-aggregate'; final argParser = ArgParser() ..addFlag('help', abbr: 'h') - ..addFlag('release', - defaultsTo: false, help: 'Build in release mode (dart2js).') - ..addOption('mode', - allowed: ['args', 'build', 'test'], - allowedHelp: { - 'args': - 'Print to stderr the build and test args needed to run browser aggregate tests.\n' - 'Useful for integrating this into other test runners.', - 'build': 'Build the browser aggregate tests.', - 'test': 'Build and run browser aggregate tests.', - }, - defaultsTo: 'test') - ..addOption('build-args', - help: 'Args to pass to the build runner process.\n' - 'Run "dart run build_runner build -h -v" to see all available ' - 'options.'); + ..addFlag( + 'release', + defaultsTo: false, + help: 'Build in release mode (dart2js).', + ) + ..addOption( + 'mode', + allowed: ['args', 'build', 'test'], + allowedHelp: { + 'args': + 'Print to stderr the build and test args needed to run browser aggregate tests.\n' + 'Useful for integrating this into other test runners.', + 'build': 'Build the browser aggregate tests.', + 'test': 'Build and run browser aggregate tests.', + }, + defaultsTo: 'test', + ) + ..addOption( + 'build-args', + help: + 'Args to pass to the build runner process.\n' + 'Run "dart run build_runner build -h -v" to see all available ' + 'options.', + ); enum Mode { // Print build and test args separated by `--` @@ -95,13 +103,15 @@ void buildAggregateTestYaml(Mode mode, {String? userBuildArgs}) { // Users may also supply additional build arguments. For example, some // repos may need to specify a custom build.yaml file to be used. ...?userBuildArgs?.split(' '), - '--build-filter=dart_test.browser_aggregate.yaml' + '--build-filter=dart_test.browser_aggregate.yaml', ]; logIf(mode != Mode.args, 'Building browser aggregate test config...'); logIf(mode != Mode.args, '$executable ${args.join(' ')}'); var result = Process.runSync(executable, args); - logIf(result.exitCode != 0 || mode != Mode.args, - '${result.stderr}\n${result.stdout}'); + logIf( + result.exitCode != 0 || mode != Mode.args, + '${result.stderr}\n${result.stdout}', + ); if (result.exitCode != 0) { exit(result.exitCode); } @@ -116,8 +126,8 @@ List parseAggregateTestPaths(Mode mode) { logIf(mode != Mode.args, '\nReading browser aggregate test config...'); final configFile = File('dart_test.browser_aggregate.yaml'); if (!configFile.existsSync()) { - stdout - .writeln(r'''browser aggregation is not enabled. Update your build.yaml: + stdout.writeln( + r'''browser aggregation is not enabled. Update your build.yaml: # build.yaml targets: @@ -125,15 +135,20 @@ targets: builders: test_html_builder: options: - browser_aggregation: true'''); + browser_aggregation: true''', + ); exit(1); } - final config = - loadYaml(configFile.readAsStringSync(), sourceUrl: configFile.uri); + final config = loadYaml( + configFile.readAsStringSync(), + sourceUrl: configFile.uri, + ); late List paths; try { - paths = List.from(config['presets']['browser-aggregate']['paths']); + paths = List.from( + config['presets']['browser-aggregate']['paths'], + ); } catch (e, stack) { stdout ..writeln('Failed to read test paths from "${configFile.uri}"') @@ -152,50 +167,66 @@ targets: /// /// [userBuildArgs] is interpreted as a space delimited string of additional /// build_runner build arguments and will also be included. -List buildRunnerBuildArgs(List testPaths, - {bool? release, String? userBuildArgs}) => - [ - ...?userBuildArgs?.split(' '), - if (release ?? false) '--release', - for (final path in testPaths) - '--build-filter=${p.setExtension(path, '.**')}', - ]; +List buildRunnerBuildArgs( + List testPaths, { + bool? release, + String? userBuildArgs, +}) => [ + ...?userBuildArgs?.split(' '), + if (release ?? false) '--release', + for (final path in testPaths) '--build-filter=${p.setExtension(path, '.**')}', +]; /// Builds aggregate tests at [testPaths]. /// /// Includes `--release` if [release] is true. -Future buildTests(List testPaths, - {bool? release, String? userBuildArgs}) async { +Future buildTests( + List testPaths, { + bool? release, + String? userBuildArgs, +}) async { final executable = 'dart'; final args = [ 'run', 'build_runner', 'build', '--delete-conflicting-outputs', - ...buildRunnerBuildArgs(testPaths, - release: release, userBuildArgs: userBuildArgs), + ...buildRunnerBuildArgs( + testPaths, + release: release, + userBuildArgs: userBuildArgs, + ), ]; stdout ..writeln() ..writeln('Building browser aggregate tests...') ..writeln('$executable ${args.join(' ')}'); - final process = await Process.start(executable, args, - mode: ProcessStartMode.inheritStdio); + final process = await Process.start( + executable, + args, + mode: ProcessStartMode.inheritStdio, + ); exitCode = await process.exitCode; } /// Builds and runs aggregate tests at [testPaths]. /// /// Includes `--release` if [release] is true. -Future runTests(List testPaths, - {bool? release, String? userBuildArgs}) async { +Future runTests( + List testPaths, { + bool? release, + String? userBuildArgs, +}) async { final executable = 'dart'; final args = [ 'run', 'build_runner', 'test', - ...buildRunnerBuildArgs(testPaths, - release: release, userBuildArgs: userBuildArgs), + ...buildRunnerBuildArgs( + testPaths, + release: release, + userBuildArgs: userBuildArgs, + ), '--', testPreset, ]; @@ -203,17 +234,22 @@ Future runTests(List testPaths, ..writeln() ..writeln('Running browser aggregate tests...') ..writeln('$executable ${args.join(' ')}'); - final process = await Process.start(executable, args, - mode: ProcessStartMode.inheritStdio); + final process = await Process.start( + executable, + args, + mode: ProcessStartMode.inheritStdio, + ); exitCode = await process.exitCode; } /// Prints the build and test args separated by `--` needed to build or run the /// browser aggregate tests. void printArgs(List testPaths) { - stdout.write([ - ...buildRunnerBuildArgs(testPaths), - '--', - testPreset, - ].join(' ')); + stdout.write( + [ + ...buildRunnerBuildArgs(testPaths), + '--', + testPreset, + ].join(' '), + ); } diff --git a/example/project/pubspec.yaml b/example/project/pubspec.yaml index 324c8f2..ff4c5fd 100644 --- a/example/project/pubspec.yaml +++ b/example/project/pubspec.yaml @@ -3,12 +3,12 @@ version: 0.0.0 publish_to: none environment: - sdk: '>=2.19.0 <3.0.0' + sdk: '>=3.0.0 <4.0.0' dev_dependencies: build_runner: ^2.1.2 - build_test: ^2.1.3 - build_web_compilers: '>=3.0.0 <5.0.0' + build_test: ^3.0.0 + build_web_compilers: ^4.0.0 test: ^1.17.12 test_html_builder: path: ../.. diff --git a/lib/src/builder.dart b/lib/src/builder.dart index 99e05a3..4ffcae1 100644 --- a/lib/src/builder.dart +++ b/lib/src/builder.dart @@ -14,12 +14,14 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'dart:math'; import 'package:build/build.dart'; import 'package:dart_style/dart_style.dart'; import 'package:glob/glob.dart'; import 'package:path/path.dart' as p; +import 'package:pub_semver/pub_semver.dart'; // This import is deprecated to discourage its use, but the build_test package // uses it for the same reason we're using it here. So if they choose to get rid // of this import, they'll presumably have a plan for a different way to @@ -40,7 +42,7 @@ class TestHtmlBuilder implements Builder { r'$package$': [ 'test/templates/default_template.html', 'test/test_html_builder_config.json', - ] + ], }; @override @@ -48,7 +50,9 @@ class TestHtmlBuilder implements Builder { // Write the default template for any browser tests that don't match one of // the templates defined in the project's config. final defaultTemplateId = AssetId( - buildStep.inputId.package, 'test/templates/default_template.html'); + buildStep.inputId.package, + 'test/templates/default_template.html', + ); await buildStep.writeAsString(defaultTemplateId, ''' @@ -61,7 +65,9 @@ class TestHtmlBuilder implements Builder { // Write the builder options so they can be used by the builders below. final configId = AssetId( - buildStep.inputId.package, 'test/test_html_builder_config.json'); + buildStep.inputId.package, + 'test/test_html_builder_config.json', + ); await buildStep.writeAsString(configId, json.encode(_config)); } } @@ -79,7 +85,8 @@ class AggregateTestBuilder extends Builder { log.fine('browser aggregation disabled'); if (config.randomizeOrderingSeed != null) { log.warning( - '`randomize_ordering_seed` option is set, but `browser_aggregation` is not enabled so it has no effect.'); + '`randomize_ordering_seed` option is set, but `browser_aggregation` is not enabled so it has no effect.', + ); } return; } @@ -90,7 +97,8 @@ class AggregateTestBuilder extends Builder { ? [Glob('test/**_test.dart')] : config.templateGlobs[templatePath] ?? []; log.fine( - 'Test globs found for template: ${buildStep.inputId}:\n${testGlobs.join('\n')}'); + 'Test globs found for template: ${buildStep.inputId}:\n${testGlobs.join('\n')}', + ); final higherPrecedenceGlobs = []; if (isDefault) { @@ -114,14 +122,18 @@ class AggregateTestBuilder extends Builder { // then it will be included in that aggregate test. if (higherPrecedenceGlobs.any((g) => g.matches(id.path))) continue; - final hasCustomHtml = - await buildStep.canRead(id.changeExtension('.custom.html')); + final hasCustomHtml = await buildStep.canRead( + id.changeExtension('.custom.html'), + ); if (hasCustomHtml) continue; Metadata testMetadata; try { testMetadata = parseMetadata( - id.path, await buildStep.readAsString(id), _platformVariables); + id.path, + await buildStep.readAsString(id), + _platformVariables, + ); } catch (e, stack) { log.severe('Error parsing test metadata: ${id.path}', e, stack); continue; @@ -130,8 +142,10 @@ class AggregateTestBuilder extends Builder { if (!_isBrowserTest(testMetadata)) continue; final prefix = _importPrefixForTest(id.path); - final path = - p.relative(id.path, from: p.dirname(buildStep.inputId.path)); + final path = p.relative( + id.path, + from: p.dirname(buildStep.inputId.path), + ); imports.add("import '$path' as $prefix;"); mains.add(" $prefix.main();"); } @@ -147,17 +161,24 @@ class AggregateTestBuilder extends Builder { final seed = _getRandomSeed(); if (seed != null) { - log.info('Shuffling test order with `randomize_ordering_seed: $seed`\n'); + log.info( + 'Shuffling test order with `randomize_ordering_seed: $seed`\n', + ); mains.shuffle(Random(seed)); - mains.insert(0, - "print('${buildStep.inputId.path} built with `randomize_ordering_seed: \"$seed\"`');"); + mains.insert( + 0, + "print('${buildStep.inputId.path} built with `randomize_ordering_seed: \"$seed\"`');", + ); } else { // If the test order was not shuffled, sort them for the same reason we // sort the imports. mains.sort(); } - - final contents = DartFormatter().format('''@TestOn('browser') + final globalLanguageVersion = Version.parse( + Platform.version.split(' ').first, + ); + final contents = DartFormatter(languageVersion: globalLanguageVersion) + .format('''@TestOn('browser') import 'package:test/test.dart'; ${imports.join('\n')} @@ -167,18 +188,19 @@ ${mains.join('\n')} } '''); - final outputId = - buildStep.inputId.changeExtension('.browser_aggregate_test.dart'); + final outputId = buildStep.inputId.changeExtension( + '.browser_aggregate_test.dart', + ); await buildStep.writeAsString(outputId, contents); } final _browserRuntimes = Runtime.builtIn.where((r) => r.isBrowser == true); Set get _platformVariables => [ - Runtime.vm, - Runtime.nodeJS, - ..._browserRuntimes, - ].map((r) => r.identifier).toSet(); + Runtime.vm, + Runtime.nodeJS, + ..._browserRuntimes, + ].map((r) => r.identifier).toSet(); String _importPrefixForTest(String path) { // Remove `test/` segment. @@ -188,8 +210,9 @@ ${mains.join('\n')} return result.replaceAll(p.separator, '_').replaceAll('.', '_'); } - bool _isBrowserTest(Metadata testMetadata) => _browserRuntimes - .any((r) => testMetadata.testOn.evaluate(SuitePlatform(r))); + bool _isBrowserTest(Metadata testMetadata) => _browserRuntimes.any( + (r) => testMetadata.testOn.evaluate(SuitePlatform(r)), + ); Future _getConfig(BuildStep buildStep) async => __config ??= await decodeConfig(buildStep); @@ -231,10 +254,14 @@ class TemplateBuilder implements Builder { static AssetId getHtmlId(AssetId assetId) => assetId.changeExtension('.html'); AssetId? getTemplateId( - Map> templates, AssetId assetId) { + Map> templates, + AssetId assetId, + ) { if (assetId.path.endsWith('.browser_aggregate_test.dart')) { - return AssetId(assetId.package, - assetId.path.replaceFirst('.browser_aggregate_test.dart', '.html')); + return AssetId( + assetId.package, + assetId.path.replaceFirst('.browser_aggregate_test.dart', '.html'), + ); } for (final templatePath in templates.keys) { @@ -254,7 +281,9 @@ class TemplateBuilder implements Builder { if (await buildStep.canRead(customHtmlId)) { log.fine('Custom html found for ${buildStep.inputId.path}'); await buildStep.writeAsBytes( - htmlId, await buildStep.readAsBytes(customHtmlId)); + htmlId, + await buildStep.readAsBytes(customHtmlId), + ); return; } @@ -269,11 +298,13 @@ class TemplateBuilder implements Builder { } log.fine( - 'Generating html for ${buildStep.inputId.path} from template at ${templateId.path}'); + 'Generating html for ${buildStep.inputId.path} from template at ${templateId.path}', + ); var htmlContents = await buildStep.readAsString(templateId); if ('{{testScript}}'.allMatches(htmlContents).length != 1) { log.severe( - 'Test html template must contain exactly one `{{testScript}}` placeholder: ${templateId.path}'); + 'Test html template must contain exactly one `{{testScript}}` placeholder: ${templateId.path}', + ); return; } @@ -309,39 +340,53 @@ class DartTestYamlBuilder extends Builder { } log.fine('Building dart_test.browser_aggregate.yaml'); - final contents = StringBuffer()..writeln('''presets: + final contents = StringBuffer() + ..writeln('''presets: browser-aggregate: platforms: [chrome] paths:'''); - final aggregateTests = buildStep - .findAssets(Glob('test/**_template.browser_aggregate_test.dart')); + final aggregateTests = buildStep.findAssets( + Glob('test/**_template.browser_aggregate_test.dart'), + ); await for (final testId in aggregateTests) { log.fine('Found aggregate test: ${testId.path}'); contents.writeln(' - ${testId.path}'); } - await for (final customHtml - in buildStep.findAssets(Glob('test/**_test.custom.html'))) { + await for (final customHtml in buildStep.findAssets( + Glob('test/**_test.custom.html'), + )) { log.fine('Found custom HTML test: ${customHtml.path}'); - final customTestPath = - customHtml.path.replaceFirst('_test.custom.html', '_test.dart'); + final customTestPath = customHtml.path.replaceFirst( + '_test.custom.html', + '_test.dart', + ); contents.writeln(' - $customTestPath'); } - final outputId = - AssetId(buildStep.inputId.package, 'dart_test.browser_aggregate.yaml'); + final outputId = AssetId( + buildStep.inputId.package, + 'dart_test.browser_aggregate.yaml', + ); await buildStep.writeAsString(outputId, contents.toString()); final backwardsCompatOutputId = AssetId( - buildStep.inputId.package, 'test/dart_test.browser_aggregate.yaml'); - await buildStep.writeAsString(backwardsCompatOutputId, contents.toString()); + buildStep.inputId.package, + 'test/dart_test.browser_aggregate.yaml', + ); + await buildStep.writeAsString( + backwardsCompatOutputId, + contents.toString(), + ); } } Future decodeConfig(BuildStep buildStep) async { - final id = - AssetId(buildStep.inputId.package, 'test/test_html_builder_config.json'); + final id = AssetId( + buildStep.inputId.package, + 'test/test_html_builder_config.json', + ); final contents = await buildStep.readAsString(id); return TestHtmlBuilderConfig.fromJson(json.decode(contents)); } diff --git a/lib/src/config.dart b/lib/src/config.dart index a4d725d..cc22ee7 100644 --- a/lib/src/config.dart +++ b/lib/src/config.dart @@ -24,25 +24,28 @@ import 'package:json_annotation/json_annotation.dart'; part 'config.g.dart'; @JsonSerializable( - anyMap: true, - checked: true, - disallowUnrecognizedKeys: true, - fieldRename: FieldRename.snake) + anyMap: true, + checked: true, + disallowUnrecognizedKeys: true, + fieldRename: FieldRename.snake, +) class TestHtmlBuilderConfig { - TestHtmlBuilderConfig( - {bool? browserAggregation, - String? randomizeOrderingSeed, - Map>? templates}) - : browserAggregation = browserAggregation ?? false, - randomizeOrderingSeed = randomizeOrderingSeed, - templates = templates ?? {}; + TestHtmlBuilderConfig({ + bool? browserAggregation, + String? randomizeOrderingSeed, + Map>? templates, + }) : browserAggregation = browserAggregation ?? false, + randomizeOrderingSeed = randomizeOrderingSeed, + templates = templates ?? {}; factory TestHtmlBuilderConfig.fromBuilderOptions(BuilderOptions options) { final config = TestHtmlBuilderConfig.fromJson(options.config); for (final path in config.templates.keys) { if (!path.startsWith('./test/') && !path.startsWith('test/')) { - throw StateError('Invalid template path: $path\n' - 'Every test html template must be located in the `test/` directory.'); + throw StateError( + 'Invalid template path: $path\n' + 'Every test html template must be located in the `test/` directory.', + ); } } return config; @@ -53,7 +56,7 @@ class TestHtmlBuilderConfig { return _$TestHtmlBuilderConfigFromJson(json); } on CheckedFromJsonException catch (e) { final lines = [ - 'Could not parse the options provided for `test_html_builder`.' + 'Could not parse the options provided for `test_html_builder`.', ]; final key = e.key; @@ -81,8 +84,9 @@ class TestHtmlBuilderConfig { final Map> templates; late final Map> templateGlobs = templates.map( - (key, globPatterns) => - MapEntry(key, globPatterns.map((pattern) => Glob(pattern)))); + (key, globPatterns) => + MapEntry(key, globPatterns.map((pattern) => Glob(pattern))), + ); Map toJson() => _$TestHtmlBuilderConfigToJson(this); } diff --git a/lib/src/config.g.dart b/lib/src/config.g.dart index 2c4151f..d015cec 100644 --- a/lib/src/config.g.dart +++ b/lib/src/config.g.dart @@ -1,42 +1,55 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of lib.src.config; +part of 'config.dart'; // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** -TestHtmlBuilderConfig _$TestHtmlBuilderConfigFromJson(Map json) { - return $checkedNew('TestHtmlBuilderConfig', json, () { - $checkKeys(json, allowedKeys: const [ - 'browser_aggregation', - 'randomize_ordering_seed', - 'templates' - ]); - final val = TestHtmlBuilderConfig( - browserAggregation: - $checkedConvert(json, 'browser_aggregation', (v) => v as bool?), - randomizeOrderingSeed: - $checkedConvert(json, 'randomize_ordering_seed', (v) => v as String?), - templates: $checkedConvert( +TestHtmlBuilderConfig _$TestHtmlBuilderConfigFromJson(Map json) => + $checkedCreate( + 'TestHtmlBuilderConfig', + json, + ($checkedConvert) { + $checkKeys( json, - 'templates', - (v) => (v as Map?)?.map( - (k, e) => MapEntry(k as String, - (e as List).map((e) => e as String).toList()), - )), + allowedKeys: const [ + 'browser_aggregation', + 'randomize_ordering_seed', + 'templates', + ], + ); + final val = TestHtmlBuilderConfig( + browserAggregation: $checkedConvert( + 'browser_aggregation', + (v) => v as bool?, + ), + randomizeOrderingSeed: $checkedConvert( + 'randomize_ordering_seed', + (v) => v as String?, + ), + templates: $checkedConvert( + 'templates', + (v) => (v as Map?)?.map( + (k, e) => MapEntry( + k as String, + (e as List).map((e) => e as String).toList(), + ), + ), + ), + ); + return val; + }, + fieldKeyMap: const { + 'browserAggregation': 'browser_aggregation', + 'randomizeOrderingSeed': 'randomize_ordering_seed', + }, ); - return val; - }, fieldKeyMap: const { - 'browserAggregation': 'browser_aggregation', - 'randomizeOrderingSeed': 'randomize_ordering_seed' - }); -} Map _$TestHtmlBuilderConfigToJson( - TestHtmlBuilderConfig instance) => - { - 'browser_aggregation': instance.browserAggregation, - 'randomize_ordering_seed': instance.randomizeOrderingSeed, - 'templates': instance.templates, - }; + TestHtmlBuilderConfig instance, +) => { + 'browser_aggregation': instance.browserAggregation, + 'randomize_ordering_seed': instance.randomizeOrderingSeed, + 'templates': instance.templates, +}; diff --git a/pubspec.yaml b/pubspec.yaml index da7821b..f85c2e8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,27 +7,28 @@ description: > environment: - sdk: '>=2.12.0 <4.0.0' + sdk: '>=3.11.0 <4.0.0' executables: browser_aggregate_tests: dependencies: args: ^2.3.0 - build: ^2.1.0 - dart_style: ^2.1.1 + build: ^4.0.4 + dart_style: ^3.1.5 glob: ^2.0.1 json_annotation: ^4.1.0 path: ^1.8.0 - test: ^1.17.12 - test_core: ">=0.4.2 <0.7.0" + pub_semver: ^2.0.0 + test_core: ^0.6.0 yaml: ^3.1.0 dev_dependencies: build_runner: ^2.1.2 - build_test: ^2.1.3 - build_web_compilers: '>=3.0.0 <5.0.0' - dependency_validator: ^3.1.0 + build_test: ^3.0.0 + build_web_compilers: ^4.0.0 + dependency_validator: ^5.0.0 + test: ^1.17.12 test_descriptor: ^2.0.0 test_process: ^2.0.2 workiva_analysis_options: ^1.4.1 @@ -35,4 +36,4 @@ dev_dependencies: # If changes are made to `lib/src/config.dart` and regeneration is needed, # uncomment this and comment out the test_html_builder definition in # `build.yaml`. - # json_serializable: ^4.0.0 + # json_serializable: ^6.0.0 diff --git a/test/bin/browser_aggregate_tests_test.dart b/test/bin/browser_aggregate_tests_test.dart index 4abcd9d..5767536 100644 --- a/test/bin/browser_aggregate_tests_test.dart +++ b/test/bin/browser_aggregate_tests_test.dart @@ -6,8 +6,10 @@ import 'package:test_descriptor/test_descriptor.dart' as d; import 'package:test_process/test_process.dart'; void main() { - Future createProject( - {bool? browserAggregation, bool? customBuildYaml}) async { + Future createProject({ + bool? browserAggregation, + bool? customBuildYaml, + }) async { browserAggregation ??= true; customBuildYaml ??= false; await d.dir('pkg', [ @@ -23,13 +25,16 @@ dev_dependencies: path: ${p.current} '''), if (browserAggregation) - d.file(customBuildYaml ? 'build.custom.yaml' : 'build.yaml', '''targets: + d.file( + customBuildYaml ? 'build.custom.yaml' : 'build.yaml', + '''targets: \$default: builders: test_html_builder: options: browser_aggregation: true -'''), +''', + ), if (browserAggregation) d.file('dart_test.yaml', 'include: dart_test.browser_aggregate.yaml'), d.dir('test', [ @@ -44,137 +49,175 @@ void main() { return d.path('pkg'); } - Future testBrowserAggregateExecutable(List args, - {String? workingDirectory}) async { - final pubGet = await TestProcess.start('dart', ['pub', 'get'], - workingDirectory: workingDirectory); + Future testBrowserAggregateExecutable( + List args, { + String? workingDirectory, + }) async { + final pubGet = await TestProcess.start('dart', [ + 'pub', + 'get', + ], workingDirectory: workingDirectory); await pubGet.shouldExit(0); - return TestProcess.start( - 'dart', ['run', 'test_html_builder:browser_aggregate_tests', ...args], - workingDirectory: workingDirectory); + return TestProcess.start('dart', [ + 'run', + 'test_html_builder:browser_aggregate_tests', + ...args, + ], workingDirectory: workingDirectory); } test('--mode=args', () async { final dir = await createProject(); - final process = await testBrowserAggregateExecutable(['--mode=args'], - workingDirectory: dir); + final process = await testBrowserAggregateExecutable([ + '--mode=args', + ], workingDirectory: dir); expect( - process.stdout, - emitsInOrder([ - '--build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate', - emitsDone - ])); + process.stdout, + emitsInOrder([ + '--build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate', + emitsDone, + ]), + ); await process.shouldExit(0); }); test('--mode=args --release --build-args="-c custom"', () async { final dir = await createProject(customBuildYaml: true); - final process = await testBrowserAggregateExecutable( - ['--mode=args', '--release', '--build-args', '-c custom'], - workingDirectory: dir); + final process = await testBrowserAggregateExecutable([ + '--mode=args', + '--release', + '--build-args', + '-c custom', + ], workingDirectory: dir); expect( - process.stdout, - emitsInOrder([ - '--build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate', - emitsDone - ])); + process.stdout, + emitsInOrder([ + '--build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate', + emitsDone, + ]), + ); await process.shouldExit(0); }); test('--mode=build', () async { final dir = await createProject(); - final process = await testBrowserAggregateExecutable(['--mode=build'], - workingDirectory: dir); + final process = await testBrowserAggregateExecutable([ + '--mode=build', + ], workingDirectory: dir); expect( - process.stdout, - emitsInOrder([ - emitsThrough('Building browser aggregate test config...'), - emitsThrough( - 'dart run build_runner build --delete-conflicting-outputs --build-filter=dart_test.browser_aggregate.yaml'), - emitsThrough(contains('Succeeded')), - emitsThrough('Reading browser aggregate test config...'), - emitsThrough('Found 1 aggregate tests to run.'), - emitsThrough( - 'dart run build_runner build --delete-conflicting-outputs --build-filter=test/templates/default_template.browser_aggregate_test.**'), - emitsThrough(contains('Succeeded')), - ])); + process.stdout, + emitsInOrder([ + emitsThrough('Building browser aggregate test config...'), + emitsThrough( + 'dart run build_runner build --delete-conflicting-outputs --build-filter=dart_test.browser_aggregate.yaml', + ), + emitsThrough(contains('Built with build_runner/jit')), + emitsThrough('Reading browser aggregate test config...'), + emitsThrough('Found 1 aggregate tests to run.'), + emitsThrough( + 'dart run build_runner build --delete-conflicting-outputs --build-filter=test/templates/default_template.browser_aggregate_test.**', + ), + emitsThrough(contains('Built with build_runner/jit')), + ]), + ); await process.shouldExit(0); }); test('--mode=build --release --build-args="-c custom"', () async { final dir = await createProject(customBuildYaml: true); - final process = await testBrowserAggregateExecutable( - ['--mode=build', '--release', '--build-args', '-c custom'], - workingDirectory: dir); + final process = await testBrowserAggregateExecutable([ + '--mode=build', + '--release', + '--build-args', + '-c custom', + ], workingDirectory: dir); expect( - process.stdout, - emitsInOrder([ - emitsThrough('Building browser aggregate test config...'), - emitsThrough( - 'dart run build_runner build --delete-conflicting-outputs -c custom --build-filter=dart_test.browser_aggregate.yaml'), - emitsThrough(contains('Succeeded')), - emitsThrough('Reading browser aggregate test config...'), - emitsThrough('Found 1 aggregate tests to run.'), - emitsThrough( - 'dart run build_runner build --delete-conflicting-outputs -c custom --release --build-filter=test/templates/default_template.browser_aggregate_test.**'), - emitsThrough(contains('Succeeded')), - ])); + process.stdout, + emitsInOrder([ + emitsThrough('Building browser aggregate test config...'), + emitsThrough( + 'dart run build_runner build --delete-conflicting-outputs -c custom --build-filter=dart_test.browser_aggregate.yaml', + ), + emitsThrough(contains('Built with build_runner/jit')), + emitsThrough('Reading browser aggregate test config...'), + emitsThrough('Found 1 aggregate tests to run.'), + emitsThrough( + 'dart run build_runner build --delete-conflicting-outputs -c custom --release --build-filter=test/templates/default_template.browser_aggregate_test.**', + ), + emitsThrough(contains('Built with build_runner/jit')), + ]), + ); await process.shouldExit(0); }); test('--mode=test', () async { final dir = await createProject(); // --mode=test is the default - final process = - await testBrowserAggregateExecutable([], workingDirectory: dir); + final process = await testBrowserAggregateExecutable( + [], + workingDirectory: dir, + ); expect( - process.stdout, - emitsInOrder([ - emitsThrough('Building browser aggregate test config...'), - emitsThrough( - 'dart run build_runner build --delete-conflicting-outputs --build-filter=dart_test.browser_aggregate.yaml'), - emitsThrough(contains('Succeeded')), - emitsThrough('Reading browser aggregate test config...'), - emitsThrough('Found 1 aggregate tests to run.'), - emitsThrough( - 'dart run build_runner test --build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate'), - emitsThrough( - anyOf(contains('All tests passed!'), contains('1 test passed.'))), - ])); + process.stdout, + emitsInOrder([ + emitsThrough('Building browser aggregate test config...'), + emitsThrough( + 'dart run build_runner build --delete-conflicting-outputs --build-filter=dart_test.browser_aggregate.yaml', + ), + emitsThrough(contains('Built with build_runner/jit')), + emitsThrough('Reading browser aggregate test config...'), + emitsThrough('Found 1 aggregate tests to run.'), + emitsThrough( + 'dart run build_runner test --build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate', + ), + emitsThrough( + anyOf(contains('All tests passed!'), contains('1 test passed.')), + ), + ]), + ); await process.shouldExit(0); }); test('--mode=test --build-args="--release -c custom"', () async { final dir = await createProject(customBuildYaml: true); // --mode=test is the default - final process = await testBrowserAggregateExecutable( - ['--build-args', '--release -c custom'], - workingDirectory: dir); + final process = await testBrowserAggregateExecutable([ + '--build-args', + '--release -c custom', + ], workingDirectory: dir); expect( - process.stdout, - emitsInOrder([ - emitsThrough('Building browser aggregate test config...'), - emitsThrough( - 'dart run build_runner build --delete-conflicting-outputs --release -c custom --build-filter=dart_test.browser_aggregate.yaml'), - emitsThrough(contains('Succeeded')), - emitsThrough('Reading browser aggregate test config...'), - emitsThrough('Found 1 aggregate tests to run.'), - emitsThrough( - 'dart run build_runner test --release -c custom --build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate'), - emitsThrough( - anyOf(contains('All tests passed!'), contains('1 test passed.'))), - ])); + process.stdout, + emitsInOrder([ + emitsThrough('Building browser aggregate test config...'), + emitsThrough( + 'dart run build_runner build --delete-conflicting-outputs --release -c custom --build-filter=dart_test.browser_aggregate.yaml', + ), + emitsThrough(contains('Built with build_runner/jit')), + emitsThrough('Reading browser aggregate test config...'), + emitsThrough('Found 1 aggregate tests to run.'), + emitsThrough( + 'dart run build_runner test --release -c custom --build-filter=test/templates/default_template.browser_aggregate_test.** -- --preset=browser-aggregate', + ), + emitsThrough( + anyOf(contains('All tests passed!'), contains('1 test passed.')), + ), + ]), + ); await process.shouldExit(0); }); test('warns when browser aggregation is not enabled', () async { final dir = await createProject(browserAggregation: false); - final process = await testBrowserAggregateExecutable(['--mode=args'], - workingDirectory: dir); + final process = await testBrowserAggregateExecutable([ + '--mode=args', + ], workingDirectory: dir); expect( - process.stdout, - emits(contains( - 'browser aggregation is not enabled. Update your build.yaml'))); + process.stdout, + emits( + contains( + 'browser aggregation is not enabled. Update your build.yaml', + ), + ), + ); await process.shouldExit(isNot(0)); }); } diff --git a/test/lib/aggregate_test_builder_test.dart b/test/lib/aggregate_test_builder_test.dart index 75b753f..e3a1e78 100644 --- a/test/lib/aggregate_test_builder_test.dart +++ b/test/lib/aggregate_test_builder_test.dart @@ -9,9 +9,11 @@ import 'package:test_html_builder/src/config.dart'; void main() { group('AggregateTestBuilder', () { test('does nothing if browser aggregate not enabled', () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/foo_template.html': ['test/foo_test.dart'], - }); + final config = TestHtmlBuilderConfig( + templates: { + 'test/foo_template.html': ['test/foo_test.dart'], + }, + ); final builder = AggregateTestBuilder(); await testBuilder(builder, { 'a|test/test_html_builder_config.json': jsonEncode(config), @@ -21,22 +23,27 @@ void main() { }); test('generates an aggregate test for each template', () async { - final config = - TestHtmlBuilderConfig(browserAggregation: true, templates: { - 'test/templates/foo_template.html': ['test/foo_test.dart'], - 'test/templates/bar_template.html': ['test/bar_test.dart'], - }); + final config = TestHtmlBuilderConfig( + browserAggregation: true, + templates: { + 'test/templates/foo_template.html': ['test/foo_test.dart'], + 'test/templates/bar_template.html': ['test/bar_test.dart'], + }, + ); final builder = AggregateTestBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/vm_test.dart': "@TestOn('vm') library vm_test;", - 'a|test/foo_test.dart': '', - 'a|test/bar_test.dart': '', - 'a|test/templates/foo_template.html': '', - 'a|test/templates/bar_template.html': '', - }, outputs: { - 'a|test/templates/foo_template.browser_aggregate_test.dart': - '''@TestOn('browser') + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/vm_test.dart': "@TestOn('vm') library vm_test;", + 'a|test/foo_test.dart': '', + 'a|test/bar_test.dart': '', + 'a|test/templates/foo_template.html': '', + 'a|test/templates/bar_template.html': '', + }, + outputs: { + 'a|test/templates/foo_template.browser_aggregate_test.dart': + '''@TestOn('browser') import 'package:test/test.dart'; import '../foo_test.dart' as foo_test; @@ -45,8 +52,8 @@ void main() { foo_test.main(); } ''', - 'a|test/templates/bar_template.browser_aggregate_test.dart': - '''@TestOn('browser') + 'a|test/templates/bar_template.browser_aggregate_test.dart': + '''@TestOn('browser') import 'package:test/test.dart'; import '../bar_test.dart' as bar_test; @@ -54,28 +61,34 @@ import '../bar_test.dart' as bar_test; void main() { bar_test.main(); } -''' - }); +''', + }, + ); }); test('generates a default aggregate test for browser tests', () async { - final config = - TestHtmlBuilderConfig(browserAggregation: true, templates: { - 'test/templates/foo_template.html': ['test/foo_test.dart'], - 'test/templates/bar_template.html': ['test/bar_test.dart'], - }); + final config = TestHtmlBuilderConfig( + browserAggregation: true, + templates: { + 'test/templates/foo_template.html': ['test/foo_test.dart'], + 'test/templates/bar_template.html': ['test/bar_test.dart'], + }, + ); final builder = AggregateTestBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/templates/default_template.html': '', - 'a|test/templates/foo_template.html': '', - 'a|test/templates/bar_template.html': '', - 'a|test/vm_test.dart': "@TestOn('vm') library vm_test;", - 'a|test/a_test.dart': '', - 'a|test/b_test.dart': '', - }, outputs: { - 'a|test/templates/default_template.browser_aggregate_test.dart': - '''@TestOn('browser') + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/templates/default_template.html': '', + 'a|test/templates/foo_template.html': '', + 'a|test/templates/bar_template.html': '', + 'a|test/vm_test.dart': "@TestOn('vm') library vm_test;", + 'a|test/a_test.dart': '', + 'a|test/b_test.dart': '', + }, + outputs: { + 'a|test/templates/default_template.browser_aggregate_test.dart': + '''@TestOn('browser') import 'package:test/test.dart'; import '../a_test.dart' as a_test; @@ -85,31 +98,36 @@ void main() { a_test.main(); b_test.main(); } -''' - }); +''', + }, + ); }); - test('randomizes the ordering of tests in aggregate for browser tests', - () async { - final config = TestHtmlBuilderConfig( - browserAggregation: true, - randomizeOrderingSeed: '2', - ); - final builder = AggregateTestBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/templates/default_template.html': '', - 'a|test/templates/foo_template.html': '', - 'a|test/templates/bar_template.html': '', - 'a|test/vm_test.dart': "@TestOn('vm') library vm_test;", - 'a|test/a_test.dart': '', - 'a|test/b_test.dart': '', - 'a|test/c_test.dart': '', - 'a|test/d_test.dart': '', - 'a|test/e_test.dart': '', - }, outputs: { - 'a|test/templates/default_template.browser_aggregate_test.dart': - '''@TestOn('browser') + test( + 'randomizes the ordering of tests in aggregate for browser tests', + () async { + final config = TestHtmlBuilderConfig( + browserAggregation: true, + randomizeOrderingSeed: '2', + ); + final builder = AggregateTestBuilder(); + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/templates/default_template.html': '', + 'a|test/templates/foo_template.html': '', + 'a|test/templates/bar_template.html': '', + 'a|test/vm_test.dart': "@TestOn('vm') library vm_test;", + 'a|test/a_test.dart': '', + 'a|test/b_test.dart': '', + 'a|test/c_test.dart': '', + 'a|test/d_test.dart': '', + 'a|test/e_test.dart': '', + }, + outputs: { + 'a|test/templates/default_template.browser_aggregate_test.dart': + '''@TestOn('browser') import 'package:test/test.dart'; import '../a_test.dart' as a_test; @@ -120,33 +138,41 @@ import '../e_test.dart' as e_test; void main() { print( - 'test/templates/default_template.html built with `randomize_ordering_seed: "2"`'); + 'test/templates/default_template.html built with `randomize_ordering_seed: "2"`', + ); c_test.main(); a_test.main(); e_test.main(); b_test.main(); d_test.main(); } -''' - }); - }); +''', + }, + ); + }, + ); test('if multiple templates match, chooses first one', () async { - final config = - TestHtmlBuilderConfig(browserAggregation: true, templates: { - 'test/templates/foo_template.html': ['test/**_test.dart'], - 'test/templates/bar_template.html': ['test/**_test.dart'], - }); + final config = TestHtmlBuilderConfig( + browserAggregation: true, + templates: { + 'test/templates/foo_template.html': ['test/**_test.dart'], + 'test/templates/bar_template.html': ['test/**_test.dart'], + }, + ); final builder = AggregateTestBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/bar_test.dart': '', - 'a|test/foo_test.dart': '', - 'a|test/templates/foo_template.html': '', - 'a|test/templates/bar_template.html': '', - }, outputs: { - 'a|test/templates/foo_template.browser_aggregate_test.dart': - '''@TestOn('browser') + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/bar_test.dart': '', + 'a|test/foo_test.dart': '', + 'a|test/templates/foo_template.html': '', + 'a|test/templates/bar_template.html': '', + }, + outputs: { + 'a|test/templates/foo_template.browser_aggregate_test.dart': + '''@TestOn('browser') import 'package:test/test.dart'; import '../bar_test.dart' as bar_test; @@ -156,27 +182,33 @@ void main() { bar_test.main(); foo_test.main(); } -''' - }); +''', + }, + ); }); test('sorts the test imports and invocations', () async { - final config = - TestHtmlBuilderConfig(browserAggregation: true, templates: { - 'test/templates/foo_template.html': ['test/**_test.dart'], - }); + final config = TestHtmlBuilderConfig( + browserAggregation: true, + templates: { + 'test/templates/foo_template.html': ['test/**_test.dart'], + }, + ); final builder = AggregateTestBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/templates/foo_template.html': '', - // These three are intentionally unsorted so that this test can verify - // that the builder sorts them. - 'a|test/foo_test.dart': '', - 'a|test/baz/baz_test.dart': '', - 'a|test/bar_test.dart': '', - }, outputs: { - 'a|test/templates/foo_template.browser_aggregate_test.dart': - '''@TestOn('browser') + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/templates/foo_template.html': '', + // These three are intentionally unsorted so that this test can verify + // that the builder sorts them. + 'a|test/foo_test.dart': '', + 'a|test/baz/baz_test.dart': '', + 'a|test/bar_test.dart': '', + }, + outputs: { + 'a|test/templates/foo_template.browser_aggregate_test.dart': + '''@TestOn('browser') import 'package:test/test.dart'; import '../bar_test.dart' as bar_test; @@ -188,8 +220,9 @@ void main() { baz_baz_test.main(); foo_test.main(); } -''' - }); +''', + }, + ); }); }); } diff --git a/test/lib/config_test.dart b/test/lib/config_test.dart index 73b2d84..13d84c9 100644 --- a/test/lib/config_test.dart +++ b/test/lib/config_test.dart @@ -19,7 +19,7 @@ void main() { 'randomize_ordering_seed': 'random', 'templates': { 'test/foo_template.html': ['test/**_test.dart'], - } + }, }); final config = TestHtmlBuilderConfig.fromBuilderOptions(options); expect(config.browserAggregation, isTrue); @@ -28,37 +28,49 @@ void main() { 'test/foo_template.html': ['test/**_test.dart'], }); expect( - config.templateGlobs['test/foo_template.html']!.first - .matches('test/foo_test.dart'), - isTrue); + config.templateGlobs['test/foo_template.html']!.first.matches( + 'test/foo_test.dart', + ), + isTrue, + ); }); test('throws StateError if any templates are not in test/', () { final options = BuilderOptions({ 'templates': { 'lib/bad_template.html': ['test/**_test.dart'], - } + }, }); expect( - () => TestHtmlBuilderConfig.fromBuilderOptions(options), - throwsA(isA().having( - (e) => e.message, 'message', contains('lib/bad_template.html')))); + () => TestHtmlBuilderConfig.fromBuilderOptions(options), + throwsA( + isA().having( + (e) => e.message, + 'message', + contains('lib/bad_template.html'), + ), + ), + ); }); test('throws StateError if unsupported option key is provided', () { final options = BuilderOptions({ 'badkey': 'foo', }); - expect(() => TestHtmlBuilderConfig.fromBuilderOptions(options), - throwsStateError); + expect( + () => TestHtmlBuilderConfig.fromBuilderOptions(options), + throwsStateError, + ); }); test('throws StateError if templates format is invalid', () { final options = BuilderOptions({ 'templates': ['invalid format'], }); - expect(() => TestHtmlBuilderConfig.fromBuilderOptions(options), - throwsStateError); + expect( + () => TestHtmlBuilderConfig.fromBuilderOptions(options), + throwsStateError, + ); }); }); } diff --git a/test/lib/dart_test_yaml_builder_test.dart b/test/lib/dart_test_yaml_builder_test.dart index 6445d05..7e3e929 100644 --- a/test/lib/dart_test_yaml_builder_test.dart +++ b/test/lib/dart_test_yaml_builder_test.dart @@ -21,32 +21,36 @@ void main() { test('generates a preset with a path for each aggregate test', () async { final config = TestHtmlBuilderConfig(browserAggregation: true); final builder = DartTestYamlBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/foo_template.browser_aggregate_test.dart': '', - 'a|test/foo_template.html': '', - // This template should be found, but ignored because there is no - // accompanying .browser_aggregate_test.dart - 'a|test/bar_template.html': '', - // This test should get included because it has a custom HTML - 'a|test/custom_test.dart': '', - 'a|test/custom_test.custom.html': '', - }, outputs: { - 'a|dart_test.browser_aggregate.yaml': '''presets: + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/foo_template.browser_aggregate_test.dart': '', + 'a|test/foo_template.html': '', + // This template should be found, but ignored because there is no + // accompanying .browser_aggregate_test.dart + 'a|test/bar_template.html': '', + // This test should get included because it has a custom HTML + 'a|test/custom_test.dart': '', + 'a|test/custom_test.custom.html': '', + }, + outputs: { + 'a|dart_test.browser_aggregate.yaml': '''presets: browser-aggregate: platforms: [chrome] paths: - test/foo_template.browser_aggregate_test.dart - test/custom_test.dart ''', - 'a|test/dart_test.browser_aggregate.yaml': '''presets: + 'a|test/dart_test.browser_aggregate.yaml': '''presets: browser-aggregate: platforms: [chrome] paths: - test/foo_template.browser_aggregate_test.dart - test/custom_test.dart -''' - }); +''', + }, + ); }); }); } diff --git a/test/lib/template_builder_test.dart b/test/lib/template_builder_test.dart index 074191d..c30bf88 100644 --- a/test/lib/template_builder_test.dart +++ b/test/lib/template_builder_test.dart @@ -33,9 +33,11 @@ void main() { }); test('does nothing if template does not match asset', () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/template.html': ['test/no_match.dart'], - }); + final config = TestHtmlBuilderConfig( + templates: { + 'test/template.html': ['test/no_match.dart'], + }, + ); final builder = TemplateBuilder(); await testBuilder(builder, { 'a|test/test_html_builder_config.json': jsonEncode(config), @@ -45,98 +47,141 @@ void main() { }); test('outputs .html if template matches asset', () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/template.html': ['test/**_test.dart'], - }); + final config = TestHtmlBuilderConfig( + templates: { + 'test/template.html': ['test/**_test.dart'], + }, + ); final builder = TemplateBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/bar_test.dart': '', - 'a|test/foo_test.dart': '', - 'a|test/template.html': '{{testScript}}', - }, outputs: { - 'a|test/bar_test.html': - '''''', - 'a|test/foo_test.html': - '''''', - }); + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/bar_test.dart': '', + 'a|test/foo_test.dart': '', + 'a|test/template.html': '{{testScript}}', + }, + outputs: { + 'a|test/bar_test.html': + '''''', + 'a|test/foo_test.html': + '''''', + }, + ); }); - test('chooses first template that matches asset if multiple match', - () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/a_template.html': ['test/bar_test.dart'], - 'test/b_template.html': ['test/**_test.dart'], - }); + test('chooses first template that matches asset if multiple match', () async { + final config = TestHtmlBuilderConfig( + templates: { + 'test/a_template.html': ['test/bar_test.dart'], + 'test/b_template.html': ['test/**_test.dart'], + }, + ); final builder = TemplateBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/bar_test.dart': '', - 'a|test/foo_test.dart': '', - 'a|test/a_template.html': - '{{testScript}}', - 'a|test/b_template.html': - '{{testScript}}', - }, outputs: { - 'a|test/bar_test.html': - '''''', - 'a|test/foo_test.html': - '''''', - }); + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/bar_test.dart': '', + 'a|test/foo_test.dart': '', + 'a|test/a_template.html': + '{{testScript}}', + 'a|test/b_template.html': + '{{testScript}}', + }, + outputs: { + 'a|test/bar_test.html': + '''''', + 'a|test/foo_test.html': + '''''', + }, + ); }); test('copies .custom.html asset if found', () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/template.html': ['test/**_test.dart'], - }); + final config = TestHtmlBuilderConfig( + templates: { + 'test/template.html': ['test/**_test.dart'], + }, + ); final builder = TemplateBuilder(); - await testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/bar_test.dart': '', - 'a|test/bar_test.custom.html': 'CUSTOM BAR TEST', - 'a|test/foo_test.dart': '', - 'a|test/foo_test.custom.html': 'CUSTOM FOO TEST', - 'a|test/template.html': '{{testScript}}', - }, outputs: { - 'a|test/bar_test.html': 'CUSTOM BAR TEST', - 'a|test/foo_test.html': 'CUSTOM FOO TEST', - }); + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/bar_test.dart': '', + 'a|test/bar_test.custom.html': 'CUSTOM BAR TEST', + 'a|test/foo_test.dart': '', + 'a|test/foo_test.custom.html': 'CUSTOM FOO TEST', + 'a|test/template.html': '{{testScript}}', + }, + outputs: { + 'a|test/bar_test.html': 'CUSTOM BAR TEST', + 'a|test/foo_test.html': 'CUSTOM FOO TEST', + }, + ); }); test('logs SEVERE if template cannot be read', () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/template.html': ['test/**_test.dart'], - }); + final config = TestHtmlBuilderConfig( + templates: { + 'test/template.html': ['test/**_test.dart'], + }, + ); final builder = TemplateBuilder(); - final logs = recordLogs(() => testBuilder(builder, { - 'a|test/test_html_builder_config.json': jsonEncode(config), - 'a|test/foo_test.dart': '', - })); + final logs = []; + await testBuilder( + builder, + { + 'a|test/test_html_builder_config.json': jsonEncode(config), + 'a|test/foo_test.dart': '', + }, + onLog: logs.add, + ); expect( - logs, - emits(severeLogOf(allOf( - contains('Could not read template'), - contains('test/template.html'), - )))); + logs, + contains( + severeLogOf( + allOf( + contains('Could not read template'), + contains('test/template.html'), + ), + ), + ), + ); }); - test('logs SEVERE if template does not contain `{{testScript}}` token', - () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/template.html': ['test/**_test.dart'], - }); - final builder = TemplateBuilder(); - final logs = recordLogs(() => testBuilder(builder, { + test( + 'logs SEVERE if template does not contain `{{testScript}}` token', + () async { + final config = TestHtmlBuilderConfig( + templates: { + 'test/template.html': ['test/**_test.dart'], + }, + ); + final builder = TemplateBuilder(); + final logs = []; + await testBuilder( + builder, + { 'a|test/test_html_builder_config.json': jsonEncode(config), 'a|test/foo_test.dart': '', 'a|test/template.html': 'MISSING TOKEN', - })); - expect( + }, + onLog: logs.add, + ); + expect( logs, - emits(severeLogOf(allOf( - contains('template must contain exactly one `{{testScript}}`'), - contains('test/template.html'), - )))); - }); + contains( + severeLogOf( + allOf( + contains('template must contain exactly one `{{testScript}}`'), + contains('test/template.html'), + ), + ), + ), + ); + }, + ); }); } diff --git a/test/lib/test_html_builder_test.dart b/test/lib/test_html_builder_test.dart index ddbbbfb..3632f77 100644 --- a/test/lib/test_html_builder_test.dart +++ b/test/lib/test_html_builder_test.dart @@ -8,14 +8,19 @@ import 'package:test_html_builder/src/config.dart'; void main() { test('TestHtmlBuilder', () async { - final config = TestHtmlBuilderConfig(templates: { - 'test/template.html': ['test/foo_test.dart'], - }); + final config = TestHtmlBuilderConfig( + templates: { + 'test/template.html': ['test/foo_test.dart'], + }, + ); final builder = TestHtmlBuilder(config); - await testBuilder(builder, { - r'a|$package$': '', - }, outputs: { - 'a|test/templates/default_template.html': ''' + await testBuilder( + builder, + { + 'a|pubspec.yaml': 'name: a', + }, + outputs: { + 'a|test/templates/default_template.html': ''' {{testName}} Test @@ -24,7 +29,8 @@ void main() { ''', - 'a|test/test_html_builder_config.json': jsonEncode(config), - }); + 'a|test/test_html_builder_config.json': jsonEncode(config), + }, + ); }); }