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
Original file line number Diff line number Diff line change
Expand Up @@ -2801,7 +2801,7 @@ private static Assembly LoadAssembly(string assemblyName, string scriptFileName)
{
if (!string.IsNullOrEmpty(scriptFileName) && !Path.IsPathRooted(assemblyFileName))
{
assemblyFileName = Path.GetDirectoryName(scriptFileName) + "\\" + assemblyFileName;
assemblyFileName = Path.Combine(Path.GetDirectoryName(scriptFileName), assemblyFileName);
}

if (File.Exists(assemblyFileName))
Expand Down
2 changes: 1 addition & 1 deletion src/System.Management.Automation/engine/parser/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5133,7 +5133,7 @@ private StringConstantExpressionAst ResolveUsingAssembly(StringConstantExpressio
workingDirectory = Path.GetDirectoryName(scriptFileName);
}

assemblyFileName = workingDirectory + @"\" + assemblyFileName;
assemblyFileName = Path.Combine(workingDirectory, assemblyFileName);
}
}
catch
Expand Down
74 changes: 34 additions & 40 deletions test/powershell/Language/Parser/UsingAssembly.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,64 +44,58 @@ public class ABC {}
$err[0].ErrorId | Should -Be CannotLoadAssemblyWithUriSchema
}

It "parse does not load the assembly" -Pending {
It "parse does not load the assembly '<Script>'" -TestCases @{ Script = "using assembly UsingAssemblyTest$guid.dll"; Expected = $false },
@{ Script = "using assembly '$(Join-Path -Path $PSScriptRoot -ChildPath UsingAssemblyTest$guid.dll)'"; Expected = $false },
@{ Script = "using assembly `"$(Join-Path -Path $PSScriptRoot -ChildPath UsingAssemblyTest$guid.dll)`""; Expected = $false } {
param ($script)
$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse

$err = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly .\UsingAssemblyTest$guid.dll", [ref]$null, [ref]$err)

$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
$err.Count | Should -Be 0

$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly '$PSScriptRoot\UsingAssemblyTest$guid.dll'", [ref]$null, [ref]$err)

$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
$err.Count | Should -Be 0

$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly `"$PSScriptRoot\UsingAssemblyTest$guid.dll`"", [ref]$null, [ref]$err)
$ast = [System.Management.Automation.Language.Parser]::ParseInput($script, [ref]$null, [ref]$err)

$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
$err.Count | Should -Be 0
}

It "reports runtime error about non-existing assembly with relative path" {
$e = { [scriptblock]::Create("using assembly .\NonExistingAssembly.dll") } | Should -Throw -ErrorId 'ParseException' -PassThru
$script = "using assembly {0}" -f (Join-Path "." "NonExistingAssembly.dll")
$e = { [scriptblock]::Create($script) } | Should -Throw -ErrorId 'ParseException' -PassThru
$e.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -Be 'ErrorLoadingAssembly'
}
#>
It "Assembly loaded at runtime" -Pending {
$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
using assembly .\UsingAssemblyTest$guid.dll
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
"@
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeTrue

$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
using assembly $PSScriptRoot\UsingAssemblyTest$guid.dll
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
"@
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeTrue

$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
using assembly System.Drawing
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
"@
$assemblies -contains "System.Drawing" | Should -BeTrue

$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
using assembly 'System.Drawing, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
"@
$assemblies -contains "System.Drawing" | Should -BeTrue
Context "Runtime loading of assemblies" {
BeforeAll {
copy-item "UsingAssemblyTest$guid.dll" $TestDrive
$assemblyPath = Join-Path $TestDrive "UsingAssemblyTest$guid.dll"

function InvokeScript ([string]$pathToScript) {
$result = & "$PSHOME/pwsh" -noprofile -file $pathToScript
return $result
}
}

It "Assembly loaded at runtime" {
$testFile = Join-Path $TestDrive "TestFile1.ps1"
$script = "using assembly UsingAssemblyTest$guid.dll`n[ABC].Assembly.Location"
Set-Content -Path $testFile -Value $script
$assembly = InvokeScript $testFile
$assembly | Should -Be $assemblyPath
}

It "Assembly loaded at runtime with fully qualified name" {
$script = "using assembly $assemblyPath`n[ABC].Assembly.Location"
$testFile = Join-Path $TestDrive "TestFile2.ps1"
Set-Content -Path $testFile -Value $script
$assembly = InvokeScript $testFile
$assembly | Should -Be $assemblyPath
}
}
}
finally
{
Remove-Item .\UsingAssemblyTest$guid.dll
Remove-Item -ErrorAction Ignore .\UsingAssemblyTest$guid.dll
Pop-Location
}
}