Skip to content

Commit 8437f60

Browse files
authored
Move project-specific getters to TestProject instead of TestContext (#2052)
1 parent 652e040 commit 8437f60

File tree

3 files changed

+66
-59
lines changed

3 files changed

+66
-59
lines changed

dwds/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ description: >-
55
A service that proxies between the Chrome debug protocol and the Dart VM
66
service protocol.
77
repository: https://github.com/dart-lang/webdev/tree/master/dwds
8-
98
environment:
109
sdk: ">=3.0.0-188.0.dev <4.0.0"
1110

@@ -26,7 +25,7 @@ dependencies:
2625
pool: ^1.5.0
2726
pub_semver: ^2.1.1
2827
shelf: ^1.3.0
29-
shelf_packages_handler: '^3.0.0'
28+
shelf_packages_handler: "^3.0.0"
3029
shelf_proxy: ^1.0.1
3130
shelf_static: ^1.1.0
3231
shelf_web_socket: ^1.0.1

dwds/test/fixtures/context.dart

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,6 @@ class TestContext {
6161
final NullSafety nullSafety;
6262
final TestSdkConfigurationProvider sdkConfigurationProvider;
6363

64-
/// Top level directory in which we run the test server, e.g.
65-
/// "/workstation/webdev/fixtures/_testSound".
66-
String get workingDirectory =>
67-
absolutePath(pathFromFixtures: project.packageDirectory);
68-
69-
/// The directory to build and serve, e.g. "example".
70-
String get directoryToServe => p.split(project.webAssetsPath).first;
71-
72-
/// The path to the HTML file to serve, relative to the [directoryToServe],
73-
/// e.g. "hello_world/index.html".
74-
String get filePathToServe {
75-
final pathParts = p.split(project.webAssetsPath).where(
76-
(pathPart) => pathPart != directoryToServe,
77-
);
78-
return webCompatiblePath([...pathParts, project.htmlEntryFileName]);
79-
}
80-
81-
/// The path to the Dart entry file, e.g,
82-
/// "/workstation/webdev/fixtures/_testSound/example/hello_world/main.dart":
83-
String get _dartEntryFilePath => absolutePath(
84-
pathFromFixtures: p.joinAll(
85-
[
86-
project.packageDirectory,
87-
project.webAssetsPath,
88-
project.dartEntryFileName,
89-
],
90-
),
91-
);
92-
93-
/// The URI for the package_config.json is located in:
94-
/// <project directory>/.dart_tool/package_config
95-
Uri get _packageConfigFile =>
96-
p.toUri(p.join(workingDirectory, '.dart_tool', 'package_config.json'));
97-
9864
String get appUrl => _appUrl!;
9965
late String? _appUrl;
10066

@@ -150,14 +116,16 @@ class TestContext {
150116

151117
TestContext(this.project, this.sdkConfigurationProvider)
152118
: nullSafety = project.nullSafety {
153-
DartUri.currentDirectory = workingDirectory;
119+
DartUri.currentDirectory = project.absolutePackageDirectory;
154120

155121
project.validate();
156122

157-
_logger.info('Serving: $directoryToServe/$filePathToServe');
158-
_logger.info('Project: $workingDirectory');
159-
_logger.info('Packages: $_packageConfigFile');
160-
_logger.info('Entry: $_dartEntryFilePath');
123+
_logger.info(
124+
'Serving: ${project.directoryToServe}/${project.filePathToServe}',
125+
);
126+
_logger.info('Project: ${project.absolutePackageDirectory}');
127+
_logger.info('Packages: ${project.packageConfigFile}');
128+
_logger.info('Entry: ${project.dartEntryFilePath}');
161129
}
162130

163131
Future<void> setUp({
@@ -187,7 +155,7 @@ class TestContext {
187155
final configuration = await sdkConfigurationProvider.configuration;
188156
configuration.validate();
189157

190-
DartUri.currentDirectory = workingDirectory;
158+
DartUri.currentDirectory = project.absolutePackageDirectory;
191159
configureLogWriter();
192160

193161
_client = IOClient(
@@ -234,7 +202,7 @@ class TestContext {
234202
await Process.run(
235203
sdkLayout.dartPath,
236204
['pub', 'upgrade'],
237-
workingDirectory: workingDirectory,
205+
workingDirectory: project.absolutePackageDirectory,
238206
);
239207

240208
ExpressionCompiler? expressionCompiler;
@@ -257,7 +225,8 @@ class TestContext {
257225
'--verbose',
258226
];
259227
_daemonClient = await connectClient(
260-
sdkLayout.dartPath, workingDirectory, options, (log) {
228+
sdkLayout.dartPath, project.absolutePackageDirectory, options,
229+
(log) {
261230
final record = log.toLogRecord();
262231
final name =
263232
record.loggerName == '' ? '' : '${record.loggerName}: ';
@@ -269,19 +238,22 @@ class TestContext {
269238
);
270239
});
271240
daemonClient.registerBuildTarget(
272-
DefaultBuildTarget((b) => b..target = directoryToServe),
241+
DefaultBuildTarget((b) => b..target = project.directoryToServe),
273242
);
274243
daemonClient.startBuild();
275244

276245
await waitForSuccessfulBuild();
277246

278-
final assetServerPort = daemonPort(workingDirectory);
247+
final assetServerPort =
248+
daemonPort(project.absolutePackageDirectory);
279249
_assetHandler = proxyHandler(
280-
'http://localhost:$assetServerPort/$directoryToServe/',
250+
'http://localhost:$assetServerPort/${project.directoryToServe}/',
281251
client: client,
282252
);
283-
assetReader =
284-
ProxyServerAssetReader(assetServerPort, root: directoryToServe);
253+
assetReader = ProxyServerAssetReader(
254+
assetServerPort,
255+
root: project.directoryToServe,
256+
);
285257

286258
if (enableExpressionEvaluation) {
287259
ddcService = ExpressionCompilerService(
@@ -306,25 +278,25 @@ class TestContext {
306278
break;
307279
case CompilationMode.frontendServer:
308280
{
309-
_logger.warning('Index: $filePathToServe');
281+
_logger.warning('Index: $project.filePathToServe');
310282

311283
final entry = p.toUri(
312284
p.join(project.webAssetsPath, project.dartEntryFileName),
313285
);
314286
final fileSystem = LocalFileSystem();
315287
final packageUriMapper = await PackageUriMapper.create(
316288
fileSystem,
317-
_packageConfigFile,
289+
project.packageConfigFile,
318290
useDebuggerModuleNames: useDebuggerModuleNames,
319291
);
320292

321293
_webRunner = ResidentWebRunner(
322294
mainUri: entry,
323295
urlTunneler: urlEncoder,
324-
projectDirectory: p.toUri(workingDirectory),
325-
packageConfigFile: _packageConfigFile,
296+
projectDirectory: p.toUri(project.absolutePackageDirectory),
297+
packageConfigFile: project.packageConfigFile,
326298
packageUriMapper: packageUriMapper,
327-
fileSystemRoots: [p.toUri(workingDirectory)],
299+
fileSystemRoots: [p.toUri(project.absolutePackageDirectory)],
328300
fileSystemScheme: 'org-dartlang-app',
329301
outputPath: outputDir.path,
330302
soundNullSafety: nullSafety == NullSafety.sound,
@@ -338,7 +310,7 @@ class TestContext {
338310
fileSystem,
339311
hostname,
340312
assetServerPort,
341-
p.join(directoryToServe, filePathToServe),
313+
p.join(project.directoryToServe, project.filePathToServe),
342314
);
343315

344316
if (enableExpressionEvaluation) {
@@ -402,7 +374,7 @@ class TestContext {
402374
assetHandler,
403375
assetReader,
404376
requireStrategy,
405-
directoryToServe,
377+
project.directoryToServe,
406378
buildResults,
407379
() async => connection,
408380
serveDevTools,
@@ -420,8 +392,8 @@ class TestContext {
420392
);
421393

422394
_appUrl = basePath.isEmpty
423-
? 'http://localhost:$port/$filePathToServe'
424-
: 'http://localhost:$port/$basePath/$filePathToServe';
395+
? 'http://localhost:$port/${project.filePathToServe}'
396+
: 'http://localhost:$port/$basePath/${project.filePathToServe}';
425397

426398
if (launchChrome) {
427399
await _webDriver?.get(appUrl);
@@ -483,7 +455,7 @@ class TestContext {
483455
required String toReplace,
484456
required String replaceWith,
485457
}) {
486-
final file = File(_dartEntryFilePath);
458+
final file = File(project.dartEntryFilePath);
487459
final fileContents = file.readAsStringSync();
488460
file.writeAsStringSync(fileContents.replaceAll(toReplace, replaceWith));
489461
}

dwds/test/fixtures/project.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,45 @@ class TestProject {
1919
final String htmlEntryFileName;
2020
final NullSafety nullSafety;
2121

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

27+
/// The directory to build and serve, e.g. "example".
28+
String get directoryToServe => p.split(webAssetsPath).first;
29+
30+
/// The path to the HTML file to serve, relative to the [directoryToServe],
31+
/// e.g. "hello_world/index.html".
32+
String get filePathToServe {
33+
final pathParts = p.split(webAssetsPath).where(
34+
(pathPart) => pathPart != directoryToServe,
35+
);
36+
return webCompatiblePath([...pathParts, htmlEntryFileName]);
37+
}
38+
39+
/// The path to the Dart entry file, e.g,
40+
/// "/workstation/webdev/fixtures/_testSound/example/hello_world/main.dart":
41+
String get dartEntryFilePath => absolutePath(
42+
pathFromFixtures: p.joinAll(
43+
[
44+
packageDirectory,
45+
webAssetsPath,
46+
dartEntryFileName,
47+
],
48+
),
49+
);
50+
51+
/// The URI for the package_config.json is located in:
52+
/// <project directory>/.dart_tool/package_config
53+
Uri get packageConfigFile => p.toUri(
54+
p.join(
55+
absolutePackageDirectory,
56+
'.dart_tool',
57+
'package_config.json',
58+
),
59+
);
60+
2561
/// The package URI of the Dart entry file, e.g,
2662
/// "org-dartlang-app:example/hello_world/main.dart":
2763
Uri get dartEntryFilePackageUri => Uri.parse('org-dartlang-app:///${p.join(

0 commit comments

Comments
 (0)