From 233f2c884d0687f0b1d1ef68e32ef75484629e17 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Mon, 22 Aug 2022 11:03:27 -0400 Subject: [PATCH 1/5] remove inheritance and fix test --- src/code/InstallHelper.cs | 6 +++--- test/InstallPSResource.Tests.ps1 | 27 +++++++++++++++++++++------ test/SavePSResource.Tests.ps1 | 27 +++++++++++++++++++++------ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/code/InstallHelper.cs b/src/code/InstallHelper.cs index 75f6d54f2..cabfbeae1 100644 --- a/src/code/InstallHelper.cs +++ b/src/code/InstallHelper.cs @@ -27,7 +27,7 @@ namespace Microsoft.PowerShell.PowerShellGet.Cmdlets /// /// Install helper class /// - internal class InstallHelper : PSCmdlet + internal class InstallHelper { #region Members @@ -507,7 +507,7 @@ private List InstallPackage( _cmdletPassedIn, out ErrorRecord errorRecord)) { - ThrowTerminatingError(errorRecord); + _cmdletPassedIn.ThrowTerminatingError(errorRecord); } if (isModule) @@ -529,7 +529,7 @@ private List InstallPackage( manifestInfo: out Hashtable parsedMetadataHashtable, error: out Exception manifestReadError)) { - WriteError( + _cmdletPassedIn.WriteError( new ErrorRecord( exception: manifestReadError, errorId: "ManifestFileReadParseError", diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index bef8111ae..cf21a7872 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -461,14 +461,24 @@ Describe 'Test Install-PSResource for Module' { # Install module that is not authenticode signed # Should FAIL to install the module It "Install module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + try { + Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } + # Install 1.4.4.1 (with incorrect catalog file) # Should FAIL to install the module It "Install module with incorrect catalog file" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + try + { + Install-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } # Install script that is signed @@ -484,8 +494,13 @@ Describe 'Test Install-PSResource for Module' { # Install script that is not signed # Should throw It "Install script that is not signed" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + try + { + Install-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } } diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index 2d5e2e27f..9b19fd3db 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -257,15 +257,25 @@ Describe 'Test Save-PSResource for PSResources' { # Save module that is not authenticode signed # Should FAIL to save the module It "Save module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { - Save-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + try + { + Save-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } # Save 1.4.4.1 (with incorrect catalog file) # Should FAIL to save the module It "Save module with incorrect catalog file" -Skip:(!(Get-IsWindows)) { - Save-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + try + { + Save-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } # Save script that is signed @@ -282,8 +292,13 @@ Describe 'Test Save-PSResource for PSResources' { # Save script that is not signed # Should throw It "Save script that is not signed" -Skip:(!(Get-IsWindows)) { - Save-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + try + { + Save-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } <# From dc18766e937260b39c0e8a7797001dfcc6c17166 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Mon, 22 Aug 2022 11:22:35 -0400 Subject: [PATCH 2/5] fix code style --- test/InstallPSResource.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index cf21a7872..6ad4b94f1 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -461,7 +461,8 @@ Describe 'Test Install-PSResource for Module' { # Install module that is not authenticode signed # Should FAIL to install the module It "Install module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { - try { + try + { Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue } catch From c1e3b3f06a6a6c1233a2615c832f1764314a165f Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Mon, 22 Aug 2022 12:06:53 -0400 Subject: [PATCH 3/5] fix test for UpdatePSResource --- test/UpdatePSResource.Tests.ps1 | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/test/UpdatePSResource.Tests.ps1 b/test/UpdatePSResource.Tests.ps1 index 901a5a0bc..129825c1f 100644 --- a/test/UpdatePSResource.Tests.ps1 +++ b/test/UpdatePSResource.Tests.ps1 @@ -351,9 +351,14 @@ Describe 'Test Update-PSResource' { # Update to module 1.4.4.1 (with incorrect catalog file) # Should FAIL to update the module It "Update module with incorrect catalog file" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name $PackageManagement -Version "1.4.2" -Repository $PSGalleryName -TrustRepository - Update-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" + try + { + Install-PSResource -Name $PackageManagement -Version "1.4.2" -Repository $PSGalleryName -TrustRepository + Update-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" } # Update script that is signed @@ -370,8 +375,13 @@ Describe 'Test Update-PSResource' { # Update script that is not signed # Should throw It "Update script that is not signed" -Skip:(!(Get-IsWindows)) { - Install-PSResource -Name "TestTestScript" -Version "1.0" -Repository $PSGalleryName -TrustRepository - Update-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - $Error[0].FullyQualifiedErrorId | Should -be "InstallPackageFailed,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" + try + { + Install-PSResource -Name "TestTestScript" -Version "1.0" -Repository $PSGalleryName -TrustRepository + Update-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + } + catch + {} + $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" } } From 49cb40dc7eff5efb4196d4136cd0b994612c9b8c Mon Sep 17 00:00:00 2001 From: Alyssa Vu <49544763+alyssa1303@users.noreply.github.com> Date: Mon, 22 Aug 2022 12:39:38 -0400 Subject: [PATCH 4/5] Update test/InstallPSResource.Tests.ps1 Co-authored-by: Paul Higinbotham --- test/InstallPSResource.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index 6ad4b94f1..c14ff018d 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -463,7 +463,8 @@ Describe 'Test Install-PSResource for Module' { It "Install module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { try { - Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue + { Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId 'GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource' + } catch {} From 4aebaf2e4a0fff340a657a3bfb2ddbcd036460d4 Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Mon, 22 Aug 2022 13:28:18 -0400 Subject: [PATCH 5/5] use should throw instead of try/catch --- test/InstallPSResource.Tests.ps1 | 25 +++---------------------- test/SavePSResource.Tests.ps1 | 24 +++--------------------- test/UpdatePSResource.Tests.ps1 | 20 ++++---------------- 3 files changed, 10 insertions(+), 59 deletions(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index c14ff018d..b9ab10495 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -461,26 +461,13 @@ Describe 'Test Install-PSResource for Module' { # Install module that is not authenticode signed # Should FAIL to install the module It "Install module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { - try - { - { Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId 'GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource' - - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + { Install-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } # Install 1.4.4.1 (with incorrect catalog file) # Should FAIL to install the module It "Install module with incorrect catalog file" -Skip:(!(Get-IsWindows)) { - try - { - Install-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + { Install-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } # Install script that is signed @@ -496,13 +483,7 @@ Describe 'Test Install-PSResource for Module' { # Install script that is not signed # Should throw It "Install script that is not signed" -Skip:(!(Get-IsWindows)) { - try - { - Install-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" + { Install-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.InstallPSResource" } } diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index 9b19fd3db..3366cdec5 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -257,25 +257,13 @@ Describe 'Test Save-PSResource for PSResources' { # Save module that is not authenticode signed # Should FAIL to save the module It "Save module that is not authenticode signed" -Skip:(!(Get-IsWindows)) { - try - { - Save-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + { Save-PSResource -Name $testModuleName -Version "5.0.0" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir } | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } # Save 1.4.4.1 (with incorrect catalog file) # Should FAIL to save the module It "Save module with incorrect catalog file" -Skip:(!(Get-IsWindows)) { - try - { - Save-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + { Save-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir } | Should -Throw -ErrorId "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } # Save script that is signed @@ -292,13 +280,7 @@ Describe 'Test Save-PSResource for PSResources' { # Save script that is not signed # Should throw It "Save script that is not signed" -Skip:(!(Get-IsWindows)) { - try - { - Save-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + { Save-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } <# diff --git a/test/UpdatePSResource.Tests.ps1 b/test/UpdatePSResource.Tests.ps1 index 129825c1f..b0c45c17d 100644 --- a/test/UpdatePSResource.Tests.ps1 +++ b/test/UpdatePSResource.Tests.ps1 @@ -351,14 +351,8 @@ Describe 'Test Update-PSResource' { # Update to module 1.4.4.1 (with incorrect catalog file) # Should FAIL to update the module It "Update module with incorrect catalog file" -Skip:(!(Get-IsWindows)) { - try - { - Install-PSResource -Name $PackageManagement -Version "1.4.2" -Repository $PSGalleryName -TrustRepository - Update-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" + Install-PSResource -Name $PackageManagement -Version "1.4.2" -Repository $PSGalleryName -TrustRepository + { Update-PSResource -Name $PackageManagement -Version "1.4.4.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "TestFileCatalogError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" } # Update script that is signed @@ -375,13 +369,7 @@ Describe 'Test Update-PSResource' { # Update script that is not signed # Should throw It "Update script that is not signed" -Skip:(!(Get-IsWindows)) { - try - { - Install-PSResource -Name "TestTestScript" -Version "1.0" -Repository $PSGalleryName -TrustRepository - Update-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -ErrorAction SilentlyContinue - } - catch - {} - $Error[0].FullyQualifiedErrorId | Should -be "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" + Install-PSResource -Name "TestTestScript" -Version "1.0" -Repository $PSGalleryName -TrustRepository + { Update-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository } | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UpdatePSResource" } }