Skip to content

Commit

Permalink
Extending tests and restructuring logic into helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Markzipan committed Dec 12, 2023
1 parent 6d5bcfe commit 2cff2ce
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dwds/lib/src/services/expression_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion frontend_server_common/lib/src/frontend_server_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(' '));
Expand Down
56 changes: 38 additions & 18 deletions test_common/lib/sdk_asset_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,53 @@ class SdkAssetGenerator {
}
}

Future<void> _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<void> _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;
Expand All @@ -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');

Expand Down
91 changes: 81 additions & 10 deletions test_common/test/sdk_asset_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -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'));
});
});
Expand Down

0 comments on commit 2cff2ce

Please sign in to comment.