Skip to content

Commit

Permalink
OPA executable path default value inconsistent between invoke scuba w…
Browse files Browse the repository at this point in the history
…ith or without configfilepath (#894)

* Add defaults

* Back out unintended change

* Update test

* Rename test

* Update tests

* Fix module name in test

* Update function scope

* Update mock in test

* Rework ScubaDefault values
  • Loading branch information
crutchfield authored Feb 13, 2024
1 parent 67bf207 commit fe53215
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 3 deletions.
3 changes: 1 addition & 2 deletions PowerShell/ScubaGear/Modules/Orchestrator.psm1
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using module 'ScubaConfig\ScubaConfig.psm1'


function Invoke-SCuBA {
<#
.SYNOPSIS
Expand Down Expand Up @@ -128,7 +127,7 @@ function Invoke-SCuBA {
[Parameter(Mandatory = $false, ParameterSetName = 'Report')]
[ValidateScript({Test-Path -PathType Container $_})]
[string]
$OPAPath = (Join-Path -Path $env:USERPROFILE -ChildPath ".scubagear\Tools"),
$OPAPath = [ScubaConfig]::ScubaDefault('DefaultOPAPath'),

[Parameter(Mandatory = $false, ParameterSetName = 'Configuration')]
[Parameter(Mandatory = $false, ParameterSetName = 'Report')]
Expand Down
9 changes: 8 additions & 1 deletion PowerShell/ScubaGear/Modules/ScubaConfig/ScubaConfig.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class ScubaConfig {
#>
hidden static [ScubaConfig]$_Instance = [ScubaConfig]::new()
hidden static [Boolean]$_IsLoaded = $false
hidden static [hashtable]$ScubaDefaults = @{
DefaultOPAPath = (Join-Path -Path $env:USERPROFILE -ChildPath ".scubagear\Tools")
}

static [object]ScubaDefault ([string]$Name){
return [ScubaConfig]::ScubaDefaults[$Name]
}

[Boolean]LoadConfig([System.IO.FileInfo]$Path){
if (-Not (Test-Path -PathType Leaf $Path)){
Expand Down Expand Up @@ -59,7 +66,7 @@ class ScubaConfig {
}

if (-Not $this.Configuration.OPAPath){
$this.Configuration.OPAPath = (Join-Path -Path $PSScriptRoot -ChildPath "..\..\..\..")
$this.Configuration.OPAPath = [ScubaConfig]::ScubaDefault('DefaultOPAPath')
}

if (-Not $this.Configuration.LogIn){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ InModuleScope Orchestrator {
function Disconnect-SCuBATenant {}
Mock -ModuleName Orchestrator Disconnect-SCuBATenant {}

function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName Orchestrator Get-ScubaDefault {"."}

Mock -CommandName New-Item {}
Mock -CommandName Copy-Item {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ InModuleScope Orchestrator {
Mock -ModuleName Orchestrator Disconnect-SCuBATenant {
$script:TestSplat.Add('DisconnectOnExit', $DisconnectOnExit)
}
function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName Orchestrator Get-ScubaDefault {"."}
Mock -CommandName New-Item {}
Mock -CommandName Copy-Item {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ using module '..\..\..\..\Modules\ScubaConfig\ScubaConfig.psm1'

InModuleScope ScubaConfig {
Describe -tag "Utils" -name 'ScubaConfigJson' {
BeforeAll {
[ScubaConfig]::ResetInstance()
}
Context 'General case'{
It 'Get Instance without loading'{
$Config1 = [ScubaConfig]::GetInstance()
Expand All @@ -16,10 +19,14 @@ InModuleScope ScubaConfig {
}
context 'JSON Configuration' {
BeforeAll {
function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName ScubaConfig Get-ScubaDefault {"."}
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'ScubaConfigTestFile')]
$ScubaConfigTestFile = Join-Path -Path $PSScriptRoot -ChildPath config_test.json
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'Result')]
$Result = [ScubaConfig]::GetInstance().LoadConfig($ScubaConfigTestFile)
function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName ScubaConfig Get-ScubaDefault {"."}
}
It 'Load valid config file'{
$Result | Should -Be $true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ using module '..\..\..\..\Modules\ScubaConfig\ScubaConfig.psm1'

InModuleScope ScubaConfig {
Describe -tag "Utils" -name 'ScubaConfigMissingDefaults' {
BeforeAll {
[ScubaConfig]::ResetInstance()
}
Context 'General case'{
It 'Get Instance without loading'{
$Config1 = [ScubaConfig]::GetInstance()
Expand All @@ -16,6 +19,8 @@ InModuleScope ScubaConfig {
}
context 'JSON Configuration' {
BeforeAll {
function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName ScubaConfig Get-ScubaDefault {"."}
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'ScubaConfigTestFile')]
$ScubaConfigTestFile = Join-Path -Path $PSScriptRoot -ChildPath config_test.json
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'Result')]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ using module '..\..\..\..\Modules\ScubaConfig\ScubaConfig.psm1'
InModuleScope ScubaConfig {
Describe -tag "Utils" -name 'ScubaConfigLoadConfig' {
context 'Handling repeated LoadConfig invocations' {
BeforeAll {
function Get-ScubaDefault {throw 'this will be mocked'}
Mock -ModuleName ScubaConfig Get-ScubaDefault {"."}
}
It 'Load valid config file followed by another'{
$cfg = [ScubaConfig]::GetInstance()
# Load the first file and check the ProductNames value.
Expand Down

0 comments on commit fe53215

Please sign in to comment.