Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit e3ef63b

Browse files
authored
Merge pull request #331 from X-Guardian/Test-PublishMetaData-Guid-Update
Test-PublishMetadata: Add Processing for InvalidGUID Error from Test-ScriptFileInfo
2 parents c29e314 + abc03e3 commit e3ef63b

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
to the relevant DSC resource GitHub Wiki ([issue #142](https://github.com/PowerShell/DscResource.Tests/issues/142)).
5757
- Update New-DscResourcePowerShellHelp to output the PowerShell help files to
5858
the resource specific path and fix the example processing.
59+
- Added processing to `Test-PublishMetaData` for the InvalidGUID error from Test-ScriptFileInfo
60+
([issue #330](https://github.com/PowerShell/DscResource.Tests/issues/330)).
5961

6062
## 0.3.0.0
6163

DscResource.GalleryDeploy/DscResource.GalleryDeploy.psm1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,16 @@ function Test-PublishMetadata
289289
$skipWarningMessage, ($script:localizedData.MissingRequiredMetadataProperties -f $errorMessage))
290290
}
291291

292+
'InvalidGuid,Test-ScriptFileInfo'
293+
{
294+
Write-Warning -Message ('{0} {1}' -f `
295+
$skipWarningMessage, ($script:localizedData.InvalidGuid -f $errorMessage))
296+
}
297+
292298
default
293299
{
294300
# If the error is not recognized then throw.
295-
throw $_
301+
throw ($script:localizedData.TestScriptFileInfoError -f $Path, $_)
296302
}
297303
}
298304
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
ConvertFrom-StringData @'
22
# English strings
3-
CannotPublish = Cannot publish to PowerShell Gallery.
4-
FoundApiKey = Found API key to use for publishing to the PowerShell Gallery.
5-
MissingApiKey = Missing API key in $env:gallery_api.
6-
EvaluatingExamples = Evaluating examples that end with 'Config' if they need publishing.
7-
NotOnMasterBranch = Not running against the branch 'master'. Will run Publish-Script with parameter WhatIf.
8-
MissingExampleValidationOptIn = Examples will not publish unless repository has opt-in for example validation. To opt-in, create a '.MetaTestOptIn.json' at the root of the repo in the following format: ["{0}"]
9-
SkipPublish = Skipping publishing example '{0}'.
10-
ScriptParseError = The example could not be read, for example if there is a required module missing when the script should be parsed, or general script parsing errors. General errors should have been caught with the common example validation test. Error: {0}
11-
MissingMetadata = The example has not opt-in by adding script metadata to the example. Error: {0}
3+
CannotPublish = Cannot publish to PowerShell Gallery.
4+
FoundApiKey = Found API key to use for publishing to the PowerShell Gallery.
5+
MissingApiKey = Missing API key in $env:gallery_api.
6+
EvaluatingExamples = Evaluating examples that end with 'Config' if they need publishing.
7+
NotOnMasterBranch = Not running against the branch 'master'. Will run Publish-Script with parameter WhatIf.
8+
MissingExampleValidationOptIn = Examples will not publish unless repository has opt-in for example validation. To opt-in, create a '.MetaTestOptIn.json' at the root of the repo in the following format: ["{0}"]
9+
SkipPublish = Skipping publishing example '{0}'.
10+
TestScriptFileInfoError = Error testing example '{0}'. {1}.
11+
ScriptParseError = The example could not be read, for example if there is a required module missing when the script should be parsed, or general script parsing errors. General errors should have been caught with the common example validation test. Error: {0}
12+
MissingMetadata = The example has not opt-in by adding script metadata to the example. Error: {0}
1213
MissingRequiredMetadataProperties = The example is missing needed metadata properties. This can also happen if code is checked out with just LF instead of CRLF (make sure core.autocrlf is set to true). Error: {0}
13-
ConfigurationNameMismatch = The configuration name and the file name are not the same, or the name does not contain only letters, numbers, and underscores. The name must also start with a letter, and it must end with a letter or a number.
14-
AddingExampleToBePublished = Adding the example '{0}' to the list to be published.
15-
DuplicateGuid = Skipping examples that was found having the same GUID. Duplicate examples: '{0}'.
16-
ExampleIsAlreadyPublished = The example '{0}' is already published (same version).
17-
PublishExample = The example '{0}' with version '{1}' is being published as '{2}'.
14+
InvalidGuid = The example has an invalid GUID. The format must be [XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX] where X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F). Error: {0}
15+
ConfigurationNameMismatch = The configuration name and the file name are not the same, or the name does not contain only letters, numbers, and underscores. The name must also start with a letter, and it must end with a letter or a number.
16+
AddingExampleToBePublished = Adding the example '{0}' to the list to be published.
17+
DuplicateGuid = Skipping examples that was found having the same GUID. Duplicate examples: '{0}'.
18+
ExampleIsAlreadyPublished = The example '{0}' is already published (same version).
19+
PublishExample = The example '{0}' with version '{1}' is being published as '{2}'.
1820
'@

Tests/Unit/DscResource.GalleryDeploy.Tests.ps1

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,37 @@ InModuleScope -ModuleName 'DscResource.GalleryDeploy' {
601601
}
602602
}
603603

604+
Context 'When a script file has an invalid GUID in its metadata' {
605+
BeforeAll {
606+
Mock -CommandName Write-Warning
607+
608+
$errorMessage = 'Cannot convert value ''a18e0a9-2a4b-4406-939e-ac2bb7b6e917'' to type ''System.Guid'''
609+
610+
Mock -CommandName Test-ScriptFileInfo -MockWith {
611+
$getInvalidArgumentRecordParameters = @{
612+
Message = $errorMessage
613+
# This is the FullyQualifiedErrorId
614+
ArgumentName = 'InvalidGuid,Test-ScriptFileInfo'
615+
}
616+
617+
throw Get-InvalidArgumentRecord @getInvalidArgumentRecordParameters
618+
}
619+
}
620+
621+
It 'Should call the correct mocks and return the correct warning message' {
622+
{ Test-PublishMetadata -Path $TestDrive } | Should -Not -Throw
623+
624+
Assert-MockCalled -CommandName Test-ScriptFileInfo -Exactly -Times 1 -Scope It
625+
626+
$warningMessage = $script:localizedData.InvalidGuid -f $errorMessage
627+
628+
Assert-MockCalled -CommandName Write-Warning -ParameterFilter {
629+
$Message -match ($script:localizedData.SkipPublish -f ($TestDrive -replace '\\','\\')) `
630+
-and $Message -match [Regex]::Escape($warningMessage)
631+
} -Exactly -Times 1 -Scope It
632+
}
633+
}
634+
604635
Context 'When cmdlet Test-ScriptFileInfo throws an unknown error' {
605636
BeforeAll {
606637
$throwMessage = 'Unknown error'
@@ -612,7 +643,8 @@ InModuleScope -ModuleName 'DscResource.GalleryDeploy' {
612643
}
613644

614645
It 'Should throw the correct error message' {
615-
{ Test-PublishMetadata -Path $TestDrive } | Should -Throw $throwMessage
646+
$errorMessage = $script:localizedData.TestScriptFileInfoError -f $TestDrive, $throwMessage
647+
{ Test-PublishMetadata -Path $TestDrive } | Should -Throw $errorMessage
616648

617649
Assert-MockCalled -CommandName Test-ScriptFileInfo -Exactly -Times 1 -Scope It
618650
}

0 commit comments

Comments
 (0)