Skip to content

Commit

Permalink
Split version interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tleed5 committed Jan 11, 2024
1 parent 475333b commit 23594b6
Show file tree
Hide file tree
Showing 33 changed files with 388 additions and 380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void MatchingVersionsShouldBeGroupedCorrectly(string version)
var ver1 = VersionFactory.CreateDockerTag(version);
var ver2 = VersionFactory.CreateDockerTag(version);

var items = new List<IVersion> {ver1, ver2}.GroupBy(i => i).ToList();
var items = new List<ISortableVersion> {ver1, ver2}.GroupBy(i => i).ToList();
Assert.AreEqual(1, items.Count);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void TestMatchingVersionsAreGroupedCorrectly(string version)
var ver1 = VersionFactory.CreateMavenVersion(version);
var ver2 = VersionFactory.CreateMavenVersion(version);

var items = new List<IVersion> {ver1, ver2}.GroupBy(i => i).ToList();
var items = new List<ISortableVersion> {ver1, ver2}.GroupBy(i => i).ToList();
Assert.AreEqual(1, items.Count);
}

Expand Down
24 changes: 12 additions & 12 deletions source/Octopus.Versioning.Tests/Maven/Ranges/RangeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,25 +691,25 @@ void checkInvalidRange(string version)
[Test]
public void testContains()
{
IVersion actualVersion = new MavenVersionParser().Parse("2.0.5");
Assert.IsTrue(enforceVersion("2.0.5", actualVersion));
Assert.IsTrue(enforceVersion("2.0.4", actualVersion));
Assert.IsTrue(enforceVersion("[2.0.5]", actualVersion));
Assert.IsFalse(enforceVersion("[2.0.6,)", actualVersion));
Assert.IsFalse(enforceVersion("[2.0.6]", actualVersion));
Assert.IsTrue(enforceVersion("[2.0,2.1]", actualVersion));
Assert.IsFalse(enforceVersion("[2.0,2.0.3]", actualVersion));
Assert.IsTrue(enforceVersion("[2.0,2.0.5]", actualVersion));
Assert.IsFalse(enforceVersion("[2.0,2.0.5)", actualVersion));
ISortableVersion actualSortableVersion = new MavenVersionParser().Parse("2.0.5");
Assert.IsTrue(enforceVersion("2.0.5", actualSortableVersion));
Assert.IsTrue(enforceVersion("2.0.4", actualSortableVersion));
Assert.IsTrue(enforceVersion("[2.0.5]", actualSortableVersion));
Assert.IsFalse(enforceVersion("[2.0.6,)", actualSortableVersion));
Assert.IsFalse(enforceVersion("[2.0.6]", actualSortableVersion));
Assert.IsTrue(enforceVersion("[2.0,2.1]", actualSortableVersion));
Assert.IsFalse(enforceVersion("[2.0,2.0.3]", actualSortableVersion));
Assert.IsTrue(enforceVersion("[2.0,2.0.5]", actualSortableVersion));
Assert.IsFalse(enforceVersion("[2.0,2.0.5)", actualSortableVersion));
}

