-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π¨βπ» Improves Parameter Validation for
Set-ItemNixMode
- Loading branch information
1 parent
d509875
commit fb76541
Showing
3 changed files
with
143 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env pwsh | ||
$ErrorActionPreference = "Stop" | ||
Set-StrictMode -Version Latest | ||
|
||
|
||
<# | ||
.SYNOPSIS | ||
Converts a value to a strongly-typed [System.IO.UnixFileMode] enum value. | ||
.DESCRIPTION | ||
Converts a value to a strongly-typed [System.IO.UnixFileMode] enum value. | ||
.EXAMPLE | ||
ConvertTo-NixMode -FromOctal 755 | ||
#> | ||
function ConvertTo-NixMode() { | ||
[CmdletBinding()] | ||
[OutputType([System.IO.UnixFileMode])] | ||
param( | ||
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'FromOctal')] | ||
[ValidateNotNullOrEmpty()] | ||
[string] $FromOctal, | ||
|
||
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'FromDecimal')] | ||
[ValidateNotNullOrEmpty()] | ||
[int] $FromDecimal | ||
) | ||
Begin { | ||
function Factor-EnumValue() { | ||
[OutputType([System.IO.UnixFileMode])] | ||
param( | ||
[Parameter(Mandatory = $true)] | ||
[int] | ||
$Value | ||
) | ||
$value = [int]$Value | ||
$result = [System.IO.UnixFileMode]::None | ||
|
||
foreach ($enumValue in [System.Enum]::GetValues([System.IO.UnixFileMode])) { | ||
if (($value -band $enumValue) -eq $enumValue) { | ||
$result = $result -bor $enumValue | ||
$value = $value -bxor $enumValue | ||
} | ||
} | ||
|
||
if ($value -ne 0) { | ||
throw [System.ArgumentException]::new("Unable to interpret value '$FromDecimal' as a value of [System.IO.UnixFileMode].") | ||
} else { | ||
return $result | ||
} | ||
} | ||
|
||
if ($FromOctal) { | ||
$FromDecimal = [Convert]::ToInt32($FromOctal, 8) | ||
} | ||
} | ||
Process { | ||
return (Factor-EnumValue $FromDecimal) | ||
} | ||
} |
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,81 @@ | ||
#!/usr/bin/env pwsh | ||
#Requires -Modules "Pester" | ||
$ErrorActionPreference = "Stop" | ||
Set-StrictMode -Version Latest | ||
|
||
|
||
BeforeAll { | ||
$SutFile = Get-Item "$PSScriptRoot/../src/ConvertTo-NixMode.ps1" | ||
$SutName = $SutFile.BaseName | ||
} | ||
|
||
Describe "SUT file" { | ||
It "should exist" { | ||
$SutFile | Should -Exist | ||
} | ||
|
||
It "should be sourceable" { | ||
{ . $SutFile.FullName } | Should -Not -Throw | ||
} | ||
|
||
Context "when sourced" { | ||
BeforeEach { | ||
. $SutFile | ||
} | ||
|
||
Describe "function" { | ||
It "should be defined" { | ||
Get-Command -Name $SutName -CommandType Function | Should -Not -BeNullOrEmpty | ||
} | ||
|
||
Context "when invoked" { | ||
Context "with no arguments" { | ||
It "should throw" { | ||
{ & $SutName } | Should -Throw | ||
} | ||
} | ||
|
||
Context "via parameter" { | ||
# single integer matching a UnixFileMode value | ||
Context "single input is integer-equivalent [System.IO.UnixFileMode] value" { | ||
It "should return the value as-is" { | ||
$parameterValue = [int][Convert]::ToString([int][System.IO.UnixFileMode]::GroupExecute, 8) | ||
$expectedResult = [System.IO.UnixFileMode]::GroupExecute | ||
|
||
$actualResult = ConvertTo-NixMode -FromOctal $parameterValue | ||
|
||
$actualResult | Should -BeOfType [System.IO.UnixFileMode] | ||
$actualResult | Should -Be $expectedResult | ||
} | ||
} | ||
|
||
# single integer that should have been parsed as base8 | ||
Context "single input is integer wrongly parsed as base10 instead of base8" { | ||
It "should return the value converted" { | ||
$parameterValue = [int]700 | ||
$expectedResult = [System.IO.UnixFileMode]([Convert]::ToInt32('700', 8)) | ||
|
||
$actualResult = ConvertTo-NixMode -FromOctal $parameterValue | ||
|
||
$actualResult | Should -BeOfType [System.IO.UnixFileMode] | ||
$actualResult | Should -Be $expectedResult | ||
} | ||
} | ||
|
||
# single integer string matching a UnixFileMode value as-is | ||
Context "single input is an integer string matching a [System.IO.UnixFileMode] value as-is" { | ||
It "should return the value as-is" { | ||
$parameterValue = [Convert]::ToString([int][System.IO.UnixFileMode]::GroupExecute, 8) | ||
$expectedResult = [System.IO.UnixFileMode]::GroupExecute | ||
|
||
$actualResult = ConvertTo-NixMode -FromOctal $parameterValue | ||
|
||
$actualResult | Should -BeOfType [System.IO.UnixFileMode] | ||
$actualResult | Should -Be $expectedResult | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |