psake / PowerShellBuild

Common build tasks for psake and Invoke-Build that build and test PowerShell modules
MIT License
133 stars 24 forks source link

Optionally import module prior to running Pester tests #46

Closed joeypiccola closed 4 years ago

joeypiccola commented 4 years ago

Description

This PR adds the property ImportModule to the test build property hash. This property is passed to Test-PSBuildPester so that Pester can optionally import the module from the specified OutDir. This feature can be optionally used by specifying $PSBPreference.Test.ImportModule = $true.

Related Issue

45

Motivation and Context

Optionally import module prior to running Pesters tests.

  1. Simplify .tests.ps1 files removing redundant code to import the project module.
  2. Ensure the module imported for testing is the one build in the OurDir.

How Has This Been Tested?

  1. Built module locally. Took contents of output directory and placed them in my module path under PowerShellBuild/0.5.0 directory.
  2. Created vanilla module via the Plaster Stucco template v0.2.0
  3. Modified psakeFile.ps1 file to use task Test -FromModule PowerShellBuild -Version '0.5.0'.
  4. run build.ps1 -bootstrap
  5. run build.ps1 -task pester

Types of changes

Checklist:

Fixes #45

pauby commented 4 years ago

I would disagree that this is a 'Bug Fix' and would suggest it's a potential 'Breaking Change'. For those that import the module anyway in their tests it's unlikely to affect them. However, for those who work on the assumption that the module is not imported during the tests, this is potentially a build breaker.

I personally would prefer to see this change being turned off by default and it can be turned on if needed.

Just my 2p worth.

joeypiccola commented 4 years ago

This latest commit is what I was thinking based on the discussion on Issue #45. The issue I see now is that this does not work if someone uses the PSBPreference.Build.ModuleOurDir. 🤔

pauby commented 4 years ago

@joeypiccola Is this because of the ModuleOutputManifest parameter? I'm struggling to understand why that's needed when it could simply be calculated (or can it not be)?

joeypiccola commented 4 years ago

@pauby You're right, it can be calculated. It took me a bit to understand what Initialize-PSBuild was doing and that it changes BHBuildOutput. You can now see I'm leveraging this calculated value in Test-PSBuildPester.

Edit: I just corrected my above statement as I still had a misunderstanding with what Initialize-PSBuild was doing. I think I got it now 🤦 .

joeypiccola commented 4 years ago

Are there any additional thoughts or questions on this PR? Thanks, everyone!

devblackops commented 4 years ago

@joeypiccola This looks good. Thanks!

I tested locally by adding a new Pester test to the TestModule module. When adding an InModuleScope test that validates the output of the private function, it works when setting $PSBPreference.Test.ImportModule to $true and fails when $false as expected.