-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add Move-PSKoanLibrary Function (#364)
* ✨ 📝 Add Move-PSKoanLibrary * 🐛 Fix issue in Set-PSKoanLocation * ✅ Add tests! * ✅ Fix unrelated test How was this not failing before??!??!?!!? * 📝 Update doc * ✅ Refactor error command tests
- Loading branch information
Showing
7 changed files
with
289 additions
and
55 deletions.
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
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,23 @@ | ||
function Move-PSKoanLibrary { | ||
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium', | ||
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Move-PSKoanLibrary.md')] | ||
[OutputType([void])] | ||
param( | ||
[Parameter(Mandatory, Position = 0, ValueFromPipeline)] | ||
[Alias('PSPath', 'Folder', 'Destination', 'TargetPath')] | ||
[string] | ||
$Path | ||
) | ||
process { | ||
if ($PSCmdlet.ShouldProcess($Path, 'Move existing koan files here')) { | ||
$OriginalPath = Get-PSKoanLocation | ||
|
||
Write-Verbose "Moving library files from '$OriginalPath' to '$Path'" | ||
Move-Item -Path $OriginalPath -Destination $Path -ErrorAction Stop -PassThru | ||
|
||
if ($?) { | ||
Set-PSKoanLocation -Path $Path | ||
} | ||
} | ||
} | ||
} |
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,63 +1,52 @@ | ||
using namespace System.Management.Automation | ||
|
||
class FolderTransformAttribute : ArgumentTransformationAttribute { | ||
[object] Transform([EngineIntrinsics]$engineIntrinsics, [object] $inputData) { | ||
switch ($inputData) { | ||
|
||
{ $_ -is [string] } { | ||
if (-not (Test-Path $_ -IsValid -PathType Container)) { | ||
throw [ArgumentTransformationMetadataException]::new( | ||
"Could not resolve path: $_", | ||
$_.Exception | ||
) | ||
} | ||
|
||
return $engineIntrinsics.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_) | ||
} | ||
|
||
{ $_ -is [System.IO.FileSystemInfo] } { | ||
|
||
if (-not (Test-Path -Path $_.FullName -PathType Container)) { | ||
throw [ArgumentTransformationMetadataException]::new( | ||
'Path could not be resolved to a valid container.' | ||
) | ||
} | ||
else { | ||
return $inputData.Fullname | ||
} | ||
|
||
} | ||
} | ||
|
||
throw [System.IO.FileNotFoundException]::new() | ||
} | ||
} | ||
|
||
function Set-PSKoanLocation { | ||
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium', | ||
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Set-PSKoanLocation.md')] | ||
[OutputType([void])] | ||
param( | ||
[Parameter(Mandatory, Position = 0)] | ||
[Alias('PSPath', 'Folder')] | ||
[FolderTransformAttribute()] | ||
[string] | ||
$Path, | ||
|
||
[Parameter()] | ||
[switch] | ||
$PassThru | ||
) | ||
begin { | ||
$resolvedPath = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path) | ||
|
||
if ($resolvedPath.Count -gt 1 -or [WildcardPattern]::ContainsWildcardCharacters($resolvedPath)) { | ||
$ErrorDetails = @{ | ||
ExceptionType = [System.Management.Automation.PSArgumentException] | ||
ExceptionMessage = 'Wildcarded paths are not supported.' | ||
ErrorId = 'InvalidPath' | ||
ErrorCategory = 'InvalidArgument' | ||
TargetObject = $Path | ||
} | ||
$PSCmdlet.ThrowTerminatingError((New-PSKoanErrorRecord @ErrorDetails)) | ||
} | ||
|
||
if (Test-Path $resolvedPath -PathType Leaf) { | ||
$ErrorDetails = @{ | ||
ExceptionType = [System.Management.Automation.PSArgumentException] | ||
ExceptionMessage = 'You cannot use a file path as the location for your PSKoans library.' | ||
ErrorId = 'InvalidPathType' | ||
ErrorCategory = 'InvalidArgument' | ||
TargetObject = $Path | ||
} | ||
$PSCmdlet.ThrowTerminatingError((New-PSKoanErrorRecord @ErrorDetails)) | ||
} | ||
} | ||
process { | ||
if ($PSCmdlet.ShouldProcess("Set PSKoans folder location to '$Path'")) { | ||
Set-PSKoanSetting -Name KoanLocation -Value $Path | ||
if ($PSCmdlet.ShouldProcess("Set PSKoans folder location to '$resolvedPath'")) { | ||
Set-PSKoanSetting -Name KoanLocation -Value $resolvedPath | ||
} | ||
else { | ||
Write-Warning "PSKoans folder location has not been changed." | ||
} | ||
|
||
if ($PassThru) { | ||
$Path | ||
$resolvedPath | ||
} | ||
} | ||
} |
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,82 @@ | ||
#Requires -Modules PSKoans | ||
|
||
Describe 'Move-PSKoanLibrary' { | ||
|
||
Context 'Unit Tests with Mocks' { | ||
|
||
BeforeAll { | ||
$OriginalPath = New-Item -Path 'TestDrive:/PSKoans' -ItemType Directory | | ||
Select-Object -ExpandProperty FullName | ||
$TestPath = New-Item -ItemType Directory -Path 'TestDrive:/TestPath' | Join-Path -ChildPath 'Koans' | ||
|
||
Mock Get-PSKoanLocation { $OriginalPath }.GetNewClosure() -ModuleName PSKoans | ||
Mock Set-PSKoanLocation -ParameterFilter { $Path -eq $TestPath } -ModuleName PSKoans | ||
Mock Move-Item -ParameterFilter { $Path -eq $OriginalPath } -MockWith { $Destination } -ModuleName PSKoans | ||
} | ||
|
||
It 'should output the new location' { | ||
Move-PSKoanLibrary -Path $TestPath | Should -BeExactly $TestPath | ||
} | ||
|
||
It 'should call Get-PSKoanLocation' { | ||
Assert-MockCalled Get-PSKoanLocation -ModuleName PSKoans | ||
} | ||
|
||
It 'should call Move-Item' { | ||
Assert-MockCalled Move-Item -ModuleName PSKoans | ||
} | ||
|
||
It 'should call Set-PSKoanLocation' { | ||
Assert-MockCalled Set-PSKoanLocation -ModuleName PSKoans | ||
} | ||
} | ||
|
||
Context 'Integration Tests' { | ||
|
||
BeforeAll { | ||
$OldLocation = Get-PSKoanLocation | ||
|
||
Set-PSKoanLocation -Path 'TestDrive:/PSKoans' | ||
Update-PSKoan -Confirm:$false | ||
|
||
$OriginalFileHashes = Get-PSKoanLocation | | ||
Get-ChildItem -Recurse -File | | ||
Get-FileHash | | ||
ForEach-Object { | ||
@{ | ||
File = ($_.Path -split [regex]::Escape([IO.Path]::DirectorySeparatorChar))[-2, -1] -join ':' | ||
Hash = $_.Hash | ||
} | ||
} | ||
|
||
$NewLocation = 'TestDrive:/NewLocation/PSKoans' | ||
New-Item -Path ($NewLocation | Split-Path -Parent) -ItemType Directory | ||
} | ||
|
||
AfterAll { | ||
Set-PSKoanLocation $OldLocation | ||
} | ||
|
||
It 'should move the folder to the new location' { | ||
Move-PSKoanLibrary -Path $NewLocation | ||
Test-Path $NewLocation -PathType Container | Should -BeTrue | ||
} | ||
|
||
It 'should update the KoanLocation' { | ||
Get-PSKoanLocation | Should -BeExactly (Get-Item -Path $NewLocation).FullName | ||
} | ||
|
||
It 'should copy <File> to the new location' -TestCases $OriginalFileHashes { | ||
param($File, $Hash) | ||
|
||
$FileName = ($File -split ':')[-1] | ||
|
||
$NewHash = Get-ChildItem -Path $NewLocation -Recurse -File -Filter "*$FileName*" | | ||
Get-FileHash | | ||
Select-Object -ExpandProperty Hash | ||
|
||
$NewHash | Should -BeExactly $Hash | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.