Skip to content

Commit

Permalink
Merge pull request #533 from dahlbyk/rkeithhill/fix-pester-tests
Browse files Browse the repository at this point in the history
Fix tests on WinPS, PS Core Windows and Linux
  • Loading branch information
dahlbyk authored Jan 17, 2018
2 parents ddaaac6 + b9c49dd commit 6877d9d
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 155 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ branches:
- develop

stages:
- linux
- osx
- name: linux
- name: osx
if: type != pull_request OR head_branch = rkeithhill/fix-pester-tests

jobs:
include:
Expand All @@ -25,7 +26,6 @@ jobs:
- pwsh -c 'Set-PSRepository -Name PSGallery -InstallationPolicy Trusted'
- pwsh -c 'Install-Module Pester -Scope CurrentUser'
- stage: osx
if: branch = develop
os: osx
before_install:
- brew update
Expand All @@ -35,4 +35,4 @@ jobs:
- pwsh -c 'Install-Module Pester -Scope CurrentUser'

script:
- pwsh -c 'Import-Module Pester; Invoke-Pester -EnableExit'
- pwsh -c 'Import-Module Pester; Invoke-Pester -Script test -EnableExit'
144 changes: 122 additions & 22 deletions test/DefaultPrompt.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,47 +1,43 @@
. $PSScriptRoot\Shared.ps1

Describe 'Default Prompt Tests' {
Describe 'Default Prompt Tests - NO ANSI' {
BeforeAll {
$GitPromptSettings.AnsiConsole = $false
$prompt = Get-Item Function:\prompt
$OFS = ''
}
BeforeEach {
# Ensure these settings start out set to the default values
$GitPromptSettings.DefaultPromptPrefix = ''
$GitPromptSettings.DefaultPromptSuffix = '$(''>'' * ($nestedPromptLevel + 1)) '
$GitPromptSettings.DefaultPromptDebugSuffix = ' [DBG]$(''>'' * ($nestedPromptLevel + 1)) '
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $false
$GitPromptSettings.DefaultPromptEnableTiming = $false
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[GitPromptSettings]::new()})
$GitPromptSettings.AnsiConsole = $false
}

Context 'Prompt with no Git summary' {
It 'Returns the expected prompt string' {
Set-Location $env:windir -ErrorAction Stop
Set-Location $env:HOME -ErrorAction Stop
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "$env:windir> "
$res | Should BeExactly "${env:HOME}> "
}
It 'Returns the expected prompt string with changed DefaultPromptPrefix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix = 'PS '
$GitPromptSettings.DefaultPromptPrefix.Text = 'PS '
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "PS $Home> "
$res | Should BeExactly "PS ${Home}> "
}
It 'Returns the expected prompt string with expanded DefaultPromptPrefix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix = '[$(hostname)] '
$GitPromptSettings.DefaultPromptPrefix.Text = '[$(hostname)] '
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "[$(hostname)] $Home> "
}
It 'Returns the expected prompt string with changed DefaultPromptSuffix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptSuffix = '`n> '
$GitPromptSettings.DefaultPromptSuffix.Text = '`n> '
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "$Home`n> "
}
It 'Returns the expected prompt string with expanded DefaultPromptSuffix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptSuffix = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptSuffix.Text = ' - $(6*7)> '
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "$Home - 42> "
}
Expand All @@ -53,8 +49,8 @@ Describe 'Default Prompt Tests' {
}
It 'Returns the expected prompt string with prefix, suffix and abbrev home set' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix = '[$(hostname)] '
$GitPromptSettings.DefaultPromptSuffix = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptPrefix.Text = '[$(hostname)] '
$GitPromptSettings.DefaultPromptSuffix.Text = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "[$(hostname)] ~ - 42> "
Expand All @@ -74,23 +70,127 @@ Describe 'Default Prompt Tests' {
}

It 'Returns the expected prompt string with status' {
Mock git {
Mock -ModuleName posh-git -CommandName git {
$OFS = " "
if ($args -contains 'rev-parse') {
$res = Invoke-Expression "git.exe $args"
$res = Invoke-Expression "&$gitbin $args"
return $res
}
return @'
Convert-NativeLineEnding -SplitLines @'
## master
A test/Foo.Tests.ps1
D test/Bar.Tests.ps1
M test/Baz.Tests.ps1
'@ -split [System.Environment]::NewLine
} -ModuleName posh-git
'@
}

$res = [string](&$prompt 6>&1)
Assert-MockCalled git -ModuleName posh-git
Assert-MockCalled git -ModuleName posh-git -Scope It
$res | Should BeExactly "$PSScriptRoot [master +1 ~0 -0 | +0 ~1 -1 !]> "
}
}
}

