From 0450de8e84f1ecb4517bc68f86dab64c16d3f61b Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Fri, 26 Aug 2022 13:44:03 -0400 Subject: [PATCH 1/2] change path handle --- help/Save-PSResource.md | 5 ++--- src/code/SavePSResource.cs | 22 +++++++--------------- test/SavePSResource.Tests.ps1 | 8 +++++++- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/help/Save-PSResource.md b/help/Save-PSResource.md index e98cf87b3..162f0011c 100644 --- a/help/Save-PSResource.md +++ b/help/Save-PSResource.md @@ -192,15 +192,14 @@ Accept wildcard characters: False ### -Path -Specifies the path to save the resource to. If no path is provided, the resource is saved to the -current location. +Specifies the path to save the resource to. ```yaml Type: System.String Parameter Sets: (All) Aliases: -Required: False +Required: True Position: Named Default value: None Accept pipeline input: False diff --git a/src/code/SavePSResource.cs b/src/code/SavePSResource.cs index 9c92a261f..0c33b3a94 100644 --- a/src/code/SavePSResource.cs +++ b/src/code/SavePSResource.cs @@ -81,7 +81,7 @@ public sealed class SavePSResource : PSCmdlet /// /// The destination where the resource is to be installed. Works for all resource types. /// - [Parameter] + [Parameter(Mandatory = true)] [ValidateNotNullOrEmpty] public string Path { @@ -90,21 +90,13 @@ public string Path set { - string resolvedPath = string.Empty; - if (string.IsNullOrEmpty(value)) - { - // If the user does not specify a path to save to, use the user's current working directory - resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(SessionState.Path.CurrentFileSystemLocation.Path).First().Path; - } - else { - resolvedPath = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; - } + if (WildcardPattern.ContainsWildcardCharacters(value)) + { + throw new PSArgumentException("Wildcard characters are not allowed in the path."); + } - // Path where resource is saved must be a directory - if (Directory.Exists(resolvedPath)) - { - _path = resolvedPath; - } + // This will throw if path cannot be resolved + _path = SessionState.Path.GetResolvedPSPathFromPSPath(value).First().Path; } } private string _path; diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index 3366cdec5..acd1162f1 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -280,7 +280,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 } | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + { Save-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir} | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" + } + + # Save module without specifying -Path + # Should FAIL to save the module + It "Save module without specifying -Path" { + {Save-PSResource -Name $testModuleName -Repository $PSGalleryName } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } <# From d53a330fa8cffb315cab2a2b7c4bb17245f807be Mon Sep 17 00:00:00 2001 From: Alyssa Vu Date: Tue, 30 Aug 2022 15:06:36 -0400 Subject: [PATCH 2/2] delete test --- test/SavePSResource.Tests.ps1 | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/SavePSResource.Tests.ps1 b/test/SavePSResource.Tests.ps1 index acd1162f1..d4300b66e 100644 --- a/test/SavePSResource.Tests.ps1 +++ b/test/SavePSResource.Tests.ps1 @@ -283,12 +283,6 @@ Describe 'Test Save-PSResource for PSResources' { { Save-PSResource -Name "TestTestScript" -Version "1.3.1.1" -AuthenticodeCheck -Repository $PSGalleryName -TrustRepository -Path $SaveDir} | Should -Throw -ErrorId "GetAuthenticodeSignatureError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" } - # Save module without specifying -Path - # Should FAIL to save the module - It "Save module without specifying -Path" { - {Save-PSResource -Name $testModuleName -Repository $PSGalleryName } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PowerShellGet.Cmdlets.SavePSResource" - } - <# # Tests should not write to module directory It "Save specific module resource by name if no -Path param is specifed" {