MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Set-Content cmdlet tests" -Tags "CI" {4 BeforeAll {5 $file1 = "file1.txt"6 $filePath1 = Join-Path $testdrive $file17 # if the registry doesn't exist, don't run those tests8 $skipRegistry = ! (Test-Path hklm:/)9 }10 11 It "A warning should be emitted if both -AsByteStream and -Encoding are used together" {12 $testfile = "${TESTDRIVE}\bfile.txt"13 "test" | Set-Content $testfile14 $result = Get-Content -AsByteStream -Encoding Unicode -Path $testfile -WarningVariable contentWarning *> $null15 $contentWarning.Message | Should -Match "-AsByteStream"16 }17 18 Context "Set-Content should create a file if it does not exist" {19 AfterEach {20 Remove-Item -Path $filePath1 -Force -ErrorAction SilentlyContinue21 }22 It "should create a file if it does not exist" {23 Set-Content -Path $filePath1 -Value "$file1"24 $result = Get-Content -Path $filePath125 $result | Should -Be "$file1"26 }27 }28 Context "Set-Content/Get-Content should set/get the content of an exisiting file" {29 BeforeAll {30 New-Item -Path $filePath1 -ItemType File -Force31 }32 It "should set-Content of testdrive\$file1" {33 Set-Content -Path $filePath1 -Value "ExpectedContent"34 $result = Get-Content -Path $filePath135 $result | Should -Be "ExpectedContent"36 }37 It "should return expected string from testdrive\$file1" {38 $result = Get-Content -Path $filePath139 $result | Should -BeExactly "ExpectedContent"40 }41 It "should Set-Content to testdrive\dynamicfile.txt with dynamic parameters" {42 Set-Content -Path $testdrive\dynamicfile.txt -Value "ExpectedContent"43 $result = Get-Content -Path $testdrive\dynamicfile.txt44 $result | Should -BeExactly "ExpectedContent"45 }46 It "should return expected string from testdrive\dynamicfile.txt" {47 $result = Get-Content -Path $testdrive\dynamicfile.txt48 $result | Should -BeExactly "ExpectedContent"49 }50 It "should remove existing content from testdrive\$file1 when the -Value is `$null" {51 $AsItWas=Get-Content $filePath152 $AsItWas | Should -BeExactly "ExpectedContent"53 Set-Content -Path $filePath1 -Value $null -ErrorAction Stop54 $AsItIs=Get-Content $filePath155 $AsItIs | Should -Not -Be $AsItWas56 }57 It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$null" {58 { Set-Content -Path $null -Value "ShouldNotWorkBecausePathIsNull" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand"59 }60 It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$()" {61 { Set-Content -Path $() -Value "ShouldNotWorkBecausePathIsInvalid" -ErrorAction Stop } | Should -Throw -ErrorId "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand"62 }63 It "should throw 'PSNotSupportedException' when you Set-Content to an unsupported provider" -Skip:$skipRegistry {64 { Set-Content -Path HKLM:\\software\\microsoft -Value "ShouldNotWorkBecausePathIsUnsupported" -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.SetContentCommand"65 }66 #[BugId(BugDatabase.WindowsOutOfBandReleases, 9058182)]67 It "should be able to pass multiple [string]`$objects to Set-Content through the pipeline to output a dynamic Path file" {68 "hello","world"|Set-Content $testdrive\dynamicfile2.txt69 $result=Get-Content $testdrive\dynamicfile2.txt70 $result.length | Should -Be 271 $result[0] | Should -BeExactly "hello"72 $result[1] | Should -BeExactly "world"73 }74 }75 Context "Set-Content should work with alternate data streams on Windows" {76 BeforeAll {77 if ( -Not $IsWindows )78 {79 return80 }81 $altStreamPath = "$TESTDRIVE/altStream.txt"82 $altStreamPath2 = "$TESTDRIVE/altStream2.txt"83 $altStreamDirectory = "$TESTDRIVE/altstreamdir"84 $altStreamDirectory2 = "$TESTDRIVE/altstream2dir"85 $stringData = "test data"86 $streamName = "test"87 $absentStreamName = "noExist"88 $item = New-Item -type file $altStreamPath89 $altstreamdiritem = New-Item -type directory $altStreamDirectory90 }91 It "Should create a new data stream on a file" -Skip:(-Not $IsWindows) {92 Set-Content -Path $altStreamPath -Stream $streamName -Value $stringData93 Get-Content -Path $altStreamPath -Stream $streamName | Should -BeExactly $stringData94 }95 It "Should create a new data stream on a file using colon syntax" -Skip:(-Not $IsWindows) {96 Set-Content -Path ${altStreamPath2}:${streamName} -Value $stringData97 Get-Content -Path ${altStreamPath2} -Stream $streamName | Should -BeExactly $stringData98 }99 It "Should create a new data stream on a directory" -Skip:(-Not $IsWindows) {100 Set-Content -Path $altStreamDirectory -Stream $streamName -Value $stringData101 Get-Content -Path $altStreamDirectory -Stream $streamName | Should -BeExactly $stringData102 }103 It "Should create a new data stream on a directory using colon syntax" -Skip:(-Not $IsWindows) {104 Set-Content -Path ${altStreamDirectory2}:${streamName} -Value $stringData105 Get-Content -Path ${altStreamDirectory2} -Stream ${streamName} | Should -BeExactly $stringData106 }107 }108}109 110Describe "Set-Content should work for PSDrive with UNC path as root" -Tags @('CI', 'RequireAdminOnWindows') {111 BeforeAll {112 $file1 = "file1.txt"113 #create a random folder114 $randomFolderName = "TestFolder_" + (Get-Random).ToString()115 $randomFolderPath = Join-Path $testdrive $randomFolderName116 $null = New-Item -Path $randomFolderPath -ItemType Directory -ErrorAction SilentlyContinue117 }118 # test is Pending due to https://github.com/PowerShell/PowerShell/issues/3883119 It "should create a file in a psdrive with UNC path as root" -Pending {120 try121 {122 # create share123 net share testshare=$randomFolderPath /grant:everyone,FULL124 New-PSDrive -Name Foo -Root \\localhost\testshare -PSProvider FileSystem125 Set-Content -Path Foo:\$file1 -Value "$file1"126 $result = Get-Content -Path Foo:\$file1127 $result | Should -BeExactly "$file1"128 }129 finally130 {131 Remove-PSDrive -Name Foo132 net share testshare /delete133 }134 }135}136 