Skip to content

Commit 4b69360

Browse files
author
annavied_microsoft
committed
add WriteWarnings property on ServerApiCalls class, ensure warning is written for Install and FindVersion scenarios as well
1 parent 964d4c6 commit 4b69360

File tree

10 files changed

+87
-9
lines changed

10 files changed

+87
-9
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
3131
#region Members
3232

3333
public override PSRepositoryInfo Repository { get; set; }
34+
internal override bool WriteWarnings { get; set; }
3435
public String Registry { get; set; }
3536
private readonly PSCmdlet _cmdletPassedIn;
3637
private HttpClient _sessionClient { get; set; }

src/code/InstallHelper.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ private List<PSResourceInfo> ProcessRepositories(
184184
{
185185
_cmdletPassedIn.WriteDebug("In InstallHelper::ProcessRepositories()");
186186
List<PSResourceInfo> allPkgsInstalled = new();
187+
bool containsWildcard = false;
187188
if (repository != null && repository.Length != 0)
188189
{
189190
// Write error and disregard repository entries containing wildcards.
@@ -199,7 +200,6 @@ private List<PSResourceInfo> ProcessRepositories(
199200

200201
// If repository entries includes wildcards and non-wildcard names, write terminating error
201202
// Ex: -Repository *Gallery, localRepo
202-
bool containsWildcard = false;
203203
bool containsNonWildcard = false;
204204
foreach (string repoName in repository)
205205
{
@@ -222,6 +222,10 @@ private List<PSResourceInfo> ProcessRepositories(
222222
_cmdletPassedIn));
223223
}
224224
}
225+
else
226+
{
227+
containsWildcard = true;
228+
}
225229

226230
// Get repositories to search.
227231
List<PSRepositoryInfo> repositoriesToSearch;
@@ -289,7 +293,8 @@ private List<PSResourceInfo> ProcessRepositories(
289293
// Set network credentials via passed in credentials, AzArtifacts CredentialProvider, or SecretManagement.
290294
_networkCredential = currentRepository.SetNetworkCredentials(_networkCredential, _cmdletPassedIn);
291295

292-
ServerApiCall currentServer = ServerFactory.GetServer(currentRepository, _cmdletPassedIn, _networkCredential);
296+
bool writeWarningsForRepo = containsWildcard;
297+
ServerApiCall currentServer = ServerFactory.GetServer(currentRepository, _cmdletPassedIn, _networkCredential, writeWarningsForRepo);
293298

294299
if (currentServer == null)
295300
{
@@ -537,7 +542,7 @@ private List<PSResourceInfo> InstallPackages(
537542
errRecord: out ErrorRecord errRecord);
538543

539544
// At this point parent package is installed to temp path.
540-
if (errRecord != null)
545+
if (errRecord != null && !currentServer.WriteWarnings)
541546
{
542547
if (errRecord.FullyQualifiedErrorId.Equals("PackageNotFound"))
543548
{

src/code/LocalServerApiCalls.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ internal class LocalServerAPICalls : ServerApiCall
2424
private readonly PSCmdlet _cmdletPassedIn;
2525
private readonly FindResponseType _localServerFindResponseType = FindResponseType.ResponseHashtable;
2626
private readonly string _fileTypeKey = "filetype";
27-
private readonly bool _writeWarnings = false;
27+
internal override bool WriteWarnings { get; set; }
2828

2929
#endregion
3030

3131
#region Constructor
3232

33-
public LocalServerAPICalls (PSRepositoryInfo repository, PSCmdlet cmdletPassedIn, NetworkCredential networkCredential, bool writeWarnings = false) : base (repository, networkCredential)
33+
public LocalServerAPICalls (PSRepositoryInfo repository, PSCmdlet cmdletPassedIn, NetworkCredential networkCredential, bool writeWarnings = false) : base (repository, networkCredential, writeWarnings)
3434
{
3535
this.Repository = repository;
36+
this.WriteWarnings = writeWarnings;
3637
_cmdletPassedIn = cmdletPassedIn;
37-
_writeWarnings = writeWarnings;
3838
}
3939

4040
#endregion
@@ -272,9 +272,9 @@ private FindResults FindNameHelper(string packageName, string[] tags, bool inclu
272272
}
273273
catch (Exception e)
274274
{
275-
if (_writeWarnings)
275+
if (WriteWarnings)
276276
{
277-
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
277+
_cmdletPassedIn.WriteWarning($"Unable to resolve repository '{Repository.Name}' with source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
278278
}
279279

280280
errRecord = new ErrorRecord(
@@ -417,6 +417,11 @@ private FindResults FindVersionHelper(string packageName, string version, string
417417
}
418418
catch (Exception e)
419419
{
420+
if (WriteWarnings)
421+
{
422+
_cmdletPassedIn.WriteWarning($"Unable to resolve repository '{Repository.Name}' with source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
423+
}
424+
420425
errRecord = new ErrorRecord(
421426
exception: e,
422427
"FileAccessFailure",
@@ -653,6 +658,11 @@ private Stream InstallVersion(string packageName, string version, out ErrorRecor
653658
}
654659
catch (Exception e)
655660
{
661+
if (WriteWarnings)
662+
{
663+
_cmdletPassedIn.WriteWarning($"Unable to resolve repository '{Repository.Name}' with source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
664+
}
665+
656666
errRecord = new ErrorRecord(
657667
exception: e,
658668
"FileAccessFailure",

src/code/NuGetServerAPICalls.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal class NuGetServerAPICalls : ServerApiCall
2121
#region Members
2222

2323
public override PSRepositoryInfo Repository { get; set; }
24+
internal override bool WriteWarnings { get; set; }
2425
private readonly PSCmdlet _cmdletPassedIn;
2526
private HttpClient _sessionClient { get; set; }
2627
private static readonly Hashtable[] emptyHashResponses = new Hashtable[]{};

src/code/ServerApiCall.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ internal abstract class ServerApiCall : IServerAPICalls
1818
#region Members
1919

2020
public abstract PSRepositoryInfo Repository { get; set; }
21+
internal abstract bool WriteWarnings { get; set; }
2122
private HttpClient _sessionClient { get; set; }
2223

2324
#endregion
2425

2526
#region Constructor
2627

28+
public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCredential, bool writeWarnings)
29+
: this(repository, networkCredential)
30+
{
31+
this.WriteWarnings = writeWarnings;
32+
}
33+
34+
2735
public ServerApiCall(PSRepositoryInfo repository, NetworkCredential networkCredential)
2836
{
2937
this.Repository = repository;

src/code/V2ServerAPICalls.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ internal class V2ServerAPICalls : ServerApiCall
3737
#region Members
3838

3939
public override PSRepositoryInfo Repository { get; set; }
40+
internal override bool WriteWarnings { get; set; }
4041
private readonly PSCmdlet _cmdletPassedIn;
4142
private HttpClient _sessionClient { get; set; }
4243
private static readonly Hashtable[] emptyHashResponses = new Hashtable[]{};

src/code/V3ServerAPICalls.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal class V3ServerAPICalls : ServerApiCall
2121
{
2222
#region Members
2323
public override PSRepositoryInfo Repository { get; set; }
24+
internal override bool WriteWarnings { get; set; }
2425
private readonly PSCmdlet _cmdletPassedIn;
2526
private HttpClient _sessionClient { get; set; }
2627
private bool _isNuGetRepo { get; set; }

test/FindPSResourceTests/FindPSResourceLocal.Tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
1212
BeforeAll{
1313
$localRepo = "psgettestlocal"
1414
$localUNCRepo = 'psgettestlocal3'
15+
$localPrivateRepo = "psgettestlocal5"
1516
$testModuleName = "test_local_mod"
1617
$testModuleName2 = "test_local_mod2"
1718
$testModuleName3 = "Test_Local_Mod3"
@@ -347,4 +348,24 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
347348
$res = Find-PSResource -Name 'Az.KeyVault' -Repository $localRepo
348349
$res.Version | Should -Be "6.3.1"
349350
}
351+
352+
It "Find should not silently fail if network connection to local private repository cannot be established and remainder repositories should be searched"
353+
{
354+
$privateRepo = Get-PSResourceRepository $localPrivateRepo
355+
$res = Find-PSResource -Name $testModuleName -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue
356+
$WarningVar | Should -Not -BeNullOrEmpty
357+
$WarningVar[0] | Should -Contain $privateRepo.Uri.LocalPath
358+
$res.Name | Should -Contain $testModuleName
359+
$res.Version | Should -Be "1.0.0"
360+
}
361+
362+
It "Find should not silently fail if network connection to local private repository cannot be established and package version was provided and remainder repositories should be searched"
363+
{
364+
$privateRepo = Get-PSResourceRepository $localPrivateRepo
365+
$res = Find-PSResource -Name $testModuleName -Version "1.0.0" -TrustRepository -WarningVariable WarningVar -WarningAction SilentlyContinue
366+
$WarningVar | Should -Not -BeNullOrEmpty
367+
$WarningVar[0] | Should -Contain $privateRepo.Uri.LocalPath
368+
$res.Name | Should -Contain $testModuleName
369+
$res.Version | Should -Be "1.0.0"
370+
}
350371
}

test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
1616
BeforeAll {
1717
$localRepo = "psgettestlocal"
1818
$localUNCRepo = "psgettestlocal3"
19+
$localPrivateRepo = "psgettestlocal5"
1920
$localNupkgRepo = "localNupkgRepo"
2021
$testModuleName = "test_local_mod"
2122
$testModuleName2 = "test_local_mod2"
@@ -296,4 +297,24 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
296297
$pkg.Name | Should -Be $nupkgName
297298
$pkg.Version | Should -Be $nupkgVersion
298299
}
300+
301+
It "Install should not silently fail if network connection to local private repository cannot be established and remainder repositories should be searched"
302+
{
303+
$privateRepo = Get-PSResourceRepository $localPrivateRepo
304+
$res = Install-PSResource -Name $testModuleName -TrustRepository -PassThru -WarningVariable WarningVar -WarningAction SilentlyContinue
305+
$WarningVar | Should -Not -BeNullOrEmpty
306+
$WarningVar[0] | Should -Contain $privateRepo.Uri.LocalPath
307+
$res.Name | Should -Contain $testModuleName
308+
$res.Version | Should -Be "1.0.0"
309+
}
310+
311+
It "Install should not silently fail if network connection to local private repository cannot be established and package version was provided and remainder repositories should be searched"
312+
{
313+
$privateRepo = Get-PSResourceRepository $localPrivateRepo
314+
$res = Install-PSResource -Name $testModuleName -Version "1.0.0" -TrustRepository -PassThru -WarningVariable WarningVar -WarningAction SilentlyContinue
315+
$WarningVar | Should -Not -BeNullOrEmpty
316+
$WarningVar[0] | Should -Contain $privateRepo.Uri.LocalPath
317+
$res.Name | Should -Contain $testModuleName
318+
$res.Version | Should -Be "1.0.0"
319+
}
299320
}

test/PSGetTestUtils.psm1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,16 @@ function Register-LocalRepos {
270270
}
271271
Register-PSResourceRepository @localRepoParams2
272272

273-
Write-Verbose "registered psgettestlocal, psgettestlocal2, psgettestlocal3, psgettestlocal4"
273+
$path4 = "\\localhost\PSRepoLocal"
274+
$localRepoParams2 = @{
275+
Name = "psgettestlocal5"
276+
Uri = $path4
277+
Priority = 40
278+
Trusted = $false
279+
}
280+
Register-PSResourceRepository @localRepoParams2
281+
282+
Write-Verbose "registered psgettestlocal, psgettestlocal2, psgettestlocal3, psgettestlocal4, psgettestlocal5"
274283
}
275284

276285
function Register-LocalTestNupkgsRepo {

0 commit comments

Comments
 (0)