From a99fb531e65b1576cdeb42e9c50e5cb45f9b47ce Mon Sep 17 00:00:00 2001 From: Ilya Date: Mon, 27 Mar 2017 08:46:39 +0400 Subject: [PATCH] Add autoload for test modules (#3342) Related #3238 1. Add autoload for test modules 2. Move TestHostCS.psm1 to 'test\tools\Modules\' folder 3. Remove explicit load TestHostCS.psm1 from test files --- build.psm1 | 15 +++ .../Scripting/ParameterBinding.Tests.ps1 | 5 +- .../Out-Host.Tests.ps1 | 4 - .../GetCredential.Tests.ps1 | 3 - .../Read-Host.Tests.ps1 | 3 - .../Write-Host.Tests.ps1 | 2 - test/tools/Modules/TestHostCS/TestHostCS.psd1 | 113 ++++++++++++++++++ .../Modules/TestHostCS}/TestHostCS.psm1 | 0 8 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 test/tools/Modules/TestHostCS/TestHostCS.psd1 rename test/{powershell/Common => tools/Modules/TestHostCS}/TestHostCS.psm1 (100%) diff --git a/build.psm1 b/build.psm1 index 66dd339ab5..12de405835 100644 --- a/build.psm1 +++ b/build.psm1 @@ -19,8 +19,13 @@ try { catch { } } +# On Unix paths is separated by colon +# On Windows paths is separated by semicolon +$TestModulePathSeparator = ':' + if ($IsWindows) { + $TestModulePathSeparator = ';' $IsAdmin = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) # Can't use $env:HOME - not available on older systems (e.g. in AppVeyor) $nugetPackagesRoot = "${env:HOMEDRIVE}${env:HOMEPATH}\.nuget\packages" @@ -51,6 +56,12 @@ if ($IsLinux) { } } +# Autoload (in current session) temporary modules used in our tests +$TestModulePath = Join-Path $PSScriptRoot "test/tools/Modules" +if ( -not $env:PSModulePath.contains($TestModulePath) ) { + $env:PSModulePath = $TestModulePath+$TestModulePathSeparator+$($env:PSModulePath) +} + # # At the moment, we just support x64 builds. When we support x86 builds, this # check may need to verify the SDK for the specified architecture. @@ -753,9 +764,13 @@ function Start-PSPester { Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose Publish-PSTestTools + # All concatenated commands/arguments are suffixed with the delimiter (space) $Command = "" + # Autoload (in subprocess) temporary modules used in our tests + $Command += '$env:PSModulePath = '+"'$TestModulePath$TestModulePathSeparator'" + '+$($env:PSModulePath);' + # Windows needs the execution policy adjusted if ($IsWindows) { $Command += "Set-ExecutionPolicy -Scope Process Unrestricted; " diff --git a/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 b/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 index 50c4a27589..4f3d2dbf27 100644 --- a/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 +++ b/test/powershell/Language/Scripting/ParameterBinding.Tests.ps1 @@ -1,7 +1,4 @@ -Import-Module $PSScriptRoot\..\..\Common\Test.Helpers.psm1 -ErrorAction SilentlyContinue -Import-Module $PSScriptRoot\..\..\Common\TestHostCS.psm1 -ErrorAction SilentlyContinue - -Describe "Tests for parameter binding" -Tags "CI" { +Describe "Tests for parameter binding" -Tags "CI" { Context 'Test of Mandatory parameters' { BeforeAll { $f = "function get-foo { param([Parameter(mandatory=`$true)] `$a) `$a };" diff --git a/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 index 15dcff926b..4ca7a747df 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Core/Out-Host.Tests.ps1 @@ -1,7 +1,3 @@ -# this is sensitive to the location of this test and the common directory" -$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" -import-module $hostmodule -ErrorAction SilentlyContinue - Describe "Out-Host Tests" -tag CI { BeforeAll { $th = New-TestHost diff --git a/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 index bced89fcfb..69f1fd6097 100755 --- a/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Security/GetCredential.Tests.ps1 @@ -1,6 +1,3 @@ -$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" -import-module $hostmodule -ErrorAction SilentlyContinue - Describe "Get-Credential Test" -tag "CI" { BeforeAll { $th = New-TestHost diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 index 321ddeac7a..3defda9031 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Read-Host.Tests.ps1 @@ -1,6 +1,3 @@ -$hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" -import-module $hostmodule -ErrorAction SilentlyContinue - Describe "Read-Host Test" -tag "CI" { BeforeAll { $th = New-TestHost diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 index 1458d751b3..67e717f10b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Write-Host.Tests.ps1 @@ -48,8 +48,6 @@ Describe "Write-Host with wrong colors" -Tags "CI" { Describe "Write-Host with TestHostCS" -Tags "CI" { BeforeAll { - $hostmodule = Join-Path $PSScriptRoot "../../Common/TestHostCS.psm1" - import-module $hostmodule -ErrorAction SilentlyContinue $th = New-TestHost $rs = [runspacefactory]::Createrunspace($th) $rs.open() diff --git a/test/tools/Modules/TestHostCS/TestHostCS.psd1 b/test/tools/Modules/TestHostCS/TestHostCS.psd1 new file mode 100644 index 0000000000..0d9052e282 --- /dev/null +++ b/test/tools/Modules/TestHostCS/TestHostCS.psd1 @@ -0,0 +1,113 @@ +# +# Module manifest for module 'TestHostCS' +# + +@{ + +# Script module or binary module file associated with this manifest. +RootModule = 'TestHostCS.psm1' + +# Version number of this module. +ModuleVersion = '1.0' + +# ID used to uniquely identify this module +GUID = '40a19c05-d765-41a1-995e-98ca5f247ee1' + +# Company or vendor of this module +CompanyName = 'Microsoft Corporation' + +# Copyright statement for this module +Copyright = 'Copyright (C) Microsoft Corporation, All rights reserved.' + +# Description of the functionality provided by this module +Description = 'Simple console host for console IO tests.' + +# Minimum version of the Windows PowerShell engine required by this module +# PowerShellVersion = '' + +# Name of the Windows PowerShell host required by this module +# PowerShellHostName = '' + +# Minimum version of the Windows PowerShell host required by this module +# PowerShellHostVersion = '' + +# Minimum version of Microsoft .NET Framework required by this module +# DotNetFrameworkVersion = '' + +# Minimum version of the common language runtime (CLR) required by this module +# CLRVersion = '' + +# Processor architecture (None, X86, Amd64) required by this module +# ProcessorArchitecture = '' + +# Modules that must be imported into the global environment prior to importing this module +# RequiredModules = @() + +# Assemblies that must be loaded prior to importing this module +# RequiredAssemblies = @() + +# Script files (.ps1) that are run in the caller's environment prior to importing this module. +# ScriptsToProcess = @() + +# Type files (.ps1xml) to be loaded when importing this module +# TypesToProcess = @() + +# Format files (.ps1xml) to be loaded when importing this module +# FormatsToProcess = @() + +# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess +# NestedModules = @() + +# Functions to export from this module +FunctionsToExport = 'New-TestHost' + +# Cmdlets to export from this module +#CmdletsToExport = '*' + +# Variables to export from this module +#VariablesToExport = '*' + +# Aliases to export from this module +#AliasesToExport = '*' + +# DSC resources to export from this module +# DscResourcesToExport = @() + +# List of all modules packaged with this module +# ModuleList = @() + +# List of all files packaged with this module +# FileList = @() + +# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. +PrivateData = @{ + + PSData = @{ + + # Tags applied to this module. These help with module discovery in online galleries. + # Tags = @() + + # A URL to the license for this module. + # LicenseUri = '' + + # A URL to the main website for this project. + # ProjectUri = '' + + # A URL to an icon representing this module. + # IconUri = '' + + # ReleaseNotes of this module + # ReleaseNotes = '' + + } # End of PSData hashtable + +} # End of PrivateData hashtable + +# HelpInfo URI of this module +# HelpInfoURI = '' + +# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. +# DefaultCommandPrefix = '' + +} + diff --git a/test/powershell/Common/TestHostCS.psm1 b/test/tools/Modules/TestHostCS/TestHostCS.psm1 similarity index 100% rename from test/powershell/Common/TestHostCS.psm1 rename to test/tools/Modules/TestHostCS/TestHostCS.psm1