WahlNetwork / vester

Easily validate and remediate your vSphere configuration
https://wahlnetwork.com
Apache License 2.0
146 stars 45 forks source link

Invoke-Pester, XMLOutputFile discards value of PassThru when both are passed. #187

Closed ghost closed 7 years ago

ghost commented 7 years ago

Expected Behavior

AS A user of Invoke-Vester I WANT parameters that produce the functionality they advertise SO THAT $Result = Invoke-Vester -XMLOutputFile $FilePath -Passthru is either supported or disallowed via parametersets.

Current Behavior

The current parameters do not utilize parametersets


# Optionally save Pester output in NUnitXML format to a specified path
# Specifying a path automatically triggers Pester in NUnitXML mode
[ValidateScript({Test-Path (Split-Path $_ -Parent)})]
[object]$XMLOutputFile,
    # Optionally returns the Pester result as an object containing the information about the whole test run, and each test
    # Defaults to false (disabled)
    [switch]$PassThru = $false

Allowing both to be used in the same execution.
At the same time, calling Pester looks like this:
```powershell
# Call Invoke-Pester based on the parameters supplied
            # Runs VesterTemplate.Tests.ps1, which constructs the .Vester.ps1 test files
            If ($XMLOutputFile) {
                Invoke-Pester -OutputFormat NUnitXml -OutputFile $XMLOutputFile -Script @{
                    Path = "$(Split-Path -Parent $PSScriptRoot)\Private\Template\VesterTemplate.Tests.ps1"
                    Parameters = @{
                        Cfg       = $cfg
                        TestFiles = $Test
                        Remediate = $Remediate
                    }
                } # Invoke-Pester
            } ElseIf ($PassThru) {
                Invoke-Pester -PassThru -Script @{
                    Path = "$(Split-Path -Parent $PSScriptRoot)\Private\Template\VesterTemplate.Tests.ps1"
                    Parameters = @{
                        Cfg       = $cfg
                        TestFiles = $Test
                        Remediate = $Remediate
                    }
                } # Invoke-Pester
            } Else {
                Invoke-Pester -Script @{
                    Path = "$(Split-Path -Parent $PSScriptRoot)\Private\Template\VesterTemplate.Tests.ps1"
                    Parameters = @{
                        Cfg       = $cfg
                        TestFiles = $Test
                        Remediate = $Remediate
                    }
                } # Invoke-Pester
            } #If XML

Meaning XMLOutput and Passthru are either or, not both.

Possible Solution

Populate Parameters variable once, then enter the if/else block to add additional attributes, finally splat the parameter variable into the Invoke-Pester call.

$Pester_Params = @{
              Path = "$(Split-Path -Parent $PSScriptRoot)\Private\Template\VesterTemplate.Tests.ps1"
                    Parameters = @{
                        Cfg       = $cfg
                        TestFiles = $Test
                        Remediate = $Remediate
                    }
                } #Pester_Params
If ($XMLOutputFile) {
     $Pester_Param.Parameters += @{
           OutputFormat = "NUnitXml"
           OutputFile = $XMLOutputFile
      }
}

Context

Pester provides support for calling both parameters in the same execution. It can be handy to export the results to xml and separately iterate through the PSObject to log any failures into the event log. I can then pass the xml through reportunit and upload to a "dashboard", simultaneously LogInsight can find read in the events and alert me that config drift exists.

brianbunke commented 7 years ago

Hello, and welcome!

I totally agree, this should be available today. We actually briefly discussed this at VMworld and did nothing about it, so I'm glad you posted!

I like the direction of the possible solution, but it'll need a little TLC. When calling Invoke-Pester, the parameters are nested like so:

OutputFile
OutputFormat
PassThru
Script
  Path
  Parameters
    Cfg
    TestFiles
    Remediate

I suspect that this will be solved with a double splat (Invoke-Pester @NormalParams @SpicyParams)...and possibly some $PSBoundParameters fun if we want to be extra fancy.

Would you like to tackle?

ghost commented 7 years ago

Hello and thank you!

I would like to tackle this issue. I put this together last night

I haven't had a chance to test it yet, but I think out will populate the parameters as desired. I'll get some testing in and if successful will get a PR submitted.

brianbunke commented 7 years ago

Also haven't tested yet, but looks pretty good at first glance.

Assigning issue.