forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuppressionFilterTests.cs
66 lines (56 loc) · 2.74 KB
/
SuppressionFilterTests.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using PSRule.Configuration;
using PSRule.Definitions;
using PSRule.Host;
using PSRule.Pipeline;
using PSRule.Rules;
using PSRule.Runtime;
using Xunit;
using Assert = Xunit.Assert;
namespace PSRule
{
public sealed class SuppressionFilterTests
{
[Fact]
public void Match()
{
var option = GetOption();
var context = new RunspaceContext(PipelineContext.New(option, null, null, null, null, null, new OptionContext(), null), new TestWriter(option));
context.Init(GetSource());
context.Begin();
var rules = HostHelper.GetRule(GetSource(), context, includeDependencies: false);
var resourceIndex = new ResourceIndex(rules);
var filter = new SuppressionFilter(context, option.Suppression, resourceIndex);
Assert.True(filter.Match(new ResourceId(".", "YAML.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject1"));
Assert.False(filter.Match(new ResourceId(".", "JSON.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject1"));
Assert.True(filter.Match(new ResourceId(".", "PS.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject1"));
Assert.True(filter.Match(new ResourceId(".", "YAML.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject2"));
Assert.True(filter.Match(new ResourceId(".", "JSON.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject2"));
Assert.False(filter.Match(new ResourceId(".", "PS.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject2"));
Assert.False(filter.Match(new ResourceId(".", "YAML.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject3"));
Assert.True(filter.Match(new ResourceId(".", "JSON.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject3"));
Assert.True(filter.Match(new ResourceId(".", "PS.RuleWithAlias1", ResourceIdKind.Unknown), "TestObject3"));
}
#region Helper methods
private Source[] GetSource()
{
var builder = new SourcePipelineBuilder(null, null);
builder.Directory(GetSourcePath("FromFileAlias.Rule.yaml"));
builder.Directory(GetSourcePath("FromFileAlias.Rule.jsonc"));
builder.Directory(GetSourcePath("FromFileAlias.Rule.ps1"));
return builder.Build();
}
private static PSRuleOption GetOption(string path = "PSRule.Tests14.yml")
{
return PSRuleOption.FromFileOrEmpty(path);
}
private static string GetSourcePath(string fileName)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
}
#endregion Helper methods
}
}