Skip to content

Commit 89f69f1

Browse files
smaglio81HowardWolosky
authored andcommitted
Reducing warning messages during unit tests configuration (#132)
During UT's, `Reset-GitHubConfiguration` and `Clear-GitHubAuthentication` are called frequently. This change moves their warning messages to be in Verbose output instead, in order to reduce the noise that is commonly seen when they're called. Additionally, the warning that is seen at the start of running a UT test suite is now completely suppressed if the user has modified `Tests/Config/Settings.ps1` (presumably to have it reference accounts that they have access for). This is achieved by storing the hash of the Settings file in the code, along with documentation to remind users to update it if the Settings file is ever permanently changed. Finally, an additional test has been added to the CI pipeline to validate that the Settings file content matches the hash that is present in the code.
1 parent 1983da9 commit 89f69f1

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

GitHubConfiguration.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ function Get-GitHubConfiguration
288288
'RetryDelaySeconds',
289289
'SuppressNoTokenWarning',
290290
'SuppressTelemetryReminder',
291+
'TestConfigSettingsHash',
291292
'WebRequestTimeoutSec')]
292293
[string] $Name
293294
)
@@ -481,6 +482,9 @@ function Reset-GitHubConfiguration
481482
Reset-GitHubConfiguration
482483
483484
Deletes the local configuration file and loads in all default configration values.
485+
486+
.NOTES
487+
This command will not clear your authentication token. Please use Clear-GitHubAuthentication to accomplish that.
484488
#>
485489
[CmdletBinding(SupportsShouldProcess)]
486490
param(
@@ -504,7 +508,7 @@ function Reset-GitHubConfiguration
504508

505509
Initialize-GitHubConfiguration
506510

507-
Write-Log -Message "This has not cleared your authentication token. Call Clear-GitHubAuthentication to accomplish that." -Level Warning
511+
Write-Log -Message "This has not cleared your authentication token. Call Clear-GitHubAuthentication to accomplish that." -Level Verbose
508512
}
509513

510514
function Read-GitHubConfiguration
@@ -622,6 +626,13 @@ function Import-GitHubConfiguration
622626
'suppressNoTokenWarning' = $false
623627
'suppressTelemetryReminder' = $false
624628
'webRequestTimeoutSec' = 0
629+
630+
# This hash is generated by using Helper.ps1's Get-Sha512Hash in Tests/Config/Settings.ps1 like so:
631+
# . ./Helpers.ps1; Get-Sha512Hash -PlainText (Get-Content -Path ./Tests/Config/Settings.ps1 -Raw -Encoding Utf8)
632+
# The hash is used to identify if the user has made changes to the config file prior to running the UT's locally.
633+
# It intentionally cannot be modified via Set-GitHubConfiguration and must be updated directly in the
634+
# source code here should the default Settings.ps1 file ever be changed.
635+
'testConfigSettingsHash' = 'A76CA42A587D10247F887F9257DB7BF5F988E8714A7C0E29D7B100A20F5D35B8E3306AC5B9BBC8851EC19846A90BB3C80FC7C594D0347A772B2B10BADB1B3E68'
625636
}
626637

627638
$jsonObject = Read-GitHubConfiguration -Path $Path
@@ -909,6 +920,9 @@ function Clear-GitHubAuthentication
909920
Clear-GitHubAuthentication
910921
911922
Clears out any GitHub API token from memory, as well as from local file storage.
923+
924+
.NOTES
925+
This command will not clear your configuration settings. Please use Reset-GitHubConfiguration to accomplish that.
912926
#>
913927
[CmdletBinding(SupportsShouldProcess)]
914928
param(
@@ -937,7 +951,7 @@ function Clear-GitHubAuthentication
937951
}
938952
}
939953

940-
Write-Log -Message "This has not cleared your configuration settings. Call Reset-GitHubConfiguration to accomplish that." -Level Warning
954+
Write-Log -Message "This has not cleared your configuration settings. Call Reset-GitHubConfiguration to accomplish that." -Level Verbose
941955
}
942956

943957
function Get-AccessToken

Tests/Common.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,26 @@ function Initialize-CommonTestSetup
3333
param()
3434

3535
$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent
36-
. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Config\Settings.ps1')
36+
$settingsPath = Join-Path -Path $moduleRootPath -ChildPath 'Tests/Config/Settings.ps1'
37+
. $settingsPath
3738
Import-Module -Name (Join-Path -Path $moduleRootPath -ChildPath 'PowerShellForGitHub.psd1') -Force
3839

