Skip to content

Commit ea4a77c

Browse files
authoredNov 12, 2024··
Test refactoring (#2600)
1 parent 7a99155 commit ea4a77c

23 files changed

+127
-225
lines changed
 

‎src/PSRule.Benchmark/packages.lock.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1986,8 +1986,8 @@
19861986
},
19871987
"YamlDotNet": {
19881988
"type": "Transitive",
1989-
"resolved": "16.1.3",
1990-
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
1989+
"resolved": "16.2.0",
1990+
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
19911991
},
19921992
"Microsoft.PSRule.Badges": {
19931993
"type": "Project",
@@ -2012,7 +2012,7 @@
20122012
"type": "Project",
20132013
"dependencies": {
20142014
"Newtonsoft.Json": "[13.0.3, )",
2015-
"YamlDotNet": "[16.1.3, )"
2015+
"YamlDotNet": "[16.2.0, )"
20162016
}
20172017
}
20182018
}

‎src/PSRule.CommandLine/packages.lock.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1635,8 +1635,8 @@
16351635
},
16361636
"YamlDotNet": {
16371637
"type": "Transitive",
1638-
"resolved": "16.1.3",
1639-
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
1638+
"resolved": "16.2.0",
1639+
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
16401640
},
16411641
"Microsoft.PSRule.Badges": {
16421642
"type": "Project",
@@ -1667,7 +1667,7 @@
16671667
"type": "Project",
16681668
"dependencies": {
16691669
"Newtonsoft.Json": "[13.0.3, )",
1670-
"YamlDotNet": "[16.1.3, )"
1670+
"YamlDotNet": "[16.2.0, )"
16711671
}
16721672
}
16731673
}

‎src/PSRule.SDK/packages.lock.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1033,8 +1033,8 @@
10331033
},
10341034
"YamlDotNet": {
10351035
"type": "Transitive",
1036-
"resolved": "16.1.3",
1037-
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
1036+
"resolved": "16.2.0",
1037+
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
10381038
},
10391039
"Microsoft.PSRule.Badges": {
10401040
"type": "Project",
@@ -1059,7 +1059,7 @@
10591059
"type": "Project",
10601060
"dependencies": {
10611061
"Newtonsoft.Json": "[13.0.3, )",
1062-
"YamlDotNet": "[16.1.3, )"
1062+
"YamlDotNet": "[16.2.0, )"
10631063
}
10641064
}
10651065
}

‎src/PSRule.Tool/packages.lock.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1632,8 +1632,8 @@
16321632
},
16331633
"YamlDotNet": {
16341634
"type": "Transitive",
1635-
"resolved": "16.1.3",
1636-
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
1635+
"resolved": "16.2.0",
1636+
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
16371637
},
16381638
"Microsoft.PSRule.Badges": {
16391639
"type": "Project",
@@ -1673,7 +1673,7 @@
16731673
"type": "Project",
16741674
"dependencies": {
16751675
"Newtonsoft.Json": "[13.0.3, )",
1676-
"YamlDotNet": "[16.1.3, )"
1676+
"YamlDotNet": "[16.2.0, )"
16771677
}
16781678
}
16791679
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using PSRule.Definitions.Selectors;
5+
using PSRule.Definitions.SuppressionGroups;
6+
using PSRule.Runtime;
7+
8+
namespace PSRule.Definitions;
9+
10+
#nullable enable
11+
12+
/// <summary>
13+
/// Extensions for resource types.
14+
/// </summary>
15+
internal static class IResourceExtensions
16+
{
17+
/// <summary>
18+
/// Convert a suppression group into a suppression group visitor.
19+
/// </summary>
20+
/// <param name="resource">The suppression group resource.</param>
21+
/// <param name="runspaceContext">A valid runspace context.</param>
22+
/// <returns>An instance of a <see cref="SuppressionGroupVisitor"/>.</returns>
23+
public static SuppressionGroupVisitor ToSuppressionGroupVisitor(this SuppressionGroupV1 resource, RunspaceContext runspaceContext)
24+
{
25+
return new SuppressionGroupVisitor(
26+
context: runspaceContext,
27+
id: resource.Id,
28+
source: resource.Source,
29+
spec: resource.Spec,
30+
info: resource.Info
31+
);
32+
}
33+
34+
/// <summary>
35+
/// Converts a selector into a selector visitor.
36+
/// </summary>
37+
/// <param name="resource">The selector resource.</param>
38+
/// <param name="runspaceContext">A valid runspace context.</param>
39+
/// <returns>An instance of a <see cref="SelectorVisitor"/>.</returns>
40+
public static SelectorVisitor ToSelectorVisitor(this SelectorV1 resource, RunspaceContext runspaceContext)
41+
{
42+
return new SelectorVisitor(
43+
runspaceContext,
44+
resource.Id,
45+
resource.Source,
46+
resource.Spec.If
47+
);
48+
}
49+
}
50+
51+
#nullable restore

