Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions manager/apt/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ func (a *PackageManager) Delete(pkgs []string, opts *manager.Options) ([]manager
return nil, err
}

// args := append([]string{"remove", ArgsFixBroken, ArgsPurge, ArgsAutoRemove}, pkgs...)
args := append([]string{"remove", ArgsFixBroken, ArgsAutoRemove}, pkgs...)
if opts == nil {
opts = &manager.Options{
DryRun: false,
Expand All @@ -206,13 +204,33 @@ func (a *PackageManager) Delete(pkgs []string, opts *manager.Options) ([]manager
}
}

// Start with base remove command
args := []string{"remove"}

// always fix broken and auto-remove unused dependencies
args = append(args, ArgsFixBroken, ArgsAutoRemove)

// Add dry-run if requested
if opts.DryRun {
args = append(args, ArgsDryRun)
}

// assume yes if not interactive, to avoid hanging
if !opts.Interactive {
args = append(args, ArgsAssumeYes)
}

// Check if purge is requested in CustomCommandArgs
for _, arg := range opts.CustomCommandArgs {
if arg == ArgsPurge {
args = append(args, ArgsPurge)
break
}
}

// Append package names to the command arguments
args = append(args, pkgs...)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()

Expand Down
74 changes: 74 additions & 0 deletions manager/apt/apt_commandrunner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,80 @@ curl 7.81.0-1ubuntu1.4
t.Errorf("Expected %d environment variables, got %d", len(expectedEnv), len(env))
}
})

t.Run("Delete with purge flag", func(t *testing.T) {
// Create mock command runner
mockRunner := manager.NewMockCommandRunner()

removeOutputWithPurge := `Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
ebtables*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 123 kB disk space will be freed.
Do you want to continue? [Y/n]
(Reading database ... 1234 files and directories currently installed.)
Removing ebtables (2.0.11-4ubuntu1) ...
Purging configuration files for ebtables (2.0.11-4ubuntu1) ...
`
expectedArgs := []string{"remove", "-f", "--autoremove", "-y", "--purge", "ebtables"}
mockRunner.AddCommand("apt", expectedArgs, []byte(removeOutputWithPurge), nil)

// Create APT package manager with mocked runner
pm := apt.NewPackageManagerWithCustomRunner(mockRunner)

// Execute Delete operation with purge flag
opts := &manager.Options{
AssumeYes: true,
CustomCommandArgs: []string{apt.ArgsPurge},
}
packages, err := pm.Delete([]string{"ebtables"}, opts)
if err != nil {
t.Fatalf("Delete with purge flag failed: %v", err)
}

// Verify return value
if packages == nil {
t.Error("Delete should return package info, got nil")
}
})

t.Run("Delete without purge flag", func(t *testing.T) {
// Create mock command runner
mockRunner := manager.NewMockCommandRunner()

removeOutputWithoutPurge := `Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
ebtables
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 123 kB disk space will be freed.
Do you want to continue? [Y/n]
(Reading database ... 1234 files and directories currently installed.)
Removing ebtables (2.0.11-4ubuntu1) ...
`
expectedArgs := []string{"remove", "-f", "--autoremove", "-y", "ebtables"}
mockRunner.AddCommand("apt", expectedArgs, []byte(removeOutputWithoutPurge), nil)

// Create APT package manager with mocked runner
pm := apt.NewPackageManagerWithCustomRunner(mockRunner)

// Execute Delete operation without purge flag
opts := &manager.Options{
AssumeYes: true,
}
packages, err := pm.Delete([]string{"ebtables"}, opts)
if err != nil {
t.Fatalf("Delete without purge flag failed: %v", err)
}

// Verify return value
if packages == nil {
t.Error("Delete should return package info, got nil")
}
})
}

// TestAPTCommandRunnerMigration verifies that the migration from CommandBuilder to CommandRunner works correctly
Expand Down