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

🐛 Fixes Error "Multiple ambiguous overloads found for ToString" #36

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/ConvertTo-NixMode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env pwsh
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest


if ([Type]::GetType("System.IO.UnixFileMode")) {
<#
.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() {
[System.Runtime.Versioning.SupportedOSPlatform("net7.0")]
[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)
}
}
} else {
<#
.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() {
[System.Runtime.Versioning.SupportedOSPlatform("net7.0")]
[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 {
throw [System.PlatformNotSupportedException]::new()
}
Process {
}
}
}
30 changes: 26 additions & 4 deletions src/Set-ItemNixMode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,38 @@ Set-StrictMode -Version Latest

if ($IsLinux) {
function Set-ItemNixMode() {
[System.Runtime.Versioning.UnsupportedOSPlatform("windows")]
param(
[Parameter(Mandatory = $true)]
[string[]]
$Path,

[Parameter(Mandatory = $true)]
[System.IO.UnixFileMode]
$Mode
# [ValidateScript({ ConvertTo-NixMode -FromOctal $_ })]
[string] $ModeOctal
)
[string] $modeInOctal = [Convert]::ToString($Mode, 8)
Get-Item $Path | ForEach-Object { chmod $modeInOctal $_.FullName } | Out-Null
DynamicParam {
# Add the ValidateScript attribute to the ModeOctal parameter, but
# only if the UnixFileMode type is available.
if ([Type]::GetType("System.IO.UnixFileMode")) {
$Attribute = [System.Management.Automation.ValidateScriptAttribute]::new(
[ScriptBlock]::Create("ConvertTo-NixMode -FromOctal $_")
)
$RuntimeParameterDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$RuntimeParameterDictionary.Add(
"ModeOctal",
[System.Management.Automation.RuntimeDefinedParameter]::new(
"ModeOctal",
[string],
[System.Collections.ObjectModel.Collection[System.Attribute]]::new($Attribute)
)
)
return $RuntimeParameterDictionary
}
}
Process {
Get-Item $Path | ForEach-Object { chmod $ModeOctal $_.FullName } | Out-Null
}
}
} else {
function Set-ItemNixMode() {
Expand Down
85 changes: 85 additions & 0 deletions test/ConvertTo-NixMode.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/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" {
BeforeDiscovery {
$FileModeSettingNotSupported = $IsWindows -or ($null -eq [Type]::GetType("System.IO.UnixFileMode"))
}

It "should be defined" {
Get-Command -Name $SutName -CommandType Function | Should -Not -BeNullOrEmpty
}

Context "when invoked" -Skip:$FileModeSettingNotSupported {
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
}
}
}
}
}
}
}
90 changes: 90 additions & 0 deletions test/Set-ItemNixMode.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env pwsh
#Requires -Modules "Pester"
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest


BeforeAll {
$SutFile = Get-Item "$PSScriptRoot/../src/Set-ItemNixMode.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" {
BeforeDiscovery {
$FileModeSettingNotSupported = $IsWindows -or ($null -eq [Type]::GetType("System.IO.UnixFileMode"))
}

It "should be defined" {
Get-Command -Name $SutName -CommandType Function | Should -Not -BeNullOrEmpty
}

Context "when invoked" -Skip:$FileModeSettingNotSupported {
Context "with no arguments" {
It "should throw" {
{ & $SutName } | Should -Throw
}
}

Context "for an existing file" {
BeforeEach {
$targetFile = New-TemporaryFile
}

AfterEach {
Remove-Item $targetFile.FullName -Force
}

Context "ModeOctal is 700" {
It "should set the mode" {
$modeOctalParameter = '700'
[System.IO.UnixFileMode] $expectedMode = [System.IO.UnixFileMode]::OwnerRead -bor [System.IO.UnixFileMode]::OwnerWrite -bor [System.IO.UnixFileMode]::OwnerExecute

Set-ItemNixMode -Path $targetFile -ModeOctal $modeOctalParameter

$targetFile.Refresh()
$targetFile.Mode | Should -Be $expectedMode
}
}

Context "ModeOctal is 555" {
It "should set the mode" {
$modeOctalParameter = '555'
[System.IO.UnixFileMode] $expectedMode = [System.IO.UnixFileMode]::OwnerRead -bor [System.IO.UnixFileMode]::OwnerExecute -bor [System.IO.UnixFileMode]::GroupRead -bor [System.IO.UnixFileMode]::GroupExecute -bor [System.IO.UnixFileMode]::OthersRead -bor [System.IO.UnixFileMode]::OthersExecute

Set-ItemNixMode -Path $targetFile -ModeOctal $modeOctalParameter

$targetFile.Refresh()
$targetFile.Mode | Should -Be $expectedMode
}
}

Context "ModeOctal is 644" {
It "should set the mode" {
$modeOctalParameter = '644'
[System.IO.UnixFileMode] $expectedMode = [System.IO.UnixFileMode]::OwnerRead -bor [System.IO.UnixFileMode]::OwnerWrite -bor [System.IO.UnixFileMode]::GroupRead -bor [System.IO.UnixFileMode]::OthersRead

Set-ItemNixMode -Path $targetFile -ModeOctal $modeOctalParameter

$targetFile.Refresh()
$targetFile.Mode | Should -Be $expectedMode
}
}
}
}
}
}
}
Loading