Skip to content
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

Fix tests on WinPS, PS Core Windows and Linux #533

Merged
merged 13 commits into from
Jan 17, 2018
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)> '
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're assuming no colors, is there a downside to setting the property directly (meaning a new span is created from the string constructor) instead of setting Text?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, just a minor perf hit no one would likely notice.

$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' {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

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> "
}
}
}
8 changes: 4 additions & 4 deletions test/Get-GitDirectory.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,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 @@ -40,8 +40,8 @@ Describe 'Get-GitDiretory Tests' {

git init $repoPath
Set-Location $repoPath
'foo' > .\README.md
git add .\README.md
'foo' > ./README.md
git 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.`""

Expand Down Expand Up @@ -102,7 +102,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