-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
155 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
cli/src/main/java/com/devonfw/tools/ide/commandlet/UninstallCommandlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
cli/src/test/java/com/devonfw/tools/ide/commandlet/UninstallCommandletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
cli/src/test/resources/ide-projects/basic/project/software/npm/bin/readme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
this is npm software of basic |