Skip to content

Commit

Permalink
XML replacement tests and Issue #76 workaround and desired behaviors (#…
Browse files Browse the repository at this point in the history
…77)

* Creating tests to fill holes in coverage.
XML with special character still WIP

* Escaping issue test completed, showing failure. Match for Issue #76 in https://github.com/openalm/Extension-UtilitiesPack

* Work around identified for Issue #76 and test created defining desired behavior
  • Loading branch information
scottcrowe authored and Harshil Lodhi committed Jun 27, 2017
1 parent f2a4fb4 commit 93b7eda
Showing 1 changed file with 163 additions and 0 deletions.
163 changes: 163 additions & 0 deletions Tests/Tokenizer/tokenizer-ps3.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,168 @@ Describe "Replace token variables" {
}
}

Describe "Replace token variables" {
It "does not escape special characters in text files"{

$env:INPUT_SOURCEPATH = $srcPath = Join-Path $env:TEMP 'source.txt'
$env:INPUT_DESTINATIONPATH = $destPath = Join-Path $env:TEMP 'dest.txt'
$env:INPUT_CONFIGURATIONJSONFILE = $jsonConfigPath = Join-Path $env:TEMP 'config.json'
$env:RELEASE_ENVIRONMENTNAME = 'Test'
$foo1val = 'I am foo1'
$bar1val = 'I am bar1'
$foobarVal = 'FOO - & BAR'
$jsonConfigContent = @{
Test=@{
CustomVariables = @{
"foo1" = $foo1val
"bar1" = $bar1val
"foo_bar" = $foobarVal
}
}
} | ConvertTo-Json

$sourceContent = '__foo1__ __bar1__ __foo_bar__ __foo.bar__'
$expectedDestinationContent = $foo1val + " " + $bar1val + " " + $foobarVal + " " + $foobarVal

try {
Set-Content -Value $sourceContent -Path $srcPath
Set-Content -Value $jsonConfigContent -Path $jsonConfigPath
Invoke-VstsTaskScript -ScriptBlock { . $scriptPath }
Get-Content -Path $destPath | Should Be $expectedDestinationContent
}
finally {
Remove-Item -Path $srcPath
Remove-Item -Path $destPath
Remove-Item -Path $jsonConfigPath
}
}
}

Describe "XML Selection"{
It "finds nodes through XPath"{

$env:INPUT_SOURCEPATH = $srcPath = Join-Path $env:TEMP 'source.xml'
$env:INPUT_DESTINATIONPATH = $destPath = Join-Path $env:TEMP 'dest.xml'
$env:INPUT_CONFIGURATIONJSONFILE = $jsonConfigPath = Join-Path $env:TEMP 'config.json'
$env:RELEASE_ENVIRONMENTNAME = 'Test'

$jsonConfigContent = '{
"Test": {
"ConfigChanges": [
{
"value": "I am replaced",
"Attribute": "bar",
"KeyName": "/configuration/foo[@key=''testExample'']"
}
]
}
}'
$sourceContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="value to replace" /></configuration>'

$expectedDestinationContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="I am replaced" /></configuration>'

try {
#cycling the expected through a write and read to normalize expected spacing
$tempPath = Join-Path $env:TEMP 'temp.xml'
Set-Content -Value $expectedDestinationContent -Path $tempPath
$expectedDestinationContent = [xml](Get-Content -Path $tempPath)

Set-Content -Value $sourceContent -Path $srcPath
Set-Content -Value $jsonConfigContent -Path $jsonConfigPath
Invoke-VstsTaskScript -ScriptBlock { . $scriptPath }
([xml](Get-Content -Path $destPath)).OuterXML | Should Be $expectedDestinationContent.OuterXML
}
finally {
Remove-Item -Path $srcPath
Remove-Item -Path $destPath
Remove-Item -Path $jsonConfigPath
}
}
}

