Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pester_configuration replaces instead of updates the default configuration #80

Open
vexx32 opened this issue Aug 11, 2021 · 0 comments
Open
Labels

Comments

@vexx32
Copy link
Contributor

vexx32 commented Aug 11, 2021

👻 Brief Description

When providing a custom pester_configuration to the verifier, this almost entirely replaces the default configuration defined by the verifier.

Select values from the default hash are actually backfilled with PowerShell code if the provided configuration is missing these values, but not all values from the default hash are backfilled currently:

$pesterConfigHash = #{ps_hash(config[:pester_configuration])}
if (-not $pesterConfigHash.ContainsKey('run')) {
$pesterConfigHash['run'] = @{}
}
if (-not $pesterConfigHash.ContainsKey('TestResult')) {
$pesterConfigHash['TestResult'] = @{}
}
if (-not $pesterConfigHash.run.path) {
$pesterConfigHash['run']['path'] = $TestPath
}
if (-not $pesterConfigHash.TestResult.TestSuiteName) {
$pesterConfigHash['TestResult']['TestSuiteName'] = 'Pester - #{instance.to_str}'
}
if (-not $pesterConfigHash.TestResult.OutputPath) {
$pesterConfigHash['TestResult']['OutputPath'] = $OutputFilePath
}
$PesterConfig = New-PesterConfiguration -Hashtable $pesterConfigHash
$result = Invoke-Pester -Configuration $PesterConfig
}

Version

kitchen-pester v1.1.1

Scenario

I would like to be able to provide a partial pester_configuration where the remaining values are backfilled with the expected defaults instead of having to provide a complete pester_configuration when I only need/want to set one or two additional values differently to the current defaults.

Steps to Reproduce

  1. Create a kitchen environment with the following verifier configuration:
verifier:
  name: pester
  pester_install:
    MaximumVersion: 5.99.999
    MinimumVersion: 5.0.0
    Force: true
    SkipPublisherCheck: true
  pester_configuration:
    Output:
      Verbosity: Normal
  1. Have some basic valid Pester test files in a ./tests/ folder
  2. Run kitchen verify
  3. Look to see if you get test results returned to ./testresults/

Expected Result

Test results XML files are returned, respecting the default configuration.

Actual Result

No results file is returned. It seems the verifier doesn't set the pester_configuration.TestResults.Enabled value back to true if the caller is setting any values in pester_configuration, even if the values the caller is setting are unrelated to the TestResults configuration.

➕ Additional context

Looking at this SO post it seems like a better option here might be to define the actual default configuration value as { } and then keep the current default config as a predefined field in the verifier class.

When running the Pester tests, we should update our internal default hash values with the hash that test kitchen passes in via configuration rather than allowing user input to accidentally remove useful configuration.

I'm thinking, basically, we set:

      default_config :pester_configuration, { )

Then we have this hash which we can copy and update from the Test Kitchen configuration:

      def pester_configuration_defaults = {
        run: {
          path: ".",
          PassThru: true,
        },
        TestResult: {
          Enabled: true,
          OutputPath: "PesterTestResults.xml",
          TestSuiteName: "",
        },
        Output: {
          Verbosity: "Detailed",
        },
      }

And I think before passing configuration into the powershell code we should be able to do something like this:

class ::Hash
  def deep_merge(second)
    merger = proc { |_, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : Array === v1 && Array === v2 ? v1 | v2 : [:undefined, nil, :nil].include?(v2) ? v1 : v2 }
    merge(second.to_h, &merger)
  end
end

def final_configuration = pester_configuration_defaults.deep_merge(:pester_configuration)

This would remove the need to manually backfill values, at least for the Pester v5 runs. It might also probably allow us to somewhat simplify the code handling translation to pester v4.

/cc @gaelcolas @gep13 @AdmiringWorm

@vexx32 vexx32 added the bug label Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant