From e49f599b6fd7cceeaf60a365c0a8fc95db9efe15 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Thu, 23 Apr 2020 06:41:09 +0200 Subject: [PATCH] Adds the cmdlet Get-TemporaryFolder (#22) - Added the cmdlet Get-TemporaryFolder. This cmdlet comes from _SqlServerDsc. --- CHANGELOG.md | 4 +- README.md | 28 +++++++++ source/Public/Get-TemporaryFolder.ps1 | 18 ++++++ .../Unit/Public/Get-TemporaryFolder.Tests.ps1 | 57 +++++++++++++++++++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 source/Public/Get-TemporaryFolder.ps1 create mode 100644 tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 diff --git a/CHANGELOG.md b/CHANGELOG.md index f4ca1c3..0bf4b5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Added the cmdlet `Assert-BoundParameter`. _This cndlet comes from_ +- Added the cmdlet `Assert-BoundParameter`. _This cmdlet comes from_ _ComputerManagementDsc._ +- Added the cmdlet `Get-TemporaryFolder`. _This cmdlet comes from_ + _SqlServerDsc._ - Added GitHub templates in the repository to help contributors. ### Changed diff --git a/README.md b/README.md index 35dc3dd..91257a1 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,34 @@ e.g. `DSC_MyResource.psd1`, or suffixed with `strings`, e.g. Read more about localization in the section [Localization](https://dsccommunity.org/styleguidelines/localization/) in the DSC Community style guideline. +### `Get-TemporaryFolder` + +Returns the path of the current user's temporary folder. + +#### Syntax + +```plaintext +Get-TemporaryFolder [] +``` + +#### Outputs + +**`System.String`** + +#### Notes + +Examples of what the cmdlet returns: + +- Windows: C:\Users\username\AppData\Local\Temp\ +- macOS: /var/folders/6x/thq2xce46bc84lr66fih2p5h0000gn/T/ +- Linux: /tmp/ + +#### Example + +```powershell +Join-Path -Path (Get-TemporaryFolder) -ChildPath 'MyTempFile` +``` + ### `New-InvalidArgumentException` Creates and throws an invalid argument exception. diff --git a/source/Public/Get-TemporaryFolder.ps1 b/source/Public/Get-TemporaryFolder.ps1 new file mode 100644 index 0000000..f990447 --- /dev/null +++ b/source/Public/Get-TemporaryFolder.ps1 @@ -0,0 +1,18 @@ +<# + .SYNOPSIS + Returns the path of the current user's temporary folder. + + .NOTES + This is the same as doing the following + - Windows: $env:TEMP + - macOS: $env:TMPDIR + - Linux: /tmp/ +#> +function Get-TemporaryFolder +{ + [CmdletBinding()] + [OutputType([System.String])] + param () + + return [IO.Path]::GetTempPath() +} diff --git a/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 b/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 new file mode 100644 index 0000000..9ea57cf --- /dev/null +++ b/tests/Unit/Public/Get-TemporaryFolder.Tests.ps1 @@ -0,0 +1,57 @@ +$ProjectPath = "$PSScriptRoot\..\..\.." | Convert-Path +$ProjectName = ((Get-ChildItem -Path $ProjectPath\*\*.psd1).Where{ + ($_.Directory.Name -match 'source|src' -or $_.Directory.Name -eq $_.BaseName) -and + $(try { Test-ModuleManifest -Path $_.FullName -ErrorAction Stop } catch { $false } ) + }).BaseName + + +Import-Module $ProjectName + +InModuleScope $ProjectName { + Describe 'SqlServerDsc.Common\Get-TemporaryFolder' -Tag 'GetTemporaryFolder' { + Context 'When getting the current temporary path' { + BeforeAll { + $mockExpectedTempPath = [IO.Path]::GetTempPath() + } + + It 'Should return the expected temporary path' { + Get-TemporaryFolder | Should -BeExactly $mockExpectedTempPath + } + } + + switch ($true) + { + # Windows PowerShell or PowerShell 6+ on Windows + (-not (Test-Path -Path variable:IsWindows) -or $IsWindows) + { + <# + $env:TEMP used short filename, Get-Item expands it then we + need to add a backslash to the path. Because $env:TEMP + is the only one not ending the path with a backslash. + #> + $mockTemporaryPath = (Get-Item -Path $env:TEMP).FullName + '\' + } + + $IsMacOS + { + $mockTemporaryPath = $env:TMPDIR + } + + $IsLinux + { + $mockTemporaryPath = '/tmp/' + } + + Default + { + throw 'Cannot set the temporary path. Unknown operating system.' + } + } + + Context 'When comparing returned temporary folder to other method of getting temporary folder' { + It 'Should return the same temporary path' { + Get-TemporaryFolder | Should -BeExactly $mockTemporaryPath + } + } + } +}