Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
20 changes: 14 additions & 6 deletions test/powershell/engine/Module/IsolatedModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
Describe "Isolated module scenario - load the whole module in custom ALC" -Tag 'CI' {
It "Loading 'IsolatedModule' should work as expected" {

Set-ItResult -Pending -Because "The test is failing as we cannot depend on Newtonsoft.Json v10.0.0 as it has security vulnerabilities."

## The 'IsolatedModule' module can be found at '<repo-root>\test\tools\Modules'.
## The module assemblies are created and deployed by '<repo-root>\test\tools\TestAlc'.
## The module defines its own custom ALC and has its module structure organized in a special way that allows the module to be loaded in that custom ALC.
Expand All @@ -14,9 +12,15 @@ Describe "Isolated module scenario - load the whole module in custom ALC" -Tag '
## │ Test.Isolated.Init.dll (contains the custom ALC and code to setup 'Resolving' handler)
## │
## └───Dependencies
## Newtonsoft.Json.dll (version 10.0.0.0 dependency)
## System.CommandLine.dll (version 2.0.0.0 dependency)
## Test.Isolated.Nested.dll (nested binary module)
## Test.Isolated.Root.dll (root binary module)

## The assembly 'System.CommandLine.dll' should not be loaded in the PowerShell default ALC by default.
[System.Runtime.Loader.AssemblyLoadContext]::Default.Assemblies |
Where-Object FullName -Like 'System.CommandLine,*' |
Should -Be $null

$module = Import-Module IsolatedModule -PassThru
$nestedCmd = Get-Command Test-NestedCommand
$rootCmd = Get-Command Test-RootCommand
Expand All @@ -30,10 +34,14 @@ Describe "Isolated module scenario - load the whole module in custom ALC" -Tag '
$context1.Name | Should -BeExactly "MyCustomALC"
$context1 | Should -Be $context2

## Test-NestedCommand depends on NewtonSoft.Json 10.0.0.0 while PowerShell depends on 13.0.0.0 or higher.
## The exact version of NewtonSoft.Json should be loaded to the custom ALC.
## Test-NestedCommand depends on 'System.CommandLine.dll' which is loaded in the module's ALC.
$foo = [Test.Isolated.Nested.Foo]::new("Hello", "World")
Test-NestedCommand -Param $foo | Should -BeExactly "Hello-World-Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"
Test-NestedCommand -Param $foo | Should -BeExactly "Hello-World-System.CommandLine, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

## The assembly 'System.CommandLine.dll' should still not be loaded in the PowerShell default ALC.
[System.Runtime.Loader.AssemblyLoadContext]::Default.Assemblies |
Where-Object FullName -Like 'System.CommandLine,*' |
Should -Be $null

## The type 'Test.Isolated.Root.Red' from the root module can be resolved and should be from the same load context.
$context3 = [System.Runtime.Loader.AssemblyLoadContext]::GetLoadContext($rootCmd.ImplementingType.Assembly)
Expand Down
4 changes: 2 additions & 2 deletions test/tools/TestAlc/nested/NestedCommand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.CommandLine;
using System.Management.Automation;
using Newtonsoft.Json;

namespace Test.Isolated.Nested
{
Expand All @@ -14,7 +14,7 @@ public class TestNestedCommand : PSCmdlet

protected override void ProcessRecord()
{
WriteObject($"{Param.Name}-{Param.Path}-{typeof(StringEscapeHandling).Assembly.FullName}");
WriteObject($"{Param.Name}-{Param.Path}-{typeof(RootCommand).Assembly.FullName}");
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/tools/TestAlc/nested/Test.Isolated.Nested.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
<Import Project="..\..\..\Test.Common.props" />

<PropertyGroup>
<!-- Suppress language folders -->
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>

<!-- Disable PDB generation -->
<DebugSymbols>false</DebugSymbols>
<DebugType>None</DebugType>

<!-- Disable deps.json generation -->
<GenerateDependencyFile>false</GenerateDependencyFile>
<NoWarn>NU1901;NU1902;NU1903;NU1904</NoWarn>

<!-- Deploy the produced assembly -->
<PublishDir>..\..\Modules\IsolatedModule\Dependencies</PublishDir>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

</Project>
Loading