Skip to content

Commit 5fac9b0

Browse files
Refactor Get-TestConfig for safety and better readability (#9751)
1 parent 0e5006c commit 5fac9b0

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

private/testing/Get-TestConfig.ps1

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,78 @@ function Get-TestConfig {
22
param(
33
[string]$LocalConfigPath = "$script:PSModuleRoot/tests/constants.local.ps1"
44
)
5-
$config = [ordered]@{}
5+
6+
$config = [ordered]@{
7+
CommonParameters = [System.Management.Automation.PSCmdlet]::CommonParameters
8+
Defaults = [System.Management.Automation.DefaultParameterDictionary]@{
9+
# We want the tests as readable as possible so we want to set Confirm globally to $false.
10+
'*:Confirm' = $false
11+
# We use a global warning variable so that we can always test
12+
# that the command does not write a warning
13+
# or that the command does write the expected warning.
14+
'*:WarningVariable' = 'WarnVar'
15+
}
16+
# We want all the tests to only write to this location.
17+
# When testing a remote SQL Server instance this must be a network share
18+
# where both the SQL Server instance and the test script can write to.
19+
Temp = 'C:\Temp'
20+
}
621

722
if (Test-Path $LocalConfigPath) {
823
. $LocalConfigPath
924
} elseif ($env:CODESPACES -or ($env:TERM_PROGRAM -eq 'vscode' -and $env:REMOTE_CONTAINERS)) {
1025
$null = Set-DbatoolsInsecureConnection
26+
1127
$config['Instance1'] = "dbatools1"
1228
$config['Instance2'] = "dbatools2"
1329
$config['Instance3'] = "dbatools3"
1430
$config['Instances'] = @($config['Instance1'], $config['Instance2'])
1531

1632
$config['SqlCred'] = [PSCredential]::new('sa', (ConvertTo-SecureString $env:SA_PASSWORD -AsPlainText -Force))
17-
$config['Defaults'] = [System.Management.Automation.DefaultParameterDictionary]@{
18-
"*:SqlCredential" = $config['SqlCred']
19-
"*:SourceSqlCredential" = $config['SqlCred']
20-
"*:DestinationSqlCredential" = $config['SqlCred']
21-
}
33+
$config['Defaults']['*:SqlCredential'] = $config['SqlCred']
34+
$config['Defaults']['*:SourceSqlCredential'] = $config['SqlCred']
35+
$config['Defaults']['*:DestinationSqlCredential'] = $config['SqlCred']
2236
} elseif ($env:GITHUB_WORKSPACE) {
2337
$config['DbaToolsCi_Computer'] = "localhost"
38+
2439
$config['Instance1'] = "localhost"
2540
$config['Instance2'] = "localhost:14333"
41+
$config['Instance3'] = "localhost"
42+
$config['Instances'] = @($config['Instance1'], $config['Instance2'])
43+
2644
$config['Instance2SQLUserName'] = $null # placeholders for -SqlCredential testing
2745
$config['Instance2SQLPassword'] = $null
28-
$config['Instance3'] = "localhost"
2946
$config['Instance2_Detailed'] = "localhost,14333" # Just to make sure things parse a port properly
47+
3048
$config['AppveyorLabRepo'] = "/tmp/appveyor-lab"
31-
$config['Instances'] = @($config['Instance1'], $config['Instance2'])
3249
$config['SsisServer'] = "localhost\sql2016"
3350
$config['AzureBlob'] = "https://dbatools.blob.core.windows.net/sql"
3451
$config['AzureBlobAccount'] = "dbatools"
3552
$config['AzureServer'] = 'psdbatools.database.windows.net'
3653
$config['AzureSqlDbLogin'] = "[email protected]"
3754
} else {
55+
# This configuration is used for the automated test on AppVeyor
3856
$config['DbaToolsCi_Computer'] = "localhost"
57+
3958
$config['Instance1'] = "localhost\sql2008r2sp2"
4059
$config['Instance2'] = "localhost\sql2016"
60+
$config['Instance3'] = "localhost\sql2017"
61+
$config['Instances'] = @($config['Instance1'], $config['Instance2'])
62+
4163
$config['Instance2SQLUserName'] = $null # placeholders for -SqlCredential testing
4264
$config['Instance2SQLPassword'] = $null
43-
$config['Instance3'] = "localhost\sql2017"
4465
$config['Instance2_Detailed'] = "localhost,14333\sql2016" # Just to make sure things parse a port properly
66+
4567
$config['AppveyorLabRepo'] = "C:\github\appveyor-lab"
46-
$config['Instances'] = @($config['Instance1'], $config['Instance2'])
4768
$config['SsisServer'] = "localhost\sql2016"
4869
$config['AzureBlob'] = "https://dbatools.blob.core.windows.net/sql"
4970
$config['AzureBlobAccount'] = "dbatools"
5071
$config['AzureServer'] = 'psdbatools.database.windows.net'
5172
$config['AzureSqlDbLogin'] = "[email protected]"
73+
5274
$config['BigDatabaseBackup'] = 'C:\github\StackOverflowMini.bak'
5375
$config['BigDatabaseBackupSourceUrl'] = 'https://github.com/BrentOzarULTD/Stack-Overflow-Database/releases/download/20230114/StackOverflowMini.bak'
5476
}
5577

56-
if ($env:appveyor) {
57-
$config['Defaults'] = [System.Management.Automation.DefaultParameterDictionary]@{
58-
'*:WarningAction' = 'SilentlyContinue'
59-
}
60-
}
61-
62-
$config['CommonParameters'] = [System.Management.Automation.PSCmdlet]::CommonParameters
63-
64-
# We want the tests as readable as possible so we want to set Confirm globally to $false
65-
$config['Defaults']['*:Confirm'] = $false
66-
67-
# We use a global warning variable so that we can always test that the command does not write a warning
68-
$config['Defaults']['*:WarningVariable'] = 'WarnVar'
69-
70-
if (-not $config['Temp']) {
71-
$config['Temp'] = 'C:\Temp'
72-
}
73-
7478
[pscustomobject]$config
7579
}

0 commit comments

Comments
 (0)