From e7884deb18ead59582bb06fb42dce5c156b90daa Mon Sep 17 00:00:00 2001 From: krymtkts Date: Fri, 31 Jan 2025 20:35:23 +0900 Subject: [PATCH 1/3] Suppress error when accessing Linux-dependent properties. --- src/pocof/Data.fs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index beb8172..d24249c 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -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 From 5f7e22772d2e6b1a50ef4ab9e36625702d36f3ab Mon Sep 17 00:00:00 2001 From: krymtkts Date: Fri, 31 Jan 2025 20:54:04 +0900 Subject: [PATCH 2/3] Add test to verify no errors when accessing platform-dependent properties. --- tests/pocof.Tests.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/pocof.Tests.ps1 b/tests/pocof.Tests.ps1 index e65275e..c47e274 100644 --- a/tests/pocof.Tests.ps1 +++ b/tests/pocof.Tests.ps1 @@ -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 '*' + } + } + } + } From 3c68ebb6aab1e2b7be3087b3f19aa39f304e919d Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sat, 1 Feb 2025 11:33:33 +0900 Subject: [PATCH 3/3] Add a test case for coverage. --- src/pocof.Test/Data.fs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/pocof.Test/Data.fs b/src/pocof.Test/Data.fs index b3ec408..b7ad2cf 100644 --- a/src/pocof.Test/Data.fs +++ b/src/pocof.Test/Data.fs @@ -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 (_) = () + + [] + 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