Skip to content

Commit

Permalink
Wrap tests in pester blocks (PowerShell#12700)
Browse files Browse the repository at this point in the history
# 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).
  • Loading branch information
xtqqczze committed May 23, 2020
1 parent 12e0adc commit 1f252f8
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 331 deletions.
96 changes: 48 additions & 48 deletions test/packaging/windows/msi.tests.ps1
@@ -1,57 +1,57 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

function Test-Elevated {
[CmdletBinding()]
[OutputType([bool])]
Param()

# if the current Powershell session was called with administrator privileges,
# the Administrator Group's well-known SID will show up in the Groups for the current identity.
# Note that the SID won't show up unless the process is elevated.
return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544")
}

function Invoke-Msiexec {
param(
[Parameter(ParameterSetName = 'Install', Mandatory)]
[Switch]$Install,

[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
[Switch]$Uninstall,

[Parameter(Mandatory)]
[ValidateScript({Test-Path -Path $_})]
[String]$MsiPath,

[Parameter(ParameterSetName = 'Install')]
[HashTable] $Properties

)
$action = "$($PSCmdlet.ParameterSetName)ing"
if ($Install.IsPresent) {
$switch = '/I'
} else {
$switch = '/x'
}

$additionalOptions = @()
if ($Properties) {
foreach ($key in $Properties.Keys) {
$additionalOptions += "$key=$($Properties.$key)"
Describe -Name "Windows MSI" -Fixture {
BeforeAll {
function Test-Elevated {
[CmdletBinding()]
[OutputType([bool])]
Param()

# if the current Powershell session was called with administrator privileges,
# the Administrator Group's well-known SID will show up in the Groups for the current identity.
# Note that the SID won't show up unless the process is elevated.
return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544")
}
}

$argumentList = "$switch $MsiPath /quiet /l*vx $msiLog $additionalOptions"
$msiExecProcess = Start-Process msiexec.exe -Wait -ArgumentList $argumentList -NoNewWindow -PassThru
if ($msiExecProcess.ExitCode -ne 0) {
$exitCode = $msiExecProcess.ExitCode
throw "$action MSI failed and returned error code $exitCode."
}
}
function Invoke-Msiexec {
param(
[Parameter(ParameterSetName = 'Install', Mandatory)]
[Switch]$Install,

[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
[Switch]$Uninstall,

[Parameter(Mandatory)]
[ValidateScript({Test-Path -Path $_})]
[String]$MsiPath,

[Parameter(ParameterSetName = 'Install')]
[HashTable] $Properties

)
$action = "$($PSCmdlet.ParameterSetName)ing"
if ($Install.IsPresent) {
$switch = '/I'
} else {
$switch = '/x'
}

$additionalOptions = @()
if ($Properties) {
foreach ($key in $Properties.Keys) {
$additionalOptions += "$key=$($Properties.$key)"
}
}

$argumentList = "$switch $MsiPath /quiet /l*vx $msiLog $additionalOptions"
$msiExecProcess = Start-Process msiexec.exe -Wait -ArgumentList $argumentList -NoNewWindow -PassThru
if ($msiExecProcess.ExitCode -ne 0) {
$exitCode = $msiExecProcess.ExitCode
throw "$action MSI failed and returned error code $exitCode."
}
}

Describe -Name "Windows MSI" -Fixture {
BeforeAll {
$msiX64Path = $env:PsMsiX64Path

# Get any existing powershell in the path
Expand Down
@@ -1,43 +1,42 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'NestedModules' -Tags "CI" {

function New-TestModule {
param(
[string]$Name,
[string]$Content,
[string[]]$NestedContents
)

New-Item -type directory -Force "TestDrive:\$Name" > $null
$manifestParams = @{
Path = "TestDrive:\$Name\$Name.psd1"
}
Describe 'NestedModules' -Tags "CI" {
BeforeAll {
function New-TestModule {
param(
[string]$Name,
[string]$Content,
[string[]]$NestedContents
)

New-Item -type directory -Force "TestDrive:\$Name" > $null
$manifestParams = @{
Path = "TestDrive:\$Name\$Name.psd1"
}

if ($Content) {
Set-Content -Path "${TestDrive}\$Name\$Name.psm1" -Value $Content
$manifestParams['RootModule'] = "$Name.psm1"
}
if ($Content) {
Set-Content -Path "${TestDrive}\$Name\$Name.psm1" -Value $Content
$manifestParams['RootModule'] = "$Name.psm1"
}

if ($NestedContents) {
$manifestParams['NestedModules'] = 1..$NestedContents.Count | ForEach-Object {
$null = New-Item -type directory TestDrive:\$Name\Nested$_
$null = Set-Content -Path "${TestDrive}\$Name\Nested$_\Nested$_.psm1" -Value $NestedContents[$_ - 1]
"Nested$_"
if ($NestedContents) {
$manifestParams['NestedModules'] = 1..$NestedContents.Count | ForEach-Object {
$null = New-Item -type directory TestDrive:\$Name\Nested$_
$null = Set-Content -Path "${TestDrive}\$Name\Nested$_\Nested$_.psm1" -Value $NestedContents[$_ - 1]
"Nested$_"
}
}
}

New-ModuleManifest @manifestParams
New-ModuleManifest @manifestParams

$resolvedTestDrivePath = Split-Path ((Get-ChildItem TestDrive:\)[0].FullName)
if (-not ($env:PSModulePath -like "*$resolvedTestDrivePath*")) {
$env:PSModulePath += "$([System.IO.Path]::PathSeparator)$resolvedTestDrivePath"
$resolvedTestDrivePath = Split-Path ((Get-ChildItem TestDrive:\)[0].FullName)
if (-not ($env:PSModulePath -like "*$resolvedTestDrivePath*")) {
$env:PSModulePath += "$([System.IO.Path]::PathSeparator)$resolvedTestDrivePath"
}
}
}

$originalPSModulePath = $env:PSModulePath

try {
$originalPSModulePath = $env:PSModulePath

# Create modules in TestDrive:\
New-TestModule -Name NoRoot -NestedContents @(
Expand All @@ -54,72 +53,74 @@ Describe 'NestedModules' -Tags "CI" {
'class A { [string] foo() { return "A"} }',
'class B { [string] foo() { return "B"} }'
) -Content 'class C { [string] foo() { return "C"} }'
}

AfterAll {
$env:PSModulePath = $originalPSModulePath
Get-Module @('ABC', 'NoRoot', 'WithRoot') | Remove-Module
}

It 'Get-Module is able to find types' {
$module = Get-Module NoRoot -ListAvailable
$module.GetExportedTypeDefinitions().Count | Should -Be 1

$module = Get-Module WithRoot -ListAvailable
$module.GetExportedTypeDefinitions().Count | Should -Be 1
It 'Get-Module is able to find types' {
$module = Get-Module NoRoot -ListAvailable
$module.GetExportedTypeDefinitions().Count | Should -Be 1

$module = Get-Module ABC -ListAvailable
$module.GetExportedTypeDefinitions().Count | Should -Be 3
}
$module = Get-Module WithRoot -ListAvailable
$module.GetExportedTypeDefinitions().Count | Should -Be 1

It 'Import-Module pick the right type' {
$module = Import-Module ABC -PassThru
$module.GetExportedTypeDefinitions().Count | Should -Be 3
$module = Import-Module ABC -PassThru -Force
$module.GetExportedTypeDefinitions().Count | Should -Be 3

$module = Import-Module NoRoot -PassThru
$module.GetExportedTypeDefinitions().Count | Should -Be 1
$module = Import-Module NoRoot -PassThru -Force
$module.GetExportedTypeDefinitions().Count | Should -Be 1
[scriptblock]::Create(@'
$module = Get-Module ABC -ListAvailable
$module.GetExportedTypeDefinitions().Count | Should -Be 3
}

It 'Import-Module pick the right type' {
$module = Import-Module ABC -PassThru
$module.GetExportedTypeDefinitions().Count | Should -Be 3
$module = Import-Module ABC -PassThru -Force
$module.GetExportedTypeDefinitions().Count | Should -Be 3

$module = Import-Module NoRoot -PassThru
$module.GetExportedTypeDefinitions().Count | Should -Be 1
$module = Import-Module NoRoot -PassThru -Force
$module.GetExportedTypeDefinitions().Count | Should -Be 1
[scriptblock]::Create(@'
using module NoRoot
[A]::new().foo()
'@
).Invoke() | Should -Be A2

$module = Import-Module WithRoot -PassThru
$module.GetExportedTypeDefinitions().Count | Should -Be 1
$module = Import-Module WithRoot -PassThru -Force
$module.GetExportedTypeDefinitions().Count | Should -Be 1
[scriptblock]::Create(@'
$module = Import-Module WithRoot -PassThru
$module.GetExportedTypeDefinitions().Count | Should -Be 1
$module = Import-Module WithRoot -PassThru -Force
$module.GetExportedTypeDefinitions().Count | Should -Be 1
[scriptblock]::Create(@'
using module WithRoot
[A]::new().foo()
'@
).Invoke() | Should -Be A0
}

Context 'execute type creation in the module context' {

# let's define types to make it more fun
class A { [string] foo() { return "local"} }
class B { [string] foo() { return "local"} }
class C { [string] foo() { return "local"} }

# We need to think about it: should it work or not.
# Currently, types are resolved in compile-time to the 'local' versions
# So at runtime we don't call the module versions.
It 'Can execute type creation in the module context with new()' -Pending {
& (Get-Module ABC) { [C]::new().foo() } | Should -Be C
& (Get-Module NoRoot) { [A]::new().foo() } | Should -Be A2
& (Get-Module WithRoot) { [A]::new().foo() } | Should -Be A0
& (Get-Module ABC) { [A]::new().foo() } | Should -Be A
}
}

It 'Can execute type creation in the module context with New-Object' {
& (Get-Module ABC) { (New-Object C).foo() } | Should -Be C
& (Get-Module NoRoot) { (New-Object A).foo() } | Should -Be A2
& (Get-Module WithRoot) { (New-Object A).foo() } | Should -Be A0
& (Get-Module ABC) { (New-Object A).foo() } | Should -Be A
}
Context 'execute type creation in the module context' {

# let's define types to make it more fun
class A { [string] foo() { return "local"} }
class B { [string] foo() { return "local"} }
class C { [string] foo() { return "local"} }

# We need to think about it: should it work or not.
# Currently, types are resolved in compile-time to the 'local' versions
# So at runtime we don't call the module versions.
It 'Can execute type creation in the module context with new()' -Pending {
& (Get-Module ABC) { [C]::new().foo() } | Should -Be C
& (Get-Module NoRoot) { [A]::new().foo() } | Should -Be A2
& (Get-Module WithRoot) { [A]::new().foo() } | Should -Be A0
& (Get-Module ABC) { [A]::new().foo() } | Should -Be A
}

} finally {
$env:PSModulePath = $originalPSModulePath
Get-Module @('ABC', 'NoRoot', 'WithRoot') | Remove-Module
It 'Can execute type creation in the module context with New-Object' {
& (Get-Module ABC) { (New-Object C).foo() } | Should -Be C
& (Get-Module NoRoot) { (New-Object A).foo() } | Should -Be A2
& (Get-Module WithRoot) { (New-Object A).foo() } | Should -Be A0
& (Get-Module ABC) { (New-Object A).foo() } | Should -Be A
}
}
}
@@ -1,12 +1,11 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
$here = Split-Path -Parent $MyInvocation.MyCommand.Path

Describe "ConvertFrom-Csv" -Tags "CI" {

BeforeAll {
$testObject = "a", "1"
$testcsv = Join-Path -Path (Join-Path -Path $here -ChildPath assets) -ChildPath TestCsv2.csv
$testcsv = Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath assets) -ChildPath TestCsv2.csv
$testName = "Zaphod BeebleBrox"
$testColumns = @"
a,b,c
Expand Down

0 comments on commit 1f252f8

Please sign in to comment.