From 1e882ac2aa092fc8875b31ee518a1a8983fbcf5f Mon Sep 17 00:00:00 2001 From: robdy Date: Thu, 20 Jun 2019 22:19:12 +0200 Subject: [PATCH 1/4] Add New-Item -Force test --- .../New-Item.Tests.ps1 | 24 +++++++++++++++++++ 1 file changed, 24 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 a9a2599891b..7abad43d9a5 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,27 @@ 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 + } + + It "Should error correctly when -Force is not used and folder in the path doesn't exist" { + # Explicitly removing folder first + Remove-Item $FullyQualifiedFolder -ErrorAction SilentlyContinue + Test-Path -Path $FullyQualifiedFolder | Should -BeFalse + + { New-Item $FullyQualifiedFile -ErrorAction Stop } | Should -Throw -ErrorId 'NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand' + } + It "Should create new file correctly when -Force is used and folder in the path doesn't exist" { + # Explicitly removing folder first + Remove-Item $FullyQualifiedFolder -ErrorAction SilentlyContinue + Test-Path -Path $FullyQualifiedFolder | Should -BeFalse + + { New-Item $FullyQualifiedFile -Force -ErrorAction Stop } | Should -Not -Throw + } +} \ No newline at end of file From ccface46d808a3d6cfd3e06d8233b309e677ea4b Mon Sep 17 00:00:00 2001 From: robdy <15113729+robdy@users.noreply.github.com> Date: Fri, 21 Jun 2019 07:06:10 +0200 Subject: [PATCH 2/4] Update test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 Co-Authored-By: Aditya Patwardhan --- .../Microsoft.PowerShell.Management/New-Item.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 7abad43d9a5..d80a336c521 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 @@ -299,7 +299,7 @@ Describe "New-Item -Force allows to create an item even if the directories in th BeforeAll { $testFile = 'testfile.txt' $testFolder = 'testfolder' - $FullyQualifiedFolder = Join-Path -Path TestDrive -ChildPath $testFolder + $FullyQualifiedFolder = Join-Path -Path $TestDrive -ChildPath $testFolder $FullyQualifiedFile = Join-Path -Path $TestDrive -ChildPath $testFolder -AdditionalChildPath $testFile } @@ -317,4 +317,4 @@ Describe "New-Item -Force allows to create an item even if the directories in th { New-Item $FullyQualifiedFile -Force -ErrorAction Stop } | Should -Not -Throw } -} \ No newline at end of file +} From 43c6f6603da9c55bee717d2a9058f82431ccd545 Mon Sep 17 00:00:00 2001 From: robdy Date: Fri, 21 Jun 2019 19:02:37 +0200 Subject: [PATCH 3/4] Addressed requested changes --- .../New-Item.Tests.ps1 | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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 d80a336c521..2b67703bbfc 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 @@ -303,18 +303,21 @@ Describe "New-Item -Force allows to create an item even if the directories in th $FullyQualifiedFile = Join-Path -Path $TestDrive -ChildPath $testFolder -AdditionalChildPath $testFile } - It "Should error correctly when -Force is not used and folder in the path doesn't exist" { - # Explicitly removing folder first + 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" { - # Explicitly removing folder first - Remove-Item $FullyQualifiedFolder -ErrorAction SilentlyContinue - Test-Path -Path $FullyQualifiedFolder | Should -BeFalse - { New-Item $FullyQualifiedFile -Force -ErrorAction Stop } | Should -Not -Throw + $FullyQualifiedFile | Should -Exist } } + From 1f2878f49677a37c77940a5a06e84e6c8ef94696 Mon Sep 17 00:00:00 2001 From: robdy <15113729+robdy@users.noreply.github.com> Date: Tue, 25 Jun 2019 11:58:38 +0200 Subject: [PATCH 4/4] Fix indentation --- .../New-Item.Tests.ps1 | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) 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 2b67703bbfc..c9c5b317323 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1 @@ -299,25 +299,25 @@ Describe "New-Item -Force allows to create an item even if the directories in th BeforeAll { $testFile = 'testfile.txt' $testFolder = 'testfolder' - $FullyQualifiedFolder = Join-Path -Path $TestDrive -ChildPath $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" { + 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 + $FullyQualifiedFile | Should -Not -Exist } - It "Should create new file correctly when -Force is used and folder in the path doesn't 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 + $FullyQualifiedFile | Should -Exist } }