Skip to content

Commit

Permalink
Merge pull request cake-contrib#14 from cake-contrib/feature/cake-con…
Browse files Browse the repository at this point in the history
…tribgh-12

(cake-contribGH-12) Add support for markdownlint-cli
  • Loading branch information
pascalberger authored May 27, 2018
2 parents 5e3fcf1 + 4f6e7c8 commit 449cd82
Show file tree
Hide file tree
Showing 16 changed files with 746 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MarkdownlintIssuesProviderFixture.cs" />
<Compile Include="MarkdownlintIssuesProviderTests.cs" />
<Compile Include="MarkdownlintIssuesSettingsTests.cs" />
<Compile Include="MarkdownlintCli\MarkdownlintCliIssuesProviderFixture.cs" />
<Compile Include="MarkdownlintCli\MarkdownlintCliIssuesProviderTests.cs" />
<Compile Include="MarkdownlintCli\MarkdownlintCliIssuesSettingsTests.cs" />
<Compile Include="Markdownlint\MarkdownlintIssuesProviderFixture.cs" />
<Compile Include="Markdownlint\MarkdownlintIssuesProviderTests.cs" />
<Compile Include="Markdownlint\MarkdownlintIssuesSettingsTests.cs" />
<Compile Include="MarkdownlintRuleUrlResolverTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -94,12 +97,14 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<EmbeddedResource Include="Testfiles\markdownlint-cli.log" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
<Analyzer Include="..\packages\xunit.analyzers.0.8.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Cake.Issues.Markdownlint.Tests
namespace Cake.Issues.Markdownlint.Tests.Markdownlint
{
using System.Collections.Generic;
using System.IO;
using Cake.Issues.Markdownlint.Markdownlint;
using Cake.Testing;
using Core.Diagnostics;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Cake.Issues.Markdownlint.Tests
namespace Cake.Issues.Markdownlint.Tests.Markdownlint
{
using System.Linq;
using Cake.Issues.Markdownlint.Markdownlint;
using Cake.Testing;
using Core.IO;
using Shouldly;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace Cake.Issues.Markdownlint.Tests
namespace Cake.Issues.Markdownlint.Tests.Markdownlint
{
using System;
using System.IO;
using System.Linq;
using System.Text;
using Cake.Issues.Markdownlint.Markdownlint;
using Shouldly;
using Testing;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Cake.Issues.Markdownlint.Tests.MarkdownlintCli
{
using System.Collections.Generic;
using System.IO;
using Cake.Issues.Markdownlint.MarkdownlintCli;
using Cake.Testing;
using Core.Diagnostics;

internal class MarkdownlintCliIssuesProviderFixture
{
public MarkdownlintCliIssuesProviderFixture(string fileResourceName)
{
this.Log = new FakeLog { Verbosity = Verbosity.Normal };

using (var stream = this.GetType().Assembly.GetManifestResourceStream("Cake.Issues.Markdownlint.Tests.Testfiles." + fileResourceName))
{
using (var sr = new StreamReader(stream))
{
this.Settings =
MarkdownlintCliIssuesSettings.FromContent(
sr.ReadToEnd());
}
}

this.RepositorySettings =
new RepositorySettings(@"c:\Source\Cake.Issues");
}

public FakeLog Log { get; set; }

public MarkdownlintCliIssuesSettings Settings { get; set; }

public RepositorySettings RepositorySettings { get; set; }

public MarkdownlintCliIssuesProvider Create()
{
var provider = new MarkdownlintCliIssuesProvider(this.Log, this.Settings);
provider.Initialize(this.RepositorySettings);
return provider;
}

public IEnumerable<IIssue> ReadIssues()
{
var issueProvider = this.Create();
return issueProvider.ReadIssues(IssueCommentFormat.PlainText);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
namespace Cake.Issues.Markdownlint.Tests.MarkdownlintCli
{
using System.Linq;
using Cake.Issues.Markdownlint.MarkdownlintCli;
using Cake.Testing;
using Core.IO;
using Shouldly;
using Testing;
using Xunit;

public sealed class MarkdownlintCliIssuesProviderTests
{
public sealed class TheCtor
{
[Fact]
public void Should_Throw_If_Log_Is_Null()
{
// Given / When
var result = Record.Exception(() =>
new MarkdownlintCliIssuesProvider(
null,
MarkdownlintCliIssuesSettings.FromContent("Foo")));

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

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
var result = Record.Exception(() =>
new MarkdownlintCliIssuesProvider(
new FakeLog(),
null));

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

public sealed class TheReadIssuesMethod
{
[Fact]
public void Should_Read_Issue_Correct()
{
// Given
var fixture = new MarkdownlintCliIssuesProviderFixture("markdownlint-cli.log");

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

// Then
issues.Count.ShouldBe(8);
CheckIssue(
issues[0],
@"docs/index.md",
1,
"MD022",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md022",
0,
"Headers should be surrounded by blank lines [Context: \"# foo\"]");
CheckIssue(
issues[1],
@"docs/index.md",
2,
"MD009",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md009",
0,
"Trailing spaces [Expected: 2; Actual: 1]");
CheckIssue(
issues[2],
@"docs/index.md",
2,
"MD013",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md013",
0,
"Line length [Expected: 100; Actual: 811]");
CheckIssue(
issues[3],
@"docs/index.md",
4,
"MD022",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md022",
0,
"Headers should be surrounded by blank lines [Context: \"# bar\"]");
CheckIssue(
issues[4],
@"docs/index.md",
4,
"MD025",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md025",
0,
"Multiple top level headers in the same document [Context: \"# bar\"]");
CheckIssue(
issues[5],
@"docs/index.md",
5,
"MD031",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md031",
0,
"Fenced code blocks should be surrounded by blank lines [Context: \"```\"]");
CheckIssue(
issues[6],
@"docs/index.md",
5,
"MD040",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md040",
0,
"Fenced code blocks should have a language specified [Context: \"```\"]");
CheckIssue(
issues[7],
@"docs/index.md",
6,
"MD009",
"https://github.com/DavidAnson/markdownlint/blob/master/doc/Rules.md#md009",
0,
"Trailing spaces [Expected: 2; Actual: 1]");
}

private static void CheckIssue(
IIssue issue,
string affectedFileRelativePath,
int? line,
string rule,
string ruleUrl,
int priority,
string message)
{
if (issue.AffectedFileRelativePath == null)
{
affectedFileRelativePath.ShouldBeNull();
}
else
{
issue.AffectedFileRelativePath.ToString().ShouldBe(new FilePath(affectedFileRelativePath).ToString());
issue.AffectedFileRelativePath.IsRelative.ShouldBe(true, "Issue path is not relative");
}

issue.Line.ShouldBe(line);
issue.Rule.ShouldBe(rule);

if (issue.RuleUrl == null)
{
ruleUrl.ShouldBeNull();
}
else
{
issue.RuleUrl.ToString().ShouldBe(ruleUrl);
}

issue.Priority.ShouldBe(priority);
issue.Message.ShouldBe(message);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace Cake.Issues.Markdownlint.Tests.MarkdownlintCli
{
using System;
using System.IO;
using System.Linq;
using System.Text;
using Cake.Issues.Markdownlint.MarkdownlintCli;
using Shouldly;
using Testing;
using Xunit;

public sealed class MarkdownlintCliIssuesSettingsTests
{
public sealed class TheCtor
{
[Fact]
public void Should_Throw_If_LogFilePath_Is_Null()
{
// Given / When
var result = Record.Exception(() =>
MarkdownlintCliIssuesSettings.FromFilePath(null));

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

[Fact]
public void Should_Throw_If_LogFileContent_Is_Null()
{
// Given / When
var result = Record.Exception(() =>
MarkdownlintCliIssuesSettings.FromContent(null));

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

[Fact]
public void Should_Throw_If_LogFileContent_Is_Empty()
{
// Given / When
var result = Record.Exception(() =>
MarkdownlintCliIssuesSettings.FromContent(string.Empty));

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

[Fact]
public void Should_Throw_If_LogFileContent_Is_WhiteSpace()
{
// Given / When
var result = Record.Exception(() =>
MarkdownlintCliIssuesSettings.FromContent(" "));

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

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

// When
var settings = MarkdownlintCliIssuesSettings.FromContent(logFileContent);

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

[Fact]
public void Should_Read_File_From_Disk()
{
var fileName = Path.GetTempFileName();
try
{
// Given
string expected;
using (var ms = new MemoryStream())
using (var stream = this.GetType().Assembly.GetManifestResourceStream("Cake.Issues.Markdownlint.Tests.Testfiles.markdownlint-cli.log"))
{
stream.CopyTo(ms);
var data = ms.ToArray();

using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
file.Write(data, 0, data.Length);
}

expected = Encoding.UTF8.GetString(data);
}

// When
var settings =
MarkdownlintCliIssuesSettings.FromFilePath(fileName);

// Then
settings.LogFileContent.ShouldBe(expected);
}
finally
{
if (File.Exists(fileName))
{
File.Delete(fileName);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
C:/Git/Test/Cake.Prca/docs/index.md: 1: MD022/blanks-around-headers Headers should be surrounded by blank lines [Context: "# foo"]
C:/Git/Test/Cake.Prca/docs/index.md: 2: MD009/no-trailing-spaces Trailing spaces [Expected: 2; Actual: 1]
C:/Git/Test/Cake.Prca/docs/index.md: 2: MD013/line-length Line length [Expected: 100; Actual: 811]
C:/Git/Test/Cake.Prca/docs/index.md: 4: MD022/blanks-around-headers Headers should be surrounded by blank lines [Context: "# bar"]
C:/Git/Test/Cake.Prca/docs/index.md: 4: MD025/single-h1 Multiple top level headers in the same document [Context: "# bar"]
C:/Git/Test/Cake.Prca/docs/index.md: 5: MD031/blanks-around-fences Fenced code blocks should be surrounded by blank lines [Context: "```"]
C:/Git/Test/Cake.Prca/docs/index.md: 5: MD040/fenced-code-language Fenced code blocks should have a language specified [Context: "```"]
C:/Git/Test/Cake.Prca/docs/index.md: 6: MD009/no-trailing-spaces Trailing spaces [Expected: 2; Actual: 1]
Loading

0 comments on commit 449cd82

Please sign in to comment.