Skip to content
This repository has been archived by the owner on Dec 28, 2017. It is now read-only.

Commit

Permalink
Merge pull request #6 from cake-contrib/feature/GH-4
Browse files Browse the repository at this point in the history
(GH-4) Add support for DocFx projects in subfolders
  • Loading branch information
pascalberger committed May 23, 2017
2 parents 14ee5e6 + cf86603 commit 3b7e13e
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/Cake.Prca.Issues.DocFx.Tests/DocFxProviderFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System.Collections.Generic;
using System.IO;
using Core.Diagnostics;
using Core.IO;
using Testing;

internal class DocFxProviderFixture
{
public DocFxProviderFixture(string fileResourceName)
public DocFxProviderFixture(string fileResourceName, DirectoryPath docRootPath)
{
this.Log = new FakeLog { Verbosity = Verbosity.Normal };

Expand All @@ -17,7 +18,8 @@ public DocFxProviderFixture(string fileResourceName)
{
this.Settings =
DocFxIssuesSettings.FromContent(
sr.ReadToEnd());
sr.ReadToEnd(),
docRootPath);
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/Cake.Prca.Issues.DocFx.Tests/DocFxProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Should_Throw_If_Log_Is_Null()
var result = Record.Exception(() =>
new DocFxIssuesProvider(
null,
DocFxIssuesSettings.FromContent("Foo")));
DocFxIssuesSettings.FromContent("Foo", @"c:\Source\Cake.Prca")));

// Then
result.IsArgumentNullException("log");
Expand All @@ -38,11 +38,17 @@ public void Should_Throw_If_Settings_Are_Null()

public sealed class TheReadIssuesMethod
{
[Fact]
public void Should_Read_Issue_Correct()
[Theory]
[InlineData(@"c:\Source\Cake.Prca\docs", "docs/")]
[InlineData(@"docs", "docs/")]
[InlineData(@"c:\Source\Cake.Prca\src\docs", "src/docs/")]
[InlineData(@"src\docs", "src/docs/")]
[InlineData(@"c:\Source\Cake.Prca", "")]
[InlineData(@"/", "")]
public void Should_Read_Issue_Correct(string docRootPath, string docRelativePath)
{
// Given
var fixture = new DocFxProviderFixture("docfx.json");
var fixture = new DocFxProviderFixture("docfx.json", docRootPath);

// When
var issues = fixture.ReadIssues().ToList();
Expand All @@ -51,7 +57,7 @@ public void Should_Read_Issue_Correct()
issues.Count.ShouldBe(1);
CheckIssue(
issues[0],
@"index.md",
docRelativePath + @"index.md",
null,
"Build Document.LinkPhaseHandler.Apply Templates",
null,
Expand Down
36 changes: 28 additions & 8 deletions src/Cake.Prca.Issues.DocFx.Tests/DocFxSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Core.IO;
using Shouldly;
using Xunit;

Expand All @@ -16,7 +17,7 @@ public void Should_Throw_If_LogFilePath_Is_Null()
{
// Given / When
var result = Record.Exception(() =>
DocFxIssuesSettings.FromFilePath(null));
DocFxIssuesSettings.FromFilePath(null, @"c:\Source\Cake.Prca\docs"));

// Then
result.IsArgumentNullException("logFilePath");
Expand All @@ -27,7 +28,7 @@ public void Should_Throw_If_LogFileContent_Is_Null()
{
// Given / When
var result = Record.Exception(() =>
DocFxIssuesSettings.FromContent(null));
DocFxIssuesSettings.FromContent(null, @"c:\Source\Cake.Prca\docs"));

// Then
result.IsArgumentNullException("logFileContent");
Expand All @@ -38,7 +39,7 @@ public void Should_Throw_If_LogFileContent_Is_Empty()
{
// Given / When
var result = Record.Exception(() =>
DocFxIssuesSettings.FromContent(string.Empty));
DocFxIssuesSettings.FromContent(string.Empty, @"c:\Source\Cake.Prca\docs"));

// Then
result.IsArgumentOutOfRangeException("logFileContent");
Expand All @@ -49,29 +50,48 @@ public void Should_Throw_If_LogFileContent_Is_WhiteSpace()
{
// Given / When
var result = Record.Exception(() =>
DocFxIssuesSettings.FromContent(" "));
DocFxIssuesSettings.FromContent(" ", @"c:\Source\Cake.Prca\docs"));

// Then
result.IsArgumentOutOfRangeException("logFileContent");
}

[Fact]
public void Should_Set_Property_Values_Passed_To_Constructor()
public void Should_Set_LogFileContent()
{
// Given
const string logFileContent = "foo";

// When
var settings = DocFxIssuesSettings.FromContent(logFileContent);
var settings =
DocFxIssuesSettings.FromContent(
logFileContent,
@"c:\Source\Cake.Prca\docs");

// Then
settings.LogFileContent.ShouldBe(logFileContent);
}

[Fact]
public void Should_Set_DocRootPath()
{
// Given
DirectoryPath docRootPath = @"c:\Source\Cake.Prca\docs";

// When
var settings =
DocFxIssuesSettings.FromContent(
"foo",
docRootPath);

// Then
settings.DocRootPath.ShouldBe(docRootPath);
}

[Fact]
public void Should_Read_File_From_Disk()
{
var fileName = Path.GetTempFileName();
var fileName = System.IO.Path.GetTempFileName();
try
{
// Given
Expand All @@ -92,7 +112,7 @@ public void Should_Read_File_From_Disk()

// When
var settings =
DocFxIssuesSettings.FromFilePath(fileName);
DocFxIssuesSettings.FromFilePath(fileName, @"c:\Source\Cake.Prca\docs");

// Then
settings.LogFileContent.ShouldBe(expected);
Expand Down
44 changes: 42 additions & 2 deletions src/Cake.Prca.Issues.DocFx/DocFxIssuesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using Core.Diagnostics;
using Core.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -30,11 +31,18 @@ public DocFxIssuesProvider(ICakeLog log, DocFxIssuesSettings settings)
/// <inheritdoc />
protected override IEnumerable<ICodeAnalysisIssue> InternalReadIssues(PrcaCommentFormat format)
{
// Determine path of the doc root.
var docRootPath = this.settings.DocRootPath;
if (docRootPath.IsRelative)
{
docRootPath = docRootPath.MakeAbsolute(this.PrcaSettings.RepositoryRoot);
}

return
from logEntry in this.settings.LogFileContent.Split(new[] { '{', '}' }, StringSplitOptions.RemoveEmptyEntries).Select(x => "{" + x + "}")
let logEntryObject = JsonConvert.DeserializeObject<JToken>(logEntry)
let severity = (string)logEntryObject.SelectToken("message_severity")
let file = (string)logEntryObject.SelectToken("file")
let file = this.TryGetFile(logEntryObject, docRootPath)
let message = (string)logEntryObject.SelectToken("message")
let source = (string)logEntryObject.SelectToken("source") ?? "DocFx"
where
Expand All @@ -48,5 +56,37 @@ protected override IEnumerable<ICodeAnalysisIssue> InternalReadIssues(PrcaCommen
0,
source);
}

/// <summary>
/// Reads the affected file path from a issue logged in a DocFx log file.
/// </summary>
/// <param name="token">JSON Token object for the current log entry.</param>
/// <param name="docRootPath">Absolute path to the root of the DocFx project.</param>
/// <returns>The full path to the affected file.</returns>
private string TryGetFile(
JToken token,
DirectoryPath docRootPath)
{
var fileName = (string)token.SelectToken("file");

if (string.IsNullOrWhiteSpace(fileName))
{
return null;
}

// Add path to repository root
fileName = docRootPath.CombineWithFilePath(fileName).FullPath;

// Make path relative to repository root.
fileName = fileName.Substring(this.PrcaSettings.RepositoryRoot.FullPath.Length);

// Remove leading directory separator.
if (fileName.StartsWith("/"))
{
fileName = fileName.Substring(1);
}

return fileName;
}
}
}
}
93 changes: 86 additions & 7 deletions src/Cake.Prca.Issues.DocFx/DocFxIssuesProviderAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public static class DocFxIssuesProviderAliases
{
/// <summary>
/// Gets an instance of a provider for warnings reported by DocFx using a log file from disk.
/// Gets an instance of a provider for warnings reported by DocFx using a log file from disk
/// for a DocFx project in the repository root.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="logFilePath">Path to the the DocFx log file.</param>
Expand All @@ -24,7 +25,7 @@ public static class DocFxIssuesProviderAliases
/// var repoRoot = new DirectoryPath("c:\repo");
/// ReportCodeAnalysisIssuesToPullRequest(
/// DocFxIssuesFromFilePath(
/// new FilePath("C:\build\docfx.log")),
/// "C:\build\docfx.log"),
/// TfsPullRequests(
/// new Uri("http://myserver:8080/tfs/defaultcollection/myproject/_git/myrepository"),
/// "refs/heads/feature/myfeature",
Expand All @@ -42,11 +43,51 @@ public static class DocFxIssuesProviderAliases
context.NotNull(nameof(context));
logFilePath.NotNull(nameof(logFilePath));

return context.DocFxIssues(DocFxIssuesSettings.FromFilePath(logFilePath));
return context.DocFxIssuesFromFilePath(logFilePath, "/");
}

/// <summary>
/// Gets an instance of a provider for warnings reported by DocFx using log file content.
/// Gets an instance of a provider for warnings reported by DocFx using a log file from disk.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="logFilePath">Path to the the DocFx log file.</param>
/// <param name="docRootPath">Path to the root directory of the DocFx project.
/// Either the full path or the path relative to the repository root.</param>
/// <returns>Instance of a provider for warnings reported by DocFx.</returns>
/// <example>
/// <para>Report warnings reported by DocFx to a TFS pull request:</para>
/// <code>
/// <![CDATA[
/// var repoRoot = new DirectoryPath("c:\repo");
/// ReportCodeAnalysisIssuesToPullRequest(
/// DocFxIssuesFromFilePath(
/// "C:\build\docfx.log",
/// "C:\build\doc"),
/// TfsPullRequests(
/// new Uri("http://myserver:8080/tfs/defaultcollection/myproject/_git/myrepository"),
/// "refs/heads/feature/myfeature",
/// TfsAuthenticationNtlm()),
/// repoRoot);
/// ]]>
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory(CakeAliasConstants.CodeAnalysisProviderCakeAliasCategory)]
public static ICodeAnalysisProvider DocFxIssuesFromFilePath(
this ICakeContext context,
FilePath logFilePath,
DirectoryPath docRootPath)
{
context.NotNull(nameof(context));
logFilePath.NotNull(nameof(logFilePath));
docRootPath.NotNull(nameof(docRootPath));

return context.DocFxIssues(DocFxIssuesSettings.FromFilePath(logFilePath, docRootPath));
}

