Skip to content

Commit

Permalink
Merge pull request #104 from FirelyTeam/75-packagecontextinstallpacka…
Browse files Browse the repository at this point in the history
…gedependency-with-version-null-should-return-the-latest-version

Creating package dependency without a version will result in "latest"
  • Loading branch information
ewoutkramer authored Mar 12, 2024
2 parents 7900fbf + 2393efd commit 73d355d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 55 deletions.
5 changes: 2 additions & 3 deletions Firely.Fhir.Packages.Tests/ManifestSerializationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void TestReadingAComplexAuthorProperty()
{
var json = File.ReadAllText($"TestData/unknown-properties-package.json");
var manifest = PackageParser.ParseManifest(json);
//manifest.AuthorInformation()
manifest.Should().NotBeNull();
}

Expand All @@ -86,7 +85,7 @@ public void TestReadingAComplexAuthorProperty()
[DataTestMethod]
public void TestAuthorStringParsing(string? fullAuthor, string? name, string? email, string? url)
{
PackageManifest manif = new(name: "authot.test", version: "1.0.1")
PackageManifest manif = new(name: "author.test", version: "1.0.1")
{
Author = fullAuthor
};
Expand All @@ -103,7 +102,7 @@ public void TestAuthorStringParsing(string? fullAuthor, string? name, string? em
[DataTestMethod]
public void TestAuthorStringSerializing(string? fullAuthor, string? name, string? email, string? url)
{
PackageManifest manif = new(name: "authot.test", version: "1.0.1")
PackageManifest manif = new(name: "author.test", version: "1.0.1")
{
AuthorInformation = new()
{
Expand Down
9 changes: 1 addition & 8 deletions Firely.Fhir.Packages.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@ static IEnumerable<PackageDependency> createDependencies(string[] dependencies)
{
foreach (var dep in dependencies)
{
var splitDependency = dep.Split('@');
if (splitDependency.Length == 1)
yield return new PackageDependency(dep, "latest");
else
{
var versionDep = splitDependency[1];
yield return new PackageDependency(splitDependency[0], versionDep);
}
yield return (PackageDependency)dep;
}
}
}
Expand Down
67 changes: 37 additions & 30 deletions Firely.Fhir.Packages.Tests/VersionsTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Firely.Fhir.Packages.Tests
Expand All @@ -20,54 +21,60 @@ public void ParseReferences()
Assert.AreEqual("hl7.fhir.r4.core", reference.Name);
Assert.AreEqual("4.0.1", reference.Version);
}
}

[TestClass]
public class VersionsTest
{
[TestMethod]
public void Resolve_PackageDependencyExistsInVersionList_FixedVersionSearchFindsVersion()
public void ParseDependencie()
{
var target = new Versions(new string[] { "1.0.0", "2.0.0" });
var dependencie = (PackageDependency)"[email protected]";

PackageReference result = target.Resolve(new PackageDependency("SomeName", "1.0.0"));

Assert.IsTrue(result.Found);
Assert.AreEqual("1.0.0", result.Version);
}

[TestMethod]
public void Resolve_PackageDependencyExistsInVersionList_RangedVersionSearchFindsVersion()
{
var target = new Versions(new string[] { "1.0.0", "2.0.0" });
Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name);
Assert.AreEqual("4.0.1", dependencie.Range);

PackageReference result = target.Resolve(new PackageDependency("SomeName", "1.x"));
dependencie = "[email protected]";

Assert.IsTrue(result.Found);
Assert.AreEqual("1.0.0", result.Version);
Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name);
Assert.AreEqual("4.0.1", dependencie.Range);
}

[TestMethod]
public void Resolve_PackageDependencyDoesNotExistInVersionList_FixedVersionSearchReturnsNotFound()
public void ParseDependencieWithoutVersion()
{
var target = new Versions(new string[] { "1.0.0", "2.0.0" });
var dependencie = (PackageDependency)"hl7.fhir.r4.core";

Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name);
Assert.AreEqual("latest", dependencie.Range);

PackageReference result = target.Resolve(new PackageDependency("SomeName", "3.0.0"));
dependencie = "hl7.fhir.r4.core";

Assert.IsFalse(result.Found);
Assert.IsTrue(result.NotFound);
Assert.AreEqual("hl7.fhir.r4.core", dependencie.Name);
Assert.AreEqual("latest", dependencie.Range);
}
}

[TestMethod]
public void Resolve_PackageDependencyDoesNotExistInVersionList_RangedVersionSearchReturnsNotFound()
[TestClass]
public class VersionsTest
{
[DataRow("1.0.0", "1.0.0")]
[DataRow("1.x", "1.0.0")]
[DataRow("latest", "2.0.0")]
[DataRow(null, "2.0.0")]
[DataRow("3.0.0", null)]
[DataRow("3.x", null)]
[DataTestMethod]
public void ResolveVersionTest(string? version, string? versionReturned)
{
var target = new Versions(new string[] { "1.0.0", "2.0.0" });

PackageReference result = target.Resolve(new PackageDependency("SomeName", "3.x"));
PackageReference result = target.Resolve(new PackageDependency("SomeName", version));

Assert.IsFalse(result.Found);
Assert.IsTrue(result.NotFound);
}
bool found = versionReturned is not null;
result.Found.Should().Be(found);
result.NotFound.Should().NotBe(found);

if (found)
{
result.Version.Should().Be(versionReturned);
}
}
}
}
18 changes: 12 additions & 6 deletions Firely.Fhir.Packages/Models/PackageDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ namespace Firely.Fhir.Packages
public struct PackageDependency
{
public string Name;
public string? Range; // 3.x, 3.1 - 3.3, 1.1 | 1.2
public string Range; // 3.x, 3.1 - 3.3, 1.1 | 1.2

/// <summary>
/// Initializes a new package dependency
/// </summary>
/// <param name="name">name of the package</param>
/// <param name="range">the version range for a specific package</param>
/// <param name="range">the version range for a specific package, when no version range is specified, the dependency will be on "latest"</param>
public PackageDependency(string name, string? range = null)
{
this.Name = name;
this.Range = range;
this.Range = range ?? "latest";
}

/// <summary>
Expand All @@ -39,7 +39,14 @@ public static implicit operator PackageDependency(KeyValuePair<string, string?>
/// <param name="reference">a <see cref="string"/> defining a package name of a new package dependency</param>
public static implicit operator PackageDependency(string reference)
{
return new PackageDependency(reference, null); // latest
var splitDependency = reference.Split('@');
if (splitDependency.Length == 1)
return new PackageDependency(reference, null);
else
{
var versionDep = splitDependency[1];
return new PackageDependency(splitDependency[0], versionDep);
}
}

/// <summary>
Expand All @@ -48,8 +55,7 @@ public static implicit operator PackageDependency(string reference)
/// <returns>A <see cref="string"/> including the package name and the version</returns>
public override string ToString()
{
string? range = string.IsNullOrEmpty(Range) ? "(latest)" : Range;
return $"{Name} {range}";
return $"{Name} ({Range})";
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions Firely.Fhir.Packages/Source/FhirPackageSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,7 @@ static IEnumerable<PackageDependency> createDependencies(string[] dependencies)
{
foreach (var dep in dependencies)
{
var splitDependency = dep.Split('@');
if (splitDependency.Length == 1)
yield return new PackageDependency(dep, "latest");
else
{
var versionDep = splitDependency[1];
yield return new PackageDependency(splitDependency[0], versionDep);
}
yield return (PackageDependency)dep;
}
}
}
Expand Down

0 comments on commit 73d355d

Please sign in to comment.