Skip to content

Commit

Permalink
test(aft): Improved testing framework
Browse files Browse the repository at this point in the history
Builds off `package:test_descriptor` to provide `PackageDescriptor` and `RepoDescriptor` for improved DX describing and setting up packages and repos.
  • Loading branch information
Dillon Nys authored and dnys1 committed Sep 12, 2023
1 parent 3c87e1a commit de9eaa4
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 437 deletions.
70 changes: 14 additions & 56 deletions packages/aft/lib/src/commands/version_bump_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ import 'package:aft/src/changelog/printer.dart';
import 'package:aft/src/options/git_ref_options.dart';
import 'package:aft/src/options/glob_options.dart';
import 'package:aft/src/repo.dart';
import 'package:path/path.dart' as p;

/// Command for bumping package versions across the repo.
class VersionBumpCommand extends AmplifyCommand
with GitRefOptions, GlobOptions {
VersionBumpCommand() {
argParser
..addFlag(
'preview',
help: 'Preview version changes without applying',
defaultsTo: false,
negatable: false,
)
..addFlag(
'yes',
abbr: 'y',
Expand Down Expand Up @@ -56,8 +49,6 @@ class VersionBumpCommand extends AmplifyCommand

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

late final bool preview = argResults!['preview'] as bool;

late final VersionBumpType? forcedBumpType = () {
final forceBreaking = argResults!['force-breaking'] as bool;
if (forceBreaking) return VersionBumpType.breaking;
Expand Down Expand Up @@ -85,40 +76,9 @@ class VersionBumpCommand extends AmplifyCommand
changesForPackage: _changesForPackage,
forcedBumpType: forcedBumpType,
);
final changelogUpdates = repo.changelogUpdates;

final bumpedPackages = <PackageInfo>[];
for (final package in repo.publishablePackages()) {
final edits = package.pubspecInfo.pubspecYamlEditor.edits;
if (edits.isEmpty) {
continue;
}
bumpedPackages.add(package);
if (preview) {
logger.info('pubspec.yaml');
for (final edit in edits) {
final originalText = package.pubspecInfo.pubspecYaml
.substring(edit.offset, edit.offset + edit.length);
logger.info('$originalText --> ${edit.replacement}');
}
} else {
await File(p.join(package.path, 'pubspec.yaml'))
.writeAsString(package.pubspecInfo.pubspecYamlEditor.toString());
}
final changelogUpdate = changelogUpdates[package];
if (changelogUpdate != null && changelogUpdate.hasUpdate) {
if (preview) {
logger
..info('CHANGELOG.md')
..info(changelogUpdate.newText!);
} else {
await File(p.join(package.path, 'CHANGELOG.md'))
.writeAsString(changelogUpdate.toString());
}
}
}

return bumpedPackages;
return repo.writeChanges(
packages: repo.publishablePackages(),
);
}

@override
Expand All @@ -130,20 +90,18 @@ class VersionBumpCommand extends AmplifyCommand

final bumpedPackages = await _updateVersions();

if (!preview) {
for (final package in bumpedPackages) {
// Run build_runner for packages which generate their version number.
final needsBuildRunner = package.pubspecInfo.pubspec.devDependencies
.containsKey('build_version');
if (!needsBuildRunner) {
continue;
}
await runBuildRunner(
package,
logger: logger,
verbose: verbose,
);
for (final package in bumpedPackages) {
// Run build_runner for packages which generate their version number.
final needsBuildRunner = package.pubspecInfo.pubspec.devDependencies
.containsKey('build_version');
if (!needsBuildRunner) {
continue;
}
await runBuildRunner(
package,
logger: logger,
verbose: verbose,
);
}

logger.info('Version successfully bumped');
Expand Down
27 changes: 24 additions & 3 deletions packages/aft/lib/src/config/config_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:io';

import 'package:aft/src/config/config.dart';
import 'package:aft/src/config/raw_config.dart';
import 'package:aws_common/aws_common.dart';
import 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:pubspec_parse/pubspec_parse.dart';
Expand Down Expand Up @@ -127,10 +128,30 @@ class AftConfigLoader {
rawComponents.map((name, component) {
final summaryPackage = switch (component.summary) {
null => null,
final summary => repoPackages[summary]!,
final summary => switch (repoPackages[summary]) {
final summaryPackage? => summaryPackage,
// Allow missing summary package for testing
_ when zDebugMode => null,
_ => throw StateError(
'Summary package "$summary" does not exist for component: '
'${component.name}',
),
},
};
final packages =
component.packages.map((name) => repoPackages[name]!).toList();
final packages = component.packages
.map(
(name) => switch (repoPackages[name]) {
final package? => package,
// Allow missing component package for testing
_ when zDebugMode => null,
_ => throw StateError(
'Component package "$name" does not exist for component: '
'${component.name}',
),
},
)
.nonNulls
.toList();
final packageGraph = UnmodifiableMapView({
for (final package in packages)
package.name: package.pubspecInfo.pubspec.dependencies.keys
Expand Down
31 changes: 31 additions & 0 deletions packages/aft/lib/src/repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Repo {

final GitDir git;

PackageInfo operator [](String packageName) => allPackages[packageName]!;

/// All packages which can be published to `pub.dev`.
List<PackageInfo> publishablePackages([
Map<String, PackageInfo>? allPackages,
Expand Down Expand Up @@ -252,6 +254,35 @@ class Repo {
}
}

/// Writes all changes made by, for example, [bumpAllVersions], to disk.
///
/// If [packages] is passed, only changes for those packages are written.
/// Otherwise, all repo packages are affected.
///
/// Returns the list of packages which both had changes and were written.
Future<List<PackageInfo>> writeChanges({
List<PackageInfo>? packages,
}) async {
final affectedPackages = <PackageInfo>[];
for (final package in packages ?? allPackages.values.toList()) {
final edits = package.pubspecInfo.pubspecYamlEditor.edits;
// Don't write changelog updates for packages with no corresponding
// pubspec update.
if (edits.isEmpty) {
continue;
}
affectedPackages.add(package);
await File(p.join(package.path, 'pubspec.yaml'))
.writeAsString(package.pubspecInfo.pubspecYamlEditor.toString());
final changelogUpdate = changelogUpdates[package];
if (changelogUpdate != null && changelogUpdate.hasUpdate) {
await File(p.join(package.path, 'CHANGELOG.md'))
.writeAsString(changelogUpdate.toString());
}
}
return affectedPackages;
}

/// Bumps the version and changelog in [package] and its component packages
/// using [commit] and returns the new version.
///
Expand Down
70 changes: 0 additions & 70 deletions packages/aft/test/common.dart

This file was deleted.

Loading

0 comments on commit de9eaa4

Please sign in to comment.