Skip to content

Commit 352bf9e

Browse files
Add tests
1 parent a823cad commit 352bf9e

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1698,9 +1698,11 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16981698

16991699
private string PrependMARPrefix(string packageName)
17001700
{
1701+
string prefix = string.IsNullOrEmpty(InternalHooks.MARPrefix) ? PSRepositoryInfo.MARPrefix : InternalHooks.MARPrefix;
1702+
17011703
// If the repostitory is MAR and its not a wildcard search, we need to prefix the package name with MAR prefix.
17021704
string updatedPackageName = Repository.IsMARRepository() && packageName.Trim() != "*"
1703-
? string.Concat(PSRepositoryInfo.MARPrefix, packageName)
1705+
? string.Concat(prefix, packageName)
17041706
: packageName;
17051707

17061708
return updatedPackageName;

src/code/InternalHooks.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System.Reflection;
5+
using Azure.Core;
56

67
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
78
{
@@ -15,6 +16,8 @@ public class InternalHooks
1516

1617
internal static string AllowedUri;
1718

19+
internal static string MARPrefix;
20+
1821
public static void SetTestHook(string property, object value)
1922
{
2023
var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic);

test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,21 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' {
228228
$res.Type.ToString() | Should -Be "Script"
229229
}
230230
}
231+
232+
Describe 'Test Find-PSResource for MAR Repository' -tags 'CI' {
233+
BeforeAll {
234+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", "azure-powershell/");
235+
Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry"
236+
}
237+
238+
AfterAll {
239+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", $null);
240+
Unregister-PSResourceRepository -Name "MAR"
241+
}
242+
243+
It "Should find resource given specific Name, Version null" {
244+
$res = Find-PSResource -Name "Az.Accounts" -Repository "MAR"
245+
$res.Name | Should -Be "Az.Accounts"
246+
$res.Version | Should -Be "3.0.4"
247+
}
248+
}

test/InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Describe 'Test Install-PSResource for ACR scenarios' -tags 'CI' {
137137

138138
It "Install resource with a dependency (should install both parent and dependency)" {
139139
Install-PSResource -Name $testModuleParentName -Repository $ACRRepoName -TrustRepository
140-
140+
141141
$parentPkg = Get-InstalledPSResource $testModuleParentName
142142
$parentPkg.Name | Should -Be $testModuleParentName
143143
$parentPkg.Version | Should -Be "1.0.0"
@@ -307,3 +307,28 @@ Describe 'Test Install-PSResource for V3Server scenarios' -tags 'ManualValidatio
307307
Set-PSResourceRepository PoshTestGallery -Trusted
308308
}
309309
}
310+
311+
Describe 'Test Install-PSResource for MAR Repository' -tags 'CI' {
312+
BeforeAll {
313+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", "azure-powershell/");
314+
Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry"
315+
}
316+
317+
AfterAll {
318+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", $null);
319+
Unregister-PSResourceRepository -Name "MAR"
320+
}
321+
322+
It "Should find resource given specific Name, Version null" {
323+
try {
324+
$pkg = Install-PSResource -Name "Az.Accounts" -Repository "MAR" -PassThru -TrustRepository -Reinstall
325+
$pkg.Name | Should -Be "Az.Accounts"
326+
$pkg.Version | Should -Be "3.0.4"
327+
}
328+
finally {
329+
if ($pkg) {
330+
Uninstall-PSResource -Name "Az.Accounts" -Version "3.0.4"
331+
}
332+
}
333+
}
334+
}

test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,24 @@ Describe "Test Publish-PSResource" -tags 'CI' {
512512
$results[0].Version | Should -Be $version
513513
}
514514
}
515+
516+
Describe 'Test Publish-PSResource for MAR Repository' -tags 'CI' {
517+
BeforeAll {
518+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", "azure-powershell/");
519+
Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry"
520+
}
521+
522+
AfterAll {
523+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", $null);
524+
Unregister-PSResourceRepository -Name "MAR"
525+
}
526+
527+
It "Should find resource given specific Name, Version null" {
528+
$fileName = "NonExistent.psd1"
529+
$modulePath = New-Item -Path "$TestDrive\NonExistent" -ItemType Directory -Force
530+
$psd1Path = Join-Path -Path $modulePath -ChildPath $fileName
531+
New-ModuleManifest -Path $psd1Path -ModuleVersion "1.0.0" -Description "NonExistent module"
532+
533+
{ Publish-PSResource -Path $modulePath -Repository "MAR" -ErrorAction Stop } | Should -Throw
534+
}
535+
}

0 commit comments

Comments
 (0)