Skip to content

Commit

Permalink
Test refactoring (microsoft#2600)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Nov 12, 2024
1 parent 7a99155 commit ea4a77c
Show file tree
Hide file tree
Showing 23 changed files with 127 additions and 225 deletions.
6 changes: 3 additions & 3 deletions src/PSRule.Benchmark/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1986,8 +1986,8 @@
},
"YamlDotNet": {
"type": "Transitive",
"resolved": "16.1.3",
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
"resolved": "16.2.0",
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
},
"Microsoft.PSRule.Badges": {
"type": "Project",
Expand All @@ -2012,7 +2012,7 @@
"type": "Project",
"dependencies": {
"Newtonsoft.Json": "[13.0.3, )",
"YamlDotNet": "[16.1.3, )"
"YamlDotNet": "[16.2.0, )"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/PSRule.CommandLine/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1635,8 +1635,8 @@
},
"YamlDotNet": {
"type": "Transitive",
"resolved": "16.1.3",
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
"resolved": "16.2.0",
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
},
"Microsoft.PSRule.Badges": {
"type": "Project",
Expand Down Expand Up @@ -1667,7 +1667,7 @@
"type": "Project",
"dependencies": {
"Newtonsoft.Json": "[13.0.3, )",
"YamlDotNet": "[16.1.3, )"
"YamlDotNet": "[16.2.0, )"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/PSRule.SDK/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,8 @@
},
"YamlDotNet": {
"type": "Transitive",
"resolved": "16.1.3",
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
"resolved": "16.2.0",
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
},
"Microsoft.PSRule.Badges": {
"type": "Project",
Expand All @@ -1059,7 +1059,7 @@
"type": "Project",
"dependencies": {
"Newtonsoft.Json": "[13.0.3, )",
"YamlDotNet": "[16.1.3, )"
"YamlDotNet": "[16.2.0, )"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/PSRule.Tool/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,8 @@
},
"YamlDotNet": {
"type": "Transitive",
"resolved": "16.1.3",
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
"resolved": "16.2.0",
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
},
"Microsoft.PSRule.Badges": {
"type": "Project",
Expand Down Expand Up @@ -1673,7 +1673,7 @@
"type": "Project",
"dependencies": {
"Newtonsoft.Json": "[13.0.3, )",
"YamlDotNet": "[16.1.3, )"
"YamlDotNet": "[16.2.0, )"
}
}
},
Expand Down
51 changes: 51 additions & 0 deletions src/PSRule/Definitions/IResourceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Definitions.Selectors;
using PSRule.Definitions.SuppressionGroups;
using PSRule.Runtime;

namespace PSRule.Definitions;

#nullable enable

/// <summary>
/// Extensions for resource types.
/// </summary>
internal static class IResourceExtensions
{
/// <summary>
/// Convert a suppression group into a suppression group visitor.
/// </summary>
/// <param name="resource">The suppression group resource.</param>
/// <param name="runspaceContext">A valid runspace context.</param>
/// <returns>An instance of a <see cref="SuppressionGroupVisitor"/>.</returns>
public static SuppressionGroupVisitor ToSuppressionGroupVisitor(this SuppressionGroupV1 resource, RunspaceContext runspaceContext)
{
return new SuppressionGroupVisitor(
context: runspaceContext,
id: resource.Id,
source: resource.Source,
spec: resource.Spec,
info: resource.Info
);
}

/// <summary>
/// Converts a selector into a selector visitor.
/// </summary>
/// <param name="resource">The selector resource.</param>
/// <param name="runspaceContext">A valid runspace context.</param>
/// <returns>An instance of a <see cref="SelectorVisitor"/>.</returns>
public static SelectorVisitor ToSelectorVisitor(this SelectorV1 resource, RunspaceContext runspaceContext)
{
return new SelectorVisitor(
runspaceContext,
resource.Id,
resource.Source,
resource.Spec.If
);
}
}

#nullable restore
6 changes: 3 additions & 3 deletions src/PSRule/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@
},
"YamlDotNet": {
"type": "Transitive",
"resolved": "16.1.3",
"contentHash": "gtHGiDvU9VTtWte8f0thIM38cL1oowOjStKpeAEKKfA+Rc4AvekJzqFDZiiPcc4kw00ZiwR4OTJS56L16q98DQ=="
"resolved": "16.2.0",
"contentHash": "iKVYog7JNiFOTr4gMHSjCqrGFD2NyABUHgSkRq9XR8A2+fsWepSWTpjHAOnKo1LVAHjy33o6I0u6lo5Ng0NtAw=="
},
"Microsoft.PSRule.Badges": {
"type": "Project",
Expand All @@ -1064,7 +1064,7 @@
"type": "Project",
"dependencies": {
"Newtonsoft.Json": "[13.0.3, )",
"YamlDotNet": "[16.1.3, )"
"YamlDotNet": "[16.2.0, )"
}
}
}
Expand Down
20 changes: 3 additions & 17 deletions tests/PSRule.Tests/AssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace PSRule;

