Skip to content

Commit b8173bd

Browse files
committed
Convert unit tests
1 parent b980e1b commit b8173bd

5 files changed

Lines changed: 308 additions & 313 deletions

File tree

tests/TestHelpers/CommonTestHelper.psm1

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,6 @@ function Get-InvalidOperationRecord
8181
return New-Object @newObjectParams
8282
}
8383

84-
<#
85-
.SYNOPSIS
86-
Enters a DSC Resource test environment.
87-
88-
.PARAMETER DscResourceModuleName
89-
The name of the module that contains the DSC Resource to test.
90-
91-
.PARAMETER DscResourceName
92-
The name of the DSC resource to test.
93-
94-
.PARAMETER TestType
95-
Specifies whether the test environment will run a Unit test or an Integration test.
96-
#>
97-
9884
Export-ModuleMember -Function `
9985
'Get-InvalidArgumentRecord', `
10086
'Get-InvalidOperationRecord'

tests/Unit/DSC_IniSettingsFile.Tests.ps1

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
22
param ()
33

4-
$script:DSCModuleName = 'FileContentDsc'
5-
$script:DSCResourceName = 'DSC_IniSettingsFile'
4+
$script:dscModuleName = 'FileContentDsc'
5+
$script:dscResourceName = 'DSC_IniSettingsFile'
66

7-
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
7+
function Invoke-TestSetup
8+
{
9+
try
10+
{
11+
Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop'
12+
}
13+
catch [System.IO.FileNotFoundException]
14+
{
15+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.'
16+
}
17+
18+
$script:testEnvironment = Initialize-TestEnvironment `
19+
-DSCModuleName $script:dscModuleName `
20+
-DSCResourceName $script:dscResourceName `
21+
-ResourceType 'Mof' `
22+
-TestType 'Unit'
23+
24+
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1')
25+
}
826