/// <summary>
/// Gets an instance of a provider for warnings reported by DocFx using log file content
/// for a DocFx project in the repository root.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="logFileContent">Content of the the DocFx log file.</param>
Expand Down Expand Up @@ -76,7 +117,46 @@ public static class DocFxIssuesProviderAliases
context.NotNull(nameof(context));
logFileContent.NotNullOrWhiteSpace(nameof(logFileContent));

return context.DocFxIssues(DocFxIssuesSettings.FromContent(logFileContent));
return context.DocFxIssues(DocFxIssuesSettings.FromContent(logFileContent, "/"));
}

/// <summary>
/// Gets an instance of a provider for warnings reported by DocFx using log file content.
/// </summary>
/// <param name="context">The context.</param>
/// <param name="logFileContent">Content of the the DocFx log file.</param>
/// <param name="docRootPath">Path to the root directory of the DocFx project.
/// Either the full path or the path relative to the repository root.</param>
/// <returns>Instance of a provider for warnings reported by DocFx.</returns>
/// <example>
/// <para>Report warnings reported by DocFx to a TFS pull request:</para>
/// <code>
/// <![CDATA[
/// var repoRoot = new DirectoryPath("c:\repo");
/// ReportCodeAnalysisIssuesToPullRequest(
/// DocFxIssuesFromContent(
/// logFileContent,
/// "C:\build\doc"),
/// TfsPullRequests(
/// new Uri("http://myserver:8080/tfs/defaultcollection/myproject/_git/myrepository"),
/// "refs/heads/feature/myfeature",
/// TfsAuthenticationNtlm()),
/// repoRoot);
/// ]]>
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory(CakeAliasConstants.CodeAnalysisProviderCakeAliasCategory)]
public static ICodeAnalysisProvider DocFxIssuesFromContent(
this ICakeContext context,
string logFileContent,
DirectoryPath docRootPath)
{
context.NotNull(nameof(context));
logFileContent.NotNullOrWhiteSpace(nameof(logFileContent));
docRootPath.NotNull(nameof(docRootPath));

return context.DocFxIssues(DocFxIssuesSettings.FromContent(logFileContent, docRootPath));
}

/// <summary>
Expand All @@ -91,8 +171,7 @@ public static class DocFxIssuesProviderAliases
/// <![CDATA[
/// var repoRoot = new DirectoryPath("c:\repo");
/// var settings =
/// new DocFxIssuesSettings(
/// new FilePath("C:\build\docfx.log"));
/// new DocFxIssuesSettings("C:\build\docfx.log");
///
/// ReportCodeAnalysisIssuesToPullRequest(
/// DocFxIssues(settings),
Expand Down
Loading

0 comments on commit 3b7e13e

Please sign in to comment.