Skip to content

Commit

Permalink
Merge pull request #130 from splatteredbits/bugfix/powershell-yaml-fa…
Browse files Browse the repository at this point in the history
…ils-import

Fixed: importing powershell-yaml fails if another version of YamlDotNet.dll is loaded
  • Loading branch information
gabriel-samfira authored Aug 23, 2024
2 parents 5dcdaa7 + f970a25 commit 6d3770c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
40 changes: 39 additions & 1 deletion Tests/powershell-yaml.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2016-2023 Cloudbase Solutions Srl
# Copyright 2016-2023 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
Expand Down Expand Up @@ -520,4 +520,42 @@ bools:
}
}
}

Describe 'StringQuotingEmitter' {
BeforeAll {
$oldYamlPkgUrl = 'https://www.nuget.org/api/v2/package/YamlDotNet/11.2.1'
$pkgPath = Join-Path -Path $TestDrive -ChildPath 'YamlDotNet-11.2.1.nupkg'
$oldYamlPkgDirPath = Join-Path -Path $TestDrive -ChildPath 'YamlDotNet-11.2.1'
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $oldYamlPkgUrl -UseBasicParsing -OutFile $pkgPath
New-Item -Path $oldYamlPkgDirPath -ItemType Directory
Add-Type -AssemblyName 'System.IO.Compression.FileSystem'
[IO.Compression.ZipFile]::ExtractToDirectory($pkgPath, $oldYamlPkgDirPath)
}

$targetFrameworks = @('net45', 'netstandard1.3')
if ($PSVersionTable['PSEdition'] -eq 'Core')
{
$targetFrameworks = @('netstandard1.3', 'netstandard2.1')
}

It 'can be compiled on import with <_>/YamlDotNet.dll loaded' -ForEach $targetFrameworks {
$targetFramework = $_
$yamlDotnetAssemblyPath =
Join-Path -Path $TestDrive -ChildPath "YamlDotNet-11.2.1\lib\${targetFramework}\YamlDotNet.dll" -Resolve
$modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\powershell-yaml.psd1' -Resolve

{
# Do this in the background because YamlDotNet.dll is already loaded in this session and the way we
# found to reproduce this issue is by loading YamlDotNet 11.2.1 then importing powershell-yaml.
Start-Job {
$yamlDotnetAssemblyPath = $using:yamlDotnetAssemblyPath
$modulePath = $using:modulePath

Add-Type -Path $yamlDotnetAssemblyPath
Import-Module $modulePath
} | Receive-Job -Wait -AutoRemoveJob -ErrorAction Stop
} | Should -Not -Throw
}
}
}
1 change: 1 addition & 0 deletions powershell-yaml.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ if (!([System.Management.Automation.PSTypeName]'StringQuotingEmitter').Type) {
$referenceList += [IO.Directory]::GetFiles([IO.Path]::Combine($PSHOME, 'ref'), 'netstandard.dll', [IO.SearchOption]::TopDirectoryOnly)
Add-Type -TypeDefinition $stringQuotingEmitterSource -ReferencedAssemblies $referenceList -Language CSharp -CompilerOptions "-nowarn:1701"
} else {
$referenceList += 'System.Runtime.dll'
Add-Type -TypeDefinition $stringQuotingEmitterSource -ReferencedAssemblies $referenceList -Language CSharp
}
}
Expand Down

0 comments on commit 6d3770c

Please sign in to comment.