Stucco icon indicating copy to clipboard operation
Stucco copied to clipboard

Pester tests failing due to missing help function

Open DelectableMechanisations opened this issue 4 years ago • 5 comments

Description After creating a new module and executing the build.ps1 script the pester tests are failing with the following error messages:

Running tests from 'C:\!_Projects\myproject\tests\Help.tests.ps1'
Describing Test help for <_.Name>
[-] Describe Test help for <_.Name> failed
  CommandNotFoundException: The term 'script:FilterOutCommonParams' is not recognized as a name of a cmdlet, function, script file, or executable program.
  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  at <ScriptBlock>, C:\!_Projects\myproject\tests\Help.tests.ps1:54

Describing Test help for <_.Name>
[-] Describe Test help for <_.Name> failed
  CommandNotFoundException: The term 'script:FilterOutCommonParams' is not recognized as a name of a cmdlet, function, script file, or executable program.
  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  at <ScriptBlock>, C:\!_Projects\myproject\tests\Help.tests.ps1:54

Describing Test help for <_.Name>
[-] Describe Test help for <_.Name> failed
  CommandNotFoundException: The term 'script:FilterOutCommonParams' is not recognized as a name of a cmdlet, function, script file, or executable program.
  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  at <ScriptBlock>, C:\!_Projects\myproject\tests\Help.tests.ps1:54

Current Behavior Individual function based Pester tests fail to run.

Expected behavior Individual function based Pester tests successfully run.

Possible Solution In ".\tests\Help.tests.ps1" I removed the "script" scope from all references to the "FilterOutCommonParams" function and moved this into its own external module file ".\tests\HelpFunctions.psm1". Then I removed the "FilterOutCommonParams" function from the "BeforeDiscovery" code block and replaced this with an Import-Module:

BeforeDiscovery {
    # Make sure HelpFunctions.psm1 is loaded - it contains FilterOutCommonParams
    Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'HelpFunctions.psm1') -Verbose:$false -Force

    $manifest             = Import-PowerShellDataFile -Path $env:BHPSModuleManifest
    $outputDir            = Join-Path -Path $env:BHProjectPath -ChildPath 'Output'
    $outputModDir         = Join-Path -Path $outputDir -ChildPath $env:BHProjectName
    $outputModVerDir      = Join-Path -Path $outputModDir -ChildPath $manifest.ModuleVersion
    $outputModVerManifest = Join-Path -Path $outputModVerDir -ChildPath "$($env:BHProjectName).psd1"
     ...

I tried experimenting with BeforeAll blocks and a few other things but this was the only solution I tried that work in both PowerShell 7 and VSCode.

To allow the Tasks to run from VSCode (Ctrl + P; task; Test) the .vscode\tasks.json file also needs the PowerShell executable path updating to "C:\Program Files\PowerShell\7\pwsh.exe" because PowerShell 5.1 fails at INIT with:

Exception: Method invocation failed because [System.IO.Path] does not contain a method named 'IsPathFullyQualified'

Environment

  • Module version used:
ModuleType Version    PreRelease Name
---------- -------    ---------- ----
Script     2.0.16                BuildHelpers
Script     0.6.1                 PowerShellBuild
Script     4.9.0                 psake
Script     1.19.1                PSScriptAnalyzer
  • Operating System and PowerShell version:
Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Decided to give Stucco a whirl today and ended up hitting this issue too running inside the devcontainer.

Build System Details:                                                                                                                                                           Build Module:       PowerShellBuild:0.6.1                                                                                                                                       PowerShell Version: 7.1.5                                                                                                                                                       

Environment variables:
BHPSModulePath      /workspaces/stuccotest/stuccotest
BHBuildOutput       /workspaces/stuccotest/Output/stuccotest/0.0.1
BHPSModuleManifest  /workspaces/stuccotest/stuccotest/stuccotest.psd1
BHBuildNumber       0
BHProjectName       stuccotest
BHBuildSystem       Unknown
BHProjectPath       /workspaces/stuccotest
BHModulePath        /workspaces/stuccotest/stuccotest
Task: CLEAN

Task: STAGEFILES

Task: GENERATEMARKDOWN

Task: GENERATEMAML

Task: BUILDHELP

Task: PESTER

Pester v5.3.1

Starting discovery in 3 files.
Discovery found 21 tests in 246ms.
Running tests.

