Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement (ci): Enhance test entrypoint script #41

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,6 @@ $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'

Describe "Position-ExplorerWindow" -Tag 'Unit' {

Context 'Powershell versions' {

It 'Runs only on Powershell <= v5' {
$paths = @(
'foo'
'bar'
)
$errorAction = 'Stop'

if ($PSVersionTable.PSVersion.Major -gt 5) {
{ Position-ExplorerWindow -Paths $paths 3>$null 6>$null -ErrorAction $errorAction } | Should -Throw
}else {
{ Position-ExplorerWindow -Paths $paths 3>$null 6>$null -ErrorAction $errorAction } | Should -Not -Throw
}
}

}

Context 'Parameters' {

function Get-PowershellVersion { 5 }
Expand Down Expand Up @@ -71,6 +53,20 @@ Describe "Position-ExplorerWindow" -Tag 'Unit' {
}
function Position-ResizeWindow {}

It 'Runs only on Powershell <= v5' {
$paths = @(
'foo'
'bar'
)
$errorAction = 'Stop'

if ($PSVersionTable.PSVersion.Major -gt 5) {
{ Position-ExplorerWindow -Paths $paths 3>$null 6>$null -ErrorAction $errorAction } | Should -Throw
}else {
{ Position-ExplorerWindow -Paths $paths 3>$null 6>$null -ErrorAction $errorAction } | Should -Not -Throw
}
}

It 'Errors without required assemblies' {
$params = @{
ModeEasy = $true
Expand Down
41 changes: 30 additions & 11 deletions test/test.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$Tag = ''
)
$MODULE_NAME = (Get-Item $PSScriptRoot/../).Name
$MODULE_DIR = "$PSScriptRoot/../src/$MODULE_NAME"
$MODULE_PATH = "$MODULE_DIR/$MODULE_NAME.psm1"
$MODULE_MANIFEST = "$MODULE_DIR/$MODULE_NAME.psd1"

Set-StrictMode -Version Latest
$global:PesterDebugPreference_ShowFullErrors = $true

# Install Pester if needed
$pester = Get-Module Pester -ListAvailable -ErrorAction SilentlyContinue
$pesterMinVersion = [version]'4.0.0'
$pesterMaxVersion = [version]'4.10.1'
if ( ! $pester -or ! ($pester.Version | ? { $_ -ge $pesterMinVersion -and $_ -le $pesterMaxVersion }) ) {
if ($PSVersionTable.PSVersion.Major -ge 6) {
Install-Module Pester -Force -Scope CurrentUser -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion -ErrorAction Stop
}else {
Install-Module Pester -Force -Scope CurrentUser -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion -SkipPublisherCheck -ErrorAction Stop
}
if (!$pester -or !($pester.Version | ? { $_ -ge $pesterMinVersion -and $_ -le $pesterMaxVersion })) {
Install-Module Pester -Force -Scope CurrentUser -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion -ErrorAction Stop -SkipPublisherCheck
}
Get-Module Pester -ListAvailable
Get-Module Pester | Remove-Module -Force
Import-Module Pester -MinimumVersion $pesterMinVersion -MaximumVersion $pesterMaxVersion -Force -ErrorAction Stop
Get-Module Pester

# Install RequiredModules if needed
$manifestObj = Invoke-Command -ScriptBlock ([scriptblock]::Create((Get-Content $MODULE_MANIFEST -Encoding utf8 -Raw)))
if ($manifestObj.Contains('RequiredModules')) {
foreach ($m in $manifestObj['RequiredModules']) {
$m = $m.Clone()
$m['Name'] = $m['ModuleName']
$m.Remove('ModuleName')
if (!(Get-InstalledModule @m -ErrorAction SilentlyContinue)) {
"Installing required module: $( $m['Name'] )" | Write-Host -ForegroundColor Green
Install-Module @m -Force -Scope CurrentUser -ErrorAction Stop
}
Get-Module $m['Name'] -ListAvailable
}
}

# Test the module manifest
Test-ModuleManifest "$MODULE_MANIFEST" -ErrorAction Stop > $null

# Import our module
Get-Module "$MODULE_NAME" | Remove-Module -Force
Import-Module $MODULE_PATH -Force -ErrorAction Stop
Import-Module $MODULE_MANIFEST -Force -ErrorAction Stop -Verbose
Get-Module "$MODULE_NAME"

$global:PesterDebugPreference_ShowFullErrors = $true # For Pester 4
if ($Tag) {
# Run Unit Tests
$res = Invoke-Pester -Script $MODULE_DIR -Tag $Tag -PassThru -ErrorAction Stop
Expand All @@ -48,7 +67,7 @@ if ($Tag) {
"$( $res2.FailedCount ) integration tests failed." | Write-Host
}

if (($res -and $res.FailedCount -gt 0) -or ($res2 -and $res2.FailedCount)) {
if (($res -and $res.FailedCount -gt 0) -or ($res2 -and $res2.FailedCount -gt 0)) {
throw
}
}
Loading