Skip to content

feat: update version detection to support ddev head version #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/main/java/de/php_perfect/intellij/ddev/version/Version.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
package de.php_perfect.intellij.ddev.version;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class Version implements Comparable<Version> {

public final int @NotNull [] numbers;


public final @NotNull String string;

/**
* The build information for head versions (e.g., "36-ge74e3a95f" in "v1.24.4-36-ge74e3a95f")
* Will be null for regular release versions
*/
@Nullable
private final String buildInfo;

/**
* Pattern to match head versions like "v1.24.4-36-ge74e3a95f"
* Group 1: The semantic version part (v1.24.4)
* Group 2: The build info part (36-ge74e3a95f)
*/
private static final Pattern HEAD_VERSION_PATTERN = Pattern.compile("^(v?\\d+(?:\\.\\d+)?(?:\\.\\d+)?)-(\\d+-g[a-f0-9]+)$");

public Version(@NotNull String version) {
this.string = version;
final String[] split = version.replaceAll("^v", "").split("-")[0].split("\\.");

// Check if this is a head version
Matcher headVersionMatcher = HEAD_VERSION_PATTERN.matcher(version);
String versionForParsing;

if (headVersionMatcher.matches()) {
// This is a head version, use the semantic version part for parsing
versionForParsing = headVersionMatcher.group(1);
this.buildInfo = headVersionMatcher.group(2);
} else {
// Regular version handling
versionForParsing = version.split("-")[0];
this.buildInfo = null;
}

final String[] split = versionForParsing.replaceAll("^v", "").split("\\.");
numbers = new int[split.length];
for (int i = 0; i < split.length; i++) {
numbers[i] = Integer.parseInt(split[i]);
}
}

public boolean isHeadVersion() {
return buildInfo != null;
}

@Nullable
public String getBuildInfo() {
return buildInfo;
}

@Override
public int compareTo(@NotNull Version another) {
final int maxLength = Math.max(numbers.length, another.numbers.length);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/de/php_perfect/intellij/ddev/cmd/DdevImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ void version() throws CommandFailedException {
Assertions.assertEquals(expected, new DdevImpl().version("ddev", getProject()));
}

@Test
void headVersion() throws CommandFailedException {
final Version expected = new Version("v1.24.4-36-ge74e3a95f");
final ProcessOutput processOutput = new ProcessOutput("ddev version v1.24.4-36-ge74e3a95f", "", 0, false, false);

final MockProcessExecutor mockProcessExecutor = (MockProcessExecutor) ApplicationManager.getApplication().getService(ProcessExecutor.class);
mockProcessExecutor.addProcessOutput("ddev --version", processOutput);

Version actual = new DdevImpl().version("ddev", getProject());
Assertions.assertEquals(expected, actual);
Assertions.assertTrue(actual.isHeadVersion());
Assertions.assertEquals("36-ge74e3a95f", actual.getBuildInfo());
}

@Test
void detailedVersions() throws CommandFailedException, IOException {
Versions expected = new Versions("v1.19.0", "20.10.12", "v2.2.2", "docker-desktop");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ void versionsWithPrefixAndSuffixAreParsedCorrectly(String versionString) {
Assertions.assertArrayEquals(new int[]{1, 26, 6}, version.numbers);
}

@Test
void headVersionIsParsedCorrectly() {
final Version version = new Version("v1.24.4-36-ge74e3a95f");

Assertions.assertArrayEquals(new int[]{1, 24, 4}, version.numbers);
Assertions.assertTrue(version.isHeadVersion());
Assertions.assertEquals("36-ge74e3a95f", version.getBuildInfo());
Assertions.assertEquals("v1.24.4-36-ge74e3a95f", version.toString());
}

@Test
void regularVersionIsNotHeadVersion() {
final Version version = new Version("v1.24.4");

Assertions.assertFalse(version.isHeadVersion());
Assertions.assertNull(version.getBuildInfo());
}

@Test
void debugVersionIsNotHeadVersion() {
final Version version = new Version("v1.24.4-DEBUG");

Assertions.assertFalse(version.isHeadVersion());
Assertions.assertNull(version.getBuildInfo());
}

@Test
void compareTo_withEarlierVersion_isGreaterThan() {
Assertions.assertEquals(1, new Version("2.0.0").compareTo(new Version("1.0.0")));
Expand Down Expand Up @@ -43,4 +69,15 @@ void compareTo_withMorePreciseEarlierVersion_isFalse() {
void compareTo_withMorePreciseLaterVersion_isLessThan() {
Assertions.assertEquals(-1, new Version("1").compareTo(new Version("1.0.1")));
}

@Test
void compareTo_headVersionWithSameBaseVersion_isEqual() {
Assertions.assertEquals(0, new Version("v1.24.4").compareTo(new Version("v1.24.4-36-ge74e3a95f")));
}

@Test
void compareTo_headVersionWithDifferentBaseVersion_isUnequal() {
Assertions.assertEquals(-1, new Version("v1.24.3-42-gabcdef12").compareTo(new Version("v1.24.4")));
Assertions.assertEquals(1, new Version("v1.24.5-42-gabcdef12").compareTo(new Version("v1.24.4")));
}
}
Loading