Describe 'Default Prompt Tests - ANSI' {
BeforeAll {
$prompt = Get-Item Function:\prompt
$OFS = ''
}
BeforeEach {
# Ensure these settings start out set to the default values
$global:GitPromptSettings = & $module.NewBoundScriptBlock({[GitPromptSettings]::new()})
$GitPromptSettings.AnsiConsole = $true
}

Context 'Prompt with no Git summary' {
It 'Returns the expected prompt string' {
Set-Location $env:HOME -ErrorAction Stop
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "${env:HOME}> "
}
It 'Returns the expected prompt string with changed DefaultPromptSuffix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptSuffix.Text = '`n> '
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptSuffix.BackgroundColor = 0xFF6000 # Orange
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "$Home${csi}34m${csi}48;2;255;96;0m`n> ${csi}0m"
}
It 'Returns the expected prompt string with expanded DefaultPromptSuffix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptSuffix.Text = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptSuffix.BackgroundColor = 0xFF6000 # Orange
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "$Home${csi}34m${csi}48;2;255;96;0m - 42> ${csi}0m"
}
It 'Returns the expected prompt string with changed DefaultPromptPrefix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix.Text = 'PS '
$GitPromptSettings.DefaultPromptPrefix.BackgroundColor = [ConsoleColor]::White
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "${csi}107mPS ${csi}0m${Home}> "
}
It 'Returns the expected prompt string with expanded DefaultPromptPrefix' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix.Text = '[$(hostname)] '
$GitPromptSettings.DefaultPromptPrefix.BackgroundColor = 0xF5F5F5
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "${csi}48;2;245;245;245m[$(hostname)] ${csi}0m$Home> "
}
It 'Returns the expected prompt path colors' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$GitPromptSettings.DefaultPromptPath.ForegroundColor = [ConsoleColor]::DarkCyan
$GitPromptSettings.DefaultPromptPath.BackgroundColor = [ConsoleColor]::DarkRed
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "${csi}36m${csi}41m~${csi}0m> "
}
It 'Returns the expected prompt string with prefix, suffix and abbrev home set' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptPrefix.Text = '[$(hostname)] '
$GitPromptSettings.DefaultPromptPrefix.ForegroundColor = 0xF5F5F5
$GitPromptSettings.DefaultPromptSuffix.Text = ' - $(6*7)> '
$GitPromptSettings.DefaultPromptSuffix.ForegroundColor = [ConsoleColor]::DarkBlue
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$res = [string](&$prompt 6>&1)
$res | Should BeExactly "${csi}38;2;245;245;245m[$(hostname)] ${csi}0m~${csi}34m - 42> ${csi}0m"
}
It 'Returns the expected prompt string with prompt timing enabled' {
Set-Location $Home -ErrorAction Stop
$GitPromptSettings.DefaultPromptEnableTiming = $true
$GitPromptSettings.DefaultPromptTimingColor.ForegroundColor = [System.ConsoleColor]::Magenta
$res = [string](&$prompt 6>&1)
$escapedHome = [regex]::Escape($Home)
$rexcsi = [regex]::Escape($csi)
$res | Should Match "$escapedHome${rexcsi}95m${rexcsi}49m \d+ms${rexcsi}0m> "
}
}

Context 'Prompt with Git summary' {
BeforeAll {
Set-Location $PSScriptRoot
}

It 'Returns the expected prompt string with status' {
Mock -ModuleName posh-git git {
if ($args -contains 'rev-parse') {
$res = Invoke-Expression "&$gitbin $args"
return $res
}
Convert-NativeLineEnding -SplitLines @'
## master
A test/Foo.Tests.ps1
D test/Bar.Tests.ps1
M test/Baz.Tests.ps1
'@
}

$res = [string](&$prompt 6>&1)
Assert-MockCalled git -ModuleName posh-git
$res | Should BeExactly "$PSScriptRoot${csi}93m [${csi}0m${csi}96mmaster${csi}0m${csi}32m${csi}0m${csi}32m${csi}49m +1${csi}0m${csi}32m${csi}49m ~0${csi}0m${csi}32m${csi}49m -0${csi}0m${csi}93m |${csi}0m${csi}31m${csi}49m +0${csi}0m${csi}31m${csi}49m ~1${csi}0m${csi}31m${csi}49m -1${csi}0m${csi}31m !${csi}0m${csi}93m]${csi}0m> "
}
}
}
36 changes: 26 additions & 10 deletions test/Get-GitDirectory.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
. $PSScriptRoot\Shared.ps1

