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

✨Allow Pester Configuration to be specified either via setting or $PesterPreference in Debug #185

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions Scripts/PesterInterface.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ param(
#An optional custom path to the Pester module.
[String]$CustomModulePath,
#Include ANSI characters in output
[String]$IncludeAnsi
[Switch]$IncludeAnsi,
#Specify the path to a PesterConfiguration.psd1 file. The script will also look for a PesterConfiguration.psd1 in the current working directory.
[String]$ConfigurationPath
)

try {
Import-Module -Name Pester -MinimumVersion '5.2.0' -ErrorAction Stop
Import-Module -Name Pester -MinimumVersion '5.2.0' -ErrorAction Stop
} catch {
if ($PSItem.FullyQualifiedErrorId -ne 'Modules_ModuleWithVersionNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand') {throw}
if ($PSItem.FullyQualifiedErrorId -ne 'Modules_ModuleWithVersionNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand') { throw }

throw [NotSupportedException]'Pester 5.2.0 or greater is required to use the Pester Tests extension but was not found on your system. Please install the latest version of Pester from the PowerShell Gallery.'
}
Expand Down Expand Up @@ -150,13 +152,25 @@ function Invoke-Main {
[void]$paths.Add($PSItem)
}
}
$config = New-PesterConfiguration @{
Run = @{
SkipRun = [bool]$Discovery
PassThru = $true
}

[PesterConfiguration]$config = if ($PesterPreference) {
Write-Debug "$PesterPreference Detected, using for base configuration"
$pesterPreference
} elseif ($ConfigurationPath) {
Write-Debug "-ConfigurationPath $ConfigurationPath was specified, looking for configuration"
$resolvedConfigPath = Resolve-Path $ConfigurationPath -ErrorAction Stop
Write-Debug "Pester Configuration found at $ConfigurationPath, using as base configuration."
Import-PowerShellDataFile $resolvedConfigPath -ErrorAction Stop
} elseif (Test-Path './PesterConfiguration.psd1') {
Write-Debug "PesterConfiguration.psd1 found in test root directory $cwd, using as base configuration."
Import-PowerShellDataFile './PesterConfiguration.psd1' -ErrorAction Stop
} else {
New-PesterConfiguration
}

$config.Run.SkipRun = [bool]$Discovery
$config.Run.PassThru = $true

#If Verbosity is $null it will use PesterPreference
if ($Discovery) { $config.Output.Verbosity = 'None' }
elseif ($Verbosity) { $config.Output.Verbosity = $Verbosity }
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
"default": false,
"deprecationMessage": "DEPRECATED: This is now handled by the VSCode native continuous run interface in the test handler.",
"markdownDescription": "Automatically runs Pester test files upon changes being detected on save. Uncheck this box to disable this behavior."
},
"pester.configurationPath": {
"type": "string",
"markdownDescription": "Specifies the path to a custom Pester configuration in `.psd1` format. This is useful if you want to use a custom configuration file, or if you want to use a configuration file that is not named `PesterConfiguration.psd1`. If this is not set then the runner will look for a file named `PesterConfiguration.psd1` at the root of the workspace folder. This is best specified as a workspace setting and not a user setting. You may also provide a configuration via a `$PesterPreference` variable in your running session, this will apply for debug runs only."
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/pesterTestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export class PesterTestController implements Disposable {
} catch (err) {
if (err instanceof PowerShellError) {
const errMessage = 'Test Discovery failed: ' + err.message
window.showErrorMessage(errMessage)
window.showErrorMessage(errMessage, 'OK')
this.log.fatal(errMessage)
}
}
Expand Down Expand Up @@ -732,6 +732,12 @@ export class PesterTestController implements Disposable {
scriptArgs.push(pesterCustomModulePath)
}

const configurationPath = workspace.getConfiguration('pester').get<string>('configurationPath')
if (configurationPath !== undefined) {
scriptArgs.push('-ConfigurationPath')
scriptArgs.push(configurationPath)
}

// Initialize the PSIC if we are using it
if (usePSExtension) {
// HACK: Calling this function indirectly starts/waits for PS Extension to be available
Expand Down Expand Up @@ -801,7 +807,7 @@ export class PesterTestController implements Disposable {
psOutput.success.removeListener('data', returnHandler)
}).bind(this, testRun))
psOutput.error.on('data', err => {
window.showErrorMessage(`An error occured running Pester: ${err}`)
window.showErrorMessage(`An error occured running Pester: ${err}`, 'OK')
this.log.error(`PesterInterface Error: ${err}`)
if (testRun) {
this.testRunStatus.set(testRun, false)
Expand Down