Skip to content

Instantly share code, notes, and snippets.

@gerane
Created May 9, 2016 18:51
Show Gist options
  • Save gerane/008fccb0b193df6bd7ed64699181004e to your computer and use it in GitHub Desktop.
Save gerane/008fccb0b193df6bd7ed64699181004e to your computer and use it in GitHub Desktop.
Function ParseMarkdown
{
$MarkdownPath = "$PSScriptRoot\Resources\PesteringSysadmins.md"
$Object = [ordered]@{}
$Sections = @($Object)
switch -regex -file $MarkdownPath
{
'^# .*' # Section
{
$Name = ($Matches[0] -replace '^# ','').Trim()
$Section = [ordered]@{}
$Count = $Sections.Count
$Sections[$Count - 1][$Name] = $Section
}
'^## .*' # Topic
{
$Topic = ($Matches[0] -replace '^## ','').Trim()
if (!$Section[$Topic]) { $Section[$Topic] = [ordered]@{} }
}
'^\* \[.*' # Entry
{
$Entry = ($Matches[0] -replace '^\* ','').Trim()
$Parse = $Entry -match '^.*\[(.*)\].*\((.*)\)$'
$Description = $Matches[1]
$uri = $Matches[2]
$Section[$Topic] += @{ $Description = $uri }
}
}
Return $Object
}
Import-Module Pester
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path).Replace('.Tests.', '.')
. "$here\$sut"
$Markdown = ParseMarkdown
Function Test-ResourceLinks ($InputObject)
{
Describe 'Testing Resources' {
foreach ($Section in $InputObject.Keys)
{
foreach ($Topic in $InputObject.$Section.Keys)
{
Context "Testing URLs in [$($Section) - $($Topic)]" {
foreach ($Entry in $InputObject.$Section.$Topic.Keys)
{
$Description = $Entry
$Uri = $InputObject.$Section.$Topic.$Description
It "[$($Description)] should have a working Url" {
Write-Verbose -Message "Testing Uri [$($Uri)]"
$Results = Invoke-WebRequest -Uri $Uri -UseBasicParsing
$Results.StatusCode | Should Be '200'
}
}
}
}
}
}
}
Test-ResourceLinks -InputObject $Markdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment