From f6afd995c4b15be2c0f60c05d67ade73da7cda54 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 18 Feb 2018 00:49:22 +0000 Subject: [PATCH 1/8] new-temporary file to throw terminating error and other improvements --- .../utility/NewTemporaryFileCommand.cs | 10 ++++---- .../NewTemporaryFile.Tests.ps1 | 24 +++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs index 36b7d3f1450..45778383652 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.Commands /// /// The implementation of the "New-TemporaryFile" cmdlet /// - [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=526726")] + [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-temporaryfile")] [OutputType(typeof(System.IO.FileInfo))] public class NewTemporaryFileCommand : Cmdlet { @@ -20,18 +20,18 @@ public class NewTemporaryFileCommand : Cmdlet protected override void EndProcessing() { string filePath = null; - string tempPath = System.Environment.GetEnvironmentVariable("TEMP"); + string tempPath = Path.GetTempPath(); if (ShouldProcess(tempPath)) { try { filePath = Path.GetTempFileName(); } - catch (Exception e) + catch (IOException ioException) { - WriteError( + ThrowTerminatingError( new ErrorRecord( - e, + ioException, "NewTemporaryFileWriteError", ErrorCategory.WriteError, tempPath)); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index 82a57e53126..c181a0379fb 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -14,20 +14,30 @@ A FileInfo object for the temporary file is returned. #> -Describe "NewTemporaryFile" -Tags "CI" { +Describe "New-TemporaryFile" -Tags "CI" { It "creates a new temporary file" { - $tempFile = New-TemporaryFile + try { + $tempFile = New-TemporaryFile - Test-Path $tempFile | Should be $true - $tempFile | Should BeOfType System.IO.FileInfo + $tempFile | Should Exist + $tempFile | Should BeOfType System.IO.FileInfo + } finally { + [System.IO.File]::Delete($tempFile) + } + } - if(Test-Path $tempFile) - { - Remove-Item $tempFile -ErrorAction SilentlyContinue -Force + It "throws terminating error when it fails to create new temporary file to Windows limit of 65535 files" { + + try { + $tempFiles = foreach ($i in (1..65536)) { New-TemporaryFile -ErrorAction Ignore } + { New-TemporaryFile } | Should Throw "The file exists" + } finally { + $tempFiles | ForEach-Object { [System.IO.File]::Delete($PSItem) } } } + It "with WhatIf does not create a file" { New-TemporaryFile -WhatIf | Should Be $null } From 11c5356876eea759cc3ecac68722b9331e74b3e4 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 18 Feb 2018 01:03:55 +0000 Subject: [PATCH 2/8] handle null in test cleanup --- .../NewTemporaryFile.Tests.ps1 | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index c181a0379fb..0aeb2ee1c71 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -33,16 +33,16 @@ Describe "New-TemporaryFile" -Tags "CI" { $tempFiles = foreach ($i in (1..65536)) { New-TemporaryFile -ErrorAction Ignore } { New-TemporaryFile } | Should Throw "The file exists" } finally { - $tempFiles | ForEach-Object { [System.IO.File]::Delete($PSItem) } + $tempFiles | ForEach-Object { if (Test-Path $PSItem) { [System.IO.File]::Delete($PSItem) } + } } - } - It "with WhatIf does not create a file" { - New-TemporaryFile -WhatIf | Should Be $null - } + It "with WhatIf does not create a file" { + New-TemporaryFile -WhatIf | Should Be $null + } - It "has an OutputType of System.IO.FileInfo" { - (Get-Command New-TemporaryFile).OutputType | Should Be "System.IO.FileInfo" + It "has an OutputType of System.IO.FileInfo" { + (Get-Command New-TemporaryFile).OutputType | Should Be "System.IO.FileInfo" + } } -} From b37e1aa05493587da26917cd56b00681d096c2ba Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 18 Feb 2018 11:57:27 +0000 Subject: [PATCH 3/8] fix help about help uri and remove rather pathetic test --- .../NewTemporaryFile.Tests.ps1 | 23 +++++-------------- .../engine/Help/assets/HelpURI/V3Cmdlets.csv | 2 +- 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index 0aeb2ee1c71..0151bd7d6c0 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -27,22 +27,11 @@ Describe "New-TemporaryFile" -Tags "CI" { } } - It "throws terminating error when it fails to create new temporary file to Windows limit of 65535 files" { - - try { - $tempFiles = foreach ($i in (1..65536)) { New-TemporaryFile -ErrorAction Ignore } - { New-TemporaryFile } | Should Throw "The file exists" - } finally { - $tempFiles | ForEach-Object { if (Test-Path $PSItem) { [System.IO.File]::Delete($PSItem) } - } - } - - - It "with WhatIf does not create a file" { - New-TemporaryFile -WhatIf | Should Be $null - } + It "with WhatIf does not create a file" { + New-TemporaryFile -WhatIf | Should Be $null + } - It "has an OutputType of System.IO.FileInfo" { - (Get-Command New-TemporaryFile).OutputType | Should Be "System.IO.FileInfo" - } + It "has an OutputType of System.IO.FileInfo" { + (Get-Command New-TemporaryFile).OutputType | Should Be "System.IO.FileInfo" } +} diff --git a/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv b/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv index 71f9f3e773b..3f1f1756e91 100644 --- a/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv +++ b/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv @@ -63,7 +63,7 @@ Enter-PSHostProcess,https://go.microsoft.com/fwlink/?LinkId=403736 Exit-PSHostProcess,https://go.microsoft.com/fwlink/?LinkId=403737 Clear-RecycleBin,https://go.microsoft.com/fwlink/?LinkId=524082 ConvertFrom-String,https://go.microsoft.com/fwlink/?LinkId=507579 -New-TemporaryFile,https://go.microsoft.com/fwlink/?LinkId=526726 +New-TemporaryFile,https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/new-temporaryfile New-Guid,https://go.microsoft.com/fwlink/?LinkId=526920 Format-Hex,https://go.microsoft.com/fwlink/?LinkId=526919 Convert-String,https://go.microsoft.com/fwlink/?LinkId=528577 From 0a3b51af6ea4d0d5e02b16a77e6d35c2222ddcce Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 18 Feb 2018 12:28:22 +0000 Subject: [PATCH 4/8] make help uri culture invariant --- .../commands/utility/NewTemporaryFileCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs index 45778383652..181921dcaa7 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.Commands /// /// The implementation of the "New-TemporaryFile" cmdlet /// - [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-temporaryfile")] + [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/new-temporaryfile")] [OutputType(typeof(System.IO.FileInfo))] public class NewTemporaryFileCommand : Cmdlet { From ff44545f68fcf8a7da30865c532099bd75715061 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 18 Feb 2018 18:06:09 +0000 Subject: [PATCH 5/8] revert updated help link and test syntax --- .../commands/utility/NewTemporaryFileCommand.cs | 2 +- .../Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 | 4 +++- test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs index 181921dcaa7..a271f185f85 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/NewTemporaryFileCommand.cs @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.Commands /// /// The implementation of the "New-TemporaryFile" cmdlet /// - [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/new-temporaryfile")] + [Cmdlet(VerbsCommon.New, "TemporaryFile", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=526726")] [OutputType(typeof(System.IO.FileInfo))] public class NewTemporaryFileCommand : Cmdlet { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index 0151bd7d6c0..78751587785 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -23,7 +23,9 @@ Describe "New-TemporaryFile" -Tags "CI" { $tempFile | Should Exist $tempFile | Should BeOfType System.IO.FileInfo } finally { - [System.IO.File]::Delete($tempFile) + if (Test-Path $tempFile) { + Remove-Item $tempFile -ErrorAction SilentlyContinue -Force + } } } diff --git a/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv b/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv index 3f1f1756e91..71f9f3e773b 100644 --- a/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv +++ b/test/powershell/engine/Help/assets/HelpURI/V3Cmdlets.csv @@ -63,7 +63,7 @@ Enter-PSHostProcess,https://go.microsoft.com/fwlink/?LinkId=403736 Exit-PSHostProcess,https://go.microsoft.com/fwlink/?LinkId=403737 Clear-RecycleBin,https://go.microsoft.com/fwlink/?LinkId=524082 ConvertFrom-String,https://go.microsoft.com/fwlink/?LinkId=507579 -New-TemporaryFile,https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/new-temporaryfile +New-TemporaryFile,https://go.microsoft.com/fwlink/?LinkId=526726 New-Guid,https://go.microsoft.com/fwlink/?LinkId=526920 Format-Hex,https://go.microsoft.com/fwlink/?LinkId=526919 Convert-String,https://go.microsoft.com/fwlink/?LinkId=528577 From 03ce79c2f9610f1705973abdf7e9dded368dcb0d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 21 Feb 2018 20:56:23 +0000 Subject: [PATCH 6/8] simplify test as discussed in PR. --- .../Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index 78751587785..ed7bf19fa8d 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -17,12 +17,11 @@ Describe "New-TemporaryFile" -Tags "CI" { It "creates a new temporary file" { - try { $tempFile = New-TemporaryFile $tempFile | Should Exist $tempFile | Should BeOfType System.IO.FileInfo - } finally { + if (Test-Path $tempFile) { Remove-Item $tempFile -ErrorAction SilentlyContinue -Force } From a5b265417cb9ea404342a8ee070a47de894fd37f Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 21 Feb 2018 20:57:57 +0000 Subject: [PATCH 7/8] fix indentation and brace --- .../NewTemporaryFile.Tests.ps1 | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index ed7bf19fa8d..33e2b97d1b9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -17,14 +17,13 @@ Describe "New-TemporaryFile" -Tags "CI" { It "creates a new temporary file" { - $tempFile = New-TemporaryFile + $tempFile = New-TemporaryFile - $tempFile | Should Exist - $tempFile | Should BeOfType System.IO.FileInfo + $tempFile | Should Exist + $tempFile | Should BeOfType System.IO.FileInfo - if (Test-Path $tempFile) { - Remove-Item $tempFile -ErrorAction SilentlyContinue -Force - } + if (Test-Path $tempFile) { + Remove-Item $tempFile -ErrorAction SilentlyContinue -Force } } From ec4b148f2b500d5e5330bd24189c8e95562ef787 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 22 Feb 2018 07:51:30 +0000 Subject: [PATCH 8/8] add additional assertion for base path to be temo oath --- .../Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 index 33e2b97d1b9..aa73e1ccfea 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/NewTemporaryFile.Tests.ps1 @@ -21,6 +21,7 @@ Describe "New-TemporaryFile" -Tags "CI" { $tempFile | Should Exist $tempFile | Should BeOfType System.IO.FileInfo + $tempFile | Should BeLikeExactly "$([System.IO.Path]::GetTempPath())*" if (Test-Path $tempFile) { Remove-Item $tempFile -ErrorAction SilentlyContinue -Force