From 2cff2ce6d8c5268958daaebf786e5c6537edb61b Mon Sep 17 00:00:00 2001 From: MarkZ Date: Tue, 12 Dec 2023 14:56:07 -0800 Subject: [PATCH] Extending tests and restructuring logic into helpers --- .../lib/src/services/expression_compiler.dart | 2 +- .../lib/src/frontend_server_client.dart | 2 +- test_common/lib/sdk_asset_generator.dart | 56 ++++++++---- .../test/sdk_asset_generator_test.dart | 91 +++++++++++++++++-- 4 files changed, 121 insertions(+), 30 deletions(-) diff --git a/dwds/lib/src/services/expression_compiler.dart b/dwds/lib/src/services/expression_compiler.dart index 7cbf262ac..c1b7306b3 100644 --- a/dwds/lib/src/services/expression_compiler.dart +++ b/dwds/lib/src/services/expression_compiler.dart @@ -17,7 +17,7 @@ class CompilerOptions { }); } -// Indicates the module system DDC is targeting. +/// Indicates the module system DDC is targeting. enum ModuleFormat { amd, ddc, es6 } /// Result of compilation of dart expression to JavaScript diff --git a/frontend_server_common/lib/src/frontend_server_client.dart b/frontend_server_common/lib/src/frontend_server_client.dart index eba9d2dbc..860a05c56 100644 --- a/frontend_server_common/lib/src/frontend_server_client.dart +++ b/frontend_server_common/lib/src/frontend_server_client.dart @@ -395,7 +395,7 @@ class ResidentCompiler { if (compilerOptions.canaryFeatures) '--dartdevc-canary', if (verbose) '--verbose', if (compilerOptions.moduleFormat == ModuleFormat.ddc) - '--dartdevc-module-format=legacy' + '--dartdevc-module-format=ddc' ]; _logger.info(args.join(' ')); diff --git a/test_common/lib/sdk_asset_generator.dart b/test_common/lib/sdk_asset_generator.dart index 338279ef2..bcf6d7977 100644 --- a/test_common/lib/sdk_asset_generator.dart +++ b/test_common/lib/sdk_asset_generator.dart @@ -55,27 +55,53 @@ class SdkAssetGenerator { } } - Future _generateSdkJavaScript({ + String resolveSdkJsPath({ required bool soundNullSafety, required bool canaryFeatures, - }) async { - Directory? outputDir; - try { - // Files to copy generated files to. - final outputJsPath = switch ((soundNullSafety, ddcModuleFormat)) { + }) => + switch ((soundNullSafety, ddcModuleFormat)) { (true, ModuleFormat.amd) => sdkLayout.soundAmdJsPath, (false, ModuleFormat.amd) => sdkLayout.weakAmdJsPath, (true, ModuleFormat.ddc) => sdkLayout.soundDdcJsPath, (false, ModuleFormat.ddc) => sdkLayout.weakDdcJsPath, _ => throw Exception('Unsupported DDC module format $ddcModuleFormat.') }; - final outputJsMapPath = switch ((soundNullSafety, ddcModuleFormat)) { + + String resolveSdkSourcemapPath({ + required bool soundNullSafety, + required bool canaryFeatures, + }) => + switch ((soundNullSafety, ddcModuleFormat)) { (true, ModuleFormat.amd) => sdkLayout.soundAmdJsMapPath, (false, ModuleFormat.amd) => sdkLayout.weakAmdJsMapPath, (true, ModuleFormat.ddc) => sdkLayout.soundDdcJsMapPath, (false, ModuleFormat.ddc) => sdkLayout.weakDdcJsMapPath, _ => throw Exception('Unsupported DDC module format $ddcModuleFormat.') }; + + String resolveSdkJsFilename({ + required bool soundNullSafety, + required bool canaryFeatures, + }) => + switch ((soundNullSafety, ddcModuleFormat)) { + (true, ModuleFormat.amd) => sdkLayout.soundAmdJsFileName, + (false, ModuleFormat.amd) => sdkLayout.weakAmdJsFileName, + (true, ModuleFormat.ddc) => sdkLayout.soundDdcJsFileName, + (false, ModuleFormat.ddc) => sdkLayout.weakDdcJsFileName, + _ => throw Exception('Unsupported DDC module format $ddcModuleFormat.') + }; + + Future _generateSdkJavaScript({ + required bool soundNullSafety, + required bool canaryFeatures, + }) async { + Directory? outputDir; + try { + // Files to copy generated files to. + final outputJsPath = resolveSdkJsPath( + soundNullSafety: soundNullSafety, canaryFeatures: canaryFeatures); + final outputJsMapPath = resolveSdkSourcemapPath( + soundNullSafety: soundNullSafety, canaryFeatures: canaryFeatures); final outputFullDillPath = soundNullSafety ? sdkLayout.soundFullDillPath : sdkLayout.weakFullDillPath; @@ -92,17 +118,11 @@ class SdkAssetGenerator { outputDir = fileSystem.systemTempDirectory.createTempSync(); // Files to generate - final jsPath = switch ((soundNullSafety, ddcModuleFormat)) { - (true, ModuleFormat.amd) => - p.join(outputDir.path, sdkLayout.soundAmdJsFileName), - (false, ModuleFormat.amd) => - p.join(outputDir.path, sdkLayout.weakAmdJsFileName), - (true, ModuleFormat.ddc) => - p.join(outputDir.path, sdkLayout.soundDdcJsFileName), - (false, ModuleFormat.ddc) => - p.join(outputDir.path, sdkLayout.weakDdcJsFileName), - _ => throw Exception('Unsupported DDC module format $ddcModuleFormat.') - }; + final jsPath = p.join( + outputDir.path, + resolveSdkJsFilename( + soundNullSafety: soundNullSafety, + canaryFeatures: canaryFeatures)); final jsMapPath = p.setExtension(jsPath, '.js.map'); final fullDillPath = p.setExtension(jsPath, '.dill'); diff --git a/test_common/test/sdk_asset_generator_test.dart b/test_common/test/sdk_asset_generator_test.dart index b609abbdc..12cb1d28d 100644 --- a/test_common/test/sdk_asset_generator_test.dart +++ b/test_common/test/sdk_asset_generator_test.dart @@ -30,8 +30,10 @@ void main() { // Missing weak assets late String weakSdkSummaryPath; late String weakSdkFullDillPath; - late String weakSdkJsPath; - late String weakSdkJsMapPath; + late String weakAmdSdkJsPath; + late String weakAmdSdkJsMapPath; + late String weakDdcSdkJsPath; + late String weakDdcSdkJsMapPath; setUp(() async { setCurrentLogWriter(debug: debug); @@ -58,20 +60,25 @@ void main() { // Simulate missing weak assets. weakSdkSummaryPath = copySdkLayout.weakSummaryPath; weakSdkFullDillPath = copySdkLayout.weakFullDillPath; - weakSdkJsPath = copySdkLayout.weakAmdJsPath; - weakSdkJsMapPath = copySdkLayout.weakAmdJsMapPath; + weakAmdSdkJsPath = copySdkLayout.weakAmdJsPath; + weakAmdSdkJsMapPath = copySdkLayout.weakAmdJsMapPath; + weakDdcSdkJsPath = copySdkLayout.weakAmdJsPath; + weakDdcSdkJsMapPath = copySdkLayout.weakAmdJsMapPath; _deleteIfExists(weakSdkSummaryPath); _deleteIfExists(weakSdkFullDillPath); - _deleteIfExists(weakSdkJsPath); - _deleteIfExists(weakSdkJsMapPath); + _deleteIfExists(weakAmdSdkJsPath); + _deleteIfExists(weakAmdSdkJsMapPath); + _deleteIfExists(weakDdcSdkJsPath); + _deleteIfExists(weakDdcSdkJsMapPath); }); tearDown(() { tempDir.deleteSync(recursive: true); }); - test('Can generate missing SDK assets and validate SDK configuration', + test( + 'Can generate missing SDK assets and validate SDK configuration for the AMD module system', () async { final sdkLayout = TestSdkLayout.createDefault(sdkDirectory); final configuration = TestSdkLayout.createConfiguration(sdkLayout); @@ -95,8 +102,8 @@ void main() { expect(sdkLayout.weakSummaryPath, equals(weakSdkSummaryPath)); expect(sdkLayout.weakFullDillPath, equals(weakSdkFullDillPath)); - expect(sdkLayout.weakAmdJsPath, equals(weakSdkJsPath)); - expect(sdkLayout.weakAmdJsMapPath, equals(weakSdkJsMapPath)); + expect(sdkLayout.weakAmdJsPath, equals(weakAmdSdkJsPath)); + expect(sdkLayout.weakAmdJsMapPath, equals(weakAmdSdkJsMapPath)); // Validate that configuration files exist. configuration.validateSdkDir(); @@ -114,6 +121,50 @@ void main() { expect(sdkLayout.weakAmdJsMapPath, _exists); }); + test( + 'Can generate missing SDK assets and validate SDK configuration for the DDC module system', + () async { + final sdkLayout = TestSdkLayout.createDefault(sdkDirectory); + final configuration = TestSdkLayout.createConfiguration(sdkLayout); + + final assetGenerator = SdkAssetGenerator( + sdkLayout: sdkLayout, + verbose: true, + canaryFeatures: false, + ddcModuleFormat: ModuleFormat.ddc, + ); + await assetGenerator.generateSdkAssets(); + + // Make sure SDK configuration and asset generator agree on the file paths. + expect(configuration.sdkDirectory, equals(sdkDirectory)); + expect(configuration.compilerWorkerPath, equals(compilerWorkerPath)); + + expect(sdkLayout.soundSummaryPath, equals(soundSdkSummaryPath)); + expect(sdkLayout.soundFullDillPath, equals(soundSdkFullDillPath)); + expect(sdkLayout.soundDdcJsPath, equals(soundSdkJsPath)); + expect(sdkLayout.soundDdcJsMapPath, equals(soundSdkJsMapPath)); + + expect(sdkLayout.weakSummaryPath, equals(weakSdkSummaryPath)); + expect(sdkLayout.weakFullDillPath, equals(weakSdkFullDillPath)); + expect(sdkLayout.weakDdcJsPath, equals(weakDdcSdkJsPath)); + expect(sdkLayout.weakDdcJsMapPath, equals(weakDdcSdkJsMapPath)); + + // Validate that configuration files exist. + configuration.validateSdkDir(); + configuration.validate(); + + // Validate all assets exist. + expect(sdkLayout.soundSummaryPath, _exists); + expect(sdkLayout.soundFullDillPath, _exists); + expect(sdkLayout.soundDdcJsPath, _exists); + expect(sdkLayout.soundDdcJsMapPath, _exists); + + expect(sdkLayout.weakSummaryPath, _exists); + expect(sdkLayout.weakFullDillPath, _exists); + expect(sdkLayout.weakDdcJsPath, _exists); + expect(sdkLayout.weakDdcJsMapPath, _exists); + }); + test('Can generate missing SDK assets with canary features enabled', () async { final sdkLayout = TestSdkLayout.createDefault(sdkDirectory); @@ -129,7 +180,27 @@ void main() { final soundSdk = File(soundSdkJsPath).readAsStringSync(); expect(soundSdk, contains('canary')); - final weakSdk = File(weakSdkJsPath).readAsStringSync(); + final weakSdk = File(weakAmdSdkJsPath).readAsStringSync(); + expect(weakSdk, contains('canary')); + }); + + test( + 'Can generate missing SDK assets with canary features enabled for the DDC module system', + () async { + final sdkLayout = TestSdkLayout.createDefault(sdkDirectory); + + final assetGenerator = SdkAssetGenerator( + sdkLayout: sdkLayout, + verbose: true, + canaryFeatures: true, + ddcModuleFormat: ModuleFormat.ddc, + ); + await assetGenerator.generateSdkAssets(); + + final soundSdk = File(soundSdkJsPath).readAsStringSync(); + expect(soundSdk, contains('canary')); + + final weakSdk = File(weakDdcSdkJsPath).readAsStringSync(); expect(weakSdk, contains('canary')); }); });