diff --git a/build.ps1 b/build.ps1 index ff922bc21..e42e3dc72 100644 --- a/build.ps1 +++ b/build.ps1 @@ -206,7 +206,7 @@ function Copy-Content ($Content) { $content = @( , ("$PSScriptRoot/src/en-US/*.txt", "$PSScriptRoot/bin/en-US/") - , ("$PSScriptRoot/src/Pester.ps1", "$PSScriptRoot/bin/") + , ("$PSScriptRoot/src/Pester.ScriptScope.ps1", "$PSScriptRoot/bin/") , ("$PSScriptRoot/src/Pester.psd1", "$PSScriptRoot/bin/") , ("$PSScriptRoot/src/Pester.Format.ps1xml", "$PSScriptRoot/bin/") ) diff --git a/publish/filesToPublish.ps1 b/publish/filesToPublish.ps1 index ca9f45457..9f655c5cb 100644 --- a/publish/filesToPublish.ps1 +++ b/publish/filesToPublish.ps1 @@ -1,5 +1,5 @@ @( - 'Pester.ps1' + 'Pester.ScriptScope.ps1' 'Pester.psd1' 'Pester.psm1' 'Pester.Format.ps1xml' diff --git a/src/Pester.Runtime.ps1 b/src/Pester.Runtime.ps1 index 5efe09925..33e2ef701 100644 --- a/src/Pester.Runtime.ps1 +++ b/src/Pester.Runtime.ps1 @@ -2693,7 +2693,8 @@ function Invoke-InNewScriptScope ([ScriptBlock] $ScriptBlock, $SessionState) { # correct session state, and then invoke the file. We can also pass a script block tied # to the current module to invoke internal function in the newly pushed script scope. - $Path = "$PSScriptRoot/Pester.ps1" + # Invoked as a standalone script file (not Pester.ps1; see Pester.ScriptScope.ps1 for why). + $Path = "$PSScriptRoot/Pester.ScriptScope.ps1" $Data = @{ ScriptBlock = $ScriptBlock } $wrapper = { diff --git a/src/Pester.ScriptScope.ps1 b/src/Pester.ScriptScope.ps1 new file mode 100644 index 000000000..ccafcf768 --- /dev/null +++ b/src/Pester.ScriptScope.ps1 @@ -0,0 +1,10 @@ +# This file is invoked as a real script file by Invoke-InNewScriptScope to push a new +# script scope into the caller's session state (see src/Pester.Runtime.ps1). +# +# Do NOT rename it to Pester.ps1. PSResourceGet treats a '.ps1' file in the +# package (i.e. Pester.ps1) as a script and prints a spurious installation-path warning +# on Install-PSResource Pester. See https://github.com/pester/Pester/issues/2826. +param ($ScriptBlock) + +. $ScriptBlock + diff --git a/src/Pester.ps1 b/src/Pester.ps1 deleted file mode 100644 index b09620420..000000000 --- a/src/Pester.ps1 +++ /dev/null @@ -1,4 +0,0 @@ -param ($ScriptBlock) - -. $ScriptBlock - diff --git a/tst/Pester.Tests.ps1 b/tst/Pester.Tests.ps1 index 9d2493c22..a01ff5aa9 100644 --- a/tst/Pester.Tests.ps1 +++ b/tst/Pester.Tests.ps1 @@ -208,6 +208,24 @@ Describe 'Style rules' -Tag StyleRules { } } +Describe 'Module packaging' -Tag StyleRules { + # Issue #2826: PSResourceGet decides a package is a script by looking for a + # '.ps1' file in the package root (i.e. Pester.ps1). When that file is + # present it prints a spurious installation-path warning on Install-PSResource Pester. + # The script-scope helper is therefore shipped as Pester.ScriptScope.ps1 instead. + BeforeAll { + $pesterRoot = (Get-Module Pester).ModuleBase + } + + It 'does not ship a Pester.ps1 that PSResourceGet would treat as a script' { + (Join-Path $pesterRoot 'Pester.ps1') | Should -Not -Exist + } + + It 'ships the script-scope helper as Pester.ScriptScope.ps1' { + (Join-Path $pesterRoot 'Pester.ScriptScope.ps1') | Should -Exist + } +} + InPesterModuleScope { Describe 'Find-File' { BeforeAll {