Skip to content

Commit

Permalink
Added unit test for helper function Start-DscResourceTests (PowerShel…
Browse files Browse the repository at this point in the history
…l#182)

* Added unit test
Added unit test for helper function Start-DscResourceTests.

* Changes to common tests
- Updated AppVeyor code so that common tests and unit tests is run on the working
  branch's tests. This is to be able to test changes to tests in pull requests
  without having to merge the pull request before seeing the result.

* Changes to common tests
- Updated the logic to check for a module manifest instead of using APPVEYOR_BUILD_FOLDER environment variable.
  • Loading branch information
johlju authored and kwirkykat committed Aug 16, 2017
1 parent 4fc3d28 commit 8ebcda4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 16 deletions.
51 changes: 39 additions & 12 deletions Meta.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,50 @@ $errorActionPreference = 'Stop'
$testHelperModulePath = Join-Path -Path $PSScriptRoot -ChildPath 'TestHelper.psm1'
Import-Module -Name $testHelperModulePath

$moduleRootFilePath = Split-Path -Path $PSScriptRoot -Parent
$moduleName = (Get-Item -Path $moduleRootFilePath).Name
$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'

<#
This is a workaround to be able to test DscResource.Tests module.
Because the name has punctuation in the name, AppVeyor replaces
that with a dash when it creates the folder structure, so the folder
name becomes 'dscresource-tests'. This sets the module name to the
correct name.
If the name can be detected in a better way, for DscResource.Tests
and all other modules, then this could be removed.
This is a workaround to be able to run these common test on DscResource.Tests
module, for testing itself.
We need to determine if we are running the code on the repository
DscResource.Tests or some other resource module.
If the parent folder does NOT contain a module manifest we will assume that
DscResource.Test is the module being tested.
Example:
Current folder: c:\source\DscResource.Tests
Parent folder: c:\source
Module manifest: $null
If the parent folder do contain a module manifest we will assume that
DscResource.Test has been cloned into another resource module and it is
that resource module that is being tested.
Example:
Current folder: c:\source\xSQLServer\DscResource.Tests
Parent folder: c:\source\xSQLServer
Module manifest: c:\source\xSQLServer\xSQLServer.psd1
#>
if ($moduleName -eq 'dscresource-tests')
$moduleRootFilePath = Split-Path -Path $PSScriptRoot -Parent

$moduleManifestExistInModuleRootFilePath = Get-ChildItem -Path $moduleRootFilePath -Filter '*.psd1'
if (-not $moduleManifestExistInModuleRootFilePath)
{
$moduleRootFilePath = $PSScriptRoot

<#
Because the repository name of 'DscResource.Tests' has punctuation in
the name, AppVeyor replaces that with a dash when it creates the folder
structure, so the folder name becomes 'dscresource-tests'.
This sets the module name to the correct name.
If the name can be detected in a better way, for DscResource.Tests
and all other modules, then this could be removed.
#>
$moduleName = 'DscResource.Tests'
}
else
{
$moduleName = (Get-Item -Path $moduleRootFilePath).Name
}

$dscResourcesFolderFilePath = Join-Path -Path $moduleRootFilePath -ChildPath 'DscResources'

# Identify the repository root path of the resource module
$repoRootPath = $moduleRootFilePath
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ Invoke-AppveyorAfterTestTask `
to 1 when running in AppVeyor to suppress warning caused by running custom rules
in PSScriptAnalyzer in the GetExternalRule() method of `Engine/ScriptAnalyzer.cs`
([issue #176](https://github.com/PowerShell/DscResource.Tests/issues/176)).
* Added unit tests for helper function Start-DscResourceTests.
* Updated AppVeyor code so that common tests and unit tests is run on the working
branch's tests. This is to be able to test changes to tests in pull requests
without having to merge the pull request before seeing the result.

### 0.2.0.0

Expand Down
48 changes: 48 additions & 0 deletions Tests/Unit/TestRunner.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$script:ModuleName = 'TestRunner'
$script:moduleRootPath = Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent

Describe "$($script:ModuleName) Unit Tests" {
BeforeAll {
Import-Module -Name (Join-Path -Path $script:moduleRootPath -ChildPath "$($script:ModuleName).psm1") -Force
}

InModuleScope $script:ModuleName {
Describe 'Start-DscResourceTests' {
BeforeAll {
# Set up TestDrive
$mockResourcePath = Join-Path -Path 'TestDrive:' -ChildPath 'DscResources'
New-Item -Path $mockResourcePath -ItemType Directory

$mockResource1Path = Join-Path -Path $mockResourcePath -ChildPath 'Resource1'
New-Item -Path $mockResource1Path -ItemType Directory

$mockResource2Path = Join-Path -Path $mockResourcePath -ChildPath 'Resource2'
New-Item -Path $mockResource2Path -ItemType Directory

Push-Location

Set-Location -Path $mockResourcePath
}

BeforeEach {
Mock -CommandName Push-Location -Verifiable
Mock -CommandName Pop-Location -Verifiable
Mock -CommandName Invoke-Pester -Verifiable
}

Context 'When starting tests' {
It 'Should call Invoke-Pester exactly 2 times' {
{ Start-DscResourceTests -ResourcesPath $mockResourcePath } | Should Not Throw

Assert-MockCalled -CommandName Invoke-Pester -Exactly -Times 2
}
}

AfterAll {
Pop-Location
}

Assert-VerifiableMocks
}
}
}
5 changes: 1 addition & 4 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
#---------------------------------#
version: 0.2.{build}.0
install:
- git clone https://github.com/PowerShell/DscResource.Tests
- ps: cd "$env:APPVEYOR_BUILD_FOLDER\DscResource.Tests"

- ps: |
Import-Module "$env:APPVEYOR_BUILD_FOLDER\DscResource.Tests\AppVeyor.psm1"
Import-Module "$env:APPVEYOR_BUILD_FOLDER\AppVeyor.psm1"
Invoke-AppveyorInstallTask
#---------------------------------#
Expand Down

0 comments on commit 8ebcda4

Please sign in to comment.