[Trait(LANGUAGE, LANGUAGEELEMENT)]
public sealed class AssertTests
public sealed class AssertTests : BaseTests
{
private const string LANGUAGE = "Language";
private const string LANGUAGEELEMENT = "Variable";
Expand Down Expand Up @@ -1559,32 +1559,18 @@ public void NotLike()

#region Helper methods

private static void SetContext()
private void SetContext()
{
var context = PipelineContext.New(new Configuration.PSRuleOption(), null, null, new TestWriter(new Configuration.PSRuleOption()), null, null);
var context = PipelineContext.New(new Configuration.PSRuleOption(), null, null, GetTestWriter(), null, null);
var runspace = new RunspaceContext(context);
runspace.PushScope(RunspaceScope.Rule);
}

private static PSObject GetObject(params (string name, object value)[] properties)
{
var result = new PSObject();
for (var i = 0; properties != null && i < properties.Length; i++)
result.Properties.Add(new PSNoteProperty(properties[i].name, properties[i].value));

return result;
}

private static Runtime.Assert GetAssertionHelper()
{
return new Runtime.Assert();
}

private static string GetSourcePath(string fileName)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
}

private bool AssertionResult(AssertResult result)
{
if (!result.Result)
Expand Down
31 changes: 30 additions & 1 deletion tests/PSRule.Tests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

using System;
using System.IO;
using System.Management.Automation;
using PSRule.Configuration;
using PSRule.Pipeline;
using PSRule.Pipeline.Emitters;

namespace PSRule;
Expand All @@ -14,9 +17,19 @@ public abstract class BaseTests
{
#region Helper methods

protected virtual PSRuleOption GetOption()
{
return new PSRuleOption();
}

internal TestWriter GetTestWriter(PSRuleOption option = default)
{
return new TestWriter(option ?? GetOption());
}

protected static string GetSourcePath(string fileName)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
return Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName));
}

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

protected static Source[] GetSource(string path)
{
var builder = new SourcePipelineBuilder(null, null);
builder.Directory(GetSourcePath(path));
return builder.Build();
}

protected static PSObject GetObject(params (string name, object value)[] properties)
{
var result = new PSObject();
for (var i = 0; properties != null && i < properties.Length; i++)
result.Properties.Add(new PSNoteProperty(properties[i].name, properties[i].value));

return result;
}

#endregion Helper methods
}
23 changes: 2 additions & 21 deletions tests/PSRule.Tests/BaselineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// Licensed under the MIT License.

using System;
using System.IO;
using System.Linq;
using System.Management.Automation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PSRule.Configuration;
using PSRule.Definitions;
using PSRule.Definitions.Baselines;
using PSRule.Host;
Expand All @@ -18,7 +16,7 @@

namespace PSRule;

public sealed class BaselineTests
public sealed class BaselineTests : BaseTests
{
private const string BaselineYamlFileName = "Baseline.Rule.yaml";
private const string BaselineJsonFileName = "Baseline.Rule.jsonc";
Expand Down Expand Up @@ -175,7 +173,7 @@ public void BaselineAsJson()

#region Helper methods

private static Baseline[] GetBaselines(Source[] source)
private Baseline[] GetBaselines(Source[] source)
{
var context = new RunspaceContext(PipelineContext.New(GetOption(), null, null, new TestWriter(GetOption()), new OptionContextBuilder(), null));
context.Init(source);
Expand All @@ -184,18 +182,6 @@ private static Baseline[] GetBaselines(Source[] source)
return baseline;
}

private static PSRuleOption GetOption()
{
return new PSRuleOption();
}

private static Source[] GetSource(string path)
{
var builder = new SourcePipelineBuilder(null, null);
builder.Directory(GetSourcePath(path));
return builder.Build();
}

private static Source[] GetSourceInModule(string path, string moduleName, SourceType type)
{
var file = new SourceFile(GetSourcePath(path), moduleName, type, null);
Expand All @@ -207,10 +193,5 @@ private static Source[] GetSourceInModule(string path, string moduleName, Source
return [source];
}

private static string GetSourcePath(string fileName)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
}

#endregion Helper methods
}
11 changes: 2 additions & 9 deletions tests/PSRule.Tests/ConventionTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.IO;
using System.Management.Automation;
using PSRule.Configuration;
using PSRule.Pipeline;
using static PSRule.PipelineTests;

namespace PSRule;

public sealed class ConventionTests
public sealed class ConventionTests : BaseTests
{
[Fact]
public void WithConventions()
Expand Down Expand Up @@ -98,14 +96,9 @@ private static Source[] GetSource()
private static PSRuleOption GetOption(string path = null)
{
var option = path == null ? new PSRuleOption() : PSRuleOption.FromFile(path);
option.Output.Culture = new[] { "en-US" };
option.Output.Culture = ["en-US"];
return option;
}

private static string GetSourcePath(string fileName)
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
}

#endregion Helper methods
}
12 changes: 1 addition & 11 deletions tests/PSRule.Tests/LockFileTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using PSRule.Pipeline.Dependencies;

namespace PSRule;

public sealed class LockFileTests
public sealed class LockFileTests : BaseTests
{
[Fact]
public void ReadFile()
Expand All @@ -18,13 +17,4 @@ public void ReadFile()
Assert.True(lockFile.Modules.TryGetValue("psrule.rules.msft.oss", out item));
Assert.Equal("1.1.0", item.Version.ToString());
}

#region Helper methods

private static string GetSourcePath(string fileName)
{
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
}

#endregion Helper methods
}
Loading

0 comments on commit ea4a77c

Please sign in to comment.