internal/Get-MyCorpConfig.ps1
|
<#
.SYNOPSIS Reads the MyCorp config from the provided path (typically the root of ./tests). .DESCRIPTION Rebranded and cleaned version of Get-MtMyCorpConfig. - Searches for mycorp-config.json in the given directory and up to 5 parent levels. - Applies overrides from ./custom/mycorp-config.json if present. - Builds TestSettingsHash for fast lookup. - Signature block removed. .NOTES Hybrid-renaming rule applied: * Public function renamed to Get-MyCorpConfig * Internal Mt helper calls remain intact .EXAMPLE $config = Get-MyCorpConfig -Path 'C:\Tests' #> function Get-MyCorpConfig { [CmdletBinding()] [OutputType([object])] param( [Parameter(Mandatory = $true)] $Path ) Write-Verbose "Getting MyCorp config from $Path" $ConfigFilePath = $null try { # If directory: search for mycorp-config.json (formerly mycorp-config.json) if (Test-Path $Path -PathType Container) { $ConfigFilePath = Join-Path -Path $Path -ChildPath 'mycorp-config.json' if (-not (Test-Path -Path $ConfigFilePath)) { Write-Verbose "Config not found directly in $Path. Checking ./tests/mycorp-config.json." $testsDir = Join-Path -Path $Path -ChildPath 'tests/mycorp-config.json' if (Test-Path -Path $testsDir) { $ConfigFilePath = $testsDir } else { Write-Verbose "Searching up to 5 parent levels for mycorp-config.json" for ($i = 1; $i -le 5; $i++) { if (Test-Path -Path $ConfigFilePath) { break } $parentDir = Split-Path -Path $Path -Parent if ($parentDir -eq $Path -or [string]::IsNullOrEmpty($parentDir)) { break } $Path = $parentDir $ConfigFilePath = Join-Path -Path $Path -ChildPath 'mycorp-config.json' } } } } elseif (Test-Path -Path $Path -PathType Leaf) { $ConfigFilePath = $Path } } catch { Write-Verbose "Error while locating config file: $_" } # If still not found, use default if (-not (Test-Path -Path $ConfigFilePath)) { Write-Verbose "No config found. Using default configuration file." $ConfigFilePath = Join-Path (Get-MtMyCorpTestFolderPath) -ChildPath 'mycorp-config.json' if (-not (Test-Path -Path $ConfigFilePath)) { Write-Warning "MyCorp config file not found at $ConfigFilePath. Provide a valid path." return $null } } # Load base config $config = Get-Content -Path $ConfigFilePath -Raw | ConvertFrom-Json # Build lookup hash Add-Member -InputObject $config -MemberType NoteProperty -Name 'TestSettingsHash' -Value @{} foreach ($testSetting in $config.TestSettings) { $config.TestSettingsHash[$testSetting.Id] = $testSetting } # Load custom config if present $customConfigPath = Join-Path -Path (Split-Path -Path $ConfigFilePath -Parent) -ChildPath 'custom' | Join-Path -ChildPath 'mycorp-config.json' if (Test-Path $customConfigPath) { Write-Verbose "Custom MyCorp config found at $customConfigPath. Applying overrides." $customConfig = Get-Content -Path $customConfigPath -Raw | ConvertFrom-Json foreach ($customSetting in $customConfig.TestSettings) { $mainSetting = $config.TestSettingsHash[$customSetting.Id] if ($mainSetting) { Write-Verbose "Applying override for TestSetting Id $($customSetting.Id)." $mainSetting.Severity = $customSetting.Severity } } } else { Write-Verbose "No custom MyCorp config found. Using base configuration only." } return $config } |