|
6 | 6 | Tests for GitHubReferences.ps1 module |
7 | 7 | #> |
8 | 8 |
|
9 | | -[String] $root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) |
10 | | -. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1') |
11 | | -Import-Module -Name $root -Force |
12 | | - |
13 | | -function Initialize-AppVeyor |
14 | | -{ |
15 | | -<# |
16 | | - .SYNOPSIS |
17 | | - Configures the tests to run with the authentication information stored in AppVeyor |
18 | | - (if that information exists in the environment). |
19 | | -
|
20 | | - .DESCRIPTION |
21 | | - Configures the tests to run with the authentication information stored in AppVeyor |
22 | | - (if that information exists in the environment). |
23 | | -
|
24 | | - The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub |
25 | | -
|
26 | | - .NOTES |
27 | | - Internal-only helper method. |
28 | | -
|
29 | | - The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute, |
30 | | - which can only be applied to functions. |
31 | | -
|
32 | | - We call this immediately after the declaration so that AppVeyor initialization can heppen |
33 | | - (if applicable). |
34 | | -
|
35 | | -#> |
36 | | - [CmdletBinding()] |
37 | | - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", |
38 | | - Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")] |
39 | | - param() |
40 | | - |
41 | | - if ($env:AppVeyor) |
42 | | - { |
43 | | - $secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force |
44 | | - $cred = New-Object System.Management.Automation.PSCredential "<username is ignored>", $secureString |
45 | | - Set-GitHubAuthentication -Credential $cred |
46 | | - |
47 | | - $script:ownerName = $env:avOwnerName |
48 | | - $script:organizationName = $env:avOrganizationName |
49 | | - |
50 | | - $message = @( |
51 | | - 'This run is executed in the AppVeyor environment.', |
52 | | - 'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.', |
53 | | - '403 errors possible due to GitHub hourly limit for unauthenticated queries.', |
54 | | - 'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,', |
55 | | - 'and run tests on your machine first.') |
56 | | - Write-Warning -Message ($message -join [Environment]::NewLine) |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -Initialize-AppVeyor |
61 | | - |
62 | | -$accessTokenConfigured = Test-GitHubAuthenticationConfigured |
63 | | -if (-not $accessTokenConfigured) |
64 | | -{ |
65 | | - $message = @( |
66 | | - 'GitHub API Token not defined, some of the tests will be skipped.', |
67 | | - '403 errors possible due to GitHub hourly limit for unauthenticated queries.') |
68 | | - Write-Warning -Message ($message -join [Environment]::NewLine) |
69 | | -} |
70 | | - |
71 | | -# Backup the user's configuration before we begin, and ensure we're at a pure state before running |
72 | | -# the tests. We'll restore it at the end. |
73 | | -$configFile = New-TemporaryFile |
| 9 | +# This is common test code setup logic for all Pester test files |
| 10 | +$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent |
| 11 | +. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1') |
74 | 12 |
|
75 | 13 | try |
76 | 14 | { |
77 | | - Backup-GitHubConfiguration -Path $configFile |
78 | | - Reset-GitHubConfiguration |
79 | | - Set-GitHubConfiguration -DisableTelemetry # We don't want UT's to impact telemetry |
80 | | - Set-GitHubConfiguration -LogRequestBody # Make it easier to debug UT failures |
81 | | - |
82 | 15 | if ($accessTokenConfigured) |
83 | 16 | { |
84 | | - |
85 | 17 | Describe 'Create a new reference(branch) in repository' { |
86 | 18 | $repositoryName = [Guid]::NewGuid() |
87 | 19 | $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit |
|
126 | 58 | $repositoryName = [Guid]::NewGuid() |
127 | 59 | $repo = New-GitHubRepository -RepositoryName $repositoryName -AutoInit |
128 | 60 | $masterRefName = "heads/master" |
129 | | - $randomRefName = "heads/someRandomRef" |
| 61 | + $randomRefName = "heads/$([Guid]::NewGuid())" |
130 | 62 |
|
131 | 63 | Context 'On getting a valid reference from a new repository' { |
132 | 64 | $reference = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $masterRefName |
|
195 | 127 | } |
196 | 128 | catch |
197 | 129 | { |
198 | | - # Restore the user's configuration to its pre-test state |
199 | | - Restore-GitHubConfiguration -Path $configFile |
| 130 | + if (Test-Path -Path $script:originalConfigFile -PathType Leaf) |
| 131 | + { |
| 132 | + # Restore the user's configuration to its pre-test state |
| 133 | + Restore-GitHubConfiguration -Path $script:originalConfigFile |
| 134 | + $script:originalConfigFile = $null |
| 135 | + } |
200 | 136 | } |
201 | 137 |
|
0 commit comments