Skip to content

Commit 661ea6a

Browse files
authored
#206: Implemented UninstallCommandlet (#258)
1 parent 64c5534 commit 661ea6a

File tree

5 files changed

+155
-12
lines changed

5 files changed

+155
-12
lines changed

cli/src/main/java/com/devonfw/tools/ide/commandlet/CommandletManagerImpl.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
package com.devonfw.tools.ide.commandlet;
22

3-
import java.util.Collection;
4-
import java.util.Collections;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
8-
93
import com.devonfw.tools.ide.context.IdeContext;
104
import com.devonfw.tools.ide.property.KeywordProperty;
115
import com.devonfw.tools.ide.property.Property;
@@ -18,6 +12,7 @@
1812
import com.devonfw.tools.ide.tool.gh.Gh;
1913
import com.devonfw.tools.ide.tool.gradle.Gradle;
2014
import com.devonfw.tools.ide.tool.helm.Helm;
15+
import com.devonfw.tools.ide.tool.jasypt.Jasypt;
2116
import com.devonfw.tools.ide.tool.java.Java;
2217
import com.devonfw.tools.ide.tool.jmc.Jmc;
2318
import com.devonfw.tools.ide.tool.kotlinc.Kotlinc;
@@ -30,7 +25,12 @@
3025
import com.devonfw.tools.ide.tool.sonar.Sonar;
3126
import com.devonfw.tools.ide.tool.terraform.Terraform;
3227
import com.devonfw.tools.ide.tool.vscode.Vscode;
33-
import com.devonfw.tools.ide.tool.jasypt.Jasypt;
28+
29+
import java.util.Collection;
30+
import java.util.Collections;
31+
import java.util.HashMap;
32+
import java.util.List;
33+
import java.util.Map;
3434