Describe "XML Selection Character Escape"{
It "does escape special characters in XML files when escaped in value"{
$env:INPUT_SOURCEPATH = $srcPath = Join-Path $env:TEMP 'source.xml'
$env:INPUT_DESTINATIONPATH = $destPath = Join-Path $env:TEMP 'dest.xml'
$env:INPUT_CONFIGURATIONJSONFILE = $jsonConfigPath = Join-Path $env:TEMP 'config.json'
$env:RELEASE_ENVIRONMENTNAME = 'Test'
$jsonConfigContent = '{
"Test": {
"ConfigChanges": [
{
"value": "I am replaced & \"happy\"",
"Attribute": "bar",
"KeyName": "/configuration/foo[@key=''testExample'']"
}
]
}
}'
$sourceContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="value to replace" /></configuration>'

$expectedDestinationContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="I am replaced &amp; &quot;happy&quot;" /></configuration>'

try {
#cycling the expected through a write and read to normalize expected spacing
$tempPath = Join-Path $env:TEMP 'temp.xml'
Set-Content -Value $expectedDestinationContent -Path $tempPath
$expectedDestinationContent = [xml](Get-Content -Path $tempPath)

Set-Content -Value $sourceContent -Path $srcPath
Set-Content -Value $jsonConfigContent -Path $jsonConfigPath
Invoke-VstsTaskScript -ScriptBlock { . $scriptPath }
([xml](Get-Content -Path $destPath)).OuterXML | Should Be $expectedDestinationContent.OuterXML
}
finally {
Remove-Item -Path $srcPath
Remove-Item -Path $destPath
Remove-Item -Path $jsonConfigPath
Remove-Item -Path $tempPath
}
}

It "may cause issues with pre-encoded strings"{
$env:INPUT_SOURCEPATH = $srcPath = Join-Path $env:TEMP 'source.xml'
$env:INPUT_DESTINATIONPATH = $destPath = Join-Path $env:TEMP 'dest.xml'
$env:INPUT_CONFIGURATIONJSONFILE = $jsonConfigPath = Join-Path $env:TEMP 'config.json'
$env:RELEASE_ENVIRONMENTNAME = 'Test'
$jsonConfigContent = '{
"Test": {
"ConfigChanges": [
{
"value": "I am replaced & &quot;happy&quot;",
"Attribute": "bar",
"KeyName": "/configuration/foo[@key=''testExample'']"
}
]
}
}'
$sourceContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="value to replace" /></configuration>'

#desired string
$expectedDestinationContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="I am replaced &amp; &quot;happy&quot;" /></configuration>'
#actual string
$expectedDestinationContent = '<?xml version="1.0" encoding="utf-8"?><configuration><foo key="testExample" bar="I am replaced &amp; &amp;quot;happy&amp;quot;" /></configuration>'

try {
#cycling the expected through a write and read to normalize expected spacing
$tempPath = Join-Path $env:TEMP 'temp.xml'
Set-Content -Value $expectedDestinationContent -Path $tempPath
$expectedDestinationContent = [xml](Get-Content -Path $tempPath)

Set-Content -Value $sourceContent -Path $srcPath
Set-Content -Value $jsonConfigContent -Path $jsonConfigPath
Invoke-VstsTaskScript -ScriptBlock { . $scriptPath }
([xml](Get-Content -Path $destPath)).OuterXML | Should Be $expectedDestinationContent.OuterXML
}
finally {
Remove-Item -Path $srcPath
Remove-Item -Path $destPath
Remove-Item -Path $jsonConfigPath
Remove-Item -Path $tempPath
}
}
}

Describe "Encoding Test" {
It "replaces multiple variables defined as env variables(configuration variables)"{

Expand Down Expand Up @@ -100,6 +262,7 @@ Describe "Not set variables should not get replaced" {

$env:INPUT_SOURCEPATH = $srcPath = Join-Path $env:TEMP 'source.txt'
$env:INPUT_DESTINATIONPATH = $destPath = Join-Path $env:TEMP 'dest.txt'
$env:INPUT_REPLACEUNDEFINEDVALUESWITHEMPTY = $false
$fooVal = "的I am foo的"
$barVal = "的I am bar的"
$secretVal = "I am secret"
Expand Down

0 comments on commit 93b7eda

Please sign in to comment.