Repository navigation
Fix set-content failure on Linux for creating file in psdrive. (#2206) #2392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 103 additions & 44 deletions
147
test/powershell/Modules/Microsoft.PowerShell.Management/Set-Content.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,110 @@ | ||
| Describe "Set-Content cmdlet tests" -Tags "CI" { | ||
| $file1 = "file1.txt" | ||
| Setup -File "$file1" -Content $file1 | ||
| Context "Set-Content should actually set content" { | ||
| It "should set-Content of testdrive:\$file1" { | ||
| $result=set-content -path testdrive:\$file1 -value "ExpectedContent" -passthru | ||
| $result| Should be "ExpectedContent" | ||
| BeforeAll { | ||
| $file1 = "file1.txt" | ||
| $filePath1 = join-path $testdrive $file1 | ||
| # if the registry doesn't exist, don't run those tests | ||
| $skipRegistry = ! (test-path hklm:/) | ||
| } | ||
| It "should return expected string from testdrive:\$file1" { | ||
| $result = get-content -path testdrive:\$file1 | ||
| $result | Should BeExactly "ExpectedContent" | ||
| Context "Set-Content should create a file if it does not exist" { | ||
| AfterEach { | ||
| Remove-Item -path $filePath1 -Force -ErrorAction SilentlyContinue | ||
| } | ||
| It "should create a file if it does not exist" { | ||
| set-content -path $filePath1 -value "$file1" | ||
| $result = Get-Content -path $filePath1 | ||
| $result| Should be "$file1" | ||
| } | ||
| } | ||
| It "should Set-Content to testdrive:\dynamicfile.txt with dynamic parameters" -Pending:($IsLinux -Or $IsOSX) {#https://github.com/PowerShell/PowerShell/issues/891 | ||
| $result=set-content -path testdrive:\dynamicfile.txt -value "ExpectedContent" -passthru | ||
| $result| Should BeExactly "ExpectedContent" | ||
| Context "Set-Content/Get-Content should set/get the content of an exisiting file" { | ||
| BeforeAll { | ||
| New-Item -Path $filePath1 -ItemType File -Force | ||
| } | ||
| It "should set-Content of testdrive\$file1" { | ||
| set-content -path $filePath1 -value "ExpectedContent" | ||
| $result = Get-Content -path $filePath1 | ||
| $result| Should be "ExpectedContent" | ||
| } | ||
| It "should return expected string from testdrive\$file1" { | ||
| $result = get-content -path $filePath1 | ||
| $result | Should BeExactly "ExpectedContent" | ||
| } | ||
| It "should Set-Content to testdrive\dynamicfile.txt with dynamic parameters" { | ||
| set-content -path $testdrive\dynamicfile.txt -value "ExpectedContent" | ||
| $result = Get-Content -path $testdrive\dynamicfile.txt | ||
| $result| Should BeExactly "ExpectedContent" | ||
| } | ||
| It "should return expected string from testdrive\dynamicfile.txt" { | ||
| $result = get-content -path $testdrive\dynamicfile.txt | ||
| $result | Should BeExactly "ExpectedContent" | ||
| } | ||
| It "should remove existing content from testdrive\$file1 when the -Value is `$null" { | ||
| $AsItWas=get-content $filePath1 | ||
| $AsItWas |Should BeExactly "ExpectedContent" | ||
| set-content -path $filePath1 -value $null -ea stop | ||
| $AsItIs=get-content $filePath1 | ||
| $AsItIs| Should Not Be $AsItWas | ||
| } | ||
| It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$null" { | ||
| try { | ||
| set-content -path $null -value "ShouldNotWorkBecausePathIsNull" -ea stop | ||
| Throw "Previous statement unexpectedly succeeded..." | ||
| } | ||
| catch { | ||
| $_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand" | ||
| } | ||
| } | ||
| It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$()" { | ||
| try { | ||
| set-content -path $() -value "ShouldNotWorkBecausePathIsInvalid" -ea stop | ||
| Throw "Previous statement unexpectedly succeeded..." | ||
| } | ||
| catch { | ||
| $_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand" | ||
| } | ||
| } | ||
| It "should throw 'PSNotSupportedException' when you set-content to an unsupported provider" -skip:$skipRegistry { | ||
| try { | ||
| set-content -path HKLM:\\software\\microsoft -value "ShouldNotWorkBecausePathIsUnsupported" -ea stop | ||
| Throw "Previous statement unexpectedly succeeded..." | ||
| } | ||
| catch { | ||
| $_.FullyQualifiedErrorId | Should Be "NotSupported,Microsoft.PowerShell.Commands.SetContentCommand" | ||
| } | ||
| } | ||
| #[BugId(BugDatabase.WindowsOutOfBandReleases, 9058182)] | ||
| It "should be able to pass multiple [string]`$objects to Set-Content through the pipeline to output a dynamic Path file" { | ||
| "hello","world"|set-content $testdrive\dynamicfile2.txt | ||
| $result=get-content $testdrive\dynamicfile2.txt | ||
| $result.length |Should be 2 | ||
| $result[0] |Should be "hello" | ||
| $result[1] |Should be "world" | ||
| } | ||
| } | ||
| It "should return expected string from testdrive:\dynamicfile.txt" -Pending:($IsLinux -Or $IsOSX) {#https://github.com/PowerShell/PowerShell/issues/891 | ||
| $result = get-content -path testdrive:\dynamicfile.txt | ||
| $result | Should BeExactly "ExpectedContent" | ||
| } | ||
| It "should remove existing content from testdrive:\$file1 when the -Value is `$null" { | ||
| $AsItWas=get-content testdrive:\$file1 | ||
| $AsItWas |Should BeExactly "ExpectedContent" | ||
| {set-content -path testdrive:\$file1 -value $null -ea stop} | Should Not Throw | ||
| $AsItIs=get-content testdrive:\$file1 | ||
| $AsItIs| Should Not Be $AsItWas | ||
| } | ||
| It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$null" { | ||
| Try {set-content -path $null -value "ShouldNotWorkBecausePathIsNull" -ea stop; Throw "Previous statement unexpectedly succeeded..." | ||
| } Catch {$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand"} | ||
| } | ||
| #[BugId(BugDatabase.WindowsOutOfBandReleases, 903880)] | ||
| It "should throw 'ParameterArgumentValidationErrorNullNotAllowed' when -Path is `$()" { | ||
| Try {set-content -path $() -value "ShouldNotWorkBecausePathIsInvalid" -ea stop; Throw "Previous statement unexpectedly succeeded..." | ||
| } Catch {$_.FullyQualifiedErrorId | Should Be "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand"} | ||
| } | ||
| #[BugId(BugDatabase.WindowsOutOfBandReleases, 906022)] | ||
| It "should throw 'PSNotSupportedException' when you set-content to an unsupported provider" -Skip:($IsLinux -Or $IsOSX) { | ||
| Try {set-content -path HKLM:\\software\\microsoft -value "ShouldNotWorkBecausePathIsUnsupported" -ea stop; Throw "Previous statement unexpectedly succeeded..." | ||
| } Catch {$_.FullyQualifiedErrorId | Should Be "NotSupported,Microsoft.PowerShell.Commands.SetContentCommand"} | ||
| } | ||
|
|
||
| Describe "Set-Content should work for PSDrive with UNC path as root" -Tags @('CI', 'RequireAdminOnWindows') { | ||
| BeforeAll { | ||
| $file1 = "file1.txt" | ||
| $filePath1 = join-path $testdrive $file1 | ||
| #create a random folder | ||
| $randomFolderName = "TestFolder_" + (Get-Random).ToString() | ||
| $randomFolderPath = join-path $testdrive $randomFolderName | ||
| $null = New-Item -Path $randomFolderPath -ItemType Directory -ErrorAction SilentlyContinue | ||
| } | ||
| #[BugId(BugDatabase.WindowsOutOfBandReleases, 9058182)] | ||
| It "should be able to pass multiple [string]`$objects to Set-Content through the pipeline to output a dynamic Path file" -Pending:($IsLinux -Or $IsOSX) {#https://github.com/PowerShell/PowerShell/issues/891 | ||
| "hello","world"|set-content testdrive:\dynamicfile2.txt | ||
| $result=get-content testdrive:\dynamicfile2.txt | ||
| $result.length |Should be 2 | ||
| $result[0] |Should be "hello" | ||
| $result[1] |Should be "world" | ||
| It "should create a file in a psdrive with UNC path as root" -skip:(-not $IsWindows){ | ||
| try | ||
| { | ||
| # create share | ||
| net share testshare=$randomFolderPath /grant:everyone,FULL | ||
| New-PSDrive -Name Foo -Root \\localhost\testshare -PSProvider FileSystem | ||
| set-content -path Foo:\$file1 -value "$file1" | ||
| $result = Get-Content -path Foo:\$file1 | ||
| $result| Should be "$file1" | ||
| } | ||
| finally | ||
| { | ||
| Remove-PSDrive -Name Foo | ||
| net share testshare /delete | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!