MegaBites-AI/Windows-powershell
0308
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Tests for the Import-PowerShellDataFile cmdlet" -Tags "CI" {4 BeforeAll {5 $largePsd1Path = Join-Path -Path $TestDrive -ChildPath 'large.psd1'6 $largePsd1Builder = [System.Text.StringBuilder]::new('@{')7 1..501 | ForEach-Object {8 $largePsd1Builder.Append("key$_ = $_;")9 }10 $largePsd1Builder.Append('}')11 Set-Content -Path $largePsd1Path -Value $largePsd1Builder.ToString()12 }13 14 It "Validates error on a missing path" {15 { Import-PowerShellDataFile -Path /SomeMissingDirectory -ErrorAction Stop } |16 Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"17 }18 19 It "Validates error on a directory" {20 { Import-PowerShellDataFile ${TESTDRIVE} -ErrorAction Stop } |21 Should -Throw -ErrorId "PathNotFound,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"22 23 }24 25 It "Generates a good error on an insecure file" {26 27 $path = Setup -f insecure.psd1 -Content '@{ Foo = Get-Process }' -pass28 { Import-PowerShellDataFile $path -ErrorAction Stop } |29 Should -Throw -ErrorId "System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"30 }31 32 It "Generates a good error on a file that isn't a PowerShell Data File (missing the hashtable root)" {33 $path = Setup -f NotAPSDataFile -Content '"Hello World"' -Pass34 { Import-PowerShellDataFile $path -ErrorAction Stop } |35 Should -Throw -ErrorId "CouldNotParseAsPowerShellDataFileNoHashtableRoot,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand"36 }37 38 It "Can parse a PowerShell Data File (detailed tests are in AST.SafeGetValue tests)" {39 $path = Setup -F gooddatafile -Content '@{ "Hello" = "World" }' -pass40 $result = Import-PowerShellDataFile $path -ErrorAction Stop41 $result.Hello | Should -BeExactly "World"42 }43 44 It 'Fails if psd1 file has more than 500 keys' {45 { Import-PowerShellDataFile $largePsd1Path } | Should -Throw -ErrorId 'System.InvalidOperationException,Microsoft.PowerShell.Commands.ImportPowerShellDataFileCommand'46 }47 48 It 'Succeeds if -NoLimit is used and has more than 500 keys' {49 $result = Import-PowerShellDataFile $largePsd1Path -SkipLimitCheck50 $result.Keys.Count | Should -Be 50151 }52}53 