Skip to content

Commit

Permalink
release-version-sane: refactor test so failing asserts include usable…
Browse files Browse the repository at this point in the history
… information. (#342)
  • Loading branch information
tmds authored Feb 16, 2024
1 parent 1449dee commit db8cb3c
Showing 1 changed file with 34 additions and 40 deletions.
74 changes: 34 additions & 40 deletions release-version-sane/VersionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,74 +14,68 @@ public class VersionTest
[Fact]
public async Task VersionIsSane()
{
var upstream = new UpstreamRelease();
var currentRuntimeVersion = GetRuntimeVersion();
var currentSdkVersion = GetSdkVersion();
// This test is meant for release pipelines and verifies the version being built
// either matches the upstream 'major.minor.patch' or 'major.minor.(patch + 1)'.

string runtimeVersionRaw = GetRuntimeVersion();
string sdkVersionRaw = GetSdkVersion();
Version runtimeVersion = Normalize(runtimeVersionRaw);
Version sdkVersion = Normalize(sdkVersionRaw);

string majorMinor = $"{currentRuntimeVersion.Major}.{currentRuntimeVersion.Minor}";
string majorMinor = $"{runtimeVersion.Major}.{runtimeVersion.Minor}";
var upstream = new UpstreamRelease();
(List<string> publicSdkVersionsRaw, string publicRuntimeVersionRaw) = await upstream.GetLatestRelease(new HttpClient(), majorMinor);
List<Version> publicSdkVersions = publicSdkVersionsRaw.Select(v => Normalize(v)).ToList();
Version publicRuntimeVersion = Normalize(publicRuntimeVersionRaw);

bool currentVersionNewerThanPublic = false;
if ((publicRuntimeVersion != currentRuntimeVersion) && (currentRuntimeVersion.Build > 0))
Version publicRuntimeVersionNextPatch = new Version(publicRuntimeVersion.Major,
publicRuntimeVersion.Minor,
publicRuntimeVersion.Build + 1);
bool matchesUpstream = runtimeVersion.Equals(publicRuntimeVersion);
bool matchesUpstreamNext = runtimeVersion.Equals(publicRuntimeVersionNextPatch);
Version expectedPublicSdkVersion = null;
if (matchesUpstream)
{
currentRuntimeVersion = new Version(currentRuntimeVersion.Major,
currentRuntimeVersion.Minor,
currentRuntimeVersion.Build - 1);
Assert.Equal(currentRuntimeVersion, publicRuntimeVersion);
currentVersionNewerThanPublic = true;
expectedPublicSdkVersion = sdkVersion;
}

if (currentVersionNewerThanPublic)
else if (matchesUpstreamNext)
{
currentSdkVersion = new Version(currentSdkVersion.Major,
currentSdkVersion.Minor,
currentSdkVersion.Build - 1);
expectedPublicSdkVersion = new Version(sdkVersion.Major,
sdkVersion.Minor,
sdkVersion.Build - 1);
}

bool sdkMatched = false;
foreach (var sdk in publicSdkVersions)
{
if (sdk == currentSdkVersion)
{
sdkMatched = true;
break;
}
}

Assert.True(sdkMatched);
Assert.True(matchesUpstream || matchesUpstreamNext, $"{runtimeVersionRaw} is not expected with public version {publicRuntimeVersionRaw}");
Assert.NotNull(expectedPublicSdkVersion);
Assert.Contains(expectedPublicSdkVersion, publicSdkVersions);
}

private Version GetRuntimeVersion()
private string GetRuntimeVersion()
{
int exitCode = RunProcessAndGetOutput(new string[] { "dotnet" , "--list-runtimes" }, out string result);
if (exitCode != 0)
{
return null;
}

return Normalize(result
.Split(Environment.NewLine)
.Where(line => line.StartsWith("Microsoft.NETCore.App "))
.Select(line => line.Split(' ')[1])
.First());

return result.Split(Environment.NewLine)
.Where(line => line.StartsWith("Microsoft.NETCore.App "))
.Select(line => line.Split(' ')[1])
.First();
}

private Version GetSdkVersion()
private string GetSdkVersion()
{
int exitCode = RunProcessAndGetOutput(new string[] { "dotnet" , "--list-sdks" }, out string result);
if (exitCode != 0)
{
return null;
}

return Normalize(result
.Split(Environment.NewLine)
.Select(line => line.Split(' ')[0])
.First());

return result
.Split(Environment.NewLine)
.Select(line => line.Split(' ')[0])
.First();
}


Expand Down

0 comments on commit db8cb3c

Please sign in to comment.