-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added the function `Assert-Module`. - Updated the localized strings to have the unique id according to style guideline.
- Loading branch information
Showing
7 changed files
with
101 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<# | ||
.SYNOPSIS | ||
Assert if the specific module is available to be imported. | ||
.PARAMETER ModuleName | ||
Specifies the name of the module to assert. | ||
.EXAMPLE | ||
Assert-Module -ModuleName 'DhcpServer' | ||
This asserts that the module DhcpServer is available on the system. | ||
#> | ||
function Assert-Module | ||
{ | ||
[CmdletBinding()] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[System.String] | ||
$ModuleName | ||
) | ||
|
||
if (-not (Get-Module -Name $ModuleName -ListAvailable)) | ||
{ | ||
$errorMessage = $script:localizedData.ModuleNotFound -f $ModuleName | ||
New-ObjectNotFoundException -Message $errorMessage | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
# Localized English (en-US) strings. | ||
|
||
ConvertFrom-StringData @' | ||
# English strings | ||
PropertyTypeInvalidForDesiredValues = Property 'DesiredValues' must be either a [System.Collections.Hashtable], [CimInstance] or [PSBoundParametersDictionary]. The type detected was {0}. | ||
PropertyTypeInvalidForValuesToCheck = If 'DesiredValues' is a CimInstance, then property 'ValuesToCheck' must contain a value. | ||
PropertyValidationError = Expected to find an array value for property {0} in the current values, but it was either not present or was null. This has caused the test method to return false. | ||
PropertiesDoesNotMatch = Found an array for property {0} in the current values, but this array does not match the desired state. Details of the changes are below. | ||
PropertyThatDoesNotMatch = {0} - {1} | ||
ValueOfTypeDoesNotMatch = {0} value for property {1} does not match. Current state is '{2}' and desired state is '{3}'. | ||
UnableToCompareProperty = Unable to compare property {0} as the type {1} is not handled by the Test-DscParameterState cmdlet. | ||
TestIsNanoServerOperatingSystemSku = OperatingSystemSKU {0} was returned by Win32_OperatingSystem when detecting if operating system is Nano Server. | ||
PropertyTypeInvalidForDesiredValues = Property 'DesiredValues' must be either a [System.Collections.Hashtable], [CimInstance] or [PSBoundParametersDictionary]. The type detected was {0}. (DRC0001) | ||
PropertyTypeInvalidForValuesToCheck = If 'DesiredValues' is a CimInstance, then property 'ValuesToCheck' must contain a value. (DRC0002) | ||
PropertyValidationError = Expected to find an array value for property {0} in the current values, but it was either not present or was null. This has caused the test method to return false. (DRC0003) | ||
PropertiesDoesNotMatch = Found an array for property {0} in the current values, but this array does not match the desired state. Details of the changes are below. (DRC0004) | ||
PropertyThatDoesNotMatch = {0} - {1} (DRC0005) | ||
ValueOfTypeDoesNotMatch = {0} value for property {1} does not match. Current state is '{2}' and desired state is '{3}'. (DRC0006) | ||
UnableToCompareProperty = Unable to compare property {0} as the type {1} is not handled by the Test-DscParameterState cmdlet. (DRC0007) | ||
TestIsNanoServerOperatingSystemSku = OperatingSystemSKU {0} was returned by Win32_OperatingSystem when detecting if operating system is Nano Server. (DRC0008) | ||
ModuleNotFound = Please ensure that the PowerShell module '{0}' is installed. (DRC0009) | ||
'@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path | ||
$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ | ||
($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and | ||
$(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) | ||
}).BaseName | ||
|
||
|
||
Import-Module $ProjectName | ||
|
||
InModuleScope $ProjectName { | ||
Describe 'Assert-Module' { | ||
BeforeAll { | ||
$testModuleName = 'TestModule' | ||
} | ||
|
||
Context 'When module is not installed' { | ||
BeforeAll { | ||
Mock -CommandName Get-Module | ||
} | ||
|
||
It 'Should throw the correct error' { | ||
{ Assert-Module -ModuleName $testModuleName -Verbose } | ` | ||
Should -Throw ($script:localizedData.RoleNotFound -f $testModuleName) | ||
} | ||
} | ||
|
||
Context 'When module is available' { | ||
BeforeAll { | ||
Mock -CommandName Import-Module | ||
Mock -CommandName Get-Module -MockWith { | ||
return @{ | ||
Name = $testModuleName | ||
} | ||
} | ||
} | ||
|
||
It 'Should not throw an error' { | ||
{ Assert-Module -ModuleName $testModuleName -Verbose } | Should -Not -Throw | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters