Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring for tests #2676

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions tests/PSRule.Tests/BaselineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void ReadBaselineYaml()
// TestBaseline4
Assert.Equal("TestBaseline4", baseline[3].Name);
Assert.Null(baseline[3].Info.Synopsis.Text);
Assert.Equal(SeverityLevel.Warning, baseline[3].Spec.Override.Level["rule1"]);
Assert.NotNull(baseline[3].Spec);
Assert.Equal(SeverityLevel.Warning, baseline[3].Spec.Override?.Level?["rule1"]);

// TestBaseline5
Assert.Equal("TestBaseline5", baseline[4].Name);
Expand Down Expand Up @@ -92,7 +93,9 @@ public void ReadBaselineJson()
// TestBaseline4
Assert.Equal("TestBaseline4", baseline[3].Name);
Assert.Null(baseline[3].Info.Synopsis.Text);
Assert.Equal(SeverityLevel.Warning, baseline[3].Spec.Override.Level["rule1"]);
Assert.NotNull(baseline[3].Spec.Override.Level);
Assert.True(baseline[3].Spec.Override.Level!.TryGetValue("rule1", out var level));
Assert.Equal(SeverityLevel.Warning, level);

// TestBaseline5
Assert.Equal("TestBaseline5", baseline[4].Name);
Expand Down Expand Up @@ -136,6 +139,7 @@ public void FilterBaseline(string path)
var filter = new BaselineFilter(["TestBaseline5"]);
var actual = baseline.FirstOrDefault(b => filter.Match(b));

Assert.NotNull(actual);
Assert.Equal("TestBaseline5", actual.Name);
}

Expand Down Expand Up @@ -170,7 +174,9 @@ public void BaselineAsJson()
{
var expected = new object[] { GetBaselines(GetSource(BaselineJsonFileName)) };
var json = JsonOutputWriter.ToJson(expected, null);
var actual = (JArray)JsonConvert.DeserializeObject<dynamic>(json);

Assert.NotNull(json);
var actual = (JArray)JsonConvert.DeserializeObject<dynamic>(json)!;

// TestBaseline1
Assert.Equal("github.com/microsoft/PSRule/v1", actual?[0]["apiVersion"]);
Expand Down
1 change: 0 additions & 1 deletion tests/PSRule.Tests/ConventionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Management.Automation;
using PSRule.Configuration;
using PSRule.Pipeline;
using static PSRule.PipelineTests;

namespace PSRule;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,13 @@
using PSRule.Configuration;
using PSRule.Definitions.Rules;
using PSRule.Options;
using PSRule.Pipeline;
using PSRule.Resources;
using PSRule.Rules;

namespace PSRule;
namespace PSRule.Pipeline;

public sealed class PipelineTests : ContextBaseTests
public sealed partial class PipelineTests : ContextBaseTests
{
internal class TestObject
{
public string Name { get; set; }
}

[Fact]
public void BuildInvokePipeline()
{
var option = GetOption();
var builder = PipelineBuilder.Invoke(GetSource(), option, null);
Assert.NotNull(builder.Build());
}

[Fact]
public void InvokePipeline()
{
Expand All @@ -49,7 +35,7 @@ public void InvokePipeline()
}

[Fact]
public void InvokePipelineWithJObject()
public void InvokePipeline_WithJObject_ShouldRunSuccessfully()
{
var parent = new JObject
{
Expand Down Expand Up @@ -94,7 +80,7 @@ public void InvokePipelineWithJObject()
}

[Fact]
public void InvokePipelineWithPathPrefix()
public void InvokePipeline_WithPathPrefix_ShouldRunSuccessfully()
{
var parent = new JObject
{
Expand Down Expand Up @@ -132,7 +118,7 @@ public void InvokePipelineWithPathPrefix()
}

[Fact]
public void InvokePipelineWithExclude()
public void InvokePipeline_WithExclude()
{
var option = GetOption(ruleExcludedAction: ExecutionActionPreference.Warn);
option.Rule.Include = ["FromFile1"];
Expand All @@ -148,13 +134,6 @@ public void InvokePipelineWithExclude()
Assert.Contains(writer.Warnings, (string s) => { return s == string.Format(Thread.CurrentThread.CurrentCulture, PSRuleResources.RuleExcluded, ".\\FromFile2"); });
}

[Fact]
public void BuildGetPipeline()
{
var builder = PipelineBuilder.Get(GetSource(), GetOption(), null);
Assert.NotNull(builder.Build());
}

[Fact]
public void GetRuleWithBaseline()
{
Expand Down
9 changes: 9 additions & 0 deletions tests/PSRule.Tests/TestObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Pipeline;

internal sealed class TestObject
{
public string Name { get; set; }
}
Loading