Skip to content

Commit d081939

Browse files
committed
Add better error handling for -Path in Publish-PSResource
1 parent 311f65d commit d081939

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/code/PublishPSResource.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ protected override void BeginProcessing()
167167
ThrowTerminatingError(
168168
new ErrorRecord(
169169
new ArgumentException(
170-
"The path to the resource to publish is not in the correct format, point to a path or file of the module or script to publish."),
171-
"InvalidSourcePath",
170+
"The path to the resource to publish is not in the correct format or does not exist. Please provide the path of the root module or the path to the .psd1."),
171+
"InvalidPublishPath",
172172
ErrorCategory.InvalidArgument,
173173
this));
174174
}
@@ -187,6 +187,15 @@ protected override void BeginProcessing()
187187
pathToScriptFileToPublish = resolvedPath;
188188
resourceType = ResourceType.Script;
189189
}
190+
else {
191+
ThrowTerminatingError(
192+
new ErrorRecord(
193+
new ArgumentException(
194+
$"The publish path provided, '{resolvedPath}', is not a valid. Please provide a path to the root module or path to the .psd1."),
195+
"InvalidPublishPath",
196+
ErrorCategory.InvalidArgument,
197+
this));
198+
}
190199

191200
if (!String.IsNullOrEmpty(DestinationPath))
192201
{

test/PublishPSResourceTests/PublishPSResource.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,12 @@ Describe "Test Publish-PSResource" -tags 'CI' {
534534

535535
{Publish-PSResource -Path $incorrectdepmoduleversion -ErrorAction Stop} | Should -Throw -ErrorId "InvalidModuleManifest,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
536536
}
537+
538+
It "Publish a module with using an invalid file path (path to .psm1), should throw" {
539+
$fileName = "$script:PublishModuleName.psm1"
540+
$psm1Path = Join-Path -Path $script:PublishModuleBase -ChildPath $fileName
541+
$null = New-Item -Path $psm1Path -ItemType File -Force
542+
543+
{Publish-PSResource -Path $psm1Path -Repository $testRepository2 -ErrorAction Stop} | Should -Throw -ErrorId "InvalidPublishPath,Microsoft.PowerShell.PSResourceGet.Cmdlets.PublishPSResource"
544+
}
537545
}

0 commit comments

Comments
 (0)