|
| 1 | +BeforeAll { |
| 2 | + $projectRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) |
| 3 | + . (Join-Path $projectRoot 'src/private/build/BuildHelp.ps1') |
| 4 | + |
| 5 | + function Stop-NovaOperation { |
| 6 | + param([string]$Message, [string]$ErrorId, [System.Management.Automation.ErrorCategory]$Category, $TargetObject) |
| 7 | + $exception = [System.Exception]::new($Message) |
| 8 | + $record = [System.Management.Automation.ErrorRecord]::new($exception, $ErrorId, $Category, $TargetObject) |
| 9 | + throw $record |
| 10 | + } |
| 11 | + function Get-NovaBuildProjectInfo {param($ProjectInfo); return $ProjectInfo} |
| 12 | + function Get-NovaHelpLocale {param($HelpMarkdownFiles); return 'en-US'} |
| 13 | + function Measure-PlatyPSMarkdown {[CmdletBinding()] param([Parameter(ValueFromPipeline)]$Input) process {}} |
| 14 | + function Import-MarkdownCommandHelp {[CmdletBinding()] param([Parameter(ValueFromPipeline)]$Input, $Path) process {}} |
| 15 | + function Export-MamlCommandHelp {[CmdletBinding()] param([Parameter(ValueFromPipeline)]$Input, $OutputFolder) process {}} |
| 16 | +} |
| 17 | + |
| 18 | +Describe 'Get-NovaHelpDocsDir' { |
| 19 | + It 'joins DocsDir and ProjectName' { |
| 20 | + $ctx = [pscustomobject]@{DocsDir = '/repo/docs'; ProjectName = 'MyMod'} |
| 21 | + Get-NovaHelpDocsDir -BuildProjectInfo $ctx | Should -Be ([IO.Path]::Combine('/repo/docs','MyMod')) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +Describe 'Get-NovaHelpMarkdownItem' { |
| 26 | + It 'returns the markdown files in the help docs dir' { |
| 27 | + $tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 28 | + $sub = Join-Path $tmp 'MyMod' |
| 29 | + New-Item -ItemType Directory -Path $sub -Force | Out-Null |
| 30 | + Set-Content -Path (Join-Path $sub 'X.md') -Value 'help' |
| 31 | + try { |
| 32 | + $ctx = [pscustomobject]@{DocsDir = $tmp; ProjectName = 'MyMod'} |
| 33 | + $files = @(Get-NovaHelpMarkdownItem -BuildProjectInfo $ctx) |
| 34 | + $files.Count | Should -Be 1 |
| 35 | + $files[0].Name | Should -Be 'X.md' |
| 36 | + } finally { |
| 37 | + Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + It 'returns nothing when help docs dir is missing' { |
| 42 | + $ctx = [pscustomobject]@{DocsDir = '/no/such/dir'; ProjectName = 'X'} |
| 43 | + Get-NovaHelpMarkdownItem -BuildProjectInfo $ctx | Should -BeNullOrEmpty |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +Describe 'Get-NovaHelpBuildContext' { |
| 48 | + BeforeEach { |
| 49 | + $script:tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 50 | + New-Item -ItemType Directory -Path $script:tmp | Out-Null |
| 51 | + Set-Content -Path (Join-Path $script:tmp 'X.md') -Value 'help' |
| 52 | + $script:fakeFiles = @(Get-Item (Join-Path $script:tmp 'X.md')) |
| 53 | + } |
| 54 | + AfterEach { Remove-Item $script:tmp -Recurse -Force -ErrorAction SilentlyContinue } |
| 55 | + |
| 56 | + It 'returns null when no PlatyPS command help files are detected' { |
| 57 | + $ctx = [pscustomobject]@{DocsDir='/x'; ProjectName='Y'} |
| 58 | + Mock Measure-PlatyPSMarkdown { @() } |
| 59 | + Get-NovaHelpBuildContext -BuildProjectInfo $ctx -HelpMarkdownFiles $script:fakeFiles | Should -BeNullOrEmpty |
| 60 | + } |
| 61 | + It 'returns a context with command help files when detected' { |
| 62 | + $ctx = [pscustomobject]@{DocsDir='/x'; ProjectName='Y'} |
| 63 | + Mock Measure-PlatyPSMarkdown { @([pscustomobject]@{FilePath='X.md'; FileType='CommandHelp'}) } |
| 64 | + Mock Get-NovaHelpLocale { 'en-US' } |
| 65 | + $result = Get-NovaHelpBuildContext -BuildProjectInfo $ctx -HelpMarkdownFiles $script:fakeFiles |
| 66 | + $result.Locale | Should -Be 'en-US' |
| 67 | + $result.CommandHelpFiles.Count | Should -Be 1 |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +Describe 'Rename-NovaGeneratedHelpFolder' { |
| 72 | + It 'throws when expected generated dir is missing' { |
| 73 | + $tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 74 | + New-Item -ItemType Directory -Path $tmp | Out-Null |
| 75 | + try { |
| 76 | + $ctx = [pscustomobject]@{OutputModuleDir=$tmp; ProjectName='X'; DocsDir=$tmp} |
| 77 | + { Rename-NovaGeneratedHelpFolder -BuildProjectInfo $ctx -Locale 'en-US' } | Should -Throw -ErrorId 'Nova.Environment.GeneratedHelpDirectoryNotFound' |
| 78 | + } finally { |
| 79 | + Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue |
| 80 | + } |
| 81 | + } |
| 82 | + It 'renames the generated help dir to the locale' { |
| 83 | + $tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 84 | + $generated = Join-Path $tmp 'X' |
| 85 | + New-Item -ItemType Directory -Path $generated -Force | Out-Null |
| 86 | + try { |
| 87 | + $ctx = [pscustomobject]@{OutputModuleDir=$tmp; ProjectName='X'; DocsDir=$tmp} |
| 88 | + Rename-NovaGeneratedHelpFolder -BuildProjectInfo $ctx -Locale 'en-US' |
| 89 | + Test-Path (Join-Path $tmp 'en-US') | Should -BeTrue |
| 90 | + } finally { |
| 91 | + Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +Describe 'Assert-NovaPlatyPSAvailable' { |
| 97 | + It 'returns silently when PlatyPS is available' { |
| 98 | + Mock Get-Module { @([pscustomobject]@{Name='Microsoft.PowerShell.PlatyPS'}) } |
| 99 | + { Assert-NovaPlatyPSAvailable } | Should -Not -Throw |
| 100 | + } |
| 101 | + It 'stops when PlatyPS is unavailable' { |
| 102 | + Mock Get-Module { @() } |
| 103 | + { Assert-NovaPlatyPSAvailable } | Should -Throw -ErrorId 'Nova.Dependency.BuildHelpDependencyMissing' |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +Describe 'Export-NovaGeneratedHelp' { |
| 108 | + It 'imports command help, exports maml to OutputModuleDir, and renames the generated folder to the locale' { |
| 109 | + $tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 110 | + $generated = Join-Path $tmp 'Y' |
| 111 | + New-Item -ItemType Directory -Path $generated -Force | Out-Null |
| 112 | + try { |
| 113 | + $ctx = [pscustomobject]@{OutputModuleDir=$tmp; ProjectName='Y'; DocsDir=$tmp} |
| 114 | + $helpContext = [pscustomobject]@{ |
| 115 | + HelpMarkdownFiles = @() |
| 116 | + CommandHelpFiles = @([pscustomobject]@{FilePath = (Join-Path $tmp 'X.md')}) |
| 117 | + Locale = 'en-US' |
| 118 | + } |
| 119 | + Mock Import-MarkdownCommandHelp {[CmdletBinding()] param([Parameter(ValueFromPipeline)]$Input, $Path) process {$_}} |
| 120 | + Mock Export-MamlCommandHelp {[CmdletBinding()] param([Parameter(ValueFromPipeline)]$Input, $OutputFolder) process {$script:exportFolder = $OutputFolder}} |
| 121 | + |
| 122 | + Export-NovaGeneratedHelp -BuildProjectInfo $ctx -HelpContext $helpContext |
| 123 | + |
| 124 | + Should -Invoke Import-MarkdownCommandHelp -Times 1 |
| 125 | + Should -Invoke Export-MamlCommandHelp -Times 1 |
| 126 | + Test-Path (Join-Path $tmp 'en-US') | Should -BeTrue |
| 127 | + } finally { |
| 128 | + Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +Describe 'Build-Help' { |
| 134 | + It 'returns when no help markdown files exist' { |
| 135 | + Mock Get-NovaBuildProjectInfo { [pscustomobject]@{DocsDir='/x'; ProjectName='Y'; OutputModuleDir='/o'} } |
| 136 | + Mock Get-NovaHelpMarkdownItem { @() } |
| 137 | + Mock Assert-NovaPlatyPSAvailable {} |
| 138 | + Mock Get-NovaHelpBuildContext {} |
| 139 | + Mock Export-NovaGeneratedHelp {} |
| 140 | + { Build-Help -ProjectInfo ([pscustomobject]@{}) } | Should -Not -Throw |
| 141 | + Assert-MockCalled Assert-NovaPlatyPSAvailable -Times 0 |
| 142 | + } |
| 143 | + |
| 144 | + It 'returns when no PlatyPS context is found' { |
| 145 | + $tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 146 | + New-Item -ItemType Directory -Path $tmp | Out-Null |
| 147 | + Set-Content -Path (Join-Path $tmp 'X.md') -Value 'help' |
| 148 | + try { |
| 149 | + Mock Get-NovaBuildProjectInfo { [pscustomobject]@{DocsDir='/x'; ProjectName='Y'; OutputModuleDir='/o'} } |
| 150 | + Mock Get-NovaHelpMarkdownItem { @(Get-Item (Join-Path $tmp 'X.md')) } |
| 151 | + Mock Assert-NovaPlatyPSAvailable {} |
| 152 | + Mock Get-NovaHelpBuildContext { $null } |
| 153 | + Mock Export-NovaGeneratedHelp {} |
| 154 | + Build-Help -ProjectInfo ([pscustomobject]@{}) |
| 155 | + Assert-MockCalled Export-NovaGeneratedHelp -Times 0 |
| 156 | + } finally { Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue } |
| 157 | + } |
| 158 | + |
| 159 | + It 'exports help when PlatyPS context is found' { |
| 160 | + $tmp = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid()) |
| 161 | + New-Item -ItemType Directory -Path $tmp | Out-Null |
| 162 | + Set-Content -Path (Join-Path $tmp 'X.md') -Value 'help' |
| 163 | + try { |
| 164 | + Mock Get-NovaBuildProjectInfo { [pscustomobject]@{DocsDir='/x'; ProjectName='Y'; OutputModuleDir='/o'} } |
| 165 | + Mock Get-NovaHelpMarkdownItem { @(Get-Item (Join-Path $tmp 'X.md')) } |
| 166 | + Mock Assert-NovaPlatyPSAvailable {} |
| 167 | + Mock Get-NovaHelpBuildContext { [pscustomobject]@{HelpMarkdownFiles=@(); CommandHelpFiles=@(); Locale='en-US'} } |
| 168 | + Mock Export-NovaGeneratedHelp {} |
| 169 | + Build-Help -ProjectInfo ([pscustomobject]@{}) |
| 170 | + Assert-MockCalled Export-NovaGeneratedHelp -Times 1 |
| 171 | + } finally { Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue } |
| 172 | + } |
| 173 | +} |
0 commit comments