Skip to content

Commit

Permalink
(GH-188) Add IIssue.FileLink and pass FileLink settings to issue prov…
Browse files Browse the repository at this point in the history
…iders (#192)

* (GH-188) Add possibility to pass file link settings while reading issues

* (GH-188) Add IIssue.FileLink
  • Loading branch information
pascalberger authored Jul 31, 2020
1 parent ba8d445 commit bece6c4
Show file tree
Hide file tree
Showing 32 changed files with 750 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/Cake.Issues.Testing/BaseIssueProviderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class BaseIssueProviderFixture<T>
protected BaseIssueProviderFixture()
{
this.Log = new FakeLog { Verbosity = Verbosity.Normal };
this.RepositorySettings = new RepositorySettings(@"c:\repo");
this.ReadIssuesSettings = new ReadIssuesSettings(@"c:\repo");
}

/// <summary>
Expand All @@ -30,7 +30,7 @@ protected BaseIssueProviderFixture()
/// <summary>
/// Gets or sets the repository settings.
/// </summary>
public RepositorySettings RepositorySettings { get; set; }
public ReadIssuesSettings ReadIssuesSettings { get; set; }

/// <summary>
/// Calls <see cref="BaseIssueProvider.ReadIssues()"/>.
Expand Down Expand Up @@ -67,12 +67,12 @@ private T CreateIssueProvider()
typeof(T),
this.GetCreateIssueProviderArguments().ToArray());

if (this.RepositorySettings == null)
if (this.ReadIssuesSettings == null)
{
throw new InvalidOperationException("No repository settings set.");
throw new InvalidOperationException("No settings for reading issues set.");
}

provider.Initialize(this.RepositorySettings);
provider.Initialize(this.ReadIssuesSettings);
return provider;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Issues.Testing/FakeConfigurableIssueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public FakeConfigurableIssueProvider(
public override string ProviderName => "Fake Issue Provider";

/// <inheritdoc/>
protected override IEnumerable<IIssue> InternalReadIssues()
protected override IEnumerable<IIssue> InternalReadIssues(FileLinkSettings fileLinkSettings)
{
return this.issues;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Issues.Testing/FakeIssueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public FakeIssueProvider(ICakeLog log, IEnumerable<IIssue> issues)
public override string ProviderName => "Fake Issue Provider";

/// <inheritdoc/>
protected override IEnumerable<IIssue> InternalReadIssues()
protected override IEnumerable<IIssue> InternalReadIssues(FileLinkSettings fileLinkSettings)
{
return this.issues;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Cake.Issues.Testing/IssueChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static void Check(
expectedIssue.EndLine,
expectedIssue.Column,
expectedIssue.EndColumn,
expectedIssue.FileLink,
expectedIssue.MessageText,
expectedIssue.MessageHtml,
expectedIssue.MessageMarkdown,
Expand Down Expand Up @@ -81,6 +82,8 @@ public static void Check(
/// <c>null</c> if the issue is not expected to be related to a file or specific column.</param>
/// <param name="endColumn">Expected end of column range.
/// <c>null</c> if the issue is not expected to be related to a file, specific column or range of columns.</param>
/// <param name="fileLink">Expected file link.
/// <c>null</c> if the issue is not expected to have a file link.</param>
/// <param name="messageText">Expected message in plain text format.</param>
/// <param name="messageHtml">Expected message in HTML format.</param>
/// <param name="messageMarkdown">Expected message in Markdown format.</param>
Expand All @@ -105,6 +108,7 @@ public static void Check(
int? endLine,
int? column,
int? endColumn,
Uri fileLink,
string messageText,
string messageHtml,
string messageMarkdown,
Expand Down Expand Up @@ -215,6 +219,12 @@ public static void Check(
$"Expected issue.EndColumn to be '{endColumn}' but was '{issue.EndColumn}'.");
}

if (issue.FileLink?.ToString() != fileLink?.ToString())
{
throw new Exception(
$"Expected issue.FileLink to be '{fileLink}' but was '{issue.FileLink}'.");
}

if (issue.MessageText != messageText)
{
throw new Exception(
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Issues.Tests/BaseMultiFormatIssueProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void Should_Read_Issues_From_Format()
"Foo".ToByteArray(),
format);
var provider = new FakeMultiFormatIssueProvider(log, settings);
provider.Initialize(new RepositorySettings(@"c:\repo"));
provider.Initialize(new ReadIssuesSettings(@"c:\repo"));

// When
var result = provider.ReadIssues();
Expand Down
140 changes: 140 additions & 0 deletions src/Cake.Issues.Tests/IIssueComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,41 @@ public void Should_Return_False_If_Column_Is_Different(int? column1, int? column
CompareIssues(issue1, issue2, false);
}

[Theory]
[InlineData("http://foo", "http://bar")]
[InlineData("http://foo", null)]
[InlineData(null, "http://foo")]
public void Should_Return_False_If_FileLink_Is_Different(string fileLink1, string fileLink2)
{
// Given
var issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink1))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink1));
}

var issue1 = issueBuilder.Create();

issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink2))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink2));
}

