Skip to content

Commit

Permalink
Added Get-OperatingSystem and Get-TempDirectory to fix TestDrive on P…
Browse files Browse the repository at this point in the history
…SCore
  • Loading branch information
adbertram authored and nohwnd committed Jan 12, 2017
1 parent d0d72d9 commit 770dec4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Functions/TestDrive.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Set-StrictMode -Version Latest

$tempPath = (Get-Item $env:temp).FullName
if ($PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows)
{
$tempPath = $env:TEMP
}
else
{
$tempPath = '/tmp'
}

Describe "Setup" {
It "returns a location that is in a temp area" {
Expand Down
3 changes: 2 additions & 1 deletion Functions/TestDrive.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ function Clear-TestDrive ([String[]]$Exclude) {
function New-RandomTempDirectory {
do
{
$Path = & $SafeCommands['Join-Path'] -Path $env:TEMP -ChildPath ([Guid]::NewGuid())
$tempPath = Get-TempDirectory
$Path = & $SafeCommands['Join-Path'] -Path $tempPath -ChildPath ([Guid]::NewGuid())
} until (-not (& $SafeCommands['Test-Path'] -Path $Path ))

& $SafeCommands['New-Item'] -ItemType Container -Path $Path
Expand Down
32 changes: 32 additions & 0 deletions Pester.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,36 @@ InModuleScope Pester {
}
}
}

describe 'Get-OperatingSystem' {

it 'returns Windows' {

Get-OperatingSystem | should be 'Windows'
}
}

describe 'Get-TempDirectory' {

it 'returns the correct temp directory for Windows' {
mock 'Get-OperatingSystem' {
'Windows'
}
Get-TempDirectory | should be $env:TEMP
}

it 'returns the correct temp directory for MacOS' {
mock 'Get-OperatingSystem' {
'MacOS'
}
Get-TempDirectory | should be '/tmp'
}

it 'returns the correct temp directory for Linux' {
mock 'Get-OperatingSystem' {
'Linux'
}
Get-TempDirectory | should be '/tmp'
}
}
}
35 changes: 35 additions & 0 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,41 @@ function Get-ScriptBlockScope
[scriptblock].GetProperty('SessionStateInternal', $flags).GetValue($ScriptBlock, $null)
}

function Get-OperatingSystem
{
[CmdletBinding()]
param()

## Prior to v6, PowerShell was solely Windows. In v6, the $IsWindows var was introduced.
if ($PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows)
{
'Windows'
}
elseif ($IsOSX)
{
'OSX'
}
elseif ($IsLinux)
{
'Linux'
}
}

function Get-TempDirectory
{
[CmdletBinding()]
param()

if ((Get-OperatingSystem) -eq 'Windows')
{
$env:TEMP
}
else
{
'/tmp'
}
}

function SafeGetCommand
{
<#
Expand Down

0 comments on commit 770dec4

Please sign in to comment.