‎src/PSRule/packages.lock.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1051,8 +1051,8 @@
10511051
},
10521052
"YamlDotNet": {
10531053
"type": "Transitive",
1054-
"resolved": "16.1.3",
1055-
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
1054+
"resolved": "16.2.0",
1055+
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
10561056
},
10571057
"Microsoft.PSRule.Badges": {
10581058
"type": "Project",
@@ -1064,7 +1064,7 @@
10641064
"type": "Project",
10651065
"dependencies": {
10661066
"Newtonsoft.Json": "[13.0.3, )",
1067-
"YamlDotNet": "[16.1.3, )"
1067+
"YamlDotNet": "[16.2.0, )"
10681068
}
10691069
}
10701070
}

‎tests/PSRule.Tests/AssertTests.cs

+3-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace PSRule;
1515

1616
[Trait(LANGUAGE, LANGUAGEELEMENT)]
17-
public sealed class AssertTests
17+
public sealed class AssertTests : BaseTests
1818
{
1919
private const string LANGUAGE = "Language";
2020
private const string LANGUAGEELEMENT = "Variable";
@@ -1559,32 +1559,18 @@ public void NotLike()
15591559

15601560
#region Helper methods
15611561

1562-
private static void SetContext()
1562+
private void SetContext()
15631563
{
1564-
var context = PipelineContext.New(new Configuration.PSRuleOption(), null, null, new TestWriter(new Configuration.PSRuleOption()), null, null);
1564+
var context = PipelineContext.New(new Configuration.PSRuleOption(), null, null, GetTestWriter(), null, null);
15651565
var runspace = new RunspaceContext(context);
15661566
runspace.PushScope(RunspaceScope.Rule);
15671567
}
15681568

1569-
private static PSObject GetObject(params (string name, object value)[] properties)
1570-
{
1571-
var result = new PSObject();
1572-
for (var i = 0; properties != null && i < properties.Length; i++)
1573-
result.Properties.Add(new PSNoteProperty(properties[i].name, properties[i].value));
1574-
1575-
return result;
1576-
}
1577-
15781569
private static Runtime.Assert GetAssertionHelper()
15791570
{
15801571
return new Runtime.Assert();
15811572
}
15821573

1583-
private static string GetSourcePath(string fileName)
1584-
{
1585-
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
1586-
}
1587-
15881574
private bool AssertionResult(AssertResult result)
15891575
{
15901576
if (!result.Result)

‎tests/PSRule.Tests/BaseTests.cs

+30-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using System;
55
using System.IO;
6+
using System.Management.Automation;
7+
using PSRule.Configuration;
8+
using PSRule.Pipeline;
69
using PSRule.Pipeline.Emitters;
710

811
namespace PSRule;
@@ -14,9 +17,19 @@ public abstract class BaseTests
1417
{
1518
#region Helper methods
1619

20+
protected virtual PSRuleOption GetOption()
21+
{
22+
return new PSRuleOption();
23+
}
24+
25+
internal TestWriter GetTestWriter(PSRuleOption option = default)
26+
{
27+
return new TestWriter(option ?? GetOption());
28+
}
29+
1730
protected static string GetSourcePath(string fileName)
1831
{
19-
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
32+
return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName));
2033
}
2134

2235
internal static InternalFileInfo GetFileInfo(string fileName)
@@ -32,5 +45,21 @@ protected static string ReadFileAsString(string fileName)
3245
return reader.ReadToEnd();
3346
}
3447

