Skip to content

Commit

Permalink
Add function Assert-Module (#14)
Browse files Browse the repository at this point in the history
- Added the function `Assert-Module`.
- Updated the localized strings to have the unique id according to style
  guideline.
  • Loading branch information
johlju authored Mar 9, 2020
1 parent 49ca59b commit 662f05b
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added the function `Assert-Module`.

### Changed

- Updated the localized strings to have the unique id according to style
guideline.

## [0.3.0] - 2020-02-15

### Added
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describing how to convert a DSC resource module to use DscResource.Common.
Refer to the comment-based help for more information about these helper
functions.

### `Assert-Module`

Assert if the specific module is available to be imported.

```powershell
Assert-Module -ModuleName 'DhcpServer'
```

### `Get-LocalizedData`

Gets language-specific data into scripts and functions based on the UI culture
Expand Down
28 changes: 28 additions & 0 deletions source/Public/Assert-Module.ps1
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
}
}
20 changes: 11 additions & 9 deletions source/en-US/DscResource.Common.psd1
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)
'@
42 changes: 42 additions & 0 deletions tests/Unit/Public/Assert-Module.Tests.ps1
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
}
}
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Public/New-InvalidArgumentException.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{
}).BaseName

Import-Module $ProjectName -Force
Describe 'DscResource.Common\New-InvalidArgumentException' {
Describe 'New-InvalidArgumentException' {
Context 'When calling with both the Message and ArgumentName parameter' {
It 'Should throw the correct error' {
$mockErrorMessage = 'Mocked error'
Expand All @@ -16,4 +16,4 @@ Describe 'DscResource.Common\New-InvalidArgumentException' {
}

Assert-VerifiableMock
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Public/New-ObjectNotFoundException.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ $ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{

Import-Module $ProjectName -Force

Describe 'DscResource.Common\New-ObjectNotFoundException' {
Describe 'New-ObjectNotFoundException' {
Context 'When calling with Message parameter only' {
It 'Should throw the correct error' {
$mockErrorMessage = 'Mocked error'
Expand Down

0 comments on commit 662f05b

Please sign in to comment.