bugfix: results of Sets were not being logged (#14)

* bugfix: results of Set scriptblocks were not being logged on the outputted RequirementEvent object

* increment version
pull/15/head
Chris Kuech 2019-07-01 22:37:19 -04:00 committed by GitHub
parent dbf1216b24
commit f774eb2a6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 129 deletions

View File

@ -4,7 +4,7 @@
RootModule = 'src\module.psm1'
# Version number of this module.
ModuleVersion = '2.2.1'
ModuleVersion = '2.2.2'
# Supported PSEditions
# CompatiblePSEditions = @()

View File

@ -13,7 +13,7 @@ function applyRequirement([Requirement]$Requirement) {
if (-not $result) {
if ($Requirement.Set) {
[RequirementEvent]::new($Requirement, "Set", "Start")
&$Requirement.Set
$result = &$Requirement.Set
[RequirementEvent]::new($Requirement, "Set", "Stop", $result)
}
if ($Requirement.Test -and $Requirement.Set) {

View File

@ -2,135 +2,151 @@
."$PSScriptRoot\core.ps1"
Describe "Core" {
Context "applyRequirement" {
It "Should not 'Set' if in desired state" {
$script:NotSetIfInDesiredState = 0
applyRequirement @{
Describe = "Simple Requirement"
Test = { $true }
Set = { $script:NotSetIfInDesiredState++ }
}
$script:NotSetIfInDesiredState | Should -Be 0
}
It "Should 'Set' if not in desired state" {
$script:SetIfNotInDesiredState = 0
applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:SetIfNotInDesiredState -eq 1 }
Set = { $script:SetIfNotInDesiredState++ }
}
$script:SetIfNotInDesiredState | Should -Be 1
}
It "Should validate once set" {
$script:TestOnceSetIsTestCount = 0
$script:TestOnceSetIsSet = $false
applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:TestOnceSetIsTestCount += 1; $script:TestOnceSetIsSet }
Set = { $script:TestOnceSetIsSet = $true }
}
$script:TestOnceSetIsSet | Should -Be $true
$script:TestOnceSetIsTestCount | Should -Be 2
}
It "Should 'Set' if no 'Test' is provided" {
$script:SetIfNoTest = $false
applyRequirement @{
Describe = "Simple Requirement"
Set = { $script:SetIfNoTest = $true }
}
$script:SetIfNoTest | Should -BeTrue
}
It "Should not 'Test' if no 'Set' is provided" {
$script:NotTestIfNoSet = 0
applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:NotTestIfNoSet++ }
}
$script:NotTestIfNoSet | Should -Be 1
}
It "Should output all log events" {
$script:SetIfNotInDesiredState = 0
$events = applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:SetIfNotInDesiredState -eq 1 }
Set = { $script:SetIfNotInDesiredState++ }
}
$expectedIds = "Test", "Set", "Validate" | % { "$_-Start", "$_-Stop" }
$foundIds = $events | % { "$($_.Method)-$($_.State)" }
$expectedIds | % { $_ -in $foundIds | Should -BeTrue }
}
}
Context "applyRequirements" {
It "Should call 'Test' on each requirement" {
$script:CallTestOnEachRequirement = 0
$requirements = 1..3 | % {
@{
Name = $_
Describe = "Simple Requirement"
Test = { $script:CallTestOnEachRequirement++ % 2 }
Set = { $false }
Context "applyRequirement" {
It "Should not 'Set' if in desired state" {
$script:NotSetIfInDesiredState = 0
applyRequirement @{
Describe = "Simple Requirement"
Test = { $true }
Set = { $script:NotSetIfInDesiredState++ }
}
$script:NotSetIfInDesiredState | Should -Be 0
}
}
applyRequirements $requirements
$script:CallTestOnEachRequirement | Should -Be 6
}
}
Context "sortRequirements" {
It "Should sort an array of requirements into topological order" {
$sorted = sortRequirements @(
@{
Name = "third"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first", "second"
},
@{
Name = "first"
Describe = "Simple Requirement"
Test = { }
Set = { }
},
@{
Name = "second"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first"
It "Should 'Set' if not in desired state" {
$script:SetIfNotInDesiredState = 0
applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:SetIfNotInDesiredState -eq 1 }
Set = { $script:SetIfNotInDesiredState++ }
}
$script:SetIfNotInDesiredState | Should -Be 1
}
It "Should validate once set" {
$script:TestOnceSetIsTestCount = 0
$script:TestOnceSetIsSet = $false
applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:TestOnceSetIsTestCount += 1; $script:TestOnceSetIsSet }
Set = { $script:TestOnceSetIsSet = $true }
}
$script:TestOnceSetIsSet | Should -Be $true
$script:TestOnceSetIsTestCount | Should -Be 2
}
It "Should 'Set' if no 'Test' is provided" {
$script:SetIfNoTest = $false
applyRequirement @{
Describe = "Simple Requirement"
Set = { $script:SetIfNoTest = $true }
}
$script:SetIfNoTest | Should -BeTrue
}
It "Should not 'Test' if no 'Set' is provided" {
$script:NotTestIfNoSet = 0
applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:NotTestIfNoSet++ }
}
$script:NotTestIfNoSet | Should -Be 1
}
It "Should output all log events" {
$script:SetIfNotInDesiredState = 0
$events = applyRequirement @{
Describe = "Simple Requirement"
Test = { $script:SetIfNotInDesiredState -eq 1 }
Set = { $script:SetIfNotInDesiredState++ }
}
$expectedIds = "Test", "Set", "Validate" | % { "$_-Start", "$_-Stop" }
$foundIds = $events | % { "$($_.Method)-$($_.State)" }
$expectedIds | % { $_ -in $foundIds | Should -BeTrue }
}
It "Should provide the result of the 'Test'" {
$expected = "This string should be the result of the 'Test' block"
$event = applyRequirement @{
Describe = "Simple Requirement"
Test = { $expected }
}
$event | Select -First 1 -Skip 1 | % Result | Should -Be $expected
}
It "Should provide the result of the 'Set'" {
$expected = "This string should be the result of the 'Set' block"
$event = applyRequirement @{
Describe = "Simple Requirement"
Set = { $expected }
}
$event | Select -First 1 -Skip 1 | % Result | Should -Be $expected
}
)
[string[]]$names = $sorted | % Name
0..($sorted.Count - 1) | % {
$i, $requirement = $_, $sorted[$_]
$requirement.DependsOn `
| % { $names.IndexOf($_) | Should -BeLessThan $i }
}
}
It "Should throw an error if there are unresolvable dependencies" {
{
sortRequirements @(
@{
Name = "third"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first", "second"
},
@{
Name = "first"
Describe = "Simple Requirement"
Test = { }
Set = { }
},
@{
Name = "second"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first", "third"
}
)
} | Should -Throw
Context "applyRequirements" {
It "Should call 'Test' on each requirement" {
$script:CallTestOnEachRequirement = 0
$requirements = 1..3 | % {
@{
Name = $_
Describe = "Simple Requirement"
Test = { $script:CallTestOnEachRequirement++ % 2 }
Set = { $false }
}
}
applyRequirements $requirements
$script:CallTestOnEachRequirement | Should -Be 6
}
}
Context "sortRequirements" {
It "Should sort an array of requirements into topological order" {
$sorted = sortRequirements @(
@{
Name = "third"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first", "second"
},
@{
Name = "first"
Describe = "Simple Requirement"
Test = { }
Set = { }
},
@{
Name = "second"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first"
}
)
[string[]]$names = $sorted | % Name
0..($sorted.Count - 1) | % {
$i, $requirement = $_, $sorted[$_]
$requirement.DependsOn `
| % { $names.IndexOf($_) | Should -BeLessThan $i }
}
}
It "Should throw an error if there are unresolvable dependencies" {
{
sortRequirements @(
@{
Name = "third"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first", "second"
},
@{
Name = "first"
Describe = "Simple Requirement"
Test = { }
Set = { }
},
@{
Name = "second"
Describe = "Simple Requirement"
Test = { }
Set = { }
DependsOn = "first", "third"
}
)
} | Should -Throw
}
}
}
}