From 9a0f14dec7bdeb5d07d6cdd42c4cf3156d4af530 Mon Sep 17 00:00:00 2001 From: "SU, Yanyu" <238259854+TohruSu@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:04:18 +0900 Subject: [PATCH] Fix New-PSFTempFile ignoring -FileName The path built for -FileName was written to the output instead of being assigned to $newPath, so the command returned two paths and only created and tracked the generated file. Fixes #747 --- .../functions/temp/New-PSFTempFile.ps1 | 2 +- .../functions/temp/New-PSFTempFile.Tests.ps1 | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 PSFramework/tests/functions/temp/New-PSFTempFile.Tests.ps1 diff --git a/PSFramework/functions/temp/New-PSFTempFile.ps1 b/PSFramework/functions/temp/New-PSFTempFile.ps1 index 8f0b6f21..d7d90644 100644 --- a/PSFramework/functions/temp/New-PSFTempFile.ps1 +++ b/PSFramework/functions/temp/New-PSFTempFile.ps1 @@ -57,7 +57,7 @@ } process { $newPath = Join-Path -Path $tempPath -ChildPath "PSF_Temp_$(New-Guid).$($Extension)" - if ($FileName) { Join-Path -Path $tempPath -ChildPath $FileName } + if ($FileName) { $newPath = Join-Path -Path $tempPath -ChildPath $FileName } try { $null = New-Item -Path $newPath -ItemType File -Force -ErrorAction Stop } catch { $PSCmdlet.ThrowTerminatingError($_) } diff --git a/PSFramework/tests/functions/temp/New-PSFTempFile.Tests.ps1 b/PSFramework/tests/functions/temp/New-PSFTempFile.Tests.ps1 new file mode 100644 index 00000000..8b0e06a3 --- /dev/null +++ b/PSFramework/tests/functions/temp/New-PSFTempFile.Tests.ps1 @@ -0,0 +1,26 @@ +Describe "New-PSFTempFile Unit Tests" -Tag "UnitTests" { + AfterEach { + Get-PSFTempItem -ModuleName PSFTests | Remove-PSFTempItem + } + + It "Should create and track the file specified by -FileName" { + $fileName = "PSF_Test_$(New-Guid).csv" + $expected = Join-Path -Path (Get-PSFPath -Name Temp) -ChildPath $fileName + + $result = New-PSFTempFile -ModuleName PSFTests -Name FileNameTest -FileName $fileName + + @($result).Count | Should -Be 1 + $result | Should -Be $expected + Test-Path -LiteralPath $expected | Should -BeTrue + (Get-PSFTempItem -ModuleName PSFTests -Name FileNameTest).Path | Should -Be $expected + } + + It "Should create and track a generated file without -FileName" { + $result = New-PSFTempFile -ModuleName PSFTests -Name GeneratedTest -Extension csv + + @($result).Count | Should -Be 1 + $result | Should -BeLike '*\PSF_Temp_*.csv' + Test-Path -LiteralPath $result | Should -BeTrue + (Get-PSFTempItem -ModuleName PSFTests -Name GeneratedTest).Path | Should -Be $result + } +} \ No newline at end of file