public bool enforceVersion(string requiredMavenVersionRange, IVersion actualVersion)
public bool enforceVersion(string requiredMavenVersionRange, ISortableVersion actualSortableVersion)
{
MavenVersionRange vr = null;

vr = MavenVersionRange.CreateFromVersionSpec(requiredMavenVersionRange);

return vr.ContainsVersion(actualVersion);
return vr.ContainsVersion(actualSortableVersion);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Octopus.Versioning.Tests.Octopus
{
[TestFixture]
public class OctopusVersionTests
public class OctopusSortableVersionTests
{
static readonly Random Random = new Random();
static readonly OctopusVersionParser OctopusVersionParser = new OctopusVersionParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void MatchingVersionsShouldBeGroupedCorrectly(string version)
var ver1 = VersionFactory.CreateOctopusVersion(version);
var ver2 = VersionFactory.CreateOctopusVersion(version);

var items = new List<IVersion> {ver1, ver2}.GroupBy(i => i).ToList();
var items = new List<ISortableVersion> {ver1, ver2}.GroupBy(i => i).ToList();
Assert.AreEqual(1, items.Count);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void IsMask(string mask, bool isMask)
[TestCase("1.i.i", "2.0.0", null)]
public void GetLatestVersionMask(string version, string latestVersion, string expected)
{
var latestVersions = new List<IVersion>
var latestVersions = new List<ISortableVersion>
{
new OctopusVersionParser().Parse(latestVersion)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static bool IsMask(string versionString)
);
}

public static IVersion GetLatestMaskedVersion(string mask, List<IVersion> versions)
public static ISortableVersion GetLatestMaskedVersion(string mask, List<ISortableVersion> versions)
{
var maskMatch = new MaskMatchedVersion(mask);

Expand Down Expand Up @@ -87,18 +87,18 @@ public static IVersion GetLatestMaskedVersion(string mask, List<IVersion> versio
.FirstOrDefault();
}

public static IVersion ApplyMask(string mask, IVersion currentVersion)
public static ISortableVersion ApplyMask(string mask, ISortableVersion currentSortableVersion)
{
var match = FormatRegex.Match(mask);
if (!match.Success)
return VersionFactory.CreateSemanticVersion(mask);

return currentVersion == null
return currentSortableVersion == null
? GenerateVersionFromMask(new MaskMatchedVersion(mask))
: GenerateVersionFromCurrent(new MaskMatchedVersion(mask), new MaskMatchedVersion(currentVersion.ToString()));
: GenerateVersionFromCurrent(new MaskMatchedVersion(mask), new MaskMatchedVersion(currentSortableVersion.ToString()));
}

static IVersion GenerateVersionFromMask(MaskMatchedVersion mask)
static ISortableVersion GenerateVersionFromMask(MaskMatchedVersion mask)
{
var result = new StringBuilder();
result.Append(mask.Major.EvaluateFromMask());
Expand All @@ -110,7 +110,7 @@ static IVersion GenerateVersionFromMask(MaskMatchedVersion mask)
return VersionFactory.CreateSemanticVersion(result.ToString());
}

static IVersion GenerateVersionFromCurrent(MaskMatchedVersion mask, MaskMatchedVersion current)
static ISortableVersion GenerateVersionFromCurrent(MaskMatchedVersion mask, MaskMatchedVersion current)
{
var result = new StringBuilder();
result.Append(mask.Major.Substitute(current.Major));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void TestMatchingVersionsAreGroupedCorrectly()
var ver1 = VersionFactory.CreateSemanticVersion(version);
var ver2 = VersionFactory.CreateSemanticVersion(version);

var items = new List<IVersion> {ver1, ver2}.GroupBy(i => i).ToList();
var items = new List<ISortableVersion> {ver1, ver2}.GroupBy(i => i).ToList();
Assert.AreEqual(1, items.Count);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,39 @@ public class UnsortableVersionCompareTests
{
static readonly UnsortableVersionParser UnsortableVersionParser = new();

[Test]
[TestCase("release", "release", 0)]
[TestCase("release", "qrelease", 1)]
[TestCase("release", "srelease", -1)]
[TestCase("123", "123", 0)]
[TestCase("123", "100", 1)]
[TestCase("123", "321", -1)]
[TestCase("123Release", "123Release", 0)]
[TestCase("123Release", "100Release", 1)]
[TestCase("123Release", "321Release", -1)]
[TestCase("release-1", "release-1", 0)]
[TestCase("release-1", "release-0", 1)]
[TestCase("release-1", "release-2", -1)]
[TestCase("release.1", "release.1", 0)]
[TestCase("release.1", "release.0", 1)]
[TestCase("release.1", "release.2", -1)]
[TestCase("release_1", "release_1", 0)]
[TestCase("release_1", "release_0", 1)]
[TestCase("release_1", "release_2", -1)]
[TestCase("release-1", "release_1", 0)]
[TestCase("release.1", "release_1", 0)]
[TestCase("release-1", "release_0", 1)]
[TestCase("release.1", "release_2", -1)]
[TestCase("release+123", "release+321", 0)]
[TestCase("release-1+123", "release-1+321", 0)]
[TestCase("release-1+123", "release-0+321", 1)]
[TestCase("release-1+123", "release-2+321", -1)]
public void TestComparisons(string version1, string version2, int result)
{
var parsedVersion1 = UnsortableVersionParser.Parse(version1);
var parsedVersion2 = UnsortableVersionParser.Parse(version2);
Assert.AreEqual(result, parsedVersion1.CompareTo(parsedVersion2));
}
// [Test]
// [TestCase("release", "release", 0)]
// [TestCase("release", "qrelease", 1)]
// [TestCase("release", "srelease", -1)]
// [TestCase("123", "123", 0)]
// [TestCase("123", "100", 1)]
// [TestCase("123", "321", -1)]
// [TestCase("123Release", "123Release", 0)]
// [TestCase("123Release", "100Release", 1)]
// [TestCase("123Release", "321Release", -1)]
// [TestCase("release-1", "release-1", 0)]
// [TestCase("release-1", "release-0", 1)]
// [TestCase("release-1", "release-2", -1)]
// [TestCase("release.1", "release.1", 0)]
// [TestCase("release.1", "release.0", 1)]
// [TestCase("release.1", "release.2", -1)]
// [TestCase("release_1", "release_1", 0)]
// [TestCase("release_1", "release_0", 1)]
// [TestCase("release_1", "release_2", -1)]
// [TestCase("release-1", "release_1", 0)]
// [TestCase("release.1", "release_1", 0)]
// [TestCase("release-1", "release_0", 1)]
// [TestCase("release.1", "release_2", -1)]
// [TestCase("release+123", "release+321", 0)]
// [TestCase("release-1+123", "release-1+321", 0)]
// [TestCase("release-1+123", "release-0+321", 1)]
// [TestCase("release-1+123", "release-2+321", -1)]
// public void TestComparisons(string version1, string version2, int result)
// {
// var parsedVersion1 = UnsortableVersionParser.Parse(version1);
// var parsedVersion2 = UnsortableVersionParser.Parse(version2);
// Assert.AreEqual(result, parsedVersion1.CompareTo(parsedVersion2));
// }

[Test]
[TestCase("release-1", "release-1", true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public void ShouldThrowExceptionOnFailureToParse()
Assert.Catch<ArgumentException>(() => new UnsortableVersionParser().Parse(input));
}

void AssertVersionNumbersAreZero(UnsortableVersion version)
void AssertVersionNumbersAreZero(UnsortableSortableVersion sortableVersion)
{
Assert.AreEqual(0, version.Major);
Assert.AreEqual(0, version.Minor);
Assert.AreEqual(0, version.Patch);
Assert.AreEqual(0, version.Revision);
Assert.AreEqual(0, sortableVersion.Major);
Assert.AreEqual(0, sortableVersion.Minor);
Assert.AreEqual(0, sortableVersion.Patch);
Assert.AreEqual(0, sortableVersion.Revision);
}
}
24 changes: 12 additions & 12 deletions source/Octopus.Versioning/Docker/DockerTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace Octopus.Versioning.Docker
{
public class DockerTag : OctopusVersion
public class DockerTag : OctopusSortableVersion
{
const string Latest = "latest";
bool IsLatest => string.Compare(OriginalString, Latest, StringComparison.Ordinal) == 0;

public DockerTag(OctopusVersion version)
: base(version.Prefix,
version.Major,
version.Minor,
version.Patch,
version.Revision,
version.Release,
version.ReleasePrefix,
version.ReleaseCounter,
version.Metadata,
version.OriginalString)
public DockerTag(OctopusSortableVersion sortableVersion)
: base(sortableVersion.Prefix,
sortableVersion.Major,
sortableVersion.Minor,
sortableVersion.Patch,
sortableVersion.Revision,
sortableVersion.Release,
sortableVersion.ReleasePrefix,
sortableVersion.ReleaseCounter,
sortableVersion.Metadata,
sortableVersion.OriginalString)
{
}

Expand Down
33 changes: 33 additions & 0 deletions source/Octopus.Versioning/ISortableVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;

namespace Octopus.Versioning
{
/// <summary>
/// Represents the base components of a version recognised by Octopus.
/// The terminolgy here comes from SemVer, but can be mapped to other
/// versioning schemes like Maven.
///
/// All classes should reference this interface, but there are some
/// exceptions.
///
/// Some modelling classes need to reference the SemanticVersionConverter
/// to ensure that constructor paramaters and properties of the type
/// IVersion are converted to and from a SemanticVersion object.
///
/// JSON converters like SemanticVersionConverter assume that
/// any version object being stored in the database is a SemanticVersion
/// object. This is because Octopus assumes the use of SemanticVersion
/// in all processes other than external feeds, which do not have
/// version information directly saved in the database.
///
/// The version factory classes also need to know about the conrete
/// classes.
///
/// Outside of those two use cases, all other references to a version
/// should be through this interface.
/// </summary>
public interface ISortableVersion : IVersion, IComparable
{
}
}
27 changes: 1 addition & 26 deletions source/Octopus.Versioning/IVersion.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
using System;
using System.Collections.Generic;

namespace Octopus.Versioning
{
/// <summary>
/// Represents the base components of a version recognised by Octopus.
/// The terminolgy here comes from SemVer, but can be mapped to other
/// versioning schemes like Maven.
///
/// All classes should reference this interface, but there are some
/// exceptions.
///
/// Some modelling classes need to reference the SemanticVersionConverter
/// to ensure that constructor paramaters and properties of the type
/// IVersion are converted to and from a SemanticVersion object.
///
/// JSON converters like SemanticVersionConverter assume that
/// any version object being stored in the database is a SemanticVersion
/// object. This is because Octopus assumes the use of SemanticVersion
/// in all processes other than external feeds, which do not have
/// version information directly saved in the database.
///
/// The version factory classes also need to know about the conrete
/// classes.
///
/// Outside of those two use cases, all other references to a version
/// should be through this interface.
/// </summary>
public interface IVersion : IComparable
public interface IVersion
{
int Major { get; }
int Minor { get; }
Expand Down
2 changes: 1 addition & 1 deletion source/Octopus.Versioning/IVersionComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Octopus.Versioning
/// IVersionComparer represents a version comparer capable of sorting and determining the equality of
/// SemanticVersion objects.
/// </summary>
public interface IVersionComparer : IEqualityComparer<IVersion?>, IComparer<IVersion?>
public interface IVersionComparer : IEqualityComparer<ISortableVersion?>, IComparer<ISortableVersion?>
{
}
}
10 changes: 5 additions & 5 deletions source/Octopus.Versioning/Maven/MavenPackageID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ public MavenPackageID([NotNull]

public MavenPackageID([NotNull]
string? id,
IVersion version) : this(id)
ISortableVersion sortableVersion) : this(id)
{
if (string.IsNullOrWhiteSpace(id) || id.Split(':').Length != 2)
throw new ArgumentException("Package ID must be in the format Group:Artifact e.g. com.google.guava:guava or junit:junit.");

if (version == null)
if (sortableVersion == null)
throw new ArgumentException("version can not be null");

Version = version.ToString();
Version = sortableVersion.ToString();
}

/// <summary>
Expand Down Expand Up @@ -154,7 +154,7 @@ public MavenPackageID([NotNull]
public string? Classifier { get; }
public string DisplayName => ToString(DISPLAY_DELIMITER);

public IVersion? SemanticVersion => Version == null ? null : new MavenVersionParser().Parse(Version);
public ISortableVersion? SemanticVersion => Version == null ? null : new MavenVersionParser().Parse(Version);

/// <summary>
/// The path to the metadata file for the artifact
Expand Down Expand Up @@ -248,7 +248,7 @@ public string DefaultArtifactPath
/// <param name="version">The optional version</param>
/// <returns>A MavenPackageID created to match the package id an optional packaging defined in the UI</returns>
/// <exception cref="ArgumentException">thrown if the input is not in the correct format</exception>
public static MavenPackageID CreatePackageIdFromOctopusInput(string input, IVersion? version = null)
public static MavenPackageID CreatePackageIdFromOctopusInput(string input, ISortableVersion? version = null)
{
var splitVersion = input.Split(':').ToList();
if (!(splitVersion.Count >= 2 && splitVersion.Count <= 4))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

namespace Octopus.Versioning.Maven
{
public class MavenVersion : IVersion
public class MavenSortableVersion : ISortableVersion
{
public MavenVersion(int major,
public MavenSortableVersion(int major,
int minor,
int patch,
int revision,
Expand Down Expand Up @@ -63,7 +63,7 @@ public string Release
public int CompareTo(object obj)
{
return new ComparableVersion(OriginalString)
.CompareTo(new ComparableVersion((obj as MavenVersion)?.OriginalString ?? ""));
.CompareTo(new ComparableVersion((obj as MavenSortableVersion)?.OriginalString ?? ""));
}

public override string ToString()
Expand Down
Loading

0 comments on commit 23594b6

Please sign in to comment.