Skip to content

Commit

Permalink
Merge pull request #318 from krymtkts:feature/fix-for-linux
Browse files Browse the repository at this point in the history
Suppress error when accessing Linux-dependent properties.
  • Loading branch information
krymtkts authored Feb 1, 2025
2 parents f9c9a52 + 3c68ebb commit 5c0c9a2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/pocof.Test/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ module LanguageExtension =
// NOTE: only for coverage.
None |> Option.dispose

module Entry =
open System.Management.Automation

type MockProperty() =
// NOTE: use a custom property inherited AdaptedProperty to call Value getter for PSObject.
inherit PSAdaptedProperty("Dummy", "dummy")

override __.Value
with get () = failwith "MockProperty.Value raises error."
and set (_) = ()

[<Fact>]
let ``shouldn't fail when accessing error-prone properties and should return None`` () =
// NOTE: only for coverage.
let a = PSObject.AsPSObject("a")
let p = MockProperty()
// NOTE: requires passing true to preValidated to skip the check for CannotAddPropertyOrMethod.
// https://github.com/PowerShell/PowerShell/blob/c505f4ba39111df8bd8a957f8632ff9697639f0b/src/System.Management.Automation/engine/MshMemberInfo.cs#L4598C29-L4598C30
a.Properties.Add(p, true)
a["Dummy"] |> shouldEqual None

module unwrap =
open System.Collections
open System.Management.Automation
Expand Down
8 changes: 7 additions & 1 deletion src/pocof/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ module LanguageExtension =
__.Properties[prop]
|> function
| null -> null
| p -> p.Value
| p ->
try
p.Value
with _ ->
// TODO: error occurs on Linux when accessing Linux-dependent properties. currently, suppress the error.
// Exception getting "Size": "There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: $this.UnixStat.Size"
null

module Data =
open System
Expand Down
9 changes: 9 additions & 0 deletions tests/pocof.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,13 @@ Describe 'pocof' {
}
}
}

if ($PSVersionTable.Platform -eq 'Unix') {
Context 'Select-Pocof cmdlet with Unix' {
It "shouldn't raise error when accessing platform-dependent properties." {
{ Get-ChildItem | Select-Pocof ':User a' -NonInteractive } | Should -Not -Throw '*'
}
}
}

}

0 comments on commit 5c0c9a2

Please sign in to comment.