Files
PowerShell-PowerShell/test/powershell/engine/COM/COM.Basic.Tests.ps1
T
xtqqczze 1f252f8bba Wrap tests in pester blocks (#12700)
# PR Summary

Wrap tests in pester blocks to prepare for pesterv5

## PR Context

<!-- Provide a little reasoning as to why this Pull Request helps and why you have opened it. -->

## PR Checklist

- [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
    - Use the present tense and imperative mood when describing your changes
- [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress).
    - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready.
- **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)**
    - [x] None
    - **OR**
    - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md)
        - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here -->
- **User-facing changes**
    - [x] Not Applicable
    - **OR**
    - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
        - [ ] Issue filed: <!-- Number/link of that issue here -->
- **Testing - New and feature**
    - [x] N/A or can only be tested interactively
    - **OR**
    - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting)
- **Tooling**
    - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted.
    - **OR**
    - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include:
        - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host).
        - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features.
        - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions).
        - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
2020-05-23 13:24:53 +00:00

117 lines
4.9 KiB
PowerShell

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Basic COM Tests' -Tags "CI" {
BeforeAll {
$defaultParamValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = ![System.Management.Automation.Platform]::IsWindowsDesktop
}
AfterAll {
$global:PSDefaultParameterValues = $defaultParamValues
}
BeforeAll {
$null = New-Item -Path $TESTDRIVE/file1 -ItemType File
$null = New-Item -Path $TESTDRIVE/file2 -ItemType File
$null = New-Item -Path $TESTDRIVE/file3 -ItemType File
}
It "Should enumerate files from a folder" {
$shell = New-Object -ComObject "Shell.Application"
$folder = $shell.Namespace("$TESTDRIVE")
$items = $folder.Items()
## $items is a collection of all items belong to the folder, and it should be enumerated.
$items.Count | Should -Be 3
$items | Measure-Object | ForEach-Object Count | Should -Be $items.Count
$names = $items | ForEach-Object { $_.Name }
$names -join "," | Should -Be "file1,file2,file3"
}
It "Should enumerate IEnumVariant interface object without exception" {
$shell = New-Object -ComObject "Shell.Application"
$folder = $shell.Namespace("$TESTDRIVE")
$items = $folder.Items()
## $enumVariant is an IEnumVariant interface of all items belong to the folder, and it should be enumerated.
$enumVariant = $items._NewEnum()
$items.Count | Should -Be 3
$enumVariant | Measure-Object | ForEach-Object Count | Should -Be $items.Count
}
It "Should enumerate drives" {
$fileSystem = New-Object -ComObject scripting.filesystemobject
$drives = $fileSystem.Drives
## $drives is a read-only collection of all available drives, and it should be enumerated.
$drives | Measure-Object | ForEach-Object Count | Should -Be $drives.Count
## $element should be the first drive from the enumeration. It shouldn't be the same as $drives,
## but it should be the same as '$drives.Item($element.DriveLetter)'
$element = $drives | Select-Object -First 1
[System.Object]::ReferenceEquals($element, $drives) | Should -BeFalse
$element | Should -Be $drives.Item($element.DriveLetter)
}
It "Should be able to enumerate 'IADsMembers' object" {
$group = [ADSI]"WinNT://./Users,Group"
$members = $group.Invoke('Members')
$names = $members | ForEach-Object { $_.GetType().InvokeMember('Name', 'GetProperty', $null, $_, $null) }
$names | Should -Contain 'INTERACTIVE'
}
It "ToString() should return method paramter names" {
$shell = New-Object -ComObject "Shell.Application"
$fullSignature = $shell.AddToRecent.ToString()
$fullSignature | Should -BeExactly "void AddToRecent (Variant varFile, string bstrCategory)"
}
Context 'GetMember/SetMember/InvokeMember binders should have more restricted rule for COM object' {
BeforeAll {
if ([System.Management.Automation.Platform]::IsWindowsDesktop) {
$null = New-Item -Path $TESTDRIVE/bar -ItemType Directory -Force
$shell = New-Object -ComObject "Shell.Application"
$folder = $shell.Namespace("$TESTDRIVE")
$item = $folder.Items().Item(0)
$item = [psobject]::AsPSObject($item)
## Create a PSObject that has an instance member 'Name' and a script method 'Windows'
$str = Add-Member -InputObject "abc" -MemberType NoteProperty -Name Name -Value "Hello" -PassThru
$str = Add-Member -InputObject $str -MemberType ScriptMethod -Name Windows -Value { "Windows" } -PassThru
}
}
It "GetMember binder should differentiate PSObject that wraps COM object from other PSObjects" {
## GetMember on the member name 'Name'.
$entry1 = ($item, "bar")
$entry2 = ($str, "Hello")
foreach ($pair in ($entry1, $entry2, $entry2, $entry1, $entry1, $entry2)) {
$pair[0].Name | Should -Be $pair[1]
}
}
It "SetMember binder should differentiate PSObject that wraps COM object from other PSObjects" {
## SetMember on the member name 'Name'
$entry1 = ($item, "foo")
$entry2 = ($str, "World")
foreach ($pair in ($entry1, $entry2)) {
$pair[0].Name = $pair[1]
$pair[0].Name | Should -Be $pair[1]
}
}
It "InvokeMember binder should differentiate PSObject that wraps COM object from other PSObjects" {
## InvokeMember on the member name 'Windows'
$shell | ForEach-Object { $_.Windows() } > $null
## '$str' is a PSObject that wraps a string, but with ScriptMethod 'Windows'
$str.Windows() | Should -Be "Windows"
}
}
}