-
-
Notifications
You must be signed in to change notification settings - Fork 814
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
Implement Add-PoshGitToProfile command #361
Merged
dahlbyk
merged 10 commits into
master
from
rkeithhill/modify-profile-on-interative-import
Jan 16, 2017
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
82cd049
WIP check user profile during interactive import.
rkeithhill f3d4973
Clean up comments and import choice script.
rkeithhill 2ac0b18
Change to utf8 and use Before/AfterEach.
rkeithhill bbabf70
Simplfy check of $MyInvocation.ScriptName.
rkeithhill 8eac200
Remove extra call to Split-Path $MyInvocation ...
rkeithhill ff4646a
Use Get-Module -List instead of PSModulePath.
rkeithhill 972d539
See if callstack check works better.
rkeithhill f51b3db
Remove modify profile from module import.
rkeithhill 539f714
Do not use Get-Module -List posh-git.
rkeithhill 48511d5
Merge branch 'master' into rkeithhill/modify-profile-on-interative-im…
rkeithhill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
param([switch]$WhatIf = $false) | ||
|
||
# Dot source for Get-FileEncoding | ||
$scriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have |
||
. $scriptRoot\Utils.ps1 | ||
|
||
if($PSVersionTable.PSVersion.Major -lt 2) { | ||
Write-Warning "posh-git requires PowerShell 2.0 or better; you have version $($Host.Version)." | ||
return | ||
|
@@ -20,22 +24,6 @@ if(!(. (Join-Path $installDir "CheckVersion.ps1"))) { | |
return | ||
} | ||
|
||
# Adapted from http://www.west-wind.com/Weblog/posts/197245.aspx | ||
function Get-FileEncoding($Path) { | ||
$bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4) | ||
|
||
if(!$bytes) { return 'utf8' } | ||
|
||
switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bytes[0],$bytes[1],$bytes[2],$bytes[3]) { | ||
'^efbbbf' { return 'utf8' } | ||
'^2b2f76' { return 'utf7' } | ||
'^fffe' { return 'unicode' } | ||
'^feff' { return 'bigendianunicode' } | ||
'^0000feff' { return 'utf32' } | ||
default { return 'ascii' } | ||
} | ||
} | ||
|
||
$profileLine = ". '$installDir\profile.example.ps1'" | ||
if(Select-String -Path $PROFILE -Pattern $profileLine -Quiet -SimpleMatch) { | ||
Write-Host "It seems posh-git is already installed..." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
. $PSScriptRoot\..\Utils.ps1 | ||
|
||
Describe 'Utils Function Tests' { | ||
Context 'Add-ImportModuleToProfile Tests' { | ||
BeforeAll { | ||
$newLine = [System.Environment]::NewLine | ||
} | ||
BeforeEach { | ||
$profilePath = [System.IO.Path]::GetTempFileName() | ||
} | ||
AfterEach { | ||
Remove-Item $profilePath -ErrorAction SilentlyContinue | ||
} | ||
It 'Creates profile file if it does not exist' { | ||
Remove-Item -LiteralPath $profilePath | ||
Test-Path -LiteralPath $profilePath | Should Be $false | ||
$scriptRoot = Split-Path $profilePath -Parent | ||
Add-ImportModuleToProfile $profilePath $scriptRoot | ||
Test-Path -LiteralPath $profilePath | Should Be $true | ||
Get-FileEncoding $profilePath | Should Be 'utf8' | ||
$content = Get-Content $profilePath | ||
$content.Count | Should Be 1 | ||
@($content)[0] | Should BeExactly "Import-Module '$scriptRoot\posh-git.psd1'" | ||
} | ||
It 'Modifies existing (Unicode) profile file correctly' { | ||
$profileContent = @' | ||
Import-Module PSCX | ||
|
||
New-Alias pscore C:\Users\Keith\GitHub\rkeithhill\PowerShell\src\powershell-win-core\bin\Debug\netcoreapp1.1\win10-x64\powershell.exe | ||
'@ | ||
Set-Content $profilePath -Value $profileContent -Encoding Unicode | ||
$scriptRoot = Split-Path $profilePath -Parent | ||
Add-ImportModuleToProfile $profilePath $scriptRoot | ||
Test-Path -LiteralPath $profilePath | Should Be $true | ||
Get-FileEncoding $profilePath | Should Be 'unicode' | ||
$content = Get-Content $profilePath | ||
$content.Count | Should Be 5 | ||
$profileContent += "${newLine}${newLine}Import-Module '$scriptRoot\posh-git.psd1'" | ||
$content -join $newLine | Should BeExactly $profileContent | ||
} | ||
} | ||
|
||
Context 'Test-PoshGitImportedInScript Tests' { | ||
BeforeEach { | ||
$profilePath = [System.IO.Path]::GetTempFileName() | ||
} | ||
AfterEach { | ||
Remove-Item $profilePath -ErrorAction SilentlyContinue | ||
} | ||
It 'Detects Import-Module posh-git in profile script' { | ||
$profileContent = "Import-Module posh-git" | ||
Set-Content $profilePath -Value $profileContent -Encoding Unicode | ||
Test-PoshGitImportedInScript $profilePath | Should Be $true | ||
} | ||
It 'Detects chocolatey installed line in profile script' { | ||
$profileContent = ". 'C:\tools\poshgit\dahlbyk-posh-git-18d600a\profile.example.ps1" | ||
Set-Content $profilePath -Value $profileContent -Encoding Unicode | ||
Test-PoshGitImportedInScript $profilePath | Should Be $true | ||
} | ||
It 'Returns false when profile script does not import posh-git' { | ||
$profileContent = "Import-Module Pscx`nImport-Module platyPS`nImport-Module Plaster" | ||
Set-Content $profilePath -Value $profileContent -Encoding Unicode | ||
Test-PoshGitImportedInScript $profilePath | Should Be $false | ||
} | ||
} | ||
|
||
Context 'Test-InModulePath Tests' { | ||
BeforeAll { | ||
$standardPSModulePath = "C:\Users\Keith\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\" | ||
} | ||
BeforeEach { | ||
$origPSModulePath = $env:PSModulePath | ||
} | ||
AfterEach { | ||
$env:PSModulePath = $origPSModulePath | ||
} | ||
It 'Works for install from PSGallery to current user modules location' { | ||
$env:PSModulePath = $standardPSModulePath | ||
$path = "C:\Users\Keith\Documents\WindowsPowerShell\Modules\posh-git\0.7.0" | ||
Test-InModulePath $path | Should Be $true | ||
} | ||
It 'Works for install from Chocolatey not in any modules path' { | ||
$env:PSModulePath = $standardPSModulePath | ||
$path = "C:\tools\posh-git\dahlbyk-posh-git-18d600a" | ||
Test-InModulePath $path | Should Be $false | ||
} | ||
It 'Works for running from posh-git Git repo and location not in modules path' { | ||
$env:PSModulePath = $standardPSModulePath | ||
$path = "C:\Users\Keith\GitHub\posh-git" | ||
Test-InModulePath $path | Should Be $false | ||
} | ||
It 'Returns false when PSModulePath is empty' { | ||
$env:PSModulePath = '' | ||
$path = "C:\Users\Keith\Documents\WindowsPowerShell\Modules\posh-git\0.7.0" | ||
Test-InModulePath $path | Should Be $false | ||
} | ||
It 'Returns false when PSModulePath is missing' { | ||
Remove-Item Env:\PSModulePath | ||
Test-Path Env:\PSModulePath | Should Be $false | ||
$path = "C:\Users\Keith\Documents\WindowsPowerShell\Modules\posh-git\0.7.0" | ||
Test-InModulePath $path | Should Be $false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
Invoke-Pester $PSScriptRoot\Utils.Tests.ps1 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead check
Get-Module posh-git -ListAvailable
to see if it finds this particular instance of the module? Or check that it will find any posh-git module and consider that good enough?I'm almost tempted to just skip straight to always hard-coding the full module path.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would avoid hard-coding the module path as a "bad practice" for PowerShell modules. I can switch to using Get-Module -ListAvailable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh, that makes the Pester test a little harder. Oh well, hopefully not too much harder. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK this is making the Pester tests a lot harder. For some reason, Mock Get-Module is not working. Will look at it more tomorrow.