mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add test for New-Item -Force (#9971)
This commit is contained in:
@@ -294,3 +294,30 @@ Describe "New-Item with links fails for non elevated user if developer mode not
|
||||
$TestFilePath | Should -Exist
|
||||
}
|
||||
}
|
||||
|
||||
Describe "New-Item -Force allows to create an item even if the directories in the path don't exist" -Tags "CI" {
|
||||
BeforeAll {
|
||||
$testFile = 'testfile.txt'
|
||||
$testFolder = 'testfolder'
|
||||
$FullyQualifiedFolder = Join-Path -Path $TestDrive -ChildPath $testFolder
|
||||
$FullyQualifiedFile = Join-Path -Path $TestDrive -ChildPath $testFolder -AdditionalChildPath $testFile
|
||||
}
|
||||
|
||||
BeforeEach {
|
||||
# Explicitly removing folder and the file before tests
|
||||
Remove-Item $FullyQualifiedFolder -ErrorAction SilentlyContinue
|
||||
Remove-Item $FullyQualifiedFile -ErrorAction SilentlyContinue
|
||||
Test-Path -Path $FullyQualifiedFolder | Should -BeFalse
|
||||
Test-Path -Path $FullyQualifiedFile | Should -BeFalse
|
||||
}
|
||||
|
||||
It "Should error correctly when -Force is not used and folder in the path doesn't exist" {
|
||||
{ New-Item $FullyQualifiedFile -ErrorAction Stop } | Should -Throw -ErrorId 'NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand'
|
||||
$FullyQualifiedFile | Should -Not -Exist
|
||||
}
|
||||
It "Should create new file correctly when -Force is used and folder in the path doesn't exist" {
|
||||
{ New-Item $FullyQualifiedFile -Force -ErrorAction Stop } | Should -Not -Throw
|
||||
$FullyQualifiedFile | Should -Exist
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user