MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Select-Xml DRT Unit Tests" -Tags "CI" {5 6 BeforeAll {7 $testfile = "TestDrive:\testfile.xml"8 $xmlContent = @"9<?xml version ="1.0" encoding="ISO-8859-1"?>10<bookstore>11 <book category="CHILDREN">12 <title lang="english">Harry Potter</title>13 <price>30.00</price>14 </book>15 <book category="WEB">16 <title lang="english">Learning XML</title>17 <price>25.00</price>18 </book>19</bookstore>20"@21 Set-Content -Path $testfile -Value $xmlContent22 }23 24 It "Can select text from an XML document"{25 $results = Select-Xml -Path $testfile -XPath "/bookstore/book/title"26 $results | Should -HaveCount 227 $results[0].Node."#text" | Should -BeExactly "Harry Potter"28 $results[1].Node."#text" | Should -BeExactly "Learning XML"29 }30}31 32Describe "Select-Xml Feature Tests" -Tags "Feature" {33 34 BeforeAll {35 $fileName = New-Item -Path "TestDrive:\testSelectXml.xml"36 $xmlContent = @"37<Root>38 <Node Attribute='blah' />39</Root>40"@41 42 Set-Content -Path $fileName -Value $xmlContent43 44 $fileNameWithDots = $fileName.FullName.Replace("\", "\.\")45 46 $driveLetter = [string]($fileName.FullName)[0]47 $fileNameAsNetworkPath = "\\localhost\$driveLetter`$" + $fileName.FullName.SubString(2)48 49 $testCases = @(50 @{testName = 'Literalpath with relative paths'; testParameter = @{LiteralPath = $fileName.Name; XPath = 'Root'}},51 @{testName = 'Literalpath with absolute paths'; testParameter = @{LiteralPath = $fileName.FullName; XPath = 'Root'}},52 @{testName = 'Literalpath with path with dots'; testParameter = @{LiteralPath = $fileNameWithDots; XPath = 'Root'}},53 @{testName = 'Path with relative paths'; testParameter = @{Path = $fileName.Name; XPath = 'Root'}},54 @{testName = 'Path with absolute paths'; testParameter = @{Path = $fileName.FullName; XPath = 'Root'}},55 @{testName = 'Path with path with dots'; testParameter = @{Path = $fileNameWithDots; XPath = 'Root'}}56 )57 58 if ( ! $IsCoreCLR ) {59 $testcases += @{testName = 'Literalpath with network paths'; testParameter = @{LiteralPath = $fileNameAsNetworkPath; XPath = 'Root'}}60 $testcases += @{testName = 'Path with network paths'; testParameter = @{LiteralPath = $fileNameAsNetworkPath; XPath = 'Root'}}61 }62 63 Push-Location -Path $fileName.Directory64 }65 66 AfterAll {67 Pop-Location68 }69 70 It "Can work with input files using <testName>" -TestCases $testCases {71 param($testParameter)72 73 $node = Select-Xml @testParameter74 $node | Should -HaveCount 175 $node.Path | Should -Be $fileName.FullName76 }77 78 It "Can work with input streams" {79 [xml]$xml = "<a xmlns='bar'><b xmlns:b='foo'>hello</b><c>world</c></a>"80 $node = Select-Xml -Xml $xml -XPath "//c:b" -Namespace @{c='bar'}81 $node.Path | Should -BeExactly "InputStream"82 $node.Pattern | Should -BeExactly "//c:b"83 $node.ToString() | Should -BeExactly "hello"84 }85 86 It "Can throw on non filesystem paths using <parameter>" -TestCases @(87 @{parameter="LiteralPath"; expectedError='ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand'},88 @{parameter="Path"; expectedError='ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand'}89 ) {90 param($parameter, $expectedError)91 92 $env:xmltestfile = $xmlContent93 $file = 'env:xmltestfile'94 $params = @{$parameter=$file}95 { Select-Xml @params "Root" -ErrorAction Stop } | Should -Throw -ErrorId $expectedError96 Remove-Item -Path 'env:xmltestfile'97 }98 99 It "Can throw for invalid XML file" {100 $testfile = "TestDrive:\test.xml"101 Set-Content -Path $testfile -Value "<a><b>"102 { Select-Xml -Path $testfile -XPath foo -ErrorAction Stop } | Should -Throw -ErrorId 'ProcessingFile,Microsoft.PowerShell.Commands.SelectXmlCommand'103 }104 105 It "Can throw for invalid XML namespace" {106 [xml]$xml = "<a xmlns='bar'><b xmlns:b='foo'>hello</b><c>world</c></a>"107 { Select-Xml -Xml $xml -XPath foo -Namespace @{c=$null} -ErrorAction Stop } | Should -Throw -ErrorId 'PrefixError,Microsoft.PowerShell.Commands.SelectXmlCommand'108 }109 110 It "Can throw for invalid XML content" {111 { Select-Xml -Content "hello" -XPath foo -ErrorAction Stop } | Should -Throw -ErrorId 'InvalidCastToXmlDocument,Microsoft.PowerShell.Commands.SelectXmlCommand'112 }113 114 It "Can use ToString() on nested XML node" {115 $node = Select-Xml -Content "<a><b>one<c>hello</c></b></a>" -XPath "//b"116 $node.ToString() | Should -BeExactly "one<c>hello</c>"117 }118 119 It "Can use ToString() with file" {120 $testfile = Join-Path $TestDrive "test.xml"121 Set-Content -Path $testfile -Value "<a><b>hello</b></a>"122 $node = Select-Xml -Path $testfile -XPath "//b"123 $node.ToString() | Should -BeExactly "hello:$testfile"124 }125}126 