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
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> getExecutedCommands() {
return executedCommands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -173,30 +175,37 @@ protected List<NativePackage> 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<PackageManagerCommand> 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<String> 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");
}
}