Skip to content

Commit

Permalink
Merge branch 'develop' into rollForward-latestMinor
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn authored Aug 13, 2024
2 parents f76f58b + ace1fe0 commit 0caa61c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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

0 comments on commit 0caa61c

Please sign in to comment.