Skip to content

Commit

Permalink
Merge pull request #71 from Ellerbach/fix-tocgenerator-tests
Browse files Browse the repository at this point in the history
Fixes in DocLinkChecker for Linux environment. Fixed tests as well
  • Loading branch information
mtirionMSFT authored Oct 28, 2024
2 parents 53786d1 + 393ab38 commit 8debe6c
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ name: Build & Test
on:
push:
branches: [ main ]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
using Bogus;
using System;
using System;

namespace DocLinkChecker.Test.Helpers
{
internal static class MarkdownExtensions
{
internal const string Newline = "\n";

internal static string AddHeading(this string s, string title, int level)
{
string content = $"{new string('#', level)} {title}" + Environment.NewLine + Environment.NewLine;
string content = $"{new string('#', level)} {title}" + Newline + Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddParagraphs(this string s, int count = 1)
{
Faker faker = new Faker();
string content = (count == 1 ? faker.Lorem.Paragraph() : faker.Lorem.Paragraphs(count)) + Environment.NewLine;
string content = (count == 1 ? faker.Lorem.Paragraph() : faker.Lorem.Paragraphs(count)) + Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddResourceLink(this string s, string url)
{
Faker faker = new Faker();
string content = $" ![some resource {faker.Random.Int(1)}]({url})" + Environment.NewLine;
string content = $" ![some resource {faker.Random.Int(1)}]({url})" + Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddLink(this string s, string url)
{
Faker faker = new Faker();
string content = $" [some link {faker.Random.Int(1)}]({url})" + Environment.NewLine;
string content = $" [some link {faker.Random.Int(1)}]({url})" + Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddCodeLink(this string s, string name, string url)
{
Faker faker = new Faker();
string content = $" [!code-csharp[{name}]({url})]" + Environment.NewLine;
string content = $" [!code-csharp[{name}]({url})]" + Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddTableStart(this string s, int columns = 3)
Expand All @@ -65,19 +66,19 @@ internal static string AddTableStart(this string s, int columns = 3)
string content = "|";
for (int col = 0; col < columns; col++)
{
content += $" {faker.Lorem.Words(2)} |";
content += $" {faker.Lorem.Sentence} |";
}
content += Environment.NewLine;
content += Newline;
for (int col = 0; col < columns; col++)
{
content += $" --- |";
}
content += Environment.NewLine;
content += Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddTableRow(this string s, params string[] columns)
Expand All @@ -88,21 +89,21 @@ internal static string AddTableRow(this string s, params string[] columns)
{
content += $" {col} |";
}
content += Environment.NewLine;
content += Newline;
if (string.IsNullOrEmpty(s))
{
return content;
}
return s + Environment.NewLine + content;
return s + Newline + content;
}

internal static string AddNewLine(this string s)
{
if (string.IsNullOrEmpty(s))
{
return Environment.NewLine;
return Newline;
}
return s + Environment.NewLine;
return s + Newline;
}

internal static string AddRawMarkdown(this string s, string markdown)
Expand Down
43 changes: 22 additions & 21 deletions src/DocLinkChecker/DocLinkChecker.Test/HyperlinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
using Moq.Contrib.HttpClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using Xunit.Abstractions;

public class HyperlinkTests
{
Expand All @@ -29,9 +31,12 @@ public class HyperlinkTests
private MockFileService _fileServiceMock;
private ICustomConsoleLogger _console;
private IServiceProvider _serviceProvider;
private readonly ITestOutputHelper _outputHelper;

public HyperlinkTests()
public HyperlinkTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;

_config = new();
_config.DocumentationFiles.SourceFolder = ".";
_config.ResourceFolderNames = new() { ".attachmensts", "images" };
Expand Down Expand Up @@ -109,8 +114,7 @@ public async void ValidateNonExistingDomainInWebLinkShouldHaveErrors()
service.Errors.Should().NotBeEmpty();
service.Errors.First().Line.Should().Be(line);
service.Errors.First().Column.Should().Be(column);
service.Errors.First().Severity.Should().Be(Enums.MarkdownErrorSeverity.Error);
service.Errors.First().Message.Should().Contain("No such host");
service.Errors.First().Severity.Should().Be(MarkdownErrorSeverity.Error);
}

[Fact]
Expand All @@ -122,7 +126,7 @@ public async void ValidateExistingLocalLinkShouldNotHaveErrors()
//Act
int line = 14;
int column = 31;
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}\\index.md", line, column, "getting-started/README.md");
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}/index.md", line, column, "getting-started/README.md");
await service.VerifyHyperlink(link);

// Assert
Expand All @@ -138,7 +142,7 @@ public async void ValidateNonExistingLocalLinkShouldHaveErrors()
//Act
int line = 124;
int column = 3381;
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}\\start-document.md", line, column, "./non-existing-document.md");
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}/start-document.md", line, column, "./non-existing-document.md");
await service.VerifyHyperlink(link);

// Assert
Expand Down Expand Up @@ -169,7 +173,7 @@ public async void ValidateLocalLinkOutsideDocsHierarchyShouldHaveErrors(string f
//Act
int line = 124;
int column = 3381;
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}\\index.md", line, column, filename);
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}/index.md", line, column, filename);
await service.VerifyHyperlink(link);

// Assert
Expand All @@ -187,7 +191,7 @@ public async void ValidateLocalLinkOutsideHierarchyWithConfigShouldNotHaveErrors
_config.DocLinkChecker.RelativeLinkStrategy = RelativeLinkType.All;

string filename = "../../src/solution1/README.md";
string path = Path.GetFullPath(Path.Combine(_fileServiceMock.Root, filename));
string path = _fileService.GetFullPath(filename);
string empty = string.Empty;
_fileServiceMock.Files.Add(path, empty
.AddHeading("Document outside docs root", 1)
Expand All @@ -198,7 +202,7 @@ public async void ValidateLocalLinkOutsideHierarchyWithConfigShouldNotHaveErrors
//Act
int line = 124;
int column = 3381;
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}\\index.md", line, column, filename);
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}/index.md", line, column, filename);
await service.VerifyHyperlink(link);

