Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<Description>Tests for the Cake.Issues.Reporting.Console addin</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Spectre.Console.Testing" />
<PackageReference Include="Verify.Xunit" />
</ItemGroup>

<ItemGroup>
<None Remove="Testfiles\**\*" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Issues.Serialization;
using Spectre.Console.Testing;

internal class ConsoleIssueReportFixture
{
public TestConsole Console { get; set; } = new();

public FakeLog Log { get; set; } = new() { Verbosity = Verbosity.Normal };

public ConsoleIssueReportFormatSettings ConsoleIssueReportFormatSettings { get; set; } = new();

public string CreateReportForTestfile(string fileResourceName, DirectoryPath repositoryRootPath)
public void CreateReportForTestfile(string fileResourceName, DirectoryPath repositoryRootPath)
{
fileResourceName.NotNullOrWhiteSpace();

Expand All @@ -28,22 +31,23 @@ public string CreateReportForTestfile(string fileResourceName, DirectoryPath rep
}

var issues = reader.ReadToEnd().DeserializeToIssues();
return this.CreateReport(issues, repositoryRootPath);
this.CreateReport(issues, repositoryRootPath);
}
}

public string CreateReport(IEnumerable<IIssue> issues, DirectoryPath repositoryRootPath)
public void CreateReport(IEnumerable<IIssue> issues, DirectoryPath repositoryRootPath)
{
var generator =
new ConsoleIssueReportGenerator(this.Log, this.ConsoleIssueReportFormatSettings);
new ConsoleIssueReportGenerator(
this.Console,
this.Log,
this.ConsoleIssueReportFormatSettings);

var createIssueReportSettings =
new CreateIssueReportSettings(repositoryRootPath, string.Empty);
_ = generator.Initialize(createIssueReportSettings);
_ = generator.CreateReport(issues);

// TODO Return console output
return string.Empty;
}

public void TestReportCreation(Action<ConsoleIssueReportFormatSettings> settings)
Expand All @@ -52,20 +56,17 @@ public void TestReportCreation(Action<ConsoleIssueReportFormatSettings> settings
settings(this.ConsoleIssueReportFormatSettings);

// When
var result =
this.CreateReport(
[
IssueBuilder
.NewIssue("Message Foo", "ProviderType Foo", "ProviderName Foo")
.InFile(@"src\Cake.Issues.Reporting.Generic.Tests\Foo.cs", 10)
.OfRule("Rule Foo")
.WithPriority(IssuePriority.Warning)
.Create(),
],
@"c:\Source\Cake.Issues.Reporting.Console");
this.CreateReport(
[
IssueBuilder
.NewIssue("Message Foo", "ProviderType Foo", "ProviderName Foo")
.InFile(@"src\Cake.Issues.Reporting.Generic.Tests\Foo.cs", 10)
.OfRule("Rule Foo")
.WithPriority(IssuePriority.Warning)
.Create(),
],
@"c:\Source\Cake.Issues.Reporting.Console");

// Then
// Currently only checks if generation failed or not without checking actual output.
_ = result.ShouldNotBeNull();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
namespace Cake.Issues.Reporting.Console.Tests;

using System.Text.RegularExpressions;
using Cake.Core.Diagnostics;
using Spectre.Console;
using Spectre.Console.Testing;
using Xunit.Abstractions;

public sealed class ConsoleIssueReportGeneratorTests
public sealed partial class ConsoleIssueReportGeneratorTests
{
public sealed class TheCtor
{
[Fact]
public void Should_Throw_If_Console_Is_Null()
{
// Given
IAnsiConsole console = null;
var log = new FakeLog();
var settings = new ConsoleIssueReportFormatSettings();

// When
var result = Record.Exception(() =>
new ConsoleIssueReportGenerator(
console,
log,
settings));

// Then
result.IsArgumentNullException("console");
}

[Fact]
public void Should_Throw_If_Log_Is_Null()
{
// Given / When
// Given
var console = new TestConsole();
ICakeLog log = null;
var settings = new ConsoleIssueReportFormatSettings();

// When
var result = Record.Exception(() =>
new ConsoleIssueReportGenerator(
null,
new ConsoleIssueReportFormatSettings()));
console,
log,
settings));

// Then
result.IsArgumentNullException("log");
Expand All @@ -22,18 +51,24 @@ public void Should_Throw_If_Log_Is_Null()
[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given / When
// Given
var console = new TestConsole();
var log = new FakeLog();
ConsoleIssueReportFormatSettings settings = null;

// When
var result = Record.Exception(() =>
new ConsoleIssueReportGenerator(
new FakeLog(),
null));
console,
log,
settings));

// Then
result.IsArgumentNullException("settings");
}
}

