2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
2017-09-29 16:28:15 -07:00
|
|
|
|
|
|
|
|
Describe "Using assembly" -Tags "CI" {
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-28 08:48:23 +00:00
|
|
|
Push-Location $PSScriptRoot
|
2017-09-29 16:28:15 -07:00
|
|
|
$guid = [Guid]::NewGuid()
|
|
|
|
|
|
|
|
|
|
Add-Type -OutputAssembly $PSScriptRoot\UsingAssemblyTest$guid.dll -TypeDefinition @"
|
|
|
|
|
public class ABC {}
|
|
|
|
|
"@
|
|
|
|
|
|
|
|
|
|
It 'parse reports error on non-existing assembly by relative path' {
|
|
|
|
|
$err = $null
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly foo.dll", [ref]$null, [ref]$err)
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
$err.Count | Should -Be 1
|
|
|
|
|
$err[0].ErrorId | Should -Be ErrorLoadingAssembly
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'parse reports error on assembly with non-existing fully qualified name' {
|
|
|
|
|
$err = $null
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly 'System.Management.Automation, Version=99.0.0.0'", [ref]$null, [ref]$err)
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
$err.Count | Should -Be 1
|
|
|
|
|
$err[0].ErrorId | Should -Be ErrorLoadingAssembly
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'not allow UNC path' {
|
|
|
|
|
$err = $null
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly \\networkshare\foo.dll", [ref]$null, [ref]$err)
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
$err.Count | Should -Be 1
|
|
|
|
|
$err[0].ErrorId | Should -Be CannotLoadAssemblyFromUncPath
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'not allow http path' {
|
|
|
|
|
$err = $null
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly http://microsoft.com/foo.dll", [ref]$null, [ref]$err)
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
$err.Count | Should -Be 1
|
|
|
|
|
$err[0].ErrorId | Should -Be CannotLoadAssemblyWithUriSchema
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
|
2020-05-07 13:00:30 +01:00
|
|
|
It "parse does not load the assembly" -Pending {
|
2017-09-29 16:28:15 -07:00
|
|
|
$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
|
2017-09-29 16:28:15 -07:00
|
|
|
|
|
|
|
|
$err = $null
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly .\UsingAssemblyTest$guid.dll", [ref]$null, [ref]$err)
|
|
|
|
|
|
|
|
|
|
$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
|
|
|
|
|
$err.Count | Should -Be 0
|
2017-09-29 16:28:15 -07:00
|
|
|
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly '$PSScriptRoot\UsingAssemblyTest$guid.dll'", [ref]$null, [ref]$err)
|
|
|
|
|
|
|
|
|
|
$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
|
|
|
|
|
$err.Count | Should -Be 0
|
2017-09-29 16:28:15 -07:00
|
|
|
|
|
|
|
|
$ast = [System.Management.Automation.Language.Parser]::ParseInput("using assembly `"$PSScriptRoot\UsingAssemblyTest$guid.dll`"", [ref]$null, [ref]$err)
|
|
|
|
|
|
|
|
|
|
$assemblies = [Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeFalse
|
|
|
|
|
$err.Count | Should -Be 0
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It "reports runtime error about non-existing assembly with relative path" {
|
2018-05-17 14:42:04 -07:00
|
|
|
$e = { [scriptblock]::Create("using assembly .\NonExistingAssembly.dll") } | Should -Throw -ErrorId 'ParseException' -PassThru
|
|
|
|
|
$e.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should -Be 'ErrorLoadingAssembly'
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
#>
|
2020-05-07 13:00:30 +01:00
|
|
|
It "Assembly loaded at runtime" -Pending {
|
2020-01-05 12:17:41 +05:00
|
|
|
$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
|
2017-09-29 16:28:15 -07:00
|
|
|
using assembly .\UsingAssemblyTest$guid.dll
|
|
|
|
|
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
|
|
|
|
"@
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeTrue
|
2017-09-29 16:28:15 -07:00
|
|
|
|
2020-01-05 12:17:41 +05:00
|
|
|
$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
|
2017-09-29 16:28:15 -07:00
|
|
|
using assembly $PSScriptRoot\UsingAssemblyTest$guid.dll
|
|
|
|
|
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
|
|
|
|
"@
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "UsingAssemblyTest$guid" | Should -BeTrue
|
2017-09-29 16:28:15 -07:00
|
|
|
|
2020-01-05 12:17:41 +05:00
|
|
|
$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
|
2017-09-29 16:28:15 -07:00
|
|
|
using assembly System.Drawing
|
|
|
|
|
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
|
|
|
|
"@
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "System.Drawing" | Should -BeTrue
|
2017-09-29 16:28:15 -07:00
|
|
|
|
2020-01-05 12:17:41 +05:00
|
|
|
$assemblies = & "$PSHOME/pwsh" -noprofile -command @"
|
2017-09-29 16:28:15 -07:00
|
|
|
using assembly 'System.Drawing, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
|
|
|
|
|
[Appdomain]::CurrentDomain.GetAssemblies().GetName().Name
|
|
|
|
|
"@
|
2018-03-21 10:47:08 -07:00
|
|
|
$assemblies -contains "System.Drawing" | Should -BeTrue
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Remove-Item .\UsingAssemblyTest$guid.dll
|
2018-12-28 08:48:23 +00:00
|
|
|
Pop-Location
|
2017-09-29 16:28:15 -07:00
|
|
|
}
|
|
|
|
|
}
|