Skip to content

Commit c6694d0

Browse files
authored
Merge pull request #59 from Ellerbach/mtirion/50-doclinkchecker-tilde
Added validation of root-reference with ~
2 parents 9f72b7f + 8ee0dec commit c6694d0

File tree

3 files changed

+77
-12
lines changed

3 files changed

+77
-12
lines changed

src/DocLinkChecker/DocLinkChecker.Test/HyperlinkTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,52 @@ public async void ValidateLocalLinkNonExistingHeadingShouldHaveErrors()
303303
headingError.Severity.Should().Be(Enums.MarkdownErrorSeverity.Warning);
304304
}
305305

306+
[Theory]
307+
[InlineData("~/general/images/nature.jpeg")]
308+
[InlineData("~\\general\\images\\nature.jpeg")]
309+
public async void ValidateRootLinkShouldHaveNoErrors(string path)
310+
{
311+
// Arrange
312+
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
313+
string sourceDoc = $"{_fileServiceMock.Root}\\general\\another-sample.md";
314+
315+
LinkValidatorService service = new LinkValidatorService(_serviceProvider, _config, _fileService, _console);
316+
317+
//Act
318+
int line = 499;
319+
int column = 75;
320+
Hyperlink link = new Hyperlink(sourceDoc, line, column, path);
321+
await service.VerifyHyperlink(link);
322+
323+
// Assert
324+
service.Errors.Should().BeEmpty();
325+
}
326+
327+
[Theory]
328+
[InlineData("~/general/images/NON_EXISTING.jpeg")]
329+
[InlineData("~\\NON_EXISTING\\images\\nature.jpeg")]
330+
public async void ValidateInvalidRootLinkShouldHaveErrors(string path)
331+
{
332+
// Arrange
333+
_config.DocumentationFiles.SourceFolder = _fileServiceMock.Root;
334+
string sourceDoc = $"{_fileServiceMock.Root}\\general\\another-sample.md";
335+
336+
LinkValidatorService service = new LinkValidatorService(_serviceProvider, _config, _fileService, _console);
337+
338+
//Act
339+
int line = 499;
340+
int column = 75;
341+
Hyperlink link = new Hyperlink(sourceDoc, line, column, path);
342+
await service.VerifyHyperlink(link);
343+
344+
// Assert
345+
service.Errors.Should().NotBeEmpty();
346+
var linkError = service.Errors.FirstOrDefault(x => x.Message.Contains("Not found"));
347+
linkError.Line.Should().Be(line);
348+
linkError.Column.Should().Be(column);
349+
linkError.Severity.Should().Be(MarkdownErrorSeverity.Error);
350+
}
351+
306352
[Fact]
307353
public async void ValidateLocalLinkWithFullPathShouldHaveErrors()
308354
{

src/DocLinkChecker/DocLinkChecker/Models/Hyperlink.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
namespace DocLinkChecker.Models
22
{
3-
using System.Diagnostics;
43
using System.IO;
54
using DocLinkChecker.Enums;
65

src/DocLinkChecker/DocLinkChecker/Services/LinkValidatorService.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,38 @@ private Task VerifyLocalHyperlink(Hyperlink hyperlink)
272272
return Task.CompletedTask;
273273
}
274274

275-
// compute link of the url relative to the path of the file.
276-
if (!_fileService.ExistsFileOrDirectory(hyperlink.UrlFullPath))
275+
if (hyperlink.Url.StartsWith("~"))
277276
{
278-
// referenced file doesn't exist
279-
_errors.Enqueue(
280-
new MarkdownError(
281-
hyperlink.FilePath,
282-
hyperlink.Line,
283-
hyperlink.Column,
284-
MarkdownErrorSeverity.Error,
285-
$"Not found: {hyperlink.Url}"));
286-
return Task.CompletedTask;
277+
// special case for a reference to the root of the docs. Calculate full path differently.
278+
string url = hyperlink.Url.Replace("~", _config.DocumentationFiles.SourceFolder);
279+
if (!_fileService.ExistsFileOrDirectory(url))
280+
{
281+
// referenced file doesn't exist
282+
_errors.Enqueue(
283+
new MarkdownError(
284+
hyperlink.FilePath,
285+
hyperlink.Line,
286+
hyperlink.Column,
287+
MarkdownErrorSeverity.Error,
288+
$"Not found: {hyperlink.Url}"));
289+
return Task.CompletedTask;
290+
}
291+
}
292+
else
293+
{
294+
// compute link of the url relative to the path of the file.
295+
if (!_fileService.ExistsFileOrDirectory(hyperlink.UrlFullPath))
296+
{
297+
// referenced file doesn't exist
298+
_errors.Enqueue(
299+
new MarkdownError(
300+
hyperlink.FilePath,
301+
hyperlink.Line,
302+
hyperlink.Column,
303+
MarkdownErrorSeverity.Error,
304+
$"Not found: {hyperlink.Url}"));
305+
return Task.CompletedTask;
306+
}
287307
}
288308

289309
switch (_config.DocLinkChecker.RelativeLinkStrategy)

0 commit comments

Comments
 (0)