// Assert
Expand Down Expand Up @@ -236,7 +240,7 @@ public async void ValidateLocalLinkHeadingShouldNotHaveErrors()
{
// Arrange
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
string source = $"{_fileServiceMock.Root}\\general\\another-sample.md";
string source = $"{_fileServiceMock.Root}/general/another-sample.md";

LinkValidatorService service = new LinkValidatorService(_serviceProvider, _config, _fileService, _console);
service.Headings.Add(new(source, 99, 1, "Third.1 Header", "third-1-header"));
Expand All @@ -245,7 +249,7 @@ public async void ValidateLocalLinkHeadingShouldNotHaveErrors()
int line = 432;
int column = 771;

Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}\\index.md", line, column, $".\\general\\another-sample.md#third-1-header");
Hyperlink link = new Hyperlink($"{_fileServiceMock.Root}/index.md", line, column, $"./general/another-sample.md#third-1-header");
await service.VerifyHyperlink(link);

// Assert
Expand All @@ -259,7 +263,7 @@ public async void ValidateLocalLinkHeadingInSameDocumentShouldNotHaveErrors(stri
{
// Arrange
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
string source = $"{_fileServiceMock.Root}\\general\\another-sample.md";
string source = $"{_fileServiceMock.Root}/general/another-sample.md";

LinkValidatorService service = new LinkValidatorService(_serviceProvider, _config, _fileService, _console);
service.Headings.Add(new(source, 99, 1, title, id));
Expand All @@ -280,11 +284,11 @@ public async void ValidateLocalLinkNonExistingHeadingShouldHaveErrors()
{
// Arrange
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
string sourceDoc = $"{_fileServiceMock.Root}\\general\\another-sample.md";
string sourceDoc = $"{_fileServiceMock.Root}/general/another-sample.md";
string sourceHeadingTitle = "Some Heading in the Source document";
string sourceHeadingId = "some-heading-in-the-source-document";
string destDoc = $"{_fileServiceMock.Root}\\general\\general-sample.md";
string destDocRelative = ".\\general-sample.md";
string destDoc = $"{_fileServiceMock.Root}/general/general-sample.md";
string destDocRelative = "./general-sample.md";
int line = 432;
int column = 771;

Expand All @@ -305,16 +309,13 @@ public async void ValidateLocalLinkNonExistingHeadingShouldHaveErrors()

[Theory]
[InlineData("~/general/images/nature.jpeg")]
[InlineData("~\\general\\images\\nature.jpeg")]
[InlineData("~/general/images/space%20image.jpeg")]
[InlineData("~\\general\\images\\space%20image.jpeg")]
[InlineData("%7E/general/images/space%20image.jpeg")]
[InlineData("%7E\\general\\images\\space%20image.jpeg")]
public async void ValidateRootLinkShouldHaveNoErrors(string path)
{
// Arrange
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
string sourceDoc = $"{_fileServiceMock.Root}\\general\\another-sample.md";
string sourceDoc = $"{_fileServiceMock.Root}/general/another-sample.md";

LinkValidatorService service = new LinkValidatorService(_serviceProvider, _config, _fileService, _console);

Expand All @@ -330,13 +331,13 @@ public async void ValidateRootLinkShouldHaveNoErrors(string path)

[Theory]
[InlineData("~/general/images/NON_EXISTING.jpeg")]
[InlineData("~\\NON_EXISTING\\images\\nature.jpeg")]
[InlineData("~/NON_EXISTING/images/nature.jpeg")]
[InlineData("~/general%2Fimages/nature.jpeg")]
public async void ValidateInvalidRootLinkShouldHaveErrors(string path)
{
// Arrange
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
string sourceDoc = $"{_fileServiceMock.Root}\\general\\another-sample.md";
string sourceDoc = $"{_fileServiceMock.Root}/general/another-sample.md";

LinkValidatorService service = new LinkValidatorService(_serviceProvider, _config, _fileService, _console);

Expand Down Expand Up @@ -387,7 +388,7 @@ public async void ValidateLocalLinkWithFullPathShouldHaveErrors()
//Act
int line = 124;
int column = 3381;
Hyperlink link = new Hyperlink("./start-document.md", line, column, @"D:\Git\Project\docs\another-document.md");
Hyperlink link = new Hyperlink("./start-document.md", line, column, Path.Combine(_fileServiceMock.Root, @"another-document.md").NormalizePath());
await service.VerifyHyperlink(link);

// Assert
Expand Down
12 changes: 11 additions & 1 deletion src/DocLinkChecker/DocLinkChecker.Test/MarkdownTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
namespace DocLinkChecker.Test
{
using System.Linq;
using Bogus;
using System.Reflection;
using System.Text.RegularExpressions;
using DocLinkChecker.Helpers;
using DocLinkChecker.Models;
using DocLinkChecker.Test.Helpers;
using FluentAssertions;
using Xunit.Abstractions;

public class MarkdownTests
{
private readonly ITestOutputHelper _outputHelper;

private string _correctDocument = string.Empty
.AddHeading("Sample General Document", 1)
.AddParagraphs(1).AddLink("https://loremipsum.io/generator/?n=5&t=p")
Expand Down Expand Up @@ -54,6 +58,11 @@ public class MarkdownTests
.AddNewLine()
.AddParagraphs(2);

public MarkdownTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}

[Fact]
public void FindAllLinks()
{
Expand Down Expand Up @@ -137,6 +146,7 @@ public void FindAllTables()
public void FindAllTablesWithErrors()
{
var result = MarkdownHelper.ParseMarkdownString(string.Empty, _errorDocument, true);

result.Errors.Count.Should().Be(5);
}
}
Expand Down
Loading

0 comments on commit 8debe6c

Please sign in to comment.