function GetMacOSAdjustedTempPath($Path) {
if (($PSVersionTable.PSVersion.Major -ge 6) -and $IsMacOS) {
# Mac OS's temp folder has a symlink in its path - /var is linked to /private/var
return "/private${Path}"
}

$Path
}

Describe 'Get-GitDiretory Tests' {
Context "Test normal repository" {
BeforeAll {
Expand All @@ -15,7 +24,7 @@ Describe 'Get-GitDiretory Tests' {
Get-GitDirectory | Should BeNullOrEmpty
}
It 'Returns $null for not a filesystem path' {
Set-Location Cert:\CurrentUser
Set-Location Alias:\
Get-GitDirectory | Should BeNullOrEmpty
}
It 'Returns correct path when in the root of repo' {
Expand All @@ -38,18 +47,23 @@ Describe 'Get-GitDiretory Tests' {
$repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())
$worktreePath = Join-Path $temp ([IO.Path]::GetRandomFileName())

git init $repoPath
&$gitbin init $repoPath
Set-Location $repoPath
'foo' > .\README.md
git add .\README.md

# Git rid of Git warnings about configuring user for the commit we do below
&$gitbin config user.email "[email protected]"
&$gitbin config user.name "Pester User"

'foo' > ./README.md
&$gitbin add ./README.md
# Quoting is a hack due to our use of the global:git function and how it converts args for invoke-expression
git commit -m "`"initial commit.`""
&$gitbin commit -m "`"initial commit.`""

if (Test-Path $worktreePath) {
Remove-Item $worktreePath -Recurse -Force
}
New-Item $worktreePath -ItemType Directory > $null
git worktree add -b test-worktree $worktreePath master 2>$null
&$gitbin worktree add -b test-worktree $worktreePath master 2>$null
}
AfterEach {
Set-Location $origPath
Expand All @@ -64,7 +78,8 @@ Describe 'Get-GitDiretory Tests' {
It 'Returns the correct dir when under a worktree' {
Set-Location $worktreePath
$worktreeBaseName = Split-Path $worktreePath -Leaf
Get-GitDirectory | Should BeExactly (MakeGitPath $repoPath\.git\worktrees\$worktreeBaseName)
$path = GetMacOSAdjustedTempPath $repoPath
Get-GitDirectory | Should BeExactly (MakeGitPath $path\.git\worktrees\$worktreeBaseName)
}
}

Expand All @@ -78,7 +93,7 @@ Describe 'Get-GitDiretory Tests' {
if (Test-Path $bareRepoPath) {
Remove-Item $bareRepoPath -Recurse -Force
}
git init --bare $bareRepoPath
&$gitbin init --bare $bareRepoPath
}
AfterAll {
Set-Location $origPath
Expand All @@ -93,7 +108,8 @@ Describe 'Get-GitDiretory Tests' {
}
It 'Returns correct path when under a child folder of the root of bare repo' {
Set-Location $bareRepoPath\hooks -ErrorVariable Stop
MakeNativePath (Get-GitDirectory) | Should BeExactly $bareRepoPath
$path = GetMacOSAdjustedTempPath $bareRepoPath
Get-GitDirectory | Should BeExactly (MakeNativePath $path)
}
}

Expand All @@ -102,7 +118,7 @@ Describe 'Get-GitDiretory Tests' {
Remove-Item Env:\GIT_DIR -ErrorAction SilentlyContinue
}
It 'Returns the value in GIT_DIR env var' {
$env:GIT_DIR = 'C:\xyzzy\posh-git\.git'
$env:GIT_DIR = MakeNativePath '/xyzzy/posh-git/.git'
Get-GitDirectory | Should BeExactly $env:GIT_DIR
}
}
Expand Down
Loading

0 comments on commit 6877d9d

Please sign in to comment.