48+
protected static Source[] GetSource(string path)
49+
{
50+
var builder = new SourcePipelineBuilder(null, null);
51+
builder.Directory(GetSourcePath(path));
52+
return builder.Build();
53+
}
54+
55+
protected static PSObject GetObject(params (string name, object value)[] properties)
56+
{
57+
var result = new PSObject();
58+
for (var i = 0; properties != null && i < properties.Length; i++)
59+
result.Properties.Add(new PSNoteProperty(properties[i].name, properties[i].value));
60+
61+
return result;
62+
}
63+
3564
#endregion Helper methods
3665
}

‎tests/PSRule.Tests/BaselineTests.cs

+2-21
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.IO;
65
using System.Linq;
76
using System.Management.Automation;
87
using Newtonsoft.Json;
98
using Newtonsoft.Json.Linq;
10-
using PSRule.Configuration;
119
using PSRule.Definitions;
1210
using PSRule.Definitions.Baselines;
1311
using PSRule.Host;
@@ -18,7 +16,7 @@
1816

1917
namespace PSRule;
2018

21-
public sealed class BaselineTests
19+
public sealed class BaselineTests : BaseTests
2220
{
2321
private const string BaselineYamlFileName = "Baseline.Rule.yaml";
2422
private const string BaselineJsonFileName = "Baseline.Rule.jsonc";
@@ -175,7 +173,7 @@ public void BaselineAsJson()
175173

176174
#region Helper methods
177175

178-
private static Baseline[] GetBaselines(Source[] source)
176+
private Baseline[] GetBaselines(Source[] source)
179177
{
180178
var context = new RunspaceContext(PipelineContext.New(GetOption(), null, null, new TestWriter(GetOption()), new OptionContextBuilder(), null));
181179
context.Init(source);
@@ -184,18 +182,6 @@ private static Baseline[] GetBaselines(Source[] source)
184182
return baseline;
185183
}
186184

187-
private static PSRuleOption GetOption()
188-
{
189-
return new PSRuleOption();
190-
}
191-
192-
private static Source[] GetSource(string path)
193-
{
194-
var builder = new SourcePipelineBuilder(null, null);
195-
builder.Directory(GetSourcePath(path));
196-
return builder.Build();
197-
}
198-
199185
private static Source[] GetSourceInModule(string path, string moduleName, SourceType type)
200186
{
201187
var file = new SourceFile(GetSourcePath(path), moduleName, type, null);
@@ -207,10 +193,5 @@ private static Source[] GetSourceInModule(string path, string moduleName, Source
207193
return [source];
208194
}
209195

210-
private static string GetSourcePath(string fileName)
211-
{
212-
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
213-
}
214-
215196
#endregion Helper methods
216197
}

‎tests/PSRule.Tests/ConventionTests.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System;
5-
using System.IO;
64
using System.Management.Automation;
75
using PSRule.Configuration;
86
using PSRule.Pipeline;
97
using static PSRule.PipelineTests;
108

119
namespace PSRule;
1210

13-
public sealed class ConventionTests
11+
public sealed class ConventionTests : BaseTests
1412
{
1513
[Fact]
1614
public void WithConventions()
@@ -98,14 +96,9 @@ private static Source[] GetSource()
9896
private static PSRuleOption GetOption(string path = null)
9997
{
10098
var option = path == null ? new PSRuleOption() : PSRuleOption.FromFile(path);
101-
option.Output.Culture = new[] { "en-US" };
99+
option.Output.Culture = ["en-US"];
102100
return option;
103101
}
104102

105-
private static string GetSourcePath(string fileName)
106-
{
107-
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
108-
}
109-
110103
#endregion Helper methods
111104
}

‎tests/PSRule.Tests/LockFileTests.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System;
54
using PSRule.Pipeline.Dependencies;
65

76
namespace PSRule;
87

9-
public sealed class LockFileTests
8+
public sealed class LockFileTests : BaseTests
109
{
1110
[Fact]
1211
public void ReadFile()
@@ -18,13 +17,4 @@ public void ReadFile()
1817
Assert.True(lockFile.Modules.TryGetValue("psrule.rules.msft.oss", out item));
1918
Assert.Equal("1.1.0", item.Version.ToString());
2019
}
21-
22-
#region Helper methods
23-
24-
private static string GetSourcePath(string fileName)
25-
{
26-
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
27-
}
28-
29-
#endregion Helper methods
3020
}

0 commit comments

Comments
 (0)
Please sign in to comment.