3535
/**
3636
* Implementation of {@link CommandletManager}.
@@ -70,6 +70,7 @@ public CommandletManagerImpl(IdeContext context) {
7070
add(new EditionListCommandlet(context));
7171
add(new VersionCommandlet(context));
7272
add(new RepositoryCommandlet(context));
73+
add(new UninstallCommandlet(context));
7374
add(new UpdateCommandlet(context));
7475
add(new CreateCommandlet(context));
7576
add(new Gh(context));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.devonfw.tools.ide.commandlet;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
5+
6+
import com.devonfw.tools.ide.context.IdeContext;
7+
import com.devonfw.tools.ide.io.FileAccess;
8+
import com.devonfw.tools.ide.property.ToolProperty;
9+
10+
/**
11+
* An internal {@link Commandlet} to uninstall a tool.
12+
*/
13+
public class UninstallCommandlet extends Commandlet {
14+
15+
/** The tool to uninstall. */
16+
public final ToolProperty tool;
17+
18+
/**
19+
* The constructor.
20+
*
21+
* @param context the {@link IdeContext}.
22+
*/
23+
public UninstallCommandlet(IdeContext context) {
24+
25+
super(context);
26+
addKeyword(getName());
27+
this.tool = add(new ToolProperty("", true, "tool"));
28+
}
29+
30+
@Override
31+
public String getName() {
32+
33+
return "uninstall";
34+
}
35+
36+
@Override
37+
public void run() {
38+
39+
String commandletName = this.tool.getValue().getName();
40+
Path softwarePath = context.getSoftwarePath().resolve(commandletName);
41+
if (Files.exists(softwarePath)) {
42+
FileAccess fileAccess = context.getFileAccess();
43+
try {
44+
fileAccess.delete(softwarePath);
45+
this.context.success("Successfully uninstalled " + commandletName);
46+
} catch (Exception e) {
47+
throw new IllegalStateException("Couldn't uninstall " + commandletName, e);
48+
}
49+
} else {
50+
this.context.info("An installed version of " + commandletName + " does not exist");
51+
}
52+
}
53+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.devonfw.tools.ide.commandlet;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.doThrow;
5+
import static org.mockito.Mockito.mock;
6+
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import com.devonfw.tools.ide.context.AbstractIdeContextTest;
13+
import com.devonfw.tools.ide.context.IdeTestContext;
14+
import com.devonfw.tools.ide.io.FileAccess;
15+
import com.devonfw.tools.ide.log.IdeLogLevel;
16+
17+
/**
18+
* Integration test of {@link UninstallCommandlet}.
19+
*/
20+
public class UninstallCommandletTest extends AbstractIdeContextTest {
21+
22+
/**
23+
* Test of {@link UninstallCommandlet} run.
24+
*
25+
*/
26+
@Test
27+
public void testUninstallCommandletRun_WithExistingCommandlet() {
28+
29+
// arrange
30+
String toolName = "npm";
31+
IdeTestContext context = newContext(PROJECT_BASIC);
32+
UninstallCommandlet uninstallCommandlet = context.getCommandletManager().getCommandlet(UninstallCommandlet.class);
33+
uninstallCommandlet.tool.setValueAsString(toolName, context);
34+
// act
35+
uninstallCommandlet.run();
36+
// assert
37+
assertLogMessage(context, IdeLogLevel.SUCCESS, "Successfully uninstalled " + toolName);
38+
assertThat(Files.notExists(context.getSoftwarePath().resolve(toolName)));
39+
}
40+
41+
@Test
42+
public void testUninstallCommandletRun_WithNonExistingCommandlet() {
43+
44+
// arrange
45+
String toolName = "eclipse";
46+
IdeTestContext context = newContext(PROJECT_BASIC);
47+
UninstallCommandlet uninstallCommandlet = context.getCommandletManager().getCommandlet(UninstallCommandlet.class);
48+
uninstallCommandlet.tool.setValueAsString(toolName, context);
49+
// act
50+
uninstallCommandlet.run();
51+
// assert
52+
assertLogMessage(context, IdeLogLevel.INFO, "An installed version of " + toolName + " does not exist");
53+
assertThat(Files.notExists(context.getSoftwarePath().resolve(toolName)));
54+
}
55+
56+
@Test
57+
public void testUninstallCommandletRun_ThrowsException() {
58+
59+
// arrange
60+
String toolName = "npm";
61+
IdeTestContext context = newContext(PROJECT_BASIC);
62+
63+
FileAccess mockFileAccess = mock(FileAccess.class);
64+
doThrow(new IllegalStateException()).when(mockFileAccess).delete(any(Path.class));
65+
context.setMockFileAccess(mockFileAccess);
66+
67+
UninstallCommandlet uninstallCommandlet = context.getCommandletManager().getCommandlet(UninstallCommandlet.class);
68+
uninstallCommandlet.tool.setValueAsString(toolName, context);
69+
// act
70+
try {
71+
uninstallCommandlet.run();
72+
} catch (IllegalStateException e) {
73+
// assert
74+
assertThat(e).hasMessageContaining("Couldn't uninstall " + toolName);
75+
}
76+
}
77+
}

cli/src/test/java/com/devonfw/tools/ide/context/AbstractIdeTestContext.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.devonfw.tools.ide.context;
22

3+
import java.nio.file.Path;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.function.Function;
7+
8+
import com.devonfw.tools.ide.io.FileAccess;
39
import com.devonfw.tools.ide.io.IdeProgressBar;
410
import com.devonfw.tools.ide.io.IdeProgressBarTestImpl;
511
import com.devonfw.tools.ide.log.IdeLogLevel;
@@ -8,11 +14,6 @@
814
import com.devonfw.tools.ide.repo.DefaultToolRepository;
915
import com.devonfw.tools.ide.repo.ToolRepository;
1016

11-
import java.nio.file.Path;
12-
import java.util.HashMap;
13-
import java.util.Map;
14-
import java.util.function.Function;
15-
1617
/**
1718
* Implementation of {@link IdeContext} for testing.
1819
*/
@@ -26,6 +27,8 @@ public class AbstractIdeTestContext extends AbstractIdeContext {
2627

2728
private SystemInfo systemInfo;
2829

30+
private FileAccess mockFileAccess;
31+
2932
/**
3033
* The constructor.
3134
*
@@ -93,4 +96,12 @@ public void setSystemInfo(SystemInfo systemInfo) {
9396

9497
this.systemInfo = systemInfo;
9598
}
99+
100+
101+
/**
102+
* @param fileAccess the {@link FileAccess} to use for testing.
103+
*/
104+
public void setMockFileAccess(FileAccess fileAccess){
105+
this.mockFileAccess = fileAccess;
106+
}
96107
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this is npm software of basic

0 commit comments

Comments
 (0)