Skip to content

Commit

Permalink
Add function Set-PSCodeHealthPlaceholdersValue to invert values into …
Browse files Browse the repository at this point in the history
…HTML report
  • Loading branch information
MathieuBuisson committed Jun 29, 2017
1 parent 951fa48 commit b3712b6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
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
}
10 changes: 10 additions & 0 deletions Tests/TestData/MockObjects.json
Original file line number Diff line number Diff line change
Expand Up @@ -843,5 +843,15 @@
]
}
}
],
"Get-Content":
[
"<title>PSCodeHealth Report - {REPORT_TITLE}</title>",
"</head>",
"<body>",
" <h1>",
" <img id=\"logo\" src= \"https://github.com/MathieuBuisson/PSCodeHealth/raw/master/PSCodeHealth/Assets/PSCodeHealthLogo.png\" alt=\"PSCodeHealth Logo\"/>",
" PSCodeHealth Report - {REPORT_TITLE} <small class=\"analyzed-path\"> {ANALYZED_PATH} - {DATE}</small>",
" </h1>"
]
}
45 changes: 45 additions & 0 deletions Tests/Unit/Private/Set-PSCodeHealthPlaceholdersValue.Tests.ps1
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>'
}
}
}
}

0 comments on commit b3712b6

Please sign in to comment.