var issue2 = issueBuilder.Create();

// When / Then
CompareIssues(issue1, issue2, false);
}

[Fact]
public void Should_Return_False_If_MessageText_Is_Different()
{
Expand Down Expand Up @@ -532,6 +567,41 @@ public void Should_Return_True_If_Column_Is_Same(int? column1, int? column2)
CompareIssues(issue1, issue2, true);
}

[Theory]
[InlineData("http://foo", "http://foo")]
[InlineData("http://foo", "http://Foo")]
[InlineData(null, null)]
public void Should_Return_True_If_FileLink_Is_Same(string fileLink1, string fileLink2)
{
// Given
var issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink1))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink1));
}

var issue1 = issueBuilder.Create();

issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink2))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink2));
}

var issue2 = issueBuilder.Create();

// When / Then
CompareIssues(issue1, issue2, true);
}

[Fact]
public void Should_Return_True_If_MessageText_Is_Same()
{
Expand Down Expand Up @@ -1322,6 +1392,76 @@ public void Should_Return_True_If_Column_Is_Same(int? column1, int? column2)
CompareIssues(issue1, issue2, true);
}

[Theory]
[InlineData("http://foo", "http://bar")]
[InlineData("http://foo", null)]
[InlineData(null, "http://foo")]
public void Should_Return_True_If_FileLink_Is_Different(string fileLink1, string fileLink2)
{
// Given
var issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink1))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink1));
}

var issue1 = issueBuilder.Create();

issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink2))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink2));
}

var issue2 = issueBuilder.Create();

// When / Then
CompareIssues(issue1, issue2, true);
}

[Theory]
[InlineData("http://foo", "http://foo")]
[InlineData("http://foo", "http://Foo")]
[InlineData(null, null)]
public void Should_Return_True_If_FileLink_Is_Same(string fileLink1, string fileLink2)
{
// Given
var issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink1))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink1));
}

var issue1 = issueBuilder.Create();

issueBuilder =
IssueBuilder
.NewIssue("message", "providerType", "providerName");
if (!string.IsNullOrEmpty(fileLink2))
{
issueBuilder =
issueBuilder
.WithFileLink(new Uri(fileLink2));
}

var issue2 = issueBuilder.Create();

// When / Then
CompareIssues(issue1, issue2, true);
}

