Skip to content

Commit

Permalink
#206: Implemented UninstallCommandlet (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndemirca authored Apr 15, 2024
1 parent 64c5534 commit 661ea6a
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package com.devonfw.tools.ide.commandlet;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.property.KeywordProperty;
import com.devonfw.tools.ide.property.Property;
Expand All @@ -18,6 +12,7 @@
import com.devonfw.tools.ide.tool.gh.Gh;
import com.devonfw.tools.ide.tool.gradle.Gradle;
import com.devonfw.tools.ide.tool.helm.Helm;
import com.devonfw.tools.ide.tool.jasypt.Jasypt;
import com.devonfw.tools.ide.tool.java.Java;
import com.devonfw.tools.ide.tool.jmc.Jmc;
import com.devonfw.tools.ide.tool.kotlinc.Kotlinc;
Expand All @@ -30,7 +25,12 @@
import com.devonfw.tools.ide.tool.sonar.Sonar;
import com.devonfw.tools.ide.tool.terraform.Terraform;
import com.devonfw.tools.ide.tool.vscode.Vscode;
import com.devonfw.tools.ide.tool.jasypt.Jasypt;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Implementation of {@link CommandletManager}.
Expand Down Expand Up @@ -70,6 +70,7 @@ public CommandletManagerImpl(IdeContext context) {
add(new EditionListCommandlet(context));
add(new VersionCommandlet(context));
add(new RepositoryCommandlet(context));
add(new UninstallCommandlet(context));
add(new UpdateCommandlet(context));
add(new CreateCommandlet(context));
add(new Gh(context));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.devonfw.tools.ide.commandlet;

import java.nio.file.Files;
import java.nio.file.Path;

import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.property.ToolProperty;

/**
* An internal {@link Commandlet} to uninstall a tool.
*/
public class UninstallCommandlet extends Commandlet {

/** The tool to uninstall. */
public final ToolProperty tool;

/**
* The constructor.
*
* @param context the {@link IdeContext}.
*/
public UninstallCommandlet(IdeContext context) {

super(context);
addKeyword(getName());
this.tool = add(new ToolProperty("", true, "tool"));
}

@Override
public String getName() {

return "uninstall";
}

@Override
public void run() {

String commandletName = this.tool.getValue().getName();
Path softwarePath = context.getSoftwarePath().resolve(commandletName);
if (Files.exists(softwarePath)) {
FileAccess fileAccess = context.getFileAccess();
try {
fileAccess.delete(softwarePath);
this.context.success("Successfully uninstalled " + commandletName);
} catch (Exception e) {
throw new IllegalStateException("Couldn't uninstall " + commandletName, e);
}
} else {
this.context.info("An installed version of " + commandletName + " does not exist");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.devonfw.tools.ide.commandlet;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;

import com.devonfw.tools.ide.context.AbstractIdeContextTest;
import com.devonfw.tools.ide.context.IdeTestContext;
import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.log.IdeLogLevel;

/**
* Integration test of {@link UninstallCommandlet}.
*/
public class UninstallCommandletTest extends AbstractIdeContextTest {

/**
* Test of {@link UninstallCommandlet} run.
*
*/
@Test
public void testUninstallCommandletRun_WithExistingCommandlet() {

// arrange
String toolName = "npm";
IdeTestContext context = newContext(PROJECT_BASIC);
UninstallCommandlet uninstallCommandlet = context.getCommandletManager().getCommandlet(UninstallCommandlet.class);
uninstallCommandlet.tool.setValueAsString(toolName, context);
// act
uninstallCommandlet.run();
// assert
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully uninstalled " + toolName);
assertThat(Files.notExists(context.getSoftwarePath().resolve(toolName)));
}

@Test
public void testUninstallCommandletRun_WithNonExistingCommandlet() {

// arrange
String toolName = "eclipse";
IdeTestContext context = newContext(PROJECT_BASIC);
UninstallCommandlet uninstallCommandlet = context.getCommandletManager().getCommandlet(UninstallCommandlet.class);
uninstallCommandlet.tool.setValueAsString(toolName, context);
// act
uninstallCommandlet.run();
// assert
assertLogMessage(context, IdeLogLevel.INFO, "An installed version of " + toolName + " does not exist");
assertThat(Files.notExists(context.getSoftwarePath().resolve(toolName)));
}

@Test
public void testUninstallCommandletRun_ThrowsException() {

// arrange
String toolName = "npm";
IdeTestContext context = newContext(PROJECT_BASIC);

FileAccess mockFileAccess = mock(FileAccess.class);
doThrow(new IllegalStateException()).when(mockFileAccess).delete(any(Path.class));
context.setMockFileAccess(mockFileAccess);

UninstallCommandlet uninstallCommandlet = context.getCommandletManager().getCommandlet(UninstallCommandlet.class);
uninstallCommandlet.tool.setValueAsString(toolName, context);
// act
try {
uninstallCommandlet.run();
} catch (IllegalStateException e) {
// assert
assertThat(e).hasMessageContaining("Couldn't uninstall " + toolName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.devonfw.tools.ide.context;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import com.devonfw.tools.ide.io.FileAccess;
import com.devonfw.tools.ide.io.IdeProgressBar;
import com.devonfw.tools.ide.io.IdeProgressBarTestImpl;
import com.devonfw.tools.ide.log.IdeLogLevel;
Expand All @@ -8,11 +14,6 @@
import com.devonfw.tools.ide.repo.DefaultToolRepository;
import com.devonfw.tools.ide.repo.ToolRepository;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
* Implementation of {@link IdeContext} for testing.
*/
Expand All @@ -26,6 +27,8 @@ public class AbstractIdeTestContext extends AbstractIdeContext {

private SystemInfo systemInfo;

private FileAccess mockFileAccess;

/**
* The constructor.
*
Expand Down Expand Up @@ -93,4 +96,12 @@ public void setSystemInfo(SystemInfo systemInfo) {

this.systemInfo = systemInfo;
}


/**
* @param fileAccess the {@link FileAccess} to use for testing.
*/
public void setMockFileAccess(FileAccess fileAccess){
this.mockFileAccess = fileAccess;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is npm software of basic

0 comments on commit 661ea6a

Please sign in to comment.