Skip to content

Commit

Permalink
Merge pull request #59 from Ellerbach/mtirion/50-doclinkchecker-tilde
Browse files Browse the repository at this point in the history
Added validation of root-reference with ~
  • Loading branch information
Ellerbach authored Aug 30, 2024
2 parents 9f72b7f + 8ee0dec commit c6694d0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
46 changes: 46 additions & 0 deletions src/DocLinkChecker/DocLinkChecker.Test/HyperlinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,52 @@ public async void ValidateLocalLinkNonExistingHeadingShouldHaveErrors()
headingError.Severity.Should().Be(Enums.MarkdownErrorSeverity.Warning);
}

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

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

//Act
int line = 499;
int column = 75;
Hyperlink link = new Hyperlink(sourceDoc, line, column, path);
await service.VerifyHyperlink(link);

// Assert
service.Errors.Should().BeEmpty();
}

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

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

//Act
int line = 499;
int column = 75;
Hyperlink link = new Hyperlink(sourceDoc, line, column, path);
await service.VerifyHyperlink(link);

// Assert
service.Errors.Should().NotBeEmpty();
var linkError = service.Errors.FirstOrDefault(x => x.Message.Contains("Not found"));
linkError.Line.Should().Be(line);
linkError.Column.Should().Be(column);
linkError.Severity.Should().Be(MarkdownErrorSeverity.Error);
}

[Fact]
public async void ValidateLocalLinkWithFullPathShouldHaveErrors()
{
Expand Down
1 change: 0 additions & 1 deletion src/DocLinkChecker/DocLinkChecker/Models/Hyperlink.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace DocLinkChecker.Models
{
using System.Diagnostics;
using System.IO;
using DocLinkChecker.Enums;

Expand Down
42 changes: 31 additions & 11 deletions src/DocLinkChecker/DocLinkChecker/Services/LinkValidatorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,38 @@ private Task VerifyLocalHyperlink(Hyperlink hyperlink)
return Task.CompletedTask;
}

// compute link of the url relative to the path of the file.
if (!_fileService.ExistsFileOrDirectory(hyperlink.UrlFullPath))
if (hyperlink.Url.StartsWith("~"))
{
// referenced file doesn't exist
_errors.Enqueue(
new MarkdownError(
hyperlink.FilePath,
hyperlink.Line,
hyperlink.Column,
MarkdownErrorSeverity.Error,
$"Not found: {hyperlink.Url}"));
return Task.CompletedTask;
// special case for a reference to the root of the docs. Calculate full path differently.
string url = hyperlink.Url.Replace("~", _config.DocumentationFiles.SourceFolder);
if (!_fileService.ExistsFileOrDirectory(url))
{
// referenced file doesn't exist
_errors.Enqueue(
new MarkdownError(
hyperlink.FilePath,
hyperlink.Line,
hyperlink.Column,
MarkdownErrorSeverity.Error,
$"Not found: {hyperlink.Url}"));
return Task.CompletedTask;
}
}
else
{
// compute link of the url relative to the path of the file.
if (!_fileService.ExistsFileOrDirectory(hyperlink.UrlFullPath))
{
// referenced file doesn't exist
_errors.Enqueue(
new MarkdownError(
hyperlink.FilePath,
hyperlink.Line,
hyperlink.Column,
MarkdownErrorSeverity.Error,
$"Not found: {hyperlink.Url}"));
return Task.CompletedTask;
}
}

switch (_config.DocLinkChecker.RelativeLinkStrategy)
Expand Down

0 comments on commit c6694d0

Please sign in to comment.