[Fact]
public void Should_Return_True_If_MessageText_Is_Same()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Cake.Issues.Tests/IIssueExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ public void Should_Throw_If_Issue_Is_Null()
[InlineData("foo {EndLine} bar", "foo 420 bar")]
[InlineData("foo {Column} bar", "foo 23 bar")]
[InlineData("foo {EndColumn} bar", "foo 230 bar")]
[InlineData("foo {FileLink} bar", "foo https://github.com/myorg/myrepo/blob/develop/src/foo.cs#L10-L12 bar")]
[InlineData("foo {Rule} bar", "foo Rule Foo bar")]
[InlineData("foo {RuleUrl} bar", "foo https://google.com/ bar")]
[InlineData("foo {MessageText} bar", "foo MessageText Foo bar")]
Expand All @@ -327,6 +328,7 @@ public void Should_Replace_Tokens(string pattern, string expectedResult)
.WithMessageInMarkdownFormat("MessageMarkdown Foo")
.InFile(@"src/Cake.Issues/foo.cs", 42, 420, 23, 230)
.InProject(@"src/Cake.Issues/Cake.Issues.csproj", "Cake.Issues")
.WithFileLink(new Uri("https://github.com/myorg/myrepo/blob/develop/src/foo.cs#L10-L12"))
.OfRule("Rule Foo", new Uri("https://google.com"))
.WithPriority(IssuePriority.Error)
.Create();
Expand Down
32 changes: 32 additions & 0 deletions src/Cake.Issues.Tests/IssueBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,38 @@ public void Should_Set_EndColumn(int endColumn)
}
}

public sealed class TheWithFileLinkMethod
{
[Fact]
public void Should_Throw_If_FileLink_Is_Null()
{
// Given
var fixture = new IssueBuilderFixture();
Uri fileLink = null;

// When
var result = Record.Exception(() =>
fixture.IssueBuilder.WithFileLink(fileLink));

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

[Theory]
[InlineData("https://google.com/")]
public void Should_Set_FileLink(string fileLink)
{
// Given
var fixture = new IssueBuilderFixture();

// When
var issue = fixture.IssueBuilder.WithFileLink(new Uri(fileLink)).Create();

// Then
issue.FileLink.ToString().ShouldBe(fileLink);
}
}

public sealed class TheWithPriorityMethod
{
[Fact]
Expand Down
53 changes: 53 additions & 0 deletions src/Cake.Issues.Tests/IssueReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,59 @@ public void Should_Set_Run_Property()
issues.ShouldContain(issue2);
issue2.Run.ShouldBe(run);
}

[Fact]
public void Should_Set_FileLink_Property()
{
// Given
var filePath1 = @"src\Cake.Issues.Tests\Foo.cs";
var line1 = 10;
var issue1 =
IssueBuilder
.NewIssue("Foo", "ProviderTypeFoo", "ProviderNameFoo")
.InFile(filePath1, line1)
.OfRule("Foo")
.WithPriority(IssuePriority.Warning)
.Create();
var filePath2 = @"src\Cake.Issues.Tests\Bar.cs";
var line2 = 12;
var issue2 =
IssueBuilder
.NewIssue("Bar", "ProviderTypeBar", "ProviderNameBar")
.InFile(filePath2, line2)
.OfRule("Bar")
.WithPriority(IssuePriority.Warning)
.Create();
var fixture = new IssuesFixture();
fixture.IssueProviders.Clear();
fixture.IssueProviders.Add(
new FakeIssueProvider(
fixture.Log,
new List<IIssue>
{
issue1,
issue2,
}));
var repoUrl = "https://github.com/cake-contrib/Cake.Issues.Website";
var branch = "develop";
fixture.Settings.FileLinkSettings =
FileLinkSettings.GitHub(
new System.Uri(repoUrl),
branch,
null);

// When
var issues = fixture.ReadIssues().ToList();

// Then
issues.Count.ShouldBe(2);
issues.ShouldContain(issue1);
issue1.FileLink.ToString()
.ShouldBe($"{repoUrl}/blob/{branch}/{filePath1.Replace(@"\", "/")}#L{line1}");
issues.ShouldContain(issue2);
issue2.FileLink.ToString()
.ShouldBe($"{repoUrl}/blob/{branch}/{filePath2.Replace(@"\", "/")}#L{line2}");
}
}
}
}
Loading

0 comments on commit bece6c4

Please sign in to comment.