40+
$originalSettingsHash = (Get-GitHubConfiguration -Name TestConfigSettingsHash)
41+
$currentSettingsHash = Get-SHA512Hash -PlainText (Get-Content -Path $settingsPath -Raw -Encoding Utf8)
42+
$settingsAreUnaltered = $originalSettingsHash -eq $currentSettingsHash
43+
3944
if ([string]::IsNullOrEmpty($env:ciAccessToken))
4045
{
41-
$message = @(
42-
'The tests are using the configuration settings defined in Tests\Config\Settings.ps1.',
43-
'If you haven''t locally modified those values, your tests are going to fail since you',
44-
'don''t have access to the default accounts referenced. If that is the case, you should',
45-
'cancel the existing tests, modify the values to ones you have access to, call',
46-
'Set-GitHubAuthentication to cache your AccessToken, and then try running the tests again.')
47-
Write-Warning -Message ($message -join [Environment]::NewLine)
46+
if ($settingsAreUnaltered)
47+
{
48+
$message = @(
49+
'The tests are using the configuration settings defined in Tests/Config/Settings.ps1.',
50+
'If you haven''t locally modified those values, your tests are going to fail since you',
51+
'don''t have access to the default accounts referenced. If that is the case, you should',
52+
'cancel the existing tests, modify the values to ones you have access to, call',
53+
'Set-GitHubAuthentication to cache your AccessToken, and then try running the tests again.')
54+
Write-Warning -Message ($message -join [Environment]::NewLine)
55+
}
4856
}
4957
else
5058
{

Tests/Config/Settings.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
<#
5+
.NOTES
6+
A hash of this file is stored in GitHubConfiguration.ps1 within the Import-GitHubConfiguration function
7+
as TestConfigSettingsHash (and can be retrieved by calling Get-GitHubConfiguration -Name TestConfigSettingsHash).
8+
It is used when trying to detect if the file has been updated with personal settings before unit tests
9+
are run locally (since unit tests will otherwise fail because users do not have account permissions to
10+
access the accounts below).
11+
12+
If this file is being modified as part of an intended change to the official repo, please be sure to
13+
additionally update the value of TestConfigSettingsHash in GitHubConfiguration.ps1 by running:
14+
15+
. ./Helpers.ps1; Get-Sha512Hash -PlainText (Get-Content -Path ./Tests/Config/Settings.ps1 -Raw -Encoding Utf8)
16+
#>
17+
418
# The account that the tests will be running under
519
$script:ownerName = 'PowerShellForGitHubTeam'
620

build/pipelines/azure-pipelines.ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ jobs:
2323
pool:
2424
vmImage: 'vs2017-win2016'
2525
steps:
26+
- template: ./templates/verify-testConfigSettingsHash.yaml
2627
- template: ./templates/run-staticAnalysis.yaml
2728
- template: ./templates/run-unitTests.yaml
2829

2930
- job: Linux
3031
pool:
3132
vmImage: 'ubuntu-16.04'
3233
steps:
34+
- template: ./templates/verify-testConfigSettingsHash.yaml
3335
- template: ./templates/run-staticAnalysis.yaml
3436
- template: ./templates/run-unitTests.yaml
3537

3638
- job: macOS
3739
pool:
3840
vmImage: 'macOS-10.13'
3941
steps:
42+
- template: ./templates/verify-testConfigSettingsHash.yaml
4043
- template: ./templates/run-staticAnalysis.yaml
4144
- template: ./templates/run-unitTests.yaml
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# This template contains a script which verifies if the $testConfigSettingsHash (GitHubConfiguration.ps1)
3+
# value matches the Get-SHA512Hash value of ./Tests/Config/Settings.ps1. Both hash values need to be
4+
# the same in order to ensure the correct warning messages are presented to developers running UTs.
5+
#
6+
7+
steps:
8+
- powershell: |
9+
. ./Helpers.ps1
10+
$content = Get-Content -Path ./Tests/Config/Settings.ps1 -Raw -Encoding UTF8
11+
$hash = Get-SHA512Hash -PlainText $content
12+
$configurationFile = Get-Content -Path ./GitHubConfiguration.ps1 -Raw -Encoding Utf8
13+
if($configurationFile -match "'testConfigSettingsHash' = '([^']+)'")
14+
{
15+
if($hash -ne $Matches[1])
16+
{
17+
$configHash = $Matches[1]
18+
$message = @(
19+
"`$testConfigSettingsHash value does not match the Get-SHA512Hash of file ./Tests/Config/Settings.ps1. If the contents of ",
20+
"Settings.ps1 has been updated, please update the `$testConfigSettingsHash value in ./GitHubConfiguration.ps1's ",
21+
"Import-GitHubConfiguration function. You can generate the new hash value using the commands:",
22+
"",
23+
"`t. ./Helpers.ps1",
24+
"`tGet-SHA512Hash -PlainText (Get-Content -Path ./Tests/Config/Settings.ps1 -Raw -Encoding Utf8)",
25+
"",
26+
"`$testConfigSettingsHash = $configHash",
27+
"Get-SHA512Hash(Settings.ps1) = $hash")
28+
throw ($message -join [Environment]::NewLine)
29+
}
30+
} else
31+
{
32+
$message = @(
33+
"`$testConfigSettingsHash value not found in ./GitHubConfiguration.ps1. Please ensure ",
34+
"the default hash value is set within the Import-GitHubConfiguration function.")
35+
throw ($message -join [Environment]::NewLine)
36+
}
37+
displayName: 'Set GitHubConfiguration.ps1 testConfigSettingsHash value.'
38+

0 commit comments

Comments
 (0)