-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function Set-PSCodeHealthPlaceholdersValue to invert values into …
…HTML report
- Loading branch information
1 parent
951fa48
commit b3712b6
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
PSCodeHealth/Private/HtmlReport/Set-PSCodeHealthPlaceholdersValue.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Function Set-PSCodeHealthPlaceholdersValue { | ||
<# | ||
.SYNOPSIS | ||
Replaces Placeholders in template files with their specified value. | ||
.DESCRIPTION | ||
Replaces Placeholders in template files with their specified string value and outputs the new content with the replaced value. | ||
.PARAMETER TemplatePath | ||
Path of the template file containing placeholders to replace. | ||
.PARAMETER PlaceholdersData | ||
Hashtable with a key-value pair for each placeholder. The key is corresponds to the name of the placeholder to replace and the value corresponds to its string value. | ||
.EXAMPLE | ||
PS C:\> $PlaceholdersData = @{ | ||
REPORT_TITLE = $HealthReport.ReportTitle | ||
ANALYZED_PATH = $HealthReport.AnalyzedPath | ||
} | ||
PS C:\> Set-PSCodeHealthPlaceholdersValue -TemplatePath '.\HealthReportTemplate.html' -PlaceholdersData $PlaceholdersData | ||
Returns the content of the template file with the placeholders 'REPORT_TITLE' and 'ANALYZED_PATH' substituted by the string values specified in the hashtable $PlaceholdersData. | ||
.OUTPUTS | ||
System.String | ||
.NOTES | ||
#> | ||
[CmdletBinding()] | ||
[OutputType([string[]])] | ||
Param ( | ||
[Parameter(Position=0, Mandatory)] | ||
[ValidateScript({ Test-Path $_ -PathType Leaf })] | ||
[string]$TemplatePath, | ||
|
||
[Parameter(Position=1, Mandatory)] | ||
[Hashtable]$PlaceholdersData | ||
) | ||
|
||
$TemplateContent = Get-Content -Path $TemplatePath | ||
|
||
Foreach ( $Placeholder in $PlaceholdersData.GetEnumerator() ) { | ||
$TemplateContent = $TemplateContent.ForEach('Replace',"{$($Placeholder.Key)}",$Placeholder.Value) | ||
} | ||
$TemplateContent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Tests/Unit/Private/Set-PSCodeHealthPlaceholdersValue.Tests.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
$ModuleName = 'PSCodeHealth' | ||
Import-Module "$PSScriptRoot\..\..\..\$ModuleName\$($ModuleName).psd1" -Force | ||
|
||
Describe 'Set-PSCodeHealthPlaceholdersValue' { | ||
InModuleScope $ModuleName { | ||
|
||
$Mocks = ConvertFrom-Json (Get-Content -Path "$($PSScriptRoot)\..\..\TestData\MockObjects.json" -Raw ) | ||
$MockedContent = $Mocks.'Get-Content' | ||
Mock Get-Content { $MockedContent } | ||
$MockedFile = New-Item -Path TestDrive:\Template.html -ItemType File | ||
|
||
Context 'The template file does not contain any placeholder matching any key of the specified PlaceholdersData' { | ||
|
||
$PlaceholdersData = @{ DOES_NOT_EXIST = 'StringValue'} | ||
$Result = Set-PSCodeHealthPlaceholdersValue -TemplatePath $MockedFile.FullName -PlaceholdersData $PlaceholdersData | ||
|
||
It 'Should return 7 lines' { | ||
$Result.Count | Should Be 7 | ||
} | ||
It 'Should return the content of the template file unmodified' { | ||
$Result | Should Be $MockedContent | ||
} | ||
} | ||
Context 'The template file contains 4 placeholders matching 3 keys of the specified PlaceholdersData' { | ||
|
||
$PlaceholdersData = @{ | ||
REPORT_TITLE = 'StringValue1' | ||
ANALYZED_PATH = 'StringValue2' | ||
DATE = Get-Date -Format u | ||
} | ||
$Result = Set-PSCodeHealthPlaceholdersValue -TemplatePath $MockedFile.FullName -PlaceholdersData $PlaceholdersData | ||
|
||
It 'Should return 7 lines' { | ||
$Result.Count | Should Be 7 | ||
} | ||
It 'Replaces properly a single placeholder in a line' { | ||
$Result[0] | Should Be '<title>PSCodeHealth Report - StringValue1</title>' | ||
} | ||
It 'Does not modify lines which do not contain any placeholder' { | ||
$Result[1] | Should Be '</head>' | ||
$Result[2] | Should Be '<body>' | ||
} | ||
} | ||
} | ||
} |