Skip to content

Commit

Permalink
Merge pull request #8 from alt3/tests
Browse files Browse the repository at this point in the history
Add tests to harden GetCustomEditUrl
  • Loading branch information
bravo-kernel authored Nov 2, 2019
2 parents 79fc509 + 16eaa23 commit a440313
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
25 changes: 13 additions & 12 deletions Source/Private/GetCustomEditUrl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ function GetCustomEditUrl() {
.DESCRIPTION
Generates a URL pointing to the Powershell source file that was used to generate the markdown file.
.NOTES
- URLs for non-monolithic modules point to a .ps1 file with same name as the markdown file
- URLs for monolithic modules will always point to a .psm1 with same name as passed module
#>
param(
[Parameter(Mandatory = $True)][string]$Module,
Expand All @@ -13,20 +17,17 @@ function GetCustomEditUrl() {
[switch]$Monolithic
)

# monolithic
if ($Monolithic) {
if (Test-Path -Path $Module) {
$sourceFile = [System.IO.Path]::GetFileNameWithoutExtension($Module) + '.psm1'
} else {
$modulePath = (Get-Module $Module).path
$sourceFile = [System.IO.Path]::GetFileName($modulePath)
}
# use command for non-monlithics
if (-not($Monolithic)) {
$command = [System.IO.Path]::GetFileNameWithoutExtension($MarkdownFile)

return $EditUrl + '/' + $sourceFile
return $EditUrl + '/' + $command + ".ps1"
}

# non-monolithic
$command = [System.IO.Path]::GetFileNameWithoutExtension($MarkdownFile)
# use module name for monolithics
if (Test-Path $Module) {
$Module = [System.IO.Path]::GetFileNameWithoutExtension($Module)
}

return $EditUrl + '/' + $command + ".ps1"
return $EditUrl + '/' + $Module + ".psm1"
}
71 changes: 71 additions & 0 deletions Tests/Private/GetCustomEditUrl.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Describe "Private$([IO.Path]::DirectorySeparatorChar)GetCustomEditUrl" {
Import-Module Alt3.Docusaurus.Powershell -DisableNameChecking -Verbose:$False

# up
$markdownFilePath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'Dummy-PesterCommand.md'
$dummyModulePath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'DummyModule.psm1'


"Dummy markdown" | Out-File -FilePath $markdownFilePath
if (-Not(Test-Path -Path $markdownFilePath)) {
throw "temporary markdown file was not created"
}

"Dummy module" | Out-File -FilePath $dummyModulePath
if (-Not(Test-Path -Path $dummyModulePath)) {
throw "temporary module file was not created"
}

${global:markdownFileItem} = Get-Item -Path $markdownFilePath
${global:dummyModuleFileItem} = Get-Item -Path $dummyModulePath

# the actual tests
Context 'for non-monolithic modules' {
$customEditUrl = InModuleScope Alt3.Docusaurus.Powershell {
GetCustomEditUrl -Module "DummyModule" -MarkdownFile ${global:markdownFileItem} -EditUrl "https://dummy.com"
}

It "generates a link pointing to a ps1 source file with same name as the markdown file" {
$customEditUrl | Should -Be 'https://dummy.com/Dummy-PesterCommand.ps1'
}
}

Context 'for monolithic modules' {
# ---------------------------------------------------------------------
# passed module resolves to a file
# ---------------------------------------------------------------------
$customEditUrl = InModuleScope Alt3.Docusaurus.Powershell {
GetCustomEditUrl -Module ${global:dummyModuleFileItem} -MarkdownFile ${global:markdownFileItem} -EditUrl "https://dummy.com" -Monolithic
}

It "generates a link pointing to a psm1 source file with same name as the (file-resolvable) module" {
$customEditUrl | Should -Be 'https://dummy.com/DummyModule.psm1'
}

# ---------------------------------------------------------------------
# passed module resolves to a loaded/imported module
# ---------------------------------------------------------------------
$customEditUrl = InModuleScope Alt3.Docusaurus.Powershell {
GetCustomEditUrl -Module Microsoft.PowerShell.Management -MarkdownFile ${global:markdownFileItem} -EditUrl "https://dummy.com" -Monolithic
}

It "generates a link pointing to a psm1 source file with same name as the (imported/loaded) module" {
$customEditUrl | Should -Be 'https://dummy.com/Microsoft.Powershell.Management.psm1'
}

# ---------------------------------------------------------------------
# passed module does not resolve to a file AND is not loaded/imported
# ---------------------------------------------------------------------
$customEditUrl = InModuleScope Alt3.Docusaurus.Powershell {
GetCustomEditUrl -Module "DummyModule" -MarkdownFile ${global:markdownFileItem} -EditUrl "https://dummy.com" -Monolithic
}

It "generates a link pointing to a psm1 source file with same name as the (non-resolvable) module" {
$customEditUrl | Should -Be 'https://dummy.com/DummyModule.psm1'
}
}

# down
Remove-Item -Path ${global:markdownFileItem}
Remove-Item -Path ${global:dummyModuleFileItem}
}
File renamed without changes.

0 comments on commit a440313

Please sign in to comment.