gaelcolas / Sampler

Module template with build pipeline and examples, including DSC elements.
MIT License
171 stars 42 forks source link

Pester QA fails when ScriptsToProcess is defined #432

Open cohdjn opened 1 year ago

cohdjn commented 1 year ago

Problem description

When I add a script to ScriptsToProcess in the module .psd1 file, the QA tests fail to load and error out. If I remove the script from the .psd1, the QA tests work.

The script I have defined is an initialization script to load a class definition.

Verbose logs

Pester v5.4.1

Starting discovery in 7 files.
[-] Discovery in C:\PSCodeGallery\Modules\Prod\SNMP\tests\QA\module.tests.ps1 failed with:
System.Management.Automation.CommandNotFoundException: The term 'SNMPOutput SNMP' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   at System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
   at System.Management.Automation.CommandProcessorBase.Complete()
at <ScriptBlock>, C:\PSCodeGallery\Modules\Prod\SNMP\tests\QA\module.tests.ps1: line 84
at BeforeDiscovery, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 5858
at <ScriptBlock>, C:\PSCodeGallery\Modules\Prod\SNMP\tests\QA\module.tests.ps1: line 82
at <ScriptBlock>, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 3003
at Invoke-File, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 3012
at Invoke-BlockContainer, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 2928      
at Discover-Test, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 1454
at Invoke-Test, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 2438
at Invoke-Pester<End>, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Pester\5.4.1\Pester.psm1: line 5284
at <ScriptBlock>, C:\Users\redacted\Documents\WindowsPowerShell\Modules\Sampler\0.116.5\tasks\Invoke-Pester.pester.build.ps1: line 906
at *Task, C:\Users\redacted\Documents\WindowsPowerShell\Modules\InvokeBuild\5.10.4\Invoke-Build.ps1: line 590
at *Task, C:\Users\redacted\Documents\WindowsPowerShell\Modules\InvokeBuild\5.10.4\Invoke-Build.ps1: line 562
at *Task, C:\Users\redacted\Documents\WindowsPowerShell\Modules\InvokeBuild\5.10.4\Invoke-Build.ps1: line 562
at *Task, C:\Users\redacted\Documents\WindowsPowerShell\Modules\InvokeBuild\5.10.4\Invoke-Build.ps1: line 562
at <ScriptBlock><End>, C:\Users\redacted\Documents\WindowsPowerShell\Modules\InvokeBuild\5.10.4\Invoke-Build.ps1: line 748
at <ScriptBlock><Begin>, C:\PSCodeGallery\Modules\Prod\SNMP\build.ps1: line 507
at <ScriptBlock>, <No file>: line 1
Discovery found 57 tests in 1.65s.
Starting code coverage.
Code Coverage preparation finished after 432 ms.
Running tests.

Running tests from 'module.tests.ps1'
Describing Changelog Management
  [!] Changelog format compliant with keepachangelog format 52ms (0ms|52ms)
  [+] Changelog should have an Unreleased header 83ms (69ms|14ms)

Describing General module control
  [+] Should import without errors 171ms (165ms|5ms)
  [+] Should remove without error 6ms (4ms|1ms)
[-] module.tests.ps1 failed with:
CommandNotFoundException: The term 'SNMPOutput SNMP' is not recognized as the name of a cmdlet, function, script file, or operable 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:\PSCodeGallery\Modules\Prod\SNMP\tests\QA\module.tests.ps1:84

How to reproduce

I created a directory called Classes beneath the source directory and put my .ps1 there with the class definition only. I added my .ps1 to ScriptsToProcess and modified build.yamlas follows...

CopyPaths: - en-US - Classes - lib

Expected behavior

I expected the QA process to complete without throwing.

Current behavior

None of the QA Pester tests run. I have to remove the entry from ScriptsToProcess and run it by hand manually to have the class loaded for the tests to function.

Suggested solution

I don't have any sadly. I don't have time to troubleshoot.

Operating system the target node is running

OsName               : Microsoft Windows 10 Enterprise        
OsOperatingSystemSKU : EnterpriseEdition
OsArchitecture       : 64-bit
WindowsVersion       : 2009
WindowsBuildLabEx    : 19041.1.amd64fre.vb_release.191206-1406
OsLanguage           : en-US
OsMuiLanguages       : {en-US}

PowerShell version and build the target node is running

Name                           Value
----                           -----
PSVersion                      5.1.19041.2364
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.2364
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Module version used

ame    Version Path
----    ------- ----
Sampler 0.116.5 C:\Users\redacted\Documents\WindowsPowerShell\Modules\Sampler\0.116.5\Sampler.psd1
gaelcolas commented 1 year ago

Hi there @cohdjn ,

If you have a source folder like source/Classes and a ps1 defining a PowerShell class, it will be merged within the PSM1 (that's what ModuleBuilder does, and our associated tasks). Then the QA will probably look for classes named [fileName] and their corresponding tests... If that's a dll, the output folder should have the right Classes folder and content in the output folder.

Assuming it's not a PowerShell class, if you still have to have a scriptToProcess, I suggest, calling the folder 'source/scriptToProcess' and update the CopyFolders accordingly. That shouldn't trigger QA, and shouldn't try to merge the content.

johlju commented 1 year ago

@cohdjn did you solve this, and if so I'm curious how?