Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit a0c7e86

Browse files
authored
Add new opt-in common test for markdown link linting (#273)
- Add new opt-in common test for markdown link linting (issue #211).
1 parent 2f6271a commit a0c7e86

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Meta.Tests.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,3 +1037,77 @@ Describe 'Common Tests - Validate Markdown Files' -Tag 'Markdown' {
10371037
"to have this text execute.")
10381038
}
10391039
}
1040+
1041+
Describe 'Common Tests - Validate Markdown Links' -Tag 'Markdown' {
1042+
$optIn = Get-PesterDescribeOptInStatus -OptIns $optIns
1043+
1044+
$dependencyModuleName = 'MarkdownLinkCheck'
1045+
$uninstallMarkdownLinkCheck = $false
1046+
1047+
Context 'When installing markdown link validation dependencies' {
1048+
It "Should not throw an error when installing and importing the module MarkdownLinkCheck" -Skip:(!$optIn) {
1049+
{
1050+
if (-not (Get-Module -Name $dependencyModuleName -ListAvailable))
1051+
{
1052+
# Remember that we installed the module, so that it gets uninstalled.
1053+
$uninstallMarkdownLinkCheck = $true
1054+
Install-Module -Name $dependencyModuleName -Force -Scope 'CurrentUser' -ErrorAction Stop
1055+
}
1056+
1057+
Import-Module -Name $dependencyModuleName -Force
1058+
} | Should -Not -Throw
1059+
}
1060+
}
1061+
1062+
$markdownFileExtensions = @('.md')
1063+
1064+
$markdownFiles = Get-TextFilesList $moduleRootFilePath |
1065+
Where-Object -FilterScript {
1066+
$markdownFileExtensions -contains $_.Extension
1067+
}
1068+
1069+
foreach ($markdownFileToValidate in $markdownFiles)
1070+
{
1071+
$contextDescriptiveName = Join-Path -Path (Split-Path $markdownFileToValidate.Directory -Leaf) -ChildPath (Split-Path $markdownFileToValidate -Leaf)
1072+
1073+
Context -Name $contextDescriptiveName {
1074+
It "Should not contain any broken links" -Skip:(!$optIn) {
1075+
$getMarkdownLinkParameters = @{
1076+
BrokenOnly = $true
1077+
Path = $markdownFileToValidate.FullName
1078+
}
1079+
1080+
# Make sure it always returns an array even if Get-MarkdownLink returns $null.
1081+
$brokenLinks = @(Get-MarkdownLink @getMarkdownLinkParameters)
1082+
1083+
if ($brokenLinks.Count -gt 0)
1084+
{
1085+
# Write out all the errors so the contributor can resolve.
1086+
foreach ($brokenLink in $brokenLinks)
1087+
{
1088+
$message = 'Line {0}: [{1}] has broken URL "{2}"' -f $brokenLink.Line, $brokenLink.Text, $brokenLink.Url
1089+
Write-Host -BackgroundColor Yellow -ForegroundColor Black -Object $message
1090+
}
1091+
}
1092+
1093+
$brokenLinks.Count | Should -Be 0
1094+
}
1095+
}
1096+
}
1097+
1098+
if ($uninstallMarkdownLinkCheck)
1099+
{
1100+
Context 'When uninstalling markdown link validation dependencies' {
1101+
It "Should not throw an error when uninstalling the module MarkdownLinkCheck" -Skip:(!$optIn) {
1102+
{
1103+
<#
1104+
Remove the module from the current session as
1105+
Uninstall-Module does not do that.
1106+
#>
1107+
Remove-Module -Name $dependencyModuleName -Force
1108+
Uninstall-Module -Name $dependencyModuleName -Force -ErrorAction Stop
1109+
} | Should -Not -Throw
1110+
}
1111+
}
1112+
}
1113+
}

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This branch is used by DSC Resource Kit modules for running common tests.
4141
- [Example Usage of DSCResource.Tests in AppVeyor.yml](#example-usage-of-dscresourcetests-in-appveyoryml)
4242
- [AppVeyor Module](#appveyor-module)
4343
- [Phased Meta test Opt-In](#phased-meta-test-opt-in)
44+
- [Common Tests - Validate Markdown Links](#common-tests---validate-markdown-links)
4445
- [Using AppVeyor.psm1 with the default shared model](#using-appveyorpsm1-with-the-default-shared-model)
4546
- [Using AppVeyor.psm1 with harness model](#using-appveyorpsm1-with-harness-model)
4647
- [Encrypt Credentials in Integration Tests](#encrypt-credentials-in-integration-tests)
@@ -293,6 +294,22 @@ The following opt-in flags are available:
293294
129 characters. 129 characters is the current (known) maximum for a relative
294295
path to be able to compile a configuration in Azure Automation using a
295296
DSC resource module.
297+
- **Common Tests - Validate Markdown Links**: fails tests if a link in
298+
a markdown file is broken.
299+
300+
#### Common Tests - Validate Markdown Links
301+
302+
The test validates the links in markdown files. Any valid GitHub markdown link
303+
will pass the linter.
304+
305+
>**NOTE!** There is currently a bug in the markdown link linter that makes it
306+
>unable to recognize absolute paths where the absolute link starts in a parent
307+
>folder.
308+
>For example, if a markdown file `/Examples/README.md`,
309+
>contains an absolute link pointing to `/Examples/Resources/SqlAG`,
310+
>that link will fail the test. Changing the link to a relative link from the
311+
>README.md file's folder, e.g `Resources/SqlAG` will pass the test.
312+
>See issue [vors/MarkdownLinkCheck#5](https://github.com/vors/MarkdownLinkCheck/issues/5).
296313
297314
### Using AppVeyor.psm1 with the default shared model
298315

@@ -1000,6 +1017,8 @@ Contributors that add or change an example to be published must make sure that
10001017
- Added a common tests to test the length of the relative file path so the paths
10011018
are not exceeding the current path hard limit in Azure Automation
10021019
([issue #188](https://github.com/PowerShell/DscResource.Tests/issues/188)).
1020+
- Add new opt-in common test for markdown link linting
1021+
([issue #211](https://github.com/PowerShell/DscResource.Tests/issues/211)).
10031022

10041023
### 0.2.0.0
10051024

0 commit comments

Comments
 (0)