public sealed class TheInternalCreateReportMethod
public sealed partial class TheInternalCreateReportMethod
{
public static IEnumerable<object[]> ReportFormatSettingsCombinations =>
from b1 in boolArray
Expand Down Expand Up @@ -68,7 +103,7 @@ public void Should_Generate_Report(
};

// When
_ = fixture.CreateReportForTestfile(
fixture.CreateReportForTestfile(
"Testfiles.issues.json",
@"c:\Source\Cake.Issues.Reporting.Console");

Expand Down Expand Up @@ -98,15 +133,21 @@ public void Should_Generate_Report_With_No_Issues(
};

// When
_ = fixture.CreateReport(
fixture.CreateReport(
[],
@"c:\Source\Cake.Issues.Reporting.Console");

// Then
}

public sealed class WithShowDiagnosticsEnabled(ITestOutputHelper output)
public sealed partial class WithShowDiagnosticsEnabled(ITestOutputHelper output)
{
// (?<=┌─\[) — positive lookbehind to assert the match is preceded by ┌─[
// [^\]]+ — matches one or more characters that are not a closing bracket ]
// (?=\]) — positive lookahead to assert the match is followed by ]
[GeneratedRegex(@"(?<=┌─\[)[^\]]+(?=\])")]
private static partial Regex DiagnosticRegEx();

[Fact]
public void Should_Filter_Issues_Without_FilePath()
{
Expand All @@ -128,7 +169,7 @@ public void Should_Filter_Issues_Without_FilePath()
};

// When
_ = fixture.CreateReport(issues, @"c:\Source\Cake.Issues.Reporting.Console");
fixture.CreateReport(issues, @"c:\Source\Cake.Issues.Reporting.Console");

// Then
fixture.Log.Entries.ShouldContain(x => x.Message == "1 issue(s) were filtered because they either don't belong to a file or the file does not exist.");
Expand All @@ -155,7 +196,7 @@ public void Should_Filter_Issues_Where_File_Does_Not_Exist()
};

// When
_ = fixture.CreateReport(issues, @"c:\Source\Cake.Issues.Reporting.Console");
fixture.CreateReport(issues, @"c:\Source\Cake.Issues.Reporting.Console");

// Then
fixture.Log.Entries.ShouldContain(x => x.Message == "1 issue(s) were filtered because they either don't belong to a file or the file does not exist.");
Expand Down Expand Up @@ -187,7 +228,7 @@ public void Should_Not_Filter_Issues_With_Existing_File()
};

// When
_ = fixture.CreateReport(issues, directory);
fixture.CreateReport(issues, directory);

// Then
fixture.Log.Entries.ShouldContain(x => x.Message == "0 issue(s) were filtered because they either don't belong to a file or the file does not exist.");
Expand Down Expand Up @@ -218,7 +259,7 @@ public void Should_Work_Without_Priority()
.Create(),
};
// When
_ = fixture.CreateReport(issues, directory);
fixture.CreateReport(issues, directory);

// Then
}
Expand Down Expand Up @@ -249,10 +290,49 @@ public void Should_Work_With_Priority()
.Create(),
};
// When
_ = fixture.CreateReport(issues, directory);
fixture.CreateReport(issues, directory);

// Then
}

[Fact]
public Task Should_Work_With_Issue_On_End_Of_Line()
{
// Given
using var tempSourceFile = new TemporarySourceFile("Testfiles.TestFile.txt");
var filePath = tempSourceFile.FilePath;
output.WriteLine($"File path: {filePath}");
var directory = Path.GetDirectoryName(filePath)!;
var fileName = Path.GetFileName(filePath);
var fixture = new ConsoleIssueReportFixture
{
ConsoleIssueReportFormatSettings =
{
ShowDiagnostics = true
}
};
var issues =
new List<IIssue>
{
IssueBuilder
.NewIssue("Message Foo", "ProviderType Foo", "ProviderName Foo")
.InFile(fileName, 1, 57) // Position after the last character on line 1
.Create(),
};
// When
fixture.CreateReport(issues, directory);

// Then
// Add a scrubber that replaces the dynamic ID in the output
var settings = new VerifySettings();
settings.AddScrubber(builder =>
{
var updated = DiagnosticRegEx().Replace(builder.ToString(), "<DYNAMIC_ID>");

_ = builder.Clear().Append(updated);
});
return Verify(fixture.Console.Output, settings);
}
}
}
}
Loading
Loading