forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSRule.TypeOf.Tests.ps1
82 lines (68 loc) · 3.02 KB
/
PSRule.TypeOf.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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Unit tests for the TypeOf 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 -- TypeOf keyword' -Tag 'TypeOf' {
BeforeAll {
$ruleFilePath = (Join-Path -Path $here -ChildPath 'FromFile.Rule.ps1');
}
Context 'TypeOf' {
It 'With defaults' {
# Test positive cases
$hashTableObject = @{ Key = 'Value' }
$hashTableObjectWithName1 = @{ Key = 'Value' }; $hashTableObjectWithName1.PSObject.TypeNames.Insert(0, 'AdditionalTypeName');
$hashTableObjectWithName2 = @{ Key = 'Value' }; $hashTableObjectWithName2.PSObject.TypeNames.Add('AdditionalTypeName');
$customObjectWithName = [PSCustomObject]@{ Key = 'Value' }; $customObjectWithName.PSObject.TypeNames.Add('PSRule.Test.OtherType');
$testObject = @($hashTableObject, $hashTableObjectWithName1, $hashTableObjectWithName2, $customObjectWithName);
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'TypeOfTest';
$result | Should -Not -BeNullOrEmpty;
$result.Count | Should -Be 4;
$result.IsSuccess() | Should -BeIn $True;
$result.RuleName | Should -BeIn 'TypeOfTest';
$result[0].Reason | Should -BeNullOrEmpty;
# Test negative cases
$testObject = [PSCustomObject]@{
Key = 'Value'
}
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'TypeOfTest';
$result | Should -Not -BeNullOrEmpty;
$result.IsSuccess() | Should -Be $False;
$result.RuleName | Should -Be 'TypeOfTest';
$result.Reason | Should -BeLike "None of the type name(s) match: *";
}
It 'If pre-condition' {
$testObject = @(
[PSCustomObject]@{
PSTypeName = 'PSRule.Test.OtherType'
Name = 'TestObject1'
}
[PSCustomObject]@{
Name = 'TestObject2'
}
)
$option = New-PSRuleOption -NotProcessedWarning $False
$result = $testObject | Invoke-PSRule -Path $ruleFilePath -Name 'TypeOfCondition' -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 -eq 'TestObject2' };
$filteredResult | Should -Not -BeNullOrEmpty;
$filteredResult.Outcome | Should -Be 'None';
}
}
}