Skip to content

Create copying sdk configuration provider #1984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 0 additions & 159 deletions dwds/test/fixtures/test_sdk_layout.dart

This file was deleted.

11 changes: 4 additions & 7 deletions test_common/lib/sdk_asset_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:test_common/test_sdk_layout.dart';
/// - sound null safety: js, source map, full dill.
/// - weak null safety: js, source map, full dill, summary.
class SdkAssetGenerator {
static bool _sdkAssetsGenerated = false;
bool _sdkAssetsGenerated = false;
final _logger = Logger('SdkAssetGenerator');

final FileSystem fileSystem;
Expand Down Expand Up @@ -88,18 +88,15 @@ class SdkAssetGenerator {
'org-dartlang-sdk:///lib/libraries.json',
'--modules',
'amd',
if (soundNullSafety)
'--sound-null-safety'
else
'--no-sound-null-safety',
soundNullSafety ? '--sound-null-safety' : '--no-sound-null-safety',
'dart:core',
'-o',
jsPath,
];

final output = <String>[];
_logger.fine('Executing dart ${args.join(' ')}');
final process = await Process.start(Platform.resolvedExecutable, args,
final process = await Process.start(sdkLayout.dartPath, args,
workingDirectory: sdkLayout.sdkDirectory);

process.stdout
Expand Down Expand Up @@ -187,7 +184,7 @@ class SdkAssetGenerator {
];

_logger.fine('Executing dart ${args.join(' ')}');
final process = await Process.start(Platform.resolvedExecutable, args,
final process = await Process.start(sdkLayout.dartPath, args,
workingDirectory: sdkLayout.sdkDirectory);

final output = <String>[];
Expand Down
75 changes: 74 additions & 1 deletion test_common/lib/test_sdk_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,85 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// TODO:(annagrin) Move to a test_common package.
import 'dart:io';

import 'package:dwds/sdk_configuration.dart';
import 'package:logging/logging.dart';

import 'package:test_common/sdk_asset_generator.dart';
import 'package:test_common/test_sdk_layout.dart';

/// Implementation for SDK configuration for tests that can generate
/// missing assets.
///
/// - Generate SDK js, source map, and full dill for weak and sound
/// modes (normally included in flutter SDK or produced by build).
/// - Need to generate SDK summary for weak null safety mode as it
/// is not provided by the SDK installation.
///
/// TODO(annagrin): update to only generating missing sound artifacts
/// for frontend server after we have no uses of weak null safety.
class TestSdkCopyConfigurationProvider extends SdkConfigurationProvider {
final _logger = Logger('TestSdkConfigurationProvider');

final bool _verbose;
late final Directory _sdkDirectory;
SdkConfiguration? _configuration;

late final TestSdkLayout sdkLayout;

TestSdkCopyConfigurationProvider({bool verbose = false})
: _verbose = verbose {
_sdkDirectory = Directory.systemTemp.createTempSync('sdk copy');
sdkLayout = TestSdkLayout.createDefault(_sdkDirectory.path);
}

@override
Future<SdkConfiguration> get configuration async =>
_configuration ??= await _create();

/// Generate missing assets in the default SDK layout.
///
/// Creates a copy of the SDK directory where all the missing assets
/// are generated. Tests using this configuration run using the copy
/// sdk layout to make sure the actual SDK is not modified.
Future<SdkConfiguration> _create() async {
try {
await copyDirectory(
TestSdkLayout.defaultSdkDirectory, _sdkDirectory.path);
} catch (e, s) {
_logger.severe('Failed to create SDK directory copy', e, s);
dispose();
rethrow;
}

try {
final assetGenerator = SdkAssetGenerator(
sdkLayout: sdkLayout,
verboseCompiler: _verbose,
);

await assetGenerator.generateSdkAssets();
return TestSdkLayout.createConfiguration(sdkLayout);
} catch (e, s) {
_logger.severe('Failed generate missing assets', e, s);
dispose();
rethrow;
}
}

void dispose({bool retry = true}) {
try {
if (_sdkDirectory.existsSync()) {
_sdkDirectory.deleteSync(recursive: true);
}
} catch (e, s) {
_logger.warning('Failed delete SDK directory copy', e, s);
dispose(retry: false);
}
}
}

/// Implementation for SDK configuration for tests that can generate
/// missing assets.
///
Expand Down
33 changes: 32 additions & 1 deletion test_common/lib/test_sdk_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:io';

import 'package:dwds/sdk_configuration.dart';
import 'package:path/path.dart' as p;

Expand Down Expand Up @@ -83,7 +85,11 @@ class TestSdkLayout {
'web',
'dart_stack_trace_mapper.js',
),
dartPath: p.join(sdkLayout.sdkDirectory, 'bin', 'dart'),
dartPath: p.join(
sdkLayout.sdkDirectory,
'bin',
Platform.isWindows ? 'dart.exe' : 'dart',
),
frontendServerSnapshotPath: p.join(
sdkLayout.sdkDirectory,
'bin',
Expand All @@ -97,6 +103,12 @@ class TestSdkLayout {
'snapshots',
'kernel_worker.dart.snapshot',
),
devToolsDirectory: p.join(
sdkLayout.sdkDirectory,
'bin',
'resources',
'devtools',
),
);

final String sdkDirectory;
Expand Down Expand Up @@ -128,6 +140,7 @@ class TestSdkLayout {
final String frontendServerSnapshotPath;
final String dartdevcSnapshotPath;
final String kernelWorkerSnapshotPath;
final String devToolsDirectory;

const TestSdkLayout({
required this.sdkDirectory,
Expand All @@ -145,6 +158,7 @@ class TestSdkLayout {
required this.frontendServerSnapshotPath,
required this.dartdevcSnapshotPath,
required this.kernelWorkerSnapshotPath,
required this.devToolsDirectory,
});

/// Creates configuration from sdk layout.
Expand All @@ -156,3 +170,20 @@ class TestSdkLayout {
compilerWorkerPath: sdkLayout.dartdevcSnapshotPath,
);
}

// Update modified files.
Future<void> copyDirectory(String from, String to) async {
if (!Directory(from).existsSync()) return;
await Directory(to).create(recursive: true);

await for (final file in Directory(from).list(followLinks: false)) {
final copyTo = p.join(to, p.relative(file.path, from: from));
if (file is Directory) {
await copyDirectory(file.path, copyTo);
} else if (file is File) {
await File(file.path).copy(copyTo);
} else if (file is Link) {
await Link(copyTo).create(await file.target(), recursive: true);
}
}
}
Loading