1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
+ using System . Text . RegularExpressions ;
4
5
using Microsoft . DotNet . Cli . Utils ;
6
+ using Newtonsoft . Json . Linq ;
5
7
6
8
namespace Microsoft . DotNet . Cli . New . IntegrationTests
7
9
{
@@ -59,9 +61,12 @@ public Task CanDisplayDetails_RemotePackage_OtherFeedWithVersion()
59
61
}
60
62
61
63
[ Fact ]
62
- public Task CanDisplayDetails_RemotePackage_OtherFeedNoVersion ( )
64
+ public async Task CanDisplayDetails_RemotePackage_OtherFeedNoVersion ( )
63
65
{
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 )
65
70
. WithCustomHive ( CreateTemporaryFolder ( folderName : "Home" ) )
66
71
. WithWorkingDirectory ( CreateTemporaryFolder ( ) )
67
72
. Execute ( ) ;
@@ -70,7 +75,9 @@ public Task CanDisplayDetails_RemotePackage_OtherFeedNoVersion()
70
75
. Should ( )
71
76
. Pass ( ) ;
72
77
73
- return Verify ( commandResult . StdOut ) ;
78
+ ExtractVersion ( commandResult . StdOut )
79
+ . Should ( )
80
+ . Be ( latestVersion ) ;
74
81
}
75
82
76
83
[ Fact ]
@@ -126,18 +133,21 @@ public Task CanDisplayDetails_InstalledPackage_NuGetFeed()
126
133
}
127
134
128
135
[ Fact ]
129
- public Task CanDisplayDetails_InstalledPackage_OtherFeed ( )
136
+ public async Task CanDisplayDetails_InstalledPackage_OtherFeed ( )
130
137
{
138
+ string packageName = "Microsoft.Azure.WebJobs.ItemTemplates" ;
139
+ string latestVersion = await GetLatestVersion ( packageName ) ;
140
+
131
141
string home = CreateTemporaryFolder ( folderName : "Home" ) ;
132
- new DotnetNewCommand ( _log , "install" , "NUnit3.DotNetNew.Template" )
142
+ new DotnetNewCommand ( _log , "install" , packageName )
133
143
. WithoutBuiltInTemplates ( ) . WithCustomHive ( home )
134
144
. WithWorkingDirectory ( CreateTemporaryFolder ( ) )
135
145
. Execute ( )
136
146
. Should ( )
137
147
. ExitWith ( 0 )
138
148
. And . NotHaveStdErr ( ) ;
139
149
140
- CommandResult commandResult = new DotnetNewCommand ( _log , "details" , "NUnit3.DotNetNew.Template" )
150
+ CommandResult commandResult = new DotnetNewCommand ( _log , "details" , packageName )
141
151
. WithCustomHive ( home ) . WithoutBuiltInTemplates ( )
142
152
. WithWorkingDirectory ( CreateTemporaryFolder ( ) )
143
153
. Execute ( ) ;
@@ -146,7 +156,9 @@ public Task CanDisplayDetails_InstalledPackage_OtherFeed()
146
156
. Should ( )
147
157
. Pass ( ) ;
148
158
149
- return Verify ( commandResult . StdOut ) ;
159
+ ExtractVersion ( commandResult . StdOut )
160
+ . Should ( )
161
+ . Be ( latestVersion ) ;
150
162
}
151
163
152
164
[ Fact ]
@@ -173,5 +185,32 @@ public Task CanDisplayDetails_InstalledPackage_FolderInstallation()
173
185
return Verify ( commandResult . StdOut )
174
186
. AddScrubber ( output => output . ScrubAndReplace ( basicFSharp , "%TEMPLATE FOLDER%" ) ) ;
175
187
}
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
+ }
176
215
}
177
216
}
0 commit comments