diff --git a/cli/src/test/java/com/devonfw/tools/ide/context/CapturingProcessContextTest.java b/cli/src/test/java/com/devonfw/tools/ide/context/CapturingProcessContextTest.java new file mode 100644 index 0000000000..070c536dfb --- /dev/null +++ b/cli/src/test/java/com/devonfw/tools/ide/context/CapturingProcessContextTest.java @@ -0,0 +1,57 @@ +package com.devonfw.tools.ide.context; + +import java.util.ArrayList; +import java.util.List; + +import com.devonfw.tools.ide.process.ProcessContext; +import com.devonfw.tools.ide.process.ProcessContextImpl; +import com.devonfw.tools.ide.process.ProcessMode; +import com.devonfw.tools.ide.process.ProcessResult; +import com.devonfw.tools.ide.process.ProcessResultImpl; + +/** + * Mock {@link ProcessContext} that captures executed commands for testing without actually running them. + */ +public class CapturingProcessContextTest extends ProcessContextImpl { + + private final List executedCommands = new ArrayList<>(); + + /** + * The constructor. + * + * @param context the {@link IdeContext}. + */ + public CapturingProcessContextTest(IdeContext context) { + super(context); + } + + @Override + public ProcessContext createChild() { + return new CapturingProcessContextTest(this.context) { + @Override + public ProcessResult run(ProcessMode processMode) { + return CapturingProcessContextTest.this.run(processMode); + } + }; + } + + @Override + public ProcessResult run(ProcessMode processMode) { + String executable = this.executable.toString(); + List args = this.arguments; + String command = executable + " " + String.join(" ", args); + executedCommands.add(command); + + this.arguments.clear(); + this.executable = null; + + return new ProcessResultImpl("bash", command, ProcessResult.SUCCESS, true, List.of()); + } + + /** + * @return the list of captured commands that would have been executed. + */ + public List getExecutedCommands() { + return executedCommands; + } +} diff --git a/cli/src/test/java/com/devonfw/tools/ide/tool/GlobalToolCommandletTest.java b/cli/src/test/java/com/devonfw/tools/ide/tool/GlobalToolCommandletTest.java index ede372170f..ce0c5f5ed9 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/tool/GlobalToolCommandletTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/tool/GlobalToolCommandletTest.java @@ -7,11 +7,13 @@ import com.devonfw.tools.ide.common.Tag; import com.devonfw.tools.ide.context.AbstractIdeContextTest; +import com.devonfw.tools.ide.context.CapturingProcessContextTest; import com.devonfw.tools.ide.context.IdeContext; import com.devonfw.tools.ide.context.IdeTestContext; import com.devonfw.tools.ide.os.SystemInfoMock; import com.devonfw.tools.ide.os.WindowsAppInstallation; import com.devonfw.tools.ide.os.WindowsHelperMock; +import com.devonfw.tools.ide.process.ProcessContext; import com.devonfw.tools.ide.process.ProcessResult; import com.devonfw.tools.ide.version.VersionIdentifier; @@ -173,30 +175,37 @@ protected List getNativePackages() { protected String getBinaryName() { return TOOL_NAME; } + + @Override + protected boolean isPackageManagerAvailable(NativePackageManager packageManager) { + // Always return true for testing - we mock the process execution anyway + return true; + } } /** - * Verifies that {@link GlobalToolCommandlet#getUninstallPackageManagerCommands()} correctly derives uninstall commands from - * {@link GlobalToolCommandlet#getNativePackages()}. + * Verifies that calling {@link GlobalToolCommandlet#uninstall()} on Linux executes the uninstall commands via package manager. Uses a mock + * {@link ProcessContext} to capture executed commands without actually running them. */ @Test - void testGetUninstallPackageManagerCommandsDerivesFromNativePackages() { + void testUninstallExecutesPackageManagerCommandsAndLogsThem() { // arrange IdeTestContext context = newContext(PROJECT_BASIC); context.setSystemInfo(SystemInfoMock.LINUX_X64); PackageManagedToolCommandlet commandlet = new PackageManagedToolCommandlet(context); + // Create a mock ProcessContext that captures commands but doesn't execute them + CapturingProcessContextTest mockProcessContext = new CapturingProcessContextTest(context); + context.setProcessContext(mockProcessContext); + // act - List uninstallCommands = commandlet.getUninstallPackageManagerCommands(); - - // assert: exactly one command for APT - assertThat(uninstallCommands).hasSize(1); - PackageManagerCommand cmd = uninstallCommands.getFirst(); - assertThat(cmd.packageManager()).isEqualTo(NativePackageManager.APT); - // The uninstall command includes the package removal and the cleanup command - assertThat(cmd.commands()).containsExactly( - "sudo apt -y autoremove --purge mytool", - "sudo rm -f /etc/apt/sources.list.d/mytool.list"); + commandlet.uninstall(); + + // assert: verify the commands were captured by our mock (no actual execution) + List executedCommands = mockProcessContext.getExecutedCommands(); + assertThat(executedCommands).hasSize(2); + assertThat(executedCommands.get(0)).contains("sudo apt -y autoremove --purge mytool"); + assertThat(executedCommands.get(1)).contains("sudo rm -f /etc/apt/sources.list.d/mytool.list"); } }