Running tests from '/workspaces/stuccotest/tests/Help.tests.ps1'
Describing Test help for <_.Name>
[-] Describe Test help for <_.Name> failed
  CommandNotFoundException: The term 'script:FilterOutCommonParams' is not recognized as a name of a cmdlet, function, script file, or executable program.
  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  at <ScriptBlock>, /workspaces/stuccotest/tests/Help.tests.ps1:54

Running tests from '/workspaces/stuccotest/tests/Manifest.tests.ps1'
Describing Module manifest
 Context Validation
   [+] Has a valid manifest 30ms (17ms|13ms)
   [+] Has a valid name in the manifest 18ms (11ms|7ms)
   [+] Has a valid root module 35ms (17ms|17ms)
   [+] Has a valid version in the manifest 17ms (9ms|7ms)
   [+] Has a valid description 37ms (9ms|28ms)
   [+] Has a valid author 16ms (6ms|10ms)
   [+] Has a valid guid 20ms (13ms|7ms)
   [+] Has a valid copyright 15ms (8ms|7ms)
   [+] Has a valid version in the changelog 23ms (15ms|8ms)
   [+] Changelog and manifest versions are the same 31ms (24ms|7ms)

Describing Git tagging
  [!] Is tagged with a valid version 6ms (0ms|6ms)
  [!] Matches manifest version 5ms (0ms|5ms)

Running tests from '/workspaces/stuccotest/tests/Meta.tests.ps1'
Describing Text files formatting
 Context File encoding
   [+] No text file uses Unicode/UTF-16 encoding 16ms (9ms|7ms)
 Context Indentations
   [+] No text file use tabs for indentations 26ms (13ms|13ms)
Tests completed in 1.53s
Tests Passed: 12, Failed: 7, Skipped: 2 NotRun: 0
BeforeAll \ AfterAll failed: 1
  - Test help for <_.Name>
Task: ANALYZE

Task: TEST


psake succeeded executing ./psakeFile.ps1

Task: BUILD TIME REPORT
Name             Duration
----             --------
Init             00:00:00.076
Clean            00:00:00.087
StageFiles       00:00:00.301
GenerateMarkdown 00:00:00.475
GenerateMAML     00:00:00.408
BuildHelp        00:00:00.004
Build            00:00:00.000
Pester           00:00:02.024
Analyze          00:00:00.148
Test             00:00:00.005
Total:           00:00:03.642

brettmillerb avatar Oct 19 '21 13:10 brettmillerb

Can you check what version of Pester you have loaded?

HeyItsGilbert avatar Oct 20 '21 14:10 HeyItsGilbert

Can you check what version of Pester you have loaded?

Hey @HeyItsGilbert I'm using the devcontainer so I believe it installs the 5.1.1 version from the requirements file.

brettmillerb avatar Oct 20 '21 16:10 brettmillerb

@devblackops Should script:FilterOutCommonParams be global? I think that's what I had to do with mine to work: https://github.com/HeyItsGilbert/RPGDice/runs/3519100933?check_suite_focus=true

My bad. I must have missed that I changed that in mine. I should have caught it and submitted a PR.

HeyItsGilbert avatar Oct 20 '21 20:10 HeyItsGilbert

I get the same error, after starting a build of my new modul:

Running tests from 'C:\Users\*\Documents\git\CCM\tests\Help.tests.ps1' Describing Test help for <_.Name> [-] Describe Test help for <_.Name> failed CommandNotFoundException: The term 'script:FilterOutCommonParams' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at <ScriptBlock>, C:\Users\*\Documents\git\CCM\tests\Help.tests.ps1:54

Build System Details: Build Module: PowerShellBuild:0.6.1 PowerShell Version: 7.2.1 Pester v5.3.1

FLeven avatar Feb 11 '22 16:02 FLeven

@devblackops Should script:FilterOutCommonParams be global? I think that's what I had to do with mine to work: https://github.com/HeyItsGilbert/RPGDice/runs/3519100933?check_suite_focus=true

My bad. I must have missed that I changed that in mine. I should have caught it and submitted a PR.

This change fixed it for me. The merged code must not be in the current release though as I only installed Stucco and started my current module a couple of days ago and got this error.

stuartluscombe avatar Sep 13 '22 11:09 stuartluscombe

v0.5.0 has been released with this fix.

devblackops avatar Sep 16 '22 05:09 devblackops