Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
revised pester tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhitsolutions committed Nov 29, 2018
1 parent 31de017 commit 3d575be
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 103 deletions.
1 change: 0 additions & 1 deletion MyTasks.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Register-ArgumentCompleter -CommandName $cmd -ParameterName Name -ScriptBlock {

[xml]$In = Get-Content -Path $MyTaskPath -Encoding UTF8

#$in.objects.object.childnodes.where( {$_.Name -eq 'Name'}).'#text'
foreach ($obj in $in.Objects.object) {
$obj.Property | ForEach-Object -Begin {$propHash = [ordered]@{}} -Process {
$propHash.Add($_.name, $_.'#text')
Expand Down
79 changes: 41 additions & 38 deletions MyTasksFunctions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Function _ImportTasks {

} #_ImportTasks

#exported functions
# region exported functions
Function New-MyTask {

[cmdletbinding(SupportsShouldProcess, DefaultParameterSetName = "Date")]
Expand Down Expand Up @@ -637,7 +637,7 @@ Function Get-MyTask {
"ID" {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Retrieving Task by ID: $ID"
#$results = $tasks.where( {$_.id -eq $ID})
$results = $tasks.where({$_.id -match "^($($id -join '|'))$" })
$results = $tasks.where( {$_.id -match "^($($id -join '|'))$" })
} #id

"All" {
Expand Down Expand Up @@ -1141,6 +1141,7 @@ Function Save-MyTask {
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "[$((Get-Date).TimeofDay) EGIN ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*4)$_"}) | Out-String) `n"
Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Using parameter set $($PSCmdlet.ParameterSetName)"

}

Process {
Expand Down Expand Up @@ -1355,9 +1356,10 @@ table { width:95%;margin-left:5px; margin-bottom:20px;}
}
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Validating Requirements"
if ((Get-Module PSScheduledJob) -And (($PSVersionTable.Platform -eq 'Win32NT') -OR ($PSVersionTable.PSEdition -eq 'Desktop'))) {

Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating a Daily job trigger for $($Time.TimeofDay)"
$trigger = New-JobTrigger -Daily -At $Time

$opt = New-ScheduledJobOption -RunElevated -RequireNetwork
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Registering the scheduled job"
if ($AsHtml) {
Expand All @@ -1372,7 +1374,7 @@ table { width:95%;margin-left:5px; margin-bottom:20px;}
MaxResultCount = 5
ScheduledJobOption = $opt
Credential = $TaskCredential
}
}
$regParams | Out-String | Write-Verbose
Register-ScheduledJob @regParams
}
Expand Down Expand Up @@ -1434,44 +1436,44 @@ Function Get-EmailReminder {
Process {
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting scheduled job myTasksEmail"
if ((Get-Module PSScheduledJob) -And (($PSVersionTable.Platform -eq 'Win32NT') -OR ($PSVersionTable.PSEdition -eq 'Desktop'))) {
$t = Get-ScheduledJob myTasksEmail
$t = Get-ScheduledJob myTasksEmail

$hash = $t.InvocationInfo.Parameters[0].where( {$_.name -eq "argumentlist"}).value
$hash = $t.InvocationInfo.Parameters[0].where( {$_.name -eq "argumentlist"}).value

Try {
#get the last run
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting last job run"
$last = Get-Job -name $t.name -Newest 1 -ErrorAction stop
}
Catch {
$last = [PSCustomObject]@{
PSEndTime = "11/30/1999" -as [datetime]
State = "The task has not yet run"
Try {
#get the last run
Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting last job run"
$last = Get-Job -name $t.name -Newest 1 -ErrorAction stop
}
Catch {
$last = [PSCustomObject]@{
PSEndTime = "11/30/1999" -as [datetime]
State = "The task has not yet run"
}
}
[pscustomobject]@{
Task = $t.Name
Frequency = $t.JobTriggers.Frequency
At = $t.JobTriggers.at.TimeOfDay
To = $hash.To
From = $hash.From
MailServer = $hash.SMTPServer
Port = $hash.Port
UseSSL = $hash.UseSSL
AsHTML = $hash.BodyAsHTML
LastRun = $last.PSEndTime
LastState = $last.State
Started = $last.psBeginTime
Ended = $last.psEndTime
Result = $last.output
Enabled = $t.Enabled
Errors = $last.Errors
Warnings = $last.warnings
}
}
[pscustomobject]@{
Task = $t.Name
Frequency = $t.JobTriggers.Frequency
At = $t.JobTriggers.at.TimeOfDay
To = $hash.To
From = $hash.From
MailServer = $hash.SMTPServer
Port = $hash.Port
UseSSL = $hash.UseSSL
AsHTML = $hash.BodyAsHTML
LastRun = $last.PSEndTime
LastState = $last.State
Started = $last.psBeginTime
Ended = $last.psEndTime
Result = $last.output
Enabled = $t.Enabled
Errors = $last.Errors
Warnings = $last.warnings
else {
Write-Warning "This command requires the PSScheduledJob module on a Windows platform."
}
}
else {
Write-Warning "This command requires the PSScheduledJob module on a Windows platform."
}
} #process

End {
Expand All @@ -1483,7 +1485,7 @@ Function Get-EmailReminder {

Function Set-MyTaskPath {
[cmdletbinding(SupportsShouldProcess)]
[OutputType("None",[System.Management.Automation.PSVariable])]
[OutputType("None", [System.Management.Automation.PSVariable])]
Param(
[Parameter(Mandatory, HelpMessage = "Enter the path to your new myTaskPath directory")]
[ValidateScript( {Test-Path $_})]
Expand All @@ -1509,4 +1511,5 @@ Function Set-MyTaskPath {
}
} #close Set-MyTaskPath

#endregion

182 changes: 182 additions & 0 deletions Tests/MyTasks.module.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
if (Get-Module -Name MyTasks) {
Remove-Module -Name MyTasks
}

Import-Module -Name "$PSScriptRoot\..\Mytasks.psd1" -Force

Describe 'MyTasks' {

$Module = Get-Module -Name MyTasks
It 'should have 15 functions' {
$Module.ExportedFunctions.count | Should -Be 15
}

It 'should have 8 aliases command' {
$Module.ExportedAliases.Count | Should -Be 8
}

It 'should not export any variables' {
$Module.ExportedVariables.Count | Should -Be 0
}

$modvariables = @(
@{Variable = 'myTaskArchivePath'}
@{Variable = 'myTaskCategory'}
@{Variable = 'mytaskhome'}
@{Variable = 'mytaskPath'}
)

It 'should define a global variable called <Variable>' -TestCases $modVariables {
param ($Variable)
{Get-Variable -Name $variable -Scope global} | Should -Not -Throw
}
It 'should have a formatting xml file' {
$Module.ExportedFormatFiles.Count | Should -Be 1
}

It 'should have an about help topic' {
{Get-Help about_mytasks} | Should -Not -Throw
}

It 'requires PowerShell 5.0' {
$Module.PowerShellVersion | Should -Be '5.0'
}
} #describe my module

Describe "Functions" {

$cmds = @(
@{Name = 'Disable-EmailReminder'}
@{Name = 'Enable-EmailReminder'}
@{Name = 'Get-EmailReminder'}
@{Name = 'New-MyTask'}
@{Name = 'Set-MyTask'}
@{Name = 'Save-MyTask'}
@{Name = 'Remove-MyTask'}
@{Name = 'Complete-MyTask'}
@{Name = 'Show-MyTask'}
@{Name = 'Get-MyTask'}
@{Name = 'Add-MyTaskCategory'}
@{Name = 'Get-MyTaskCategory'}
@{Name = 'Remove-MyTaskCategory'}
@{Name = 'Backup-MyTaskFile'}
@{Name = 'Set-MyTaskPath'}
)

It "<Name> has external help defined with at least one example" -TestCases $cmds {
param($Name)
$help = Get-Help $Name
$help.Description | Should -Not -BeNullOrEmpty
$help.Examples | Should -Not -BeNullOrEmpty
}

Context 'Enable-EmailReminder' {
$cmd = Get-Command -Name Enable-EmailReminder
$params = "TO", "TASKCREDENTIAL"
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
}

Context 'New-MyTask' {
$cmd = Get-Command -Name New-MyTask
$params = "NAME", "CATEGORY"
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
$params = 'NAME'.'DUEDATE'.'DESCRIPTION'.'CATEGORY'
Foreach ($item in $params) {
It "should have a $item parameter that accepts pipeline input by name" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).ValueFromPipelineByPropertyName | Should -be $True
}
}
}
Context 'Set-MyTask' {
$cmd = Get-Command -Name Set-MyTask
$params = "NAME"
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}

$params = "NAME"
Foreach ($item in $params) {
It "should have a $item parameter that accepts pipeline input by name" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).ValueFromPipelineByPropertyName | Should -be $True
}
}
}
Context 'Save-MyTask' {
$cmd = Get-Command -Name Save-MyTask
$params = "TASK"
Foreach ($item in $params) {
It "should have a $item parameter that accepts pipeline input by value" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).ValueFromPipeline| Should -be $True
}
}
}
Context 'Remove-MyTask' {
$cmd = Get-Command -Name Remove-MyTask
$params = "NAME", 'INPUTOBJECT'
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
}
Context 'Complete-MyTask' {
$cmd = Get-Command -Name Complete-MyTask
$params = "NAME", 'ID'
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
}

Context 'Add-MyTaskCategory' {
$cmd = Get-Command -Name Add-MyTaskCategory
$params = "CATEGORY"
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
Foreach ($item in $params) {
It "should have a $item parameter that accepts pipeline input by value" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).ValueFromPipeline| Should -be $True
}
}
}

Context 'Remove-MyTaskCategory' {
$cmd = Get-Command -Name Remove-MyTaskCategory
$params = "CATEGORY"
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
Foreach ($item in $params) {
It "should have a $item parameter that accepts pipeline input by value" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).ValueFromPipeline| Should -be $True
}
}
}

Context 'Set-MyTaskPath' {
$cmd = Get-Command -Name Set-MyTaskPath
$params = "PATH"
Foreach ($item in $params) {
It "should have a mandatory $item parameter" {
($cmd.parameters["$item"].Attributes).where( {$_.typeid.name -match 'parameterAttribute'}).Mandatory | Should -be $True
}
}
}

} #describe functions
Loading

0 comments on commit 3d575be

Please sign in to comment.