Skip to content

Move project-specific getters to TestProject instead of TestContext #2052

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 4 commits into from
Mar 31, 2023
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
3 changes: 1 addition & 2 deletions dwds/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ description: >-
A service that proxies between the Chrome debug protocol and the Dart VM
service protocol.
repository: https://github.com/dart-lang/webdev/tree/master/dwds

environment:
sdk: ">=3.0.0-188.0.dev <4.0.0"

Expand All @@ -26,7 +25,7 @@ dependencies:
pool: ^1.5.0
pub_semver: ^2.1.1
shelf: ^1.3.0
shelf_packages_handler: '^3.0.0'
shelf_packages_handler: "^3.0.0"
shelf_proxy: ^1.0.1
shelf_static: ^1.1.0
shelf_web_socket: ^1.0.1
Expand Down
86 changes: 29 additions & 57 deletions dwds/test/fixtures/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,6 @@ class TestContext {
final NullSafety nullSafety;
final TestSdkConfigurationProvider sdkConfigurationProvider;

/// Top level directory in which we run the test server, e.g.
/// "/workstation/webdev/fixtures/_testSound".
String get workingDirectory =>
absolutePath(pathFromFixtures: project.packageDirectory);

/// The directory to build and serve, e.g. "example".
String get directoryToServe => p.split(project.webAssetsPath).first;

/// The path to the HTML file to serve, relative to the [directoryToServe],
/// e.g. "hello_world/index.html".
String get filePathToServe {
final pathParts = p.split(project.webAssetsPath).where(
(pathPart) => pathPart != directoryToServe,
);
return webCompatiblePath([...pathParts, project.htmlEntryFileName]);
}

/// The path to the Dart entry file, e.g,
/// "/workstation/webdev/fixtures/_testSound/example/hello_world/main.dart":
String get _dartEntryFilePath => absolutePath(
pathFromFixtures: p.joinAll(
[
project.packageDirectory,
project.webAssetsPath,
project.dartEntryFileName,
],
),
);

/// The URI for the package_config.json is located in:
/// <project directory>/.dart_tool/package_config
Uri get _packageConfigFile =>
p.toUri(p.join(workingDirectory, '.dart_tool', 'package_config.json'));

String get appUrl => _appUrl!;
late String? _appUrl;

Expand Down Expand Up @@ -150,14 +116,16 @@ class TestContext {

TestContext(this.project, this.sdkConfigurationProvider)
: nullSafety = project.nullSafety {
DartUri.currentDirectory = workingDirectory;
DartUri.currentDirectory = project.absolutePackageDirectory;

project.validate();

_logger.info('Serving: $directoryToServe/$filePathToServe');
_logger.info('Project: $workingDirectory');
_logger.info('Packages: $_packageConfigFile');
_logger.info('Entry: $_dartEntryFilePath');
_logger.info(
'Serving: ${project.directoryToServe}/${project.filePathToServe}',
);
_logger.info('Project: ${project.absolutePackageDirectory}');
_logger.info('Packages: ${project.packageConfigFile}');
_logger.info('Entry: ${project.dartEntryFilePath}');
}

Future<void> setUp({
Expand Down Expand Up @@ -187,7 +155,7 @@ class TestContext {
final configuration = await sdkConfigurationProvider.configuration;
configuration.validate();

DartUri.currentDirectory = workingDirectory;
DartUri.currentDirectory = project.absolutePackageDirectory;
configureLogWriter();

_client = IOClient(
Expand Down Expand Up @@ -234,7 +202,7 @@ class TestContext {
await Process.run(
sdkLayout.dartPath,
['pub', 'upgrade'],
workingDirectory: workingDirectory,
workingDirectory: project.absolutePackageDirectory,
);

ExpressionCompiler? expressionCompiler;
Expand All @@ -257,7 +225,8 @@ class TestContext {
'--verbose',
];
_daemonClient = await connectClient(
sdkLayout.dartPath, workingDirectory, options, (log) {
sdkLayout.dartPath, project.absolutePackageDirectory, options,
(log) {
final record = log.toLogRecord();
final name =
record.loggerName == '' ? '' : '${record.loggerName}: ';
Expand All @@ -269,19 +238,22 @@ class TestContext {
);
});
daemonClient.registerBuildTarget(
DefaultBuildTarget((b) => b..target = directoryToServe),
DefaultBuildTarget((b) => b..target = project.directoryToServe),
);
daemonClient.startBuild();

await waitForSuccessfulBuild();

final assetServerPort = daemonPort(workingDirectory);
final assetServerPort =
daemonPort(project.absolutePackageDirectory);
_assetHandler = proxyHandler(
'http://localhost:$assetServerPort/$directoryToServe/',
'http://localhost:$assetServerPort/${project.directoryToServe}/',
client: client,
);
assetReader =
ProxyServerAssetReader(assetServerPort, root: directoryToServe);
assetReader = ProxyServerAssetReader(
assetServerPort,
root: project.directoryToServe,
);

if (enableExpressionEvaluation) {
ddcService = ExpressionCompilerService(
Expand All @@ -306,25 +278,25 @@ class TestContext {
break;
case CompilationMode.frontendServer:
{
_logger.warning('Index: $filePathToServe');
_logger.warning('Index: $project.filePathToServe');

final entry = p.toUri(
p.join(project.webAssetsPath, project.dartEntryFileName),
);
final fileSystem = LocalFileSystem();
final packageUriMapper = await PackageUriMapper.create(
fileSystem,
_packageConfigFile,
project.packageConfigFile,
useDebuggerModuleNames: useDebuggerModuleNames,
);

_webRunner = ResidentWebRunner(
mainUri: entry,
urlTunneler: urlEncoder,
projectDirectory: p.toUri(workingDirectory),
packageConfigFile: _packageConfigFile,
projectDirectory: p.toUri(project.absolutePackageDirectory),
packageConfigFile: project.packageConfigFile,
packageUriMapper: packageUriMapper,
fileSystemRoots: [p.toUri(workingDirectory)],
fileSystemRoots: [p.toUri(project.absolutePackageDirectory)],
fileSystemScheme: 'org-dartlang-app',
outputPath: outputDir.path,
soundNullSafety: nullSafety == NullSafety.sound,
Expand All @@ -338,7 +310,7 @@ class TestContext {
fileSystem,
hostname,
assetServerPort,
p.join(directoryToServe, filePathToServe),
p.join(project.directoryToServe, project.filePathToServe),
);

if (enableExpressionEvaluation) {
Expand Down Expand Up @@ -402,7 +374,7 @@ class TestContext {
assetHandler,
assetReader,
requireStrategy,
directoryToServe,
project.directoryToServe,
buildResults,
() async => connection,
serveDevTools,
Expand All @@ -420,8 +392,8 @@ class TestContext {
);

_appUrl = basePath.isEmpty
? 'http://localhost:$port/$filePathToServe'
: 'http://localhost:$port/$basePath/$filePathToServe';
? 'http://localhost:$port/${project.filePathToServe}'
: 'http://localhost:$port/$basePath/${project.filePathToServe}';

if (launchChrome) {
await _webDriver?.get(appUrl);
Expand Down Expand Up @@ -483,7 +455,7 @@ class TestContext {
required String toReplace,
required String replaceWith,
}) {
final file = File(_dartEntryFilePath);
final file = File(project.dartEntryFilePath);
final fileContents = file.readAsStringSync();
file.writeAsStringSync(fileContents.replaceAll(toReplace, replaceWith));
}
Expand Down
36 changes: 36 additions & 0 deletions dwds/test/fixtures/project.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,45 @@ class TestProject {
final String htmlEntryFileName;
final NullSafety nullSafety;

/// The top level directory in which we run the test server, e.g.
/// "/workstation/webdev/fixtures/_testSound".
String get absolutePackageDirectory =>
absolutePath(pathFromFixtures: packageDirectory);

/// The directory to build and serve, e.g. "example".
String get directoryToServe => p.split(webAssetsPath).first;

/// The path to the HTML file to serve, relative to the [directoryToServe],
/// e.g. "hello_world/index.html".
String get filePathToServe {
final pathParts = p.split(webAssetsPath).where(
(pathPart) => pathPart != directoryToServe,
);
return webCompatiblePath([...pathParts, htmlEntryFileName]);
}

/// The path to the Dart entry file, e.g,
/// "/workstation/webdev/fixtures/_testSound/example/hello_world/main.dart":
String get dartEntryFilePath => absolutePath(
pathFromFixtures: p.joinAll(
[
packageDirectory,
webAssetsPath,
dartEntryFileName,
],
),
);

/// The URI for the package_config.json is located in:
/// <project directory>/.dart_tool/package_config
Uri get packageConfigFile => p.toUri(
p.join(
absolutePackageDirectory,
'.dart_tool',
'package_config.json',
),
);

/// The package URI of the Dart entry file, e.g,
/// "org-dartlang-app:example/hello_world/main.dart":
Uri get dartEntryFilePackageUri => Uri.parse('org-dartlang-app:///${p.join(
Expand Down