-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Implement -Extension parameter in New-TemporaryFile cmdlet #4612
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bae68f7
#4215 Add ability to create a temporary file with a specific extensio…
bergmeister dc781d9
#4215 Refactored code to cope with race condition whereby a file coul…
bergmeister c919fa5
#4215 Addressed suggestions in code review in PR #4612:
bergmeister 6e102da
Fixed trivial typo.
bergmeister 9f8c3fb
#4215 Add Pester test for invalid character handling for -Extension p…
bergmeister 61e5300
#4215 Add -GuidBasedName switch as proposed in PR #4612
bergmeister 27e2b5a
#4612 Revert introduction of -GuidBasedFileName switch as outcome of …
bergmeister 3af7fd3
Changed algorithm after discussion in PR 4612 to use call the origina…
bergmeister 2c3ae88
Call WriteDebug instead of WriteVerbose since this cmdlet is mainly u…
bergmeister 6c0cb5c
Implement suggestions of review in PR 4612 consisting mainly of:
bergmeister a1d6b2a
Enhance Unix specific test to pick out the line that is Unix specific
bergmeister c076c9b
Remove redundant check for defaultExtension since this is now already…
bergmeister cafd031
Make test not run on Mac since '.tmp' and '.TMP' would be the same wh…
bergmeister d43b02a
Minor improvements to address the reamaining comments:
bergmeister db3bd43
Merge branch 'master' of https://github.com/PowerShell/PowerShell int…
bergmeister 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
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
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 |
|---|---|---|
|
|
@@ -6,25 +6,77 @@ | |
| <# | ||
| Purpose: | ||
| Verify that New-TemporaryFile creates a temporary file. | ||
| It has an 'Extension' parameter to change the default extension '.tmp'. | ||
|
|
||
| Action: | ||
| Run New-TemporaryFile. | ||
|
|
||
| Expected Result: | ||
| A FileInfo object for the temporary file is returned. | ||
| A FileInfo object for the temporary file is returned and the temporary file gets created correctly. | ||
| #> | ||
|
|
||
| Describe "NewTemporaryFile" -Tags "CI" { | ||
|
|
||
| BeforeAll { | ||
| $defaultExtension = '.tmp' | ||
| } | ||
|
|
||
| AfterEach { | ||
| if ($null -ne $script:tempFile) | ||
| { | ||
| # tempFile variable needs script scope because it gets defined in It block | ||
| Remove-Item $script:tempFile -ErrorAction SilentlyContinue -Force | ||
| } | ||
| } | ||
|
|
||
| It "creates a new temporary file" { | ||
| $tempFile = New-TemporaryFile | ||
| $script:tempFile = New-TemporaryFile | ||
| $tempFile | Should Exist | ||
| $tempFile | Should BeOfType System.IO.FileInfo | ||
| $tempFile.Extension | Should be $defaultExtension | ||
| } | ||
|
|
||
| Test-Path $tempFile | Should be $true | ||
| It "creates a new temporary file with the Extension parameter being the default" { | ||
| $script:tempFile = New-TemporaryFile -Extension $defaultExtension | ||
| $tempFile | Should Exist | ||
| $tempFile.Extension | Should be $defaultExtension | ||
| } | ||
|
|
||
| It "creates a new temporary file with the Extension parameter being the default but different casing" { | ||
| $defaultExtensionWithUpperCasing = $defaultExtension.ToUpper() | ||
| $script:tempFile = New-TemporaryFile -Extension $defaultExtensionWithUpperCasing | ||
| $tempFile | Should Exist | ||
| # On Linux '.TMP' and '.tmp' would not be the same, therefore we need to check that the initial 'tmp' file got removed. | ||
| if ($IsLinux) | ||
| { | ||
| [System.IO.Path]::ChangeExtension($tempFile, $defaultExtension) | Should Not Exist | ||
| } | ||
| $tempFile.Extension | Should be $defaultExtensionWithUpperCasing | ||
| } | ||
|
|
||
| It "creates a new temporary file with a specific extension" -TestCases @( | ||
| @{ extension = 'csv' } | ||
| @{ extension = '.csv' } | ||
| @{ extension = '..csv' } | ||
| @{ extension = 'txt.csv' } | ||
| @{ extension = '.txt.csv' } | ||
| ) -Test { | ||
| Param ([string]$extension) | ||
|
|
||
| $script:tempFile = New-TemporaryFile -Extension $extension | ||
| $tempFile | Should Exist | ||
| # Because the internal algorithm does renaming it is worthwhile checking that the original file does not get left behind | ||
| [System.IO.Path]::ChangeExtension($tempFile, $defaultExtension) | Should Not Exist | ||
| $tempFile | Should BeOfType System.IO.FileInfo | ||
| $tempFile.FullName.EndsWith($extension) | Should be $true | ||
| $tempFile.Extension | Should be ".csv" | ||
| } | ||
|
|
||
| if(Test-Path $tempFile) | ||
| It "New-TemporaryItem with an an invalid character in the -Extension parameter should throw NewTemporaryInvalidArgument error" { | ||
| $invalidFileNameChars = [System.IO.Path]::GetInvalidFileNameChars() | ||
| foreach($invalidFileNameChar in $invalidFileNameChars) | ||
| { | ||
| Remove-Item $tempFile -ErrorAction SilentlyContinue -Force | ||
| { New-TemporaryFile -Extension $invalidFileNameChar -ErrorAction Stop } | ShouldBeErrorId "NewTemporaryInvalidArgument,Microsoft.PowerShell.Commands.NewTemporaryFileCommand" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't make sense to test for all invalid chars - enough of one - we should check only the error id. |
||
| } | ||
| } | ||
|
|
||
|
|
||
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.
Please add a comment section here that describes this public property.