Skip to content

Commit b7bfc8a

Browse files
authored
Fixed snippet with short relative template causes exception #26 (#33)
* Fixed snippet with short relative template causes exception #26 * Updated expect content test
1 parent 9650dbb commit b7bfc8a

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
What's changed since v0.1.0:
6+
7+
- Bug fixes:
8+
- Fixed snippet with short relative template causes exception. [#26](https://github.com/Azure/PSDocs.Azure/issues/26)
9+
510
## v0.1.0
611

712
- Initial release.

src/PSDocs.Azure/docs/Azure.Template.Doc.ps1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ function global:GetTemplateExample {
3333
[String]$Path
3434
)
3535
process {
36+
if (![System.IO.Path]::IsPathRooted($Path)) {
37+
$Path = Join-Path -Path $PWD -ChildPath $Path;
38+
}
3639
$template = Get-Content -Path $Path -Raw | ConvertFrom-Json;
37-
$normalPath = $Path.Substring(([String]$PWD).Length);
38-
$normalPath = ($normalPath -replace '\\', '/').TrimStart('/');
40+
$normalPath = $Path;
41+
if ($normalPath.StartsWith($PWD, [System.StringComparison]::InvariantCultureIgnoreCase)) {
42+
$normalPath = $Path.Substring(([String]$PWD).Length);
43+
$normalPath = ($normalPath -replace '\\', '/').TrimStart('/');
44+
}
3945
$baseContent = [PSCustomObject]@{
4046
'$schema'= "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json`#"
4147
contentVersion = '1.0.0.0'

tests/PSDocs.Azure.Tests/Azure.Common.Tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ Describe 'PSDocs' -Tag 'PSDocs', 'Common' {
4141
$result = Invoke-PSDocument @invokeParams;
4242
$result | Should -Not -BeNullOrEmpty;
4343
}
44+
45+
It 'With relative path' {
46+
Push-Location -Path (Join-Path -Path $rootPath -ChildPath 'templates/storage/v1/');
47+
try {
48+
$invokeParams = @{
49+
Module = 'PSDocs.Azure'
50+
OutputPath = $outputPath
51+
InputObject = './template.json'
52+
Option = @{ 'Output.Culture' = 'en-US' }
53+
}
54+
$result = Invoke-PSDocument @invokeParams;
55+
$result | Should -Not -BeNullOrEmpty;
56+
}
57+
finally {
58+
Pop-Location;
59+
}
60+
}
4461
}
4562
}
4663

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
#
5+
# Unit tests for PSDocs.Azure functionality using example templates
6+
#
7+
8+
[CmdletBinding()]
9+
param ()
10+
11+
# Setup error handling
12+
$ErrorActionPreference = 'Stop';
13+
Set-StrictMode -Version latest;
14+
15+
# Setup tests paths
16+
$rootPath = $PWD;
17+
18+
Import-Module (Join-Path -Path $rootPath -ChildPath out/modules/PSDocs.Azure) -Force;
19+
20+
$outputPath = Join-Path -Path $rootPath -ChildPath out/tests/PSDocs.Azure.Tests/Common;
21+
Remove-Item -Path $outputPath -Force -Recurse -Confirm:$False -ErrorAction Ignore;
22+
$Null = New-Item -Path $outputPath -ItemType Directory -Force;
23+
24+
Describe 'Templates' -Tag 'Template' {
25+
$templatePath = Join-Path -Path $rootPath -ChildPath 'templates/';
26+
27+
Context 'Documentation for examples' {
28+
It 'Generates expected output' {
29+
$invokeParams = @{
30+
Module = 'PSDocs.Azure'
31+
}
32+
33+
# Generates templates
34+
$result = Get-AzDocTemplateFile -Path $templatePath | ForEach-Object {
35+
$template = Get-Item -Path $_.TemplateFile;
36+
$parentPath = $template.Directory.FullName;
37+
$expectedContent = Get-Content -Path (Join-Path -Path $parentPath -ChildPath 'README.md') -Raw;
38+
$actualContent = Invoke-PSDocument @invokeParams -OutputPath $outputPath -InputObject $template.FullName -PassThru;
39+
$actualContent | Should -BeExactly $expectedContent;
40+
$actualContent;
41+
}
42+
$result | Should -Not -BeNullOrEmpty;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)