Skip to content

Commit

Permalink
version: ensure versions to compare are not blank (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Feb 19, 2024
1 parent fded16b commit 6e703a8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 67 deletions.
84 changes: 47 additions & 37 deletions src/main/java/me/itzg/helpers/versions/CompareVersionsCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.itzg.helpers.versions;

import java.util.concurrent.Callable;
import me.itzg.helpers.errors.InvalidParameterException;
import org.apache.maven.artifact.versioning.ComparableVersion;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
Expand All @@ -9,43 +10,52 @@
description = "Used for shell scripting, exits with success(0) when comparison is satisfied or 1 when not"
)
public class CompareVersionsCommand implements Callable<Integer> {
@Parameters(index = "0")
String leftVersion;

@Parameters(index = "1")
Comparison comparison;

@Parameters(index = "2")
String rightVersion;

@Override
public Integer call() throws Exception {
char leftVersionChannel = leftVersion.charAt(0);
leftVersionChannel = Character.isDigit(leftVersionChannel) ? 'r' : leftVersionChannel;
char rightVersionChannel = rightVersion.charAt(0);
rightVersionChannel = Character.isDigit(rightVersionChannel) ? 'r' : rightVersionChannel;

if (comparison == Comparison.lt && leftVersionChannel < rightVersionChannel) {
return 0;
} else if (comparison == Comparison.lt && leftVersionChannel > rightVersionChannel) {
return 1;
}

if (leftVersion.startsWith("a") || leftVersion.startsWith("b")) {
leftVersion = leftVersion.substring(1);
}
if (rightVersion.startsWith("a") || rightVersion.startsWith("b")) {
rightVersion = rightVersion.substring(1);
}

final ComparableVersion lhs = new ComparableVersion(leftVersion);
final ComparableVersion rhs = new ComparableVersion(rightVersion);

if (comparison == Comparison.lt && lhs.compareTo(rhs) < 0) {
return 0;
}
else {
return 1;
@Parameters(index = "0")
String leftVersion;

@Parameters(index = "1")
Comparison comparison;

@Parameters(index = "2")
String rightVersion;

@Override
public Integer call() throws Exception {
if (leftVersion.isEmpty()) {
throw new InvalidParameterException("Left version is required");
}
if (rightVersion.isEmpty()) {
throw new InvalidParameterException("Right version is required");
}

char leftVersionChannel = leftVersion.charAt(0);
leftVersionChannel = Character.isDigit(leftVersionChannel) ? 'r' : leftVersionChannel;
char rightVersionChannel = rightVersion.charAt(0);
rightVersionChannel = Character.isDigit(rightVersionChannel) ? 'r' : rightVersionChannel;

if (comparison == Comparison.lt && leftVersionChannel < rightVersionChannel) {
return 0;
}
else if (comparison == Comparison.lt && leftVersionChannel > rightVersionChannel) {
return 1;
}

if (leftVersion.startsWith("a") || leftVersion.startsWith("b")) {
leftVersion = leftVersion.substring(1);
}
if (rightVersion.startsWith("a") || rightVersion.startsWith("b")) {
rightVersion = rightVersion.substring(1);
}

final ComparableVersion lhs = new ComparableVersion(leftVersion);
final ComparableVersion rhs = new ComparableVersion(rightVersion);

if (comparison == Comparison.lt && lhs.compareTo(rhs) < 0) {
return 0;
}
else {
return 1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,60 @@

import static org.assertj.core.api.Assertions.assertThat;

import me.itzg.helpers.LatchingExecutionExceptionHandler;
import me.itzg.helpers.errors.InvalidParameterException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import picocli.CommandLine;
import picocli.CommandLine.ExitCode;

class CompareVersionsCommandTest {

@Test
void noArgs() {
final int status =
new CommandLine(new CompareVersionsCommand())
.execute();

assertThat(status).isEqualTo(2);
}

@ParameterizedTest
@CsvSource({
"1.18, lt, 1.18.1, 0",
"1.18.1, lt, 1.18.1, 1",
"1.12.1, lt, 1.12.2, 0",
"b1.7.3, lt, 1.18, 0",
"b1.7.3, lt, b1.6, 1",
"a1.4, lt, b1.7.3, 0",
"1.18, lt, a1.4, 1",
"1.18.1-rc3, lt, 1.18.1, 0",
"21w44a, lt, 1.18.1, 1",
})
void combinations(String left, String op, String right, int expected) {
final int status =
new CommandLine(new CompareVersionsCommand())
.execute(
left, op, right
);

assertThat(status).isEqualTo(expected);
}
@Test
void noArgs() {
final int status =
new CommandLine(new CompareVersionsCommand())
.execute();

assertThat(status).isEqualTo(2);
}

@ParameterizedTest
@CsvSource({
"1.18, lt, 1.18.1, 0",
"1.18.1, lt, 1.18.1, 1",
"1.12.1, lt, 1.12.2, 0",
"b1.7.3, lt, 1.18, 0",
"b1.7.3, lt, b1.6, 1",
"a1.4, lt, b1.7.3, 0",
"1.18, lt, a1.4, 1",
"1.18.1-rc3, lt, 1.18.1, 0",
"21w44a, lt, 1.18.1, 1",
"21w44a, lt, 1.18.1, 1",
})
void combinations(String left, String op, String right, int expected) {
final int status =
new CommandLine(new CompareVersionsCommand())
.execute(
left, op, right
);

assertThat(status).isEqualTo(expected);
}

@Test
void failsUsageWithBlankVersion() {
final LatchingExecutionExceptionHandler executionExceptionHandler = new LatchingExecutionExceptionHandler();
final int status =
new CommandLine(new CompareVersionsCommand())
.setExecutionExceptionHandler(executionExceptionHandler)
.execute(
"", "lt", "1.16.5"
);

assertThat(status).isNotEqualTo(ExitCode.OK);
assertThat(executionExceptionHandler.getExecutionException())
.isInstanceOf(InvalidParameterException.class);
}
}

0 comments on commit 6e703a8

Please sign in to comment.