forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSRule.Exists.Tests.ps1
84 lines (71 loc) · 2.83 KB
/
PSRule.Exists.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Unit tests for the Exists keyword
#
[CmdletBinding()]
param (
)
BeforeAll {
# Setup error handling
$ErrorActionPreference = 'Stop';
Set-StrictMode -Version latest;
# Setup tests paths
$rootPath = $PWD;
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSRule) -Force;
$here = (Resolve-Path $PSScriptRoot).Path;
}
Describe 'PSRule -- Exists keyword' -Tag 'Exists' {
BeforeAll {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
$invokeParams = @{
Path = $ruleFilePath
}
}
Context 'Exists' {
It 'With defaults' {
$testObject = [PSCustomObject]@{
Name = 'TestObject1'
Value = @{
Value1 = 1
}
Value2 = '2'
Properties = $Null
}
$result = $testObject | Invoke-PSRule @invokeParams -Tag @{ keyword = 'Exists' };
# Test positive cases
$filteredResult = $result | Where-Object { $_.RuleName -eq 'ExistsTest' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.IsSuccess() | Should -Be $True;
$filteredResult.Reason | Should -BeNullOrEmpty;
# Test negative cases
$filteredResult = $result | Where-Object { $_.RuleName -eq 'ExistsTestNegative' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.IsSuccess() | Should -Be $False;
$filteredResult.Reason[0..4] | Should -BeLike "None of the field(s) existed: *";
$filteredResult.Reason[5] | Should -BeLike "The field(s) existed: *";
$filteredResult.Reason[6] | Should -BeLike "None of the field(s) existed: *";
$filteredResult.Reason[7] | Should -BeLike "The field(s) existed: *";
}
It 'If pre-condition' {
$testObject = @(
[PSCustomObject]@{
Name = 'TestObject1'
}
[PSCustomObject]@{
NotName = 'TestObject2'
}
)
$option = New-PSRuleOption -NotProcessedWarning $False
$result = $testObject | Invoke-PSRule @invokeParams -Name 'ExistsCondition' -Outcome All -Option $option;
# Test positive cases
$filteredResult = $result | Where-Object { $_.TargetName -eq 'TestObject1' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.Outcome | Should -Be 'Pass';
# Test negative cases
$filteredResult = $result | Where-Object { $_.TargetName -ne 'TestObject1' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.Outcome | Should -Be 'None';
}
}
}