Skip to content

Commit

Permalink
Assert-Module: Reduce verbose output (dsccommunity#67)
Browse files Browse the repository at this point in the history
- `Assert-Module`
  - Now it possible to forcibly import a module using `-ImportModule -Force`
  - It no longer outputs verbose messages that is normally generated when
    using `Get-Module -ListAvailable` if the module that is asserted is
    already in the session (issue dsccommunity#66).
  • Loading branch information
johlju authored Mar 24, 2021
1 parent c09b5ab commit 8bb53ee
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 28 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Renamed default branch to `main` - fixes [issue #62](https://github.com/dsccommunity/DscResource.Common/issues/62).
- DscResource.Common
- Renamed default branch to `main` - fixes [issue #62](https://github.com/dsccommunity/DscResource.Common/issues/62).
- Changed to use the new GitHub deploy tasks.
- `Assert-Module`
- Now it possible to forcibly import a module using `-ImportModule -Force`
- It no longer outputs verbose messages that is normally generated when
using `Get-Module -ListAvailable` if the module that is asserted is
already in the session ([issue #66](https://github.com/dsccommunity/DscResource.Common/issues/66)).
- `Compare-DscParameterState`
- Fix verbose message to only show when using parameter `IncludeInDesiredState`.
Also made the verbose message more intuitive when the value being compared
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ import the module.
#### Syntax

```plaintext
Assert-Module [-ModuleName] <string> [-ImportModule] [<CommonParameters>]
Assert-Module [-ModuleName] <string> [-ImportModule] [-Force] [<CommonParameters>]
```

#### Outputs
Expand All @@ -145,6 +145,14 @@ This will assert that the module DhcpServer is available and that it has
been imported into the session. If the module is not available an exception
will be thrown.

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

This will assert that the module DhcpServer is available and it will be
forcibly imported into the session (even if it was already in the session).
If the module is not available an exception will be thrown.

### `Compare-DscParameterState`

Compare current against desired property state for any DSC resource and return
Expand Down
1 change: 1 addition & 0 deletions RequiredModules.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ModuleBuilder = 'latest'
ChangelogManagement = 'latest'
Sampler = 'latest'
'Sampler.GitHubTasks' = 'latest'
MarkdownLinkCheck = 'latest'
'DscResource.AnalyzerRules' = 'latest'
xDscResourceDesigner = 'latest'
Expand Down
2 changes: 2 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Resolve-Dependency:
ModuleBuildTasks:
Sampler:
- '*.build.Sampler.ib.tasks'
Sampler.GitHubTasks:
- '*.ib.tasks'

TaskHeader: |
param($Path)
Expand Down
49 changes: 42 additions & 7 deletions source/Public/Assert-Module.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,29 @@
Specifies the name of the module to assert.
.PARAMETER ImportModule
Specfiies to import the module if it is asserted.
Specifies to import the module if it is asserted.
.PARAMETER Force
Specifies to forcibly import the module even if it is already in the
session. This parameter is ignored unless parameter `ImportModule` is
also used.
.EXAMPLE
Assert-Module -ModuleName 'DhcpServer'
This asserts that the module DhcpServer is available on the system.
.EXAMPLE
Assert-Module -ModuleName 'DhcpServer' -ImportModule
This asserts that the module DhcpServer is available on the system and
imports it.
.EXAMPLE
Assert-Module -ModuleName 'DhcpServer' -ImportModule -Force
This asserts that the module DhcpServer is available on the system and
forcibly imports it.
#>
function Assert-Module
{
Expand All @@ -27,17 +44,35 @@ function Assert-Module

[Parameter()]
[System.Management.Automation.SwitchParameter]
$ImportModule
$ImportModule,

[Parameter()]
[System.Management.Automation.SwitchParameter]
$Force
)

if (-not (Get-Module -Name $ModuleName -ListAvailable))
<#
If the module is already in the session there is no need to use -ListAvailable.
This is a fix for issue #66.
#>
if (-not (Get-Module -Name $ModuleName))
{
$errorMessage = $script:localizedData.ModuleNotFound -f $ModuleName
New-ObjectNotFoundException -Message $errorMessage
if (-not (Get-Module -Name $ModuleName -ListAvailable))
{
$errorMessage = $script:localizedData.ModuleNotFound -f $ModuleName
New-ObjectNotFoundException -Message $errorMessage
}

# Only import it here if $Force is not set, otherwise it will be imported below.
if ($ImportModule -and -not $Force)
{
Import-Module -Name $ModuleName
}
}

if ($ImportModule)
# Always import the module even if already in session.
if ($ImportModule -and $Force)
{
Import-Module -Name $ModuleName
Import-Module -Name $ModuleName -Force
}
}
81 changes: 62 additions & 19 deletions tests/Unit/Public/Assert-Module.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,76 @@ InModuleScope $ProjectName {
}
}

Context 'When module is available' {
BeforeAll {
Mock -CommandName Import-Module
Mock -CommandName Get-Module -MockWith {
return @{
Name = $testModuleName
Context 'When module is installed' {
Context 'When module is already present in session' {
BeforeAll {
Mock -CommandName Import-Module
Mock -CommandName Get-Module -MockWith {
return @{
Name = $testModuleName
}
}
}
}

Context 'When module should not be imported' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName } | Should -Not -Throw
Context 'When asserting that module is available' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName } | Should -Not -Throw
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Import-Module -Exactly -Times 0
}
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Import-Module -Exactly -Times 0

Context 'When using ImportModule but module is already imported' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName -ImportModule } | Should -Not -Throw
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Import-Module -Exactly -Times 0
}
}

Context 'When module should be forcibly imported' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName -ImportModule -Force } | Should -Not -Throw
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Import-Module -ParameterFilter {
$Force -eq $true
} -Exactly -Times 1
}
}
}

Context 'When module should be imported' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName -ImportModule } | Should -Not -Throw
Context 'When module is not present in the session' {
BeforeAll {
Mock -CommandName Import-Module
Mock -CommandName Get-Module -MockWith {
return $null
} -ParameterFilter {
$ListAvailable -eq $false
}

Mock -CommandName Get-Module -MockWith {
return @{
Name = $testModuleName
}
} -ParameterFilter {
$ListAvailable -eq $true
}
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Import-Module -Exactly -Times 1

Context 'When module should be imported' {
It 'Should not throw an error' {
{ Assert-Module -ModuleName $testModuleName -ImportModule } | Should -Not -Throw
}

It 'Should call the expected mocks' {
Assert-MockCalled -CommandName Import-Module -Exactly -Times 1
}
}
}
}
Expand Down

0 comments on commit 8bb53ee

Please sign in to comment.