Skip to content

Commit cd8e74d

Browse files
committed
Support --version in pub workspace
Closes #2535 Add a check for the `workspace_ref.json` file in place of the `pubspec.lock` file that exists for single package pub solutions. When the workspace ref exists, follow it and read the `package_graph.json` file from the workspace root.
1 parent b99d556 commit cd8e74d

File tree

2 files changed

+89
-49
lines changed

2 files changed

+89
-49
lines changed

pkgs/test_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Restrict to latest version of analyzer package.
44
* Require Dart 3.7
55
* Add `--coverage-path` and `--branch-coverage` options to `dart test`.
6+
* Add support for reading test package version within pub workspaces.
67

78
## 0.6.12
89

pkgs/test_core/lib/src/runner/version.dart

Lines changed: 88 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,99 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
56
import 'dart:io';
67

8+
import 'package:collection/collection.dart';
9+
import 'package:path/path.dart' as p;
710
import 'package:yaml/yaml.dart';
811

912
/// The version number of the test runner, or `null` if it couldn't be loaded.
1013
///
1114
/// This is a semantic version, optionally followed by a space and additional
1215
/// data about its source.
13-
final String? testVersion =
14-
(() {
15-
dynamic lockfile;
16-
try {
17-
lockfile = loadYaml(File('pubspec.lock').readAsStringSync());
18-
} on FormatException catch (_) {
19-
return null;
20-
} on IOException catch (_) {
21-
return null;
22-
}
23-
24-
if (lockfile is! Map) return null;
25-
var packages = lockfile['packages'];
26-
if (packages is! Map) return null;
27-
var package = packages['test'];
28-
if (package is! Map) return null;
29-
30-
var source = package['source'];
31-
if (source is! String) return null;
32-
33-
switch (source) {
34-
case 'hosted':
35-
var version = package['version'];
36-
return (version is String) ? version : null;
37-
38-
case 'git':
39-
var version = package['version'];
40-
if (version is! String) return null;
41-
var description = package['description'];
42-
if (description is! Map) return null;
43-
var ref = description['resolved-ref'];
44-
if (ref is! String) return null;
45-
46-
return '$version (${ref.substring(0, 7)})';
47-
48-
case 'path':
49-
var version = package['version'];
50-
if (version is! String) return null;
51-
var description = package['description'];
52-
if (description is! Map) return null;
53-
var path = description['path'];
54-
if (path is! String) return null;
55-
56-
return '$version (from $path)';
57-
58-
default:
59-
return null;
60-
}
61-
})();
16+
final String? testVersion = _readWorkspaceRef() ?? _readPubspecLock();
17+
18+
String? _readWorkspaceRef() {
19+
try {
20+
final workspaceRefPath = p.join('.dart_tool', 'pub', 'workspace_ref.json');
21+
final workspaceRefFile = File(workspaceRefPath);
22+
if (!workspaceRefFile.existsSync()) return null;
23+
final ref = jsonDecode(workspaceRefFile.readAsStringSync());
24+
if (ref is! Map) return null;
25+
final relativeRoot = ref['workspaceRoot'];
26+
if (relativeRoot is! String) return null;
27+
final packageGraphPath = p.normalize(
28+
p.join(
29+
'.dart_tool',
30+
'pub',
31+
relativeRoot,
32+
'.dart_tool',
33+
'package_graph.json',
34+
),
35+
);
36+
final packageGraphFile = File(packageGraphPath);
37+
final packageGraph = jsonDecode(packageGraphFile.readAsStringSync());
38+
if (packageGraph is! Map) return null;
39+
final packages = packageGraph['packages'];
40+
if (packages is! List) return null;
41+
final testPackage = packages.firstWhereOrNull(
42+
(p) => p is Map && p['name'] == 'test',
43+
);
44+
if (testPackage == null) return null;
45+
return (testPackage as Map)['version'] as String;
46+
} on FormatException {
47+
return null;
48+
} on IOException {
49+
return null;
50+
}
51+
}
52+
53+
String? _readPubspecLock() {
54+
dynamic lockfile;
55+
try {
56+
lockfile = loadYaml(File('pubspec.lock').readAsStringSync());
57+
} on FormatException {
58+
return null;
59+
} on IOException {
60+
return null;
61+
}
62+
63+
if (lockfile is! Map) return null;
64+
var packages = lockfile['packages'];
65+
if (packages is! Map) return null;
66+
var package = packages['test'];
67+
if (package is! Map) return null;
68+
69+
var source = package['source'];
70+
if (source is! String) return null;
71+
72+
switch (source) {
73+
case 'hosted':
74+
var version = package['version'];
75+
return (version is String) ? version : null;
76+
77+
case 'git':
78+
var version = package['version'];
79+
if (version is! String) return null;
80+
var description = package['description'];
81+
if (description is! Map) return null;
82+
var ref = description['resolved-ref'];
83+
if (ref is! String) return null;
84+
85+
return '$version (${ref.substring(0, 7)})';
86+
87+
case 'path':
88+
var version = package['version'];
89+
if (version is! String) return null;
90+
var description = package['description'];
91+
if (description is! Map) return null;
92+
var path = description['path'];
93+
if (path is! String) return null;
94+
95+
return '$version (from $path)';
96+
97+
default:
98+
return null;
99+
}
100+
}

0 commit comments

Comments
 (0)