Skip to content

Commit 695d69a

Browse files
feat: Inject-ArmContent.ps1: support absolute paths to external files to inject (#280)
Co-authored-by: Stijn Moreels <9039753+stijnmoreels@users.noreply.github.com>
1 parent d2eca71 commit 695d69a

7 files changed

Lines changed: 66 additions & 8 deletions

File tree

docs/preview/02-Features/powershell/arm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,12 @@ It is possible to supply injection instructions in the injection annotation, thi
7070
| `InjectAsJsonObject` | Tests if the content is valid JSON and makes sure the content is injected without surrounding double quotes |
7171

7272
Usage of multiple injection instructions is supported as well, for example if you need both the `EscapeJson` and `ReplaceSpecialChars` functionality.
73+
The reference to the file to inject can either be a path relative to the 'parent' file or an absolute path.
7374

7475
Some examples are:
7576
```powershell
7677
${ FileToInject = ".\Parent Directory\file.xml" }
78+
${ FileToInject = "c:\Parent Directory\file.xml" }
7779
${ FileToInject = ".\Parent Directory\file.xml", EscapeJson, ReplaceSpecialChars }
7880
${ FileToInject = '.\Parent Directory\file.json', InjectAsJsonObject }
7981
```

src/Arcus.Scripting.ARM/Scripts/Inject-ArmContent.ps1

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
44
${ fileToInject.xml }
55
${ FileToInject=file.xml }
6+
${ FileToInject=c:\file.xml }
67
${ FileToInject = ".\Parent Directory\file.xml" }
8+
${ FileToInject = "c:\Parent Directory\file.xml" }
79
${ FileToInject = ".\Parent Directory\file.xml", EscapeJson, ReplaceSpecialChars }
810
${ FileToInject = '.\Parent Directory\file.json', InjectAsJsonObject }
911
#>
@@ -12,6 +14,20 @@ param (
1214
[string] $Path = $PSScriptRoot
1315
)
1416

17+
function Get-FullyQualifiedChildFilePath {
18+
param(
19+
[parameter(mandatory=$true)] [string] $ParentFilePath,
20+
[parameter(mandatory=$true)] [string] $ChildFilePath
21+
)
22+
23+
$parentDirectoryPath = Split-Path $ParentFilePath -Parent
24+
# Note: in case of a fully qualified (i.e. absolute) child path the Combine-function discards the parent directory path;
25+
# otherwise the relative child path is combined with the parent directory
26+
$combinedPath = [System.IO.Path]::Combine($parentDirectoryPath, $ChildFilePath)
27+
$fullPath = [System.IO.Path]::GetFullPath($combinedPath)
28+
return $fullPath
29+
}
30+
1531
function InjectFile {
1632
param(
1733
[string] $filePath
@@ -34,11 +50,9 @@ function InjectFile {
3450
throw "The file part '$filePart' of the injection instruction could not be parsed correctly"
3551
}
3652

37-
$relativePathOfFileToInject = $fileMatch.Groups["File"];
38-
$fullPathOfFileToInject = Join-Path (Split-Path $filePath -Parent) $relativePathOfFileToInject
39-
$fileToInjectIsFound = Test-Path -Path $fullPathOfFileToInject -PathType Leaf
40-
if ($false -eq $fileToInjectIsFound) {
41-
throw "No file can be found at '$fullPathofFileToInject'"
53+
$fullPathOfFileToInject = Get-FullyQualifiedChildFilePath -ParentFilePath $filePath -ChildFilePath $fileMatch.Groups["File"]
54+
if (-not(Test-Path -Path $fullPathOfFileToInject -PathType Leaf)) {
55+
throw "No file can be found at '$fullPathOfFileToInject'"
4256
}
4357

4458
# Inject content recursively first
@@ -116,7 +130,7 @@ $psScriptFileName = $MyInvocation.MyCommand.Name
116130

117131
$PathIsFound = Test-Path -Path $Path
118132
if ($false -eq $PathIsFound) {
119-
throw "Passed allong path '$Path' doesn't point to valid file path"
133+
throw "Passed along path '$Path' doesn't point to valid file path"
120134
}
121135

122136
Write-Host "Starting $psScriptFileName script on path $Path"

src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.ARM.tests.ps1

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ InModuleScope Arcus.Scripting.ARM {
2020
}
2121
}
2222
if ([Environment]::OSVersion.VersionString -like "*Windows*") {
23-
It "Replaces file path with file contents as JSON object (windows)" {
23+
It "Replaces relative file path with file contents as JSON object (windows)" {
2424
# Arrange
2525
$armTemplateFile = "$PSScriptRoot\Files\arm-template-object (windows).json"
2626
try {
@@ -36,8 +36,29 @@ InModuleScope Arcus.Scripting.ARM {
3636
Get-Content $originalFile | Out-File -FilePath $armTemplateFile
3737
}
3838
}
39+
It "Replaces absolute file path with file contents as JSON object (windows)" {
40+
# Arrange
41+
$armTemplateFile = "$PSScriptRoot\Files\arm-template-object-absolutepath (windows).json"
42+
$armTemplateDirectory = Split-Path $armTemplateFile -Parent
43+
$armTemplate = Get-Content -path $armTemplateFile -Raw
44+
$armTemplate = $armTemplate -replace '#{ArmTemplateDirectory}#', $armTemplateDirectory
45+
$armTemplate | Set-Content -Path $armTemplateFile
46+
47+
try {
48+
# Act
49+
Inject-ArmContent -Path $armTemplateFile
50+
51+
# Assert
52+
$expected = Get-Content "$PSScriptRoot\Files\arm-template-object-value (windows).json"
53+
$actual = Get-Content $armTemplateFile
54+
$actual[7] | Should -Be ' "value": "{\r\n \"test\": \"this is a test value\"\r\n}",'
55+
} finally {
56+
$originalFile = "$PSScriptRoot\Files\arm-template-object-absolutepath-org (windows).json"
57+
Get-Content $originalFile | Out-File -FilePath $armTemplateFile
58+
}
59+
}
3960
} else {
40-
It "Replaces file path with file contents as JSON object (linux)" {
61+
It "Replaces relative file path with file contents as JSON object (linux)" {
4162
# Arrange
4263
$armTemplateFile = "$PSScriptRoot\Files\arm-template-object (linux).json"
4364
try {
@@ -53,6 +74,27 @@ InModuleScope Arcus.Scripting.ARM {
5374
Get-Content $originalFile | Out-File -FilePath $armTemplateFile
5475
}
5576
}
77+
It "Replaces absolute file path with file contents as JSON object (linux)" {
78+
# Arrange
79+
$armTemplateFile = "$PSScriptRoot\Files\arm-template-object-absolutepath (linux).json"
80+
$armTemplateDirectory = Split-Path $armTemplateFile -Parent
81+
$armTemplate = Get-Content -path $armTemplateFile -Raw
82+
$armTemplate = $armTemplate -replace '#{ArmTemplateDirectory}#', $armTemplateDirectory
83+
$armTemplate | Set-Content -Path $armTemplateFile
84+
85+
try {
86+
# Act
87+
Inject-ArmContent -Path $armTemplateFile
88+
89+
# Assert
90+
$expected = Get-Content "$PSScriptRoot\Files\arm-template-object-value (linux).json"
91+
$actual = Get-Content $armTemplateFile
92+
$actual[7] | Should -Be ' "value": "{\n \"test\": \"this is a test value\"\n}",'
93+
} finally {
94+
$originalFile = "$PSScriptRoot\Files\arm-template-object-absolutepath-org (linux).json"
95+
Get-Content $originalFile | Out-File -FilePath $armTemplateFile
96+
}
97+
}
5698
}
5799
It "Replaces file path with file contents as escaped JSON and replaced special characters" {
58100
# Arrange
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)