Skip to content
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

feat: Improve parsing of Docker image tag version MatchVersion(Predicate<Version>) #1231

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
12 changes: 10 additions & 2 deletions src/Testcontainers/Images/DockerImage.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DotNet.Testcontainers.Images
{
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
Expand Down Expand Up @@ -125,8 +126,15 @@ public bool MatchVersion(Predicate<string> predicate)
/// <inheritdoc />
public bool MatchVersion(Predicate<Version> predicate)
{
var versionMatch = Regex.Match(Tag, "^\\d+(\\.\\d+)?(\\.\\d+)?", RegexOptions.None, TimeSpan.FromSeconds(1));
return versionMatch.Success && Version.TryParse(versionMatch.Value, out var version) && predicate(version);
var versionMatch = Regex.Match(Tag, @"^(\d+)(\.\d+)?(\.\d+)?", RegexOptions.None, TimeSpan.FromSeconds(1));
if (!versionMatch.Success)
return false;

if (Version.TryParse(versionMatch.Value, out var version))
return predicate(version);

// If the regex matches and Version.TryParse fails then it means it's a major version only (i.e. without any . in the version)
return predicate(new Version(int.Parse(versionMatch.Groups[1].Value, NumberStyles.None), 0));
}

private static string TrimOrDefault(string value, string defaultValue = default)
Expand Down
14 changes: 10 additions & 4 deletions tests/Testcontainers.Tests/Unit/Images/TestcontainersImageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,18 @@ public void MatchLatestOrNightly_TagIsNeither_ReturnsFalse()
Assert.False(result);
}

[Fact]
public void MatchVersion_ReturnsTrue_WhenVersionMatchesPredicate()
[Theory]
[InlineData("foo:2", 2, 0, -1, -1)]
[InlineData("foo:2-variant", 2, 0, -1, -1)]
[InlineData("foo:2.3", 2, 3, -1, -1)]
[InlineData("foo:2.3-variant", 2, 3, -1, -1)]
[InlineData("foo:2.3.4", 2, 3, 4, -1)]
[InlineData("foo:2.3.4-variant", 2, 3, 4, -1)]
public void MatchVersion_ReturnsTrue_WhenVersionMatchesPredicate(string image, int major, int minor, int build, int revision)
{
// Given
Predicate<Version> predicate = v => v.Major == 1 && v.Minor == 0 && v.Build == 0;
IImage dockerImage = new DockerImage("foo:1.0.0");
Predicate<Version> predicate = v => v.Major == major && v.Minor == minor && v.Build == build && v.Revision == revision;
IImage dockerImage = new DockerImage(image);

// When
var result = dockerImage.MatchVersion(predicate);
Expand Down
Loading