Skip to content

Commit

Permalink
Handle remove all command
Browse files Browse the repository at this point in the history
  • Loading branch information
udda1996 committed Jan 4, 2024
1 parent 246d147 commit 99403b0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 14 deletions.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ swan-lake-version=2201.2.0
swan-lake-spec-version=2022R3
swan-lake-latest-version=2201.8.2
swan-lake-latest-spec-version=2023R1
swan-lake-latest-dependency-version=jdk-17.0.7+7-jre
1-x-channel-version=1.2.0
1-x-channel-dependency-version=jdk8u202-b08-jre
1-x-channel-spec-version=2020R1
1-x-channel-latest-version=1.2.44
19 changes: 17 additions & 2 deletions src/main/java/org/ballerinalang/command/cmd/RemoveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,23 @@ private void removeAll() {
}
}
getPrintStream().println("All non-active distributions are successfully removed");
// String activeDistribution = ToolUtil.getCurrentBallerinaVersion();
// String dependencyForActiveDistribution = ToolUtil.getDependency(activeDistribution);
String activeDistribution = ToolUtil.getCurrentBallerinaVersion();
String dependencyForActiveDistribution = ToolUtil.getDependency(getPrintStream(), activeDistribution,
ToolUtil.getType(activeDistribution), activeDistribution);
File[] dependencies = new File(ToolUtil.getDependencyPath()).listFiles();
if (dependencies != null) {
if (dependencies.length > 1) {
getPrintStream().println("Removing unused dependencies");
for (File dependency : dependencies) {
if (dependency.isDirectory() && !dependency.getName().equals(dependencyForActiveDistribution)) {
OSUtils.deleteFiles(dependency.toPath());
}
}
}
} else {
throw ErrorUtil.createCommandException("No dependencies found");
}

} catch (IOException | NullPointerException e) {
throw ErrorUtil.createCommandException("error occurred while removing the distributions" + e);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/ballerinalang/command/util/ToolUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
HttpsURLConnection redirectedConn = getServerUrlWithProxyAuthentication(new URL(newUrl));
redirectedConn.setRequestProperty("content-type", "binary/data");
ToolUtil.downloadDistributionZip(printStream, redirectedConn, distribution);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution,
distributionType, distributionVersion);
if (!ToolUtil.checkDependencyAvailable(dependencyForDistribution)) {
downloadDependency(printStream, dependencyForDistribution, distributionType,
distributionVersion);
Expand All @@ -541,7 +542,8 @@ public static boolean downloadDistribution(PrintStream printStream, String distr
} else if (conn.getResponseCode() == 200) {
printStream.println("Fetching the '" + distribution + "' distribution from the remote server...");
ToolUtil.downloadDistributionZip(printStream, conn, distribution);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution, distributionType, distributionVersion);
String dependencyForDistribution = ToolUtil.getDependency(printStream, distribution,
distributionType, distributionVersion);
if (!ToolUtil.checkDependencyAvailable(dependencyForDistribution)) {
downloadDependency(printStream, dependencyForDistribution, distributionType,
distributionVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,10 @@ public void removeAllCommandwithMultipleArgsTest() {
} catch (CommandException e) {
Assert.assertTrue(e.getMessages().get(0).contains("too many arguments"));
}

RemoveCommand removeAllCommand = new RemoveCommand(testStream);
new CommandLine(removeAllCommand).parse("-a");
removeAllCommand.execute();
Assert.assertTrue(outContent.toString().contains("Removing unused dependencies"));
}
}
11 changes: 11 additions & 0 deletions src/test/java/org/ballerinalang/distribution/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class TestUtils {
public static final String PATH_ARG = TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("bin").resolve("bal").
toString();
private static final Path DIST_PATH = TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("distributions");
private static final Path DEPENDENCY_PATH = TEST_DISTRIBUTION_PATH.resolve(DIST_NAME).resolve("dependencies");
public static final Path TEST_DIR = TEST_DISTRIBUTION_PATH.resolve("test");

/**
Expand Down Expand Up @@ -292,6 +293,16 @@ public static Path getDistPath (String version) {
return DIST_PATH.resolve(type + "-" + version);
}

/**
* Get dependency path for the version.
*
* @param version dependency version
* @return returns dependency path for available distributions
*/
public static Path getDependencyPath (String version) {
return DEPENDENCY_PATH.resolve(version);
}

/**
* Get the display text using distribution version
*
Expand Down
27 changes: 17 additions & 10 deletions src/test/java/org/ballerinalang/distribution/UpdateToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public class UpdateToolTest {
String swanLakeVersion = System.getProperty("swan-lake-version");
String swanLakeSpecVersion = System.getProperty("swan-lake-spec-version");
String swanLakeLatestVersion = System.getProperty("swan-lake-latest-version");
String swanLakeLatestVersionDependency = System.getProperty("swan-lake-latest-dependency-version");
String swanLakeLatestSpecVersion = System.getProperty("swan-lake-latest-spec-version");
String previouschannelVersion = System.getProperty("1-x-channel-version");
String previousChannelVersion = System.getProperty("1-x-channel-version");
String previousChannelDependencyVersion = System.getProperty("1-x-channel-dependency-version");
String previousChannelSpecVersion = System.getProperty("1-x-channel-spec-version");
String previousChanneLatestVersion = System.getProperty("1-x-channel-latest-version");

Expand All @@ -61,17 +63,17 @@ public void testPullCommand() throws IOException, InterruptedException {
Assert.assertTrue(output.contains("a distribution must be specified to pull"));

args.add("--test");
args.add(previouschannelVersion);
args.add(previousChannelVersion);
output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("Fetching the '" + previouschannelVersion +
Assert.assertTrue(output.contains("Fetching the '" + previousChannelVersion +
"' distribution from the remote server"));
Assert.assertTrue(output.contains("Fetching the dependencies for '" + previouschannelVersion +
Assert.assertTrue(output.contains("Fetching the dependencies for '" + previousChannelVersion +
"' from the remote server"));
Assert.assertTrue(output.contains("successfully set as the active distribution"));
Assert.assertTrue(Files.isDirectory(TestUtils.getDistPath(previouschannelVersion)));
Assert.assertTrue(Files.isDirectory(TestUtils.getDistPath(previousChannelVersion)));
output = TestUtils.testInstallation();
Assert.assertEquals(output, TestUtils.getVersionOutput(previouschannelVersion, previousChannelSpecVersion,
TestUtils.MAVEN_VERSION, previouschannelVersion));
Assert.assertEquals(output, TestUtils.getVersionOutput(previousChannelVersion, previousChannelSpecVersion,
TestUtils.MAVEN_VERSION, previousChannelVersion));

args.remove(args.size() - 1);
args.add(swanLakeVersion);
Expand Down Expand Up @@ -163,7 +165,7 @@ public void testUpdateCommand() throws IOException, InterruptedException {
List<String> useArgs = TestUtils.addPathArg();
useArgs.add("dist");
useArgs.add("use");
useArgs.add(previouschannelVersion);
useArgs.add(previousChannelVersion);
output = TestUtils.executeCommand(useArgs);
Assert.assertTrue(output.contains("successfully set as the active distribution"));

Expand Down Expand Up @@ -281,10 +283,11 @@ public void testRemoveCommand() throws IOException, InterruptedException {
Assert.assertTrue(output.contains("The active Ballerina distribution cannot be removed"));

args.remove(args.size() - 1);
args.add(previouschannelVersion);
args.add(previousChannelVersion);
output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("successfully removed"));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(previouschannelVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(previousChannelVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDependencyPath(previousChannelDependencyVersion)));

args.add("arg1");
output = TestUtils.executeCommand(args);
Expand All @@ -295,9 +298,13 @@ public void testRemoveCommand() throws IOException, InterruptedException {
args.add("-a");
output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("All non-active distributions are successfully removed"));
Assert.assertTrue(output.contains("Removing unused dependencies"));
Assert.assertTrue(Files.exists(TestUtils.getDistPath(swanLakeLatestVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(swanLakeVersion)));
Assert.assertFalse(Files.exists(TestUtils.getDistPath(previousChanneLatestVersion)));
Assert.assertTrue(Files.exists(TestUtils.getDependencyPath(swanLakeLatestVersionDependency)));
Assert.assertEquals(Files.list(TestUtils.getDependencyPath(swanLakeLatestVersionDependency).getParent()).count()
, 1);

output = TestUtils.executeCommand(args);
Assert.assertTrue(output.contains("There is nothing to remove. Only active distribution is remaining"));
Expand Down

0 comments on commit 99403b0

Please sign in to comment.