@@ -1037,3 +1037,77 @@ Describe 'Common Tests - Validate Markdown Files' -Tag 'Markdown' {
1037
1037
" to have this text execute." )
1038
1038
}
1039
1039
}
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
+ }
0 commit comments