Skip to content

Commit fb1acec

Browse files
Copilotswissspidy
andcommitted
Track actual package updates and provide accurate success messaging
- Add event listener for 'post-package-update' to track which packages are actually updated - Differentiate success messages based on update outcomes: - "Package already at latest version" for single package with no update - "Packages already at latest versions" for multiple packages with no updates - "Package updated successfully" for single package updated - "All N packages updated successfully" when all requested packages are updated - "Updated X of Y packages" when only some packages are updated - Update tests to reflect new messaging behavior - Add test scenario for package already at latest version Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 0f8cfc6 commit fb1acec

2 files changed

Lines changed: 49 additions & 5 deletions

File tree

features/package-update.feature

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,26 @@ Feature: Update WP-CLI packages
157157
"""
158158
And STDOUT should contain:
159159
"""
160-
Success: 2 packages updated successfully.
160+
Success: Updated 1 of 2 packages.
161+
"""
162+
163+
Scenario: Update package that is already up to date
164+
Given an empty directory
165+
166+
When I run `wp package install danielbachhuber/wp-cli-reset-post-date-command`
167+
Then STDOUT should contain:
168+
"""
169+
Success: Package installed.
170+
"""
171+
172+
When I run `wp package update danielbachhuber/wp-cli-reset-post-date-command`
173+
Then STDOUT should contain:
174+
"""
175+
Using Composer to update packages...
176+
"""
177+
And STDOUT should contain:
178+
"""
179+
Success: Package already at latest version.
161180
"""
162181

163182
Scenario: Error when trying to update a non-existent package

src/Package_Command.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,23 @@ public function update( $args = [] ) {
554554

555555
$composer = $this->get_composer();
556556

557-
// Set up the EventSubscriber
557+
// Set up the EventSubscriber with tracking for updates
558+
$updated_packages = [];
558559
$event_subscriber = new PackageManagerEventSubscriber();
559560
$composer->getEventDispatcher()->addSubscriber( $event_subscriber );
560561

562+
// Add a listener to track actual package updates
563+
$composer->getEventDispatcher()->addListener(
564+
'post-package-update',
565+
function ( $event ) use ( &$updated_packages ) {
566+
$operation = $event->getOperation();
567+
if ( method_exists( $operation, 'getTargetPackage' ) ) {
568+
$package = $operation->getTargetPackage();
569+
$updated_packages[] = $package->getPrettyName();
570+
}
571+
}
572+
);
573+
561574
// Set up the installer
562575
$install = Installer::create( new ComposerIO(), $composer );
563576
$install->setUpdate( true ); // Installer class will only override composer.lock with this flag
@@ -583,10 +596,22 @@ public function update( $args = [] ) {
583596
if ( 0 === $res ) {
584597
$num_packages = count( $packages_to_update );
585598
if ( $num_packages > 0 ) {
586-
if ( 1 === $num_packages ) {
587-
WP_CLI::success( 'Package updated successfully.' );
599+
// When specific packages were requested, report on actual updates
600+
$num_updated = count( $updated_packages );
601+
if ( 0 === $num_updated ) {
602+
if ( 1 === $num_packages ) {
603+
WP_CLI::success( 'Package already at latest version.' );
604+
} else {
605+
WP_CLI::success( 'Packages already at latest versions.' );
606+
}
607+
} elseif ( $num_updated === $num_packages ) {
608+
if ( 1 === $num_packages ) {
609+
WP_CLI::success( 'Package updated successfully.' );
610+
} else {
611+
WP_CLI::success( sprintf( 'All %d packages updated successfully.', $num_packages ) );
612+
}
588613
} else {
589-
WP_CLI::success( sprintf( '%d packages updated successfully.', $num_packages ) );
614+
WP_CLI::success( sprintf( 'Updated %d of %d packages.', $num_updated, $num_packages ) );
590615
}
591616
} else {
592617
WP_CLI::success( 'Packages updated.' );

0 commit comments

Comments
 (0)