Skip to content

Commit de9eaa4

Browse files
Dillon Nysdnys1
authored andcommitted
test(aft): Improved testing framework
Builds off `package:test_descriptor` to provide `PackageDescriptor` and `RepoDescriptor` for improved DX describing and setting up packages and repos.
1 parent 3c87e1a commit de9eaa4

12 files changed

+670
-437
lines changed

packages/aft/lib/src/commands/version_bump_command.dart

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,12 @@ import 'package:aft/src/changelog/printer.dart';
1010
import 'package:aft/src/options/git_ref_options.dart';
1111
import 'package:aft/src/options/glob_options.dart';
1212
import 'package:aft/src/repo.dart';
13-
import 'package:path/path.dart' as p;
1413

1514
/// Command for bumping package versions across the repo.
1615
class VersionBumpCommand extends AmplifyCommand
1716
with GitRefOptions, GlobOptions {
1817
VersionBumpCommand() {
1918
argParser
20-
..addFlag(
21-
'preview',
22-
help: 'Preview version changes without applying',
23-
defaultsTo: false,
24-
negatable: false,
25-
)
2619
..addFlag(
2720
'yes',
2821
abbr: 'y',
@@ -56,8 +49,6 @@ class VersionBumpCommand extends AmplifyCommand
5649

5750
late final bool yes = argResults!['yes'] as bool;
5851

59-
late final bool preview = argResults!['preview'] as bool;
60-
6152
late final VersionBumpType? forcedBumpType = () {
6253
final forceBreaking = argResults!['force-breaking'] as bool;
6354
if (forceBreaking) return VersionBumpType.breaking;
@@ -85,40 +76,9 @@ class VersionBumpCommand extends AmplifyCommand
8576
changesForPackage: _changesForPackage,
8677
forcedBumpType: forcedBumpType,
8778
);
88-
final changelogUpdates = repo.changelogUpdates;
89-
90-
final bumpedPackages = <PackageInfo>[];
91-
for (final package in repo.publishablePackages()) {
92-
final edits = package.pubspecInfo.pubspecYamlEditor.edits;
93-
if (edits.isEmpty) {
94-
continue;
95-
}
96-
bumpedPackages.add(package);
97-
if (preview) {
98-
logger.info('pubspec.yaml');
99-
for (final edit in edits) {
100-
final originalText = package.pubspecInfo.pubspecYaml
101-
.substring(edit.offset, edit.offset + edit.length);
102-
logger.info('$originalText --> ${edit.replacement}');
103-
}
104-
} else {
105-
await File(p.join(package.path, 'pubspec.yaml'))
106-
.writeAsString(package.pubspecInfo.pubspecYamlEditor.toString());
107-
}
108-
final changelogUpdate = changelogUpdates[package];
109-
if (changelogUpdate != null && changelogUpdate.hasUpdate) {
110-
if (preview) {
111-
logger
112-
..info('CHANGELOG.md')
113-
..info(changelogUpdate.newText!);
114-
} else {
115-
await File(p.join(package.path, 'CHANGELOG.md'))
116-
.writeAsString(changelogUpdate.toString());
117-
}
118-
}
119-
}
120-
121-
return bumpedPackages;
79+
return repo.writeChanges(
80+
packages: repo.publishablePackages(),
81+
);
12282
}
12383

12484
@override
@@ -130,20 +90,18 @@ class VersionBumpCommand extends AmplifyCommand
13090

13191
final bumpedPackages = await _updateVersions();
13292

133-
if (!preview) {
134-
for (final package in bumpedPackages) {
135-
// Run build_runner for packages which generate their version number.
136-
final needsBuildRunner = package.pubspecInfo.pubspec.devDependencies
137-
.containsKey('build_version');
138-
if (!needsBuildRunner) {
139-
continue;
140-
}
141-
await runBuildRunner(
142-
package,
143-
logger: logger,
144-
verbose: verbose,
145-
);
93+
for (final package in bumpedPackages) {
94+
// Run build_runner for packages which generate their version number.
95+
final needsBuildRunner = package.pubspecInfo.pubspec.devDependencies
96+
.containsKey('build_version');
97+
if (!needsBuildRunner) {
98+
continue;
14699
}
100+
await runBuildRunner(
101+
package,
102+
logger: logger,
103+
verbose: verbose,
104+
);
147105
}
148106

149107
logger.info('Version successfully bumped');

packages/aft/lib/src/config/config_loader.dart

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:io';
66

77
import 'package:aft/src/config/config.dart';
88
import 'package:aft/src/config/raw_config.dart';
9+
import 'package:aws_common/aws_common.dart';
910
import 'package:collection/collection.dart';
1011
import 'package:path/path.dart' as p;
1112
import 'package:pubspec_parse/pubspec_parse.dart';
@@ -127,10 +128,30 @@ class AftConfigLoader {
127128
rawComponents.map((name, component) {
128129
final summaryPackage = switch (component.summary) {
129130
null => null,
130-
final summary => repoPackages[summary]!,
131+
final summary => switch (repoPackages[summary]) {
132+
final summaryPackage? => summaryPackage,
133+
// Allow missing summary package for testing
134+
_ when zDebugMode => null,
135+
_ => throw StateError(
136+
'Summary package "$summary" does not exist for component: '
137+
'${component.name}',
138+
),
139+
},
131140
};
132-
final packages =
133-
component.packages.map((name) => repoPackages[name]!).toList();
141+
final packages = component.packages
142+
.map(
143+
(name) => switch (repoPackages[name]) {
144+
final package? => package,
145+
// Allow missing component package for testing
146+
_ when zDebugMode => null,
147+
_ => throw StateError(
148+
'Component package "$name" does not exist for component: '
149+
'${component.name}',
150+
),
151+
},
152+
)
153+
.nonNulls
154+
.toList();
134155
final packageGraph = UnmodifiableMapView({
135156
for (final package in packages)
136157
package.name: package.pubspecInfo.pubspec.dependencies.keys

packages/aft/lib/src/repo.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class Repo {
5151

5252
final GitDir git;
5353

54+
PackageInfo operator [](String packageName) => allPackages[packageName]!;
55+
5456
/// All packages which can be published to `pub.dev`.
5557
List<PackageInfo> publishablePackages([
5658
Map<String, PackageInfo>? allPackages,
@@ -252,6 +254,35 @@ class Repo {
252254
}
253255
}
254256

257+
/// Writes all changes made by, for example, [bumpAllVersions], to disk.
258+
///
259+
/// If [packages] is passed, only changes for those packages are written.
260+
/// Otherwise, all repo packages are affected.
261+
///
262+
/// Returns the list of packages which both had changes and were written.
263+
Future<List<PackageInfo>> writeChanges({
264+
List<PackageInfo>? packages,
265+
}) async {
266+
final affectedPackages = <PackageInfo>[];
267+
for (final package in packages ?? allPackages.values.toList()) {
268+
final edits = package.pubspecInfo.pubspecYamlEditor.edits;
269+
// Don't write changelog updates for packages with no corresponding
270+
// pubspec update.
271+
if (edits.isEmpty) {
272+
continue;
273+
}
274+
affectedPackages.add(package);
275+
await File(p.join(package.path, 'pubspec.yaml'))
276+
.writeAsString(package.pubspecInfo.pubspecYamlEditor.toString());
277+
final changelogUpdate = changelogUpdates[package];
278+
if (changelogUpdate != null && changelogUpdate.hasUpdate) {
279+
await File(p.join(package.path, 'CHANGELOG.md'))
280+
.writeAsString(changelogUpdate.toString());
281+
}
282+
}
283+
return affectedPackages;
284+
}
285+
255286
/// Bumps the version and changelog in [package] and its component packages
256287
/// using [commit] and returns the new version.
257288
///

packages/aft/test/common.dart

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)