Skip to content

Commit aa0f779

Browse files
author
Jason Zhai
committed
Ensure test checks for latest package version
1 parent acce94c commit aa0f779

3 files changed

+46
-31
lines changed

test/dotnet-new.Tests/Approvals/DotnetNewDetailsTest.CanDisplayDetails_InstalledPackage_OtherFeed.verified.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/dotnet-new.Tests/Approvals/DotnetNewDetailsTest.CanDisplayDetails_RemotePackage_OtherFeedNoVersion.verified.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/dotnet-new.Tests/DotnetNewDetailsTest.Approval.cs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Text.RegularExpressions;
45
using Microsoft.DotNet.Cli.Utils;
6+
using Newtonsoft.Json.Linq;
57

68
namespace Microsoft.DotNet.Cli.New.IntegrationTests
79
{
@@ -59,9 +61,12 @@ public Task CanDisplayDetails_RemotePackage_OtherFeedWithVersion()
5961
}
6062

6163
[Fact]
62-
public Task CanDisplayDetails_RemotePackage_OtherFeedNoVersion()
64+
public async Task CanDisplayDetails_RemotePackage_OtherFeedNoVersion()
6365
{
64-
CommandResult commandResult = new DotnetNewCommand(_log, "details", "NUnit3.DotNetNew.Template")
66+
string packageName = "Microsoft.Azure.WebJobs.ItemTemplates";
67+
string latestVersion = await GetLatestVersion(packageName);
68+
69+
CommandResult commandResult = new DotnetNewCommand(_log, "details", packageName)
6570
.WithCustomHive(CreateTemporaryFolder(folderName: "Home"))
6671
.WithWorkingDirectory(CreateTemporaryFolder())
6772
.Execute();
@@ -70,7 +75,9 @@ public Task CanDisplayDetails_RemotePackage_OtherFeedNoVersion()
7075
.Should()
7176
.Pass();
7277

73-
return Verify(commandResult.StdOut);
78+
ExtractVersion(commandResult.StdOut)
79+
.Should()
80+
.Be(latestVersion);
7481
}
7582

7683
[Fact]
@@ -126,18 +133,21 @@ public Task CanDisplayDetails_InstalledPackage_NuGetFeed()
126133
}
127134

128135
[Fact]
129-
public Task CanDisplayDetails_InstalledPackage_OtherFeed()
136+
public async Task CanDisplayDetails_InstalledPackage_OtherFeed()
130137
{
138+
string packageName = "Microsoft.Azure.WebJobs.ItemTemplates";
139+
string latestVersion = await GetLatestVersion(packageName);
140+
131141
string home = CreateTemporaryFolder(folderName: "Home");
132-
new DotnetNewCommand(_log, "install", "NUnit3.DotNetNew.Template")
142+
new DotnetNewCommand(_log, "install", packageName)
133143
.WithoutBuiltInTemplates().WithCustomHive(home)
134144
.WithWorkingDirectory(CreateTemporaryFolder())
135145
.Execute()
136146
.Should()
137147
.ExitWith(0)
138148
.And.NotHaveStdErr();
139149

140-
CommandResult commandResult = new DotnetNewCommand(_log, "details", "NUnit3.DotNetNew.Template")
150+
CommandResult commandResult = new DotnetNewCommand(_log, "details", packageName)
141151
.WithCustomHive(home).WithoutBuiltInTemplates()
142152
.WithWorkingDirectory(CreateTemporaryFolder())
143153
.Execute();
@@ -146,7 +156,9 @@ public Task CanDisplayDetails_InstalledPackage_OtherFeed()
146156
.Should()
147157
.Pass();
148158

149-
return Verify(commandResult.StdOut);
159+
ExtractVersion(commandResult.StdOut)
160+
.Should()
161+
.Be(latestVersion);
150162
}
151163

152164
[Fact]
@@ -173,5 +185,32 @@ public Task CanDisplayDetails_InstalledPackage_FolderInstallation()
173185
return Verify(commandResult.StdOut)
174186
.AddScrubber(output => output.ScrubAndReplace(basicFSharp, "%TEMPLATE FOLDER%"));
175187
}
188+
189+
private async Task<string> GetLatestVersion(string packageName)
190+
{
191+
using (HttpClient client = new HttpClient())
192+
{
193+
string json = await client.GetStringAsync($"https://api.nuget.org/v3-flatcontainer/{packageName}/index.json");
194+
JObject obj = JObject.Parse(json);
195+
196+
var versions = obj["versions"]?.ToObject<List<string>>();
197+
if (versions == null || versions.Count == 0)
198+
{
199+
throw new Exception("No versions found.");
200+
}
201+
202+
return versions.Last();
203+
}
204+
}
205+
206+
private string ExtractVersion(string stdOut)
207+
{
208+
var match = Regex.Match(stdOut, @"Package version:\s*(\S+)");
209+
if (match.Success)
210+
{
211+
return match.Groups[1].Value;
212+
}
213+
throw new Exception("Version not found in the output.");
214+
}
176215
}
177216
}

0 commit comments

Comments
 (0)