9-
#region HEADER
10-
# Unit Test Template Version: 1.1.0
11-
[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
12-
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
13-
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
27+
function Invoke-TestCleanup
1428
{
15-
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
29+
Restore-TestEnvironment -TestEnvironment $script:testEnvironment
1630
}
1731

18-
Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
19-
$TestEnvironment = Initialize-TestEnvironment `
20-
-DSCModuleName $script:DSCModuleName `
21-
-DSCResourceName $script:DSCResourceName `
22-
-TestType Unit
23-
#endregion HEADER
32+
Invoke-TestSetup
2433

2534
# Begin Testing
2635
try
2736
{
28-
<#
29-
The InModuleScope command allows you to perform white-box unit testing on the internal
30-
(non-exported) code of a Script Module.
31-
#>
32-
InModuleScope 'DSC_IniSettingsFile' {
37+
InModuleScope $script:dscResourceName {
3338
$script:testTextFile = 'TestFile.ini'
3439
$script:testText = 'Test Text'
3540
$script:testSecret = 'Test Secret'
@@ -709,7 +714,5 @@ try
709714
}
710715
finally
711716
{
712-
#region FOOTER
713-
Restore-TestEnvironment -TestEnvironment $TestEnvironment
714-
#endregion
717+
Invoke-TestCleanup
715718
}

tests/Unit/DSC_KeyValuePairFile.Tests.ps1

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
22
param ()
33

4-
$script:DSCModuleName = 'FileContentDsc'
5-
$script:DSCResourceName = 'DSC_KeyValuePairFile'
4+
$script:dscModuleName = 'FileContentDsc'
5+
$script:dscResourceName = 'DSC_KeyValuePairFile'
66

7-
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
7+
function Invoke-TestSetup
8+
{
9+
try
10+
{
11+
Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop'
12+
}
13+
catch [System.IO.FileNotFoundException]
14+
{
15+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.'
16+
}
17+
18+
$script:testEnvironment = Initialize-TestEnvironment `
19+
-DSCModuleName $script:dscModuleName `
20+
-DSCResourceName $script:dscResourceName `
21+
-ResourceType 'Mof' `
22+
-TestType 'Unit'
23+
24+
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1')
25+
}
826

9-
#region HEADER
10-
# Unit Test Template Version: 1.1.0
11-
[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
12-
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
13-
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
27+
function Invoke-TestCleanup
1428
{
15-
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
29+
Restore-TestEnvironment -TestEnvironment $script:testEnvironment
1630
}
1731

18-
Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
19-
$TestEnvironment = Initialize-TestEnvironment `
20-
-DSCModuleName $script:DSCModuleName `
21-
-DSCResourceName $script:DSCResourceName `
22-
-TestType Unit
23-
#endregion HEADER
32+
Invoke-TestSetup
2433

2534
# Begin Testing
2635
try
2736
{
28-
<#
29-
The InModuleScope command allows you to perform white-box unit testing on the internal
30-
(non-exported) code of a Script Module.
31-
#>
32-
InModuleScope 'DSC_KeyValuePairFile' {
37+
InModuleScope $script:dscResourceName {
3338
$script:testTextFile = 'TestFile.txt'
3439
$script:testName = 'Setting.Two'
3540
$script:testNameNotFound = 'Setting.NotFound'
@@ -1375,7 +1380,5 @@ $($script:testAddedName)=$($script:testText)
13751380
}
13761381
finally
13771382
{
1378-
#region FOOTER
1379-
Restore-TestEnvironment -TestEnvironment $TestEnvironment
1380-
#endregion
1383+
Invoke-TestCleanup
13811384
}

tests/Unit/DSC_ReplaceText.Tests.ps1

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")]
22
param ()
33

4-
$script:DSCModuleName = 'FileContentDsc'
5-
$script:DSCResourceName = 'DSC_ReplaceText'
4+
$script:dscModuleName = 'FileContentDsc'
5+
$script:dscResourceName = 'DSC_ReplaceText'
66

7-
Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global
7+
function Invoke-TestSetup
8+
{
9+
try
10+
{
11+
Import-Module -Name DscResource.Test -Force -ErrorAction 'Stop'
12+
}
13+
catch [System.IO.FileNotFoundException]
14+
{
15+
throw 'DscResource.Test module dependency not found. Please run ".\build.ps1 -Tasks build" first.'
16+
}
17+
18+
$script:testEnvironment = Initialize-TestEnvironment `
19+
-DSCModuleName $script:dscModuleName `
20+
-DSCResourceName $script:dscResourceName `
21+
-ResourceType 'Mof' `
22+
-TestType 'Unit'
23+
24+
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath '..\TestHelpers\CommonTestHelper.psm1')
25+
}
826

9-
#region HEADER
10-
# Unit Test Template Version: 1.1.0
11-
[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
12-
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
13-
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
27+
function Invoke-TestCleanup
1428
{
15-
& git @('clone','https://github.com/PowerShell/DscResource.Tests.git',(Join-Path -Path $script:moduleRoot -ChildPath '\DSCResource.Tests\'))
29+
Restore-TestEnvironment -TestEnvironment $script:testEnvironment
1630
}
1731

18-
Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
19-
$TestEnvironment = Initialize-TestEnvironment `
20-
-DSCModuleName $script:DSCModuleName `
21-
-DSCResourceName $script:DSCResourceName `
22-
-TestType Unit
23-
#endregion HEADER
32+
Invoke-TestSetup
2433

2534
# Begin Testing
2635
try
2736
{
28-
<#
29-
The InModuleScope command allows you to perform white-box unit testing on the internal
30-
(non-exported) code of a Script Module.
31-
#>
32-
InModuleScope 'DSC_ReplaceText' {
37+
InModuleScope $script:dscResourceName {
3338
$script:testTextFile = 'TestFile.txt'
3439
$script:testText = 'TestText'
3540
$script:testSecret = 'TestSecret'
@@ -1109,7 +1114,5 @@ Setting3.Test=Value4
11091114
}
11101115
finally
11111116
{
1112-
#region FOOTER
1113-
Restore-TestEnvironment -TestEnvironment $TestEnvironment
1114-
#endregion
1117+
Invoke-TestCleanup
11151118
}

0 commit comments

Comments
 (0)