From aa07eef187abee664cd2705336f8aac8de4e27e2 Mon Sep 17 00:00:00 2001 From: robdy <15113729+robdy@users.noreply.github.com> Date: Tue, 25 Jun 2019 20:03:07 +0200 Subject: [PATCH] Add test for `New-Item -Force` (#9971) --- .../New-Item.Tests.ps1 | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 index a9a259989..c9c5b3173 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 @@ -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 + } +} +