Repository navigation
Expand file tree
/
Copy pathGitHubAnalytics.tests.ps1
More file actions
383 lines (301 loc) · 15.7 KB
/
Copy pathGitHubAnalytics.tests.ps1
File metadata and controls
383 lines (301 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
<#
.Synopsis
Tests for GitHubAnalytics.ps1 module
#>
[String] $root = Split-Path -Parent (Split-Path -Parent $Script:MyInvocation.MyCommand.Path)
. (Join-Path -Path $root -ChildPath 'Tests\Config\Settings.ps1')
Import-Module -Name $root -Force
function Initialize-AppVeyor
{
<#
.SYNOPSIS
Configures the tests to run with the authentication information stored in AppVeyor
(if that information exists in the environment).
.DESCRIPTION
Configures the tests to run with the authentication information stored in AppVeyor
(if that information exists in the environment).
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
.NOTES
Internal-only helper method.
The only reason this exists is so that we can leverage CodeAnalysis.SuppressMessageAttribute,
which can only be applied to functions.
We call this immediately after the declaration so that AppVeyor initialization can heppen
(if applicable).
#>
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "", Justification="Needed to configure with the stored, encrypted string value in AppVeyor.")]
param()
if ($env:AppVeyor)
{
$secureString = $env:avAccessToken | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential "<username is ignored>", $secureString
Set-GitHubAuthentication -Credential $cred
$script:ownerName = $env:avOwnerName
$script:organizationName = $env:avOrganizationName
$message = @(
'This run is executed in the AppVeyor environment.',
'The GitHub Api Token won''t be decrypted in PR runs causing some tests to fail.',
'403 errors possible due to GitHub hourly limit for unauthenticated queries.',
'Use Set-GitHubAuthentication manually. modify the values in Tests\Config\Settings.ps1,',
'and run tests on your machine first.')
Write-Warning -Message ($message -join [Environment]::NewLine)
}
}
Initialize-AppVeyor
$script:accessTokenConfigured = Test-GitHubAuthenticationConfigured
if (-not $script:accessTokenConfigured)
{
$message = @(
'GitHub API Token not defined, some of the tests will be skipped.',
'403 errors possible due to GitHub hourly limit for unauthenticated queries.')
Write-Warning -Message ($message -join [Environment]::NewLine)
}
# Backup the user's configuration before we begin, and ensure we're at a pure state before running
# the tests. We'll restore it at the end.
$configFile = New-TemporaryFile
try
{
Backup-GitHubConfiguration -Path $configFile
Reset-GitHubConfiguration
Set-GitHubConfiguration -DisableTelemetry # We don't want UT's to impact telemetry
Set-GitHubConfiguration -LogRequestBody # Make it easier to debug UT failures
Describe 'Obtaining issues for repository' {
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
Context 'When initially created, there are no issues' {
$issues = Get-GitHubIssue -Uri $repo.svn_url
It 'Should return expected number of issues' {
@($issues).Count | Should be 0
}
}
Context 'When there are issues present' {
$newIssues = @()
for ($i = 0; $i -lt 4; $i++)
{
$newIssues += New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Title ([guid]::NewGuid().Guid)
Start-Sleep -Seconds 5
}
$newIssues[0] = Update-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $newIssues[0].number -State Closed
$newIssues[-1] = Update-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $newIssues[-1].number -State Closed
$issues = Get-GitHubIssue -Uri $repo.svn_url
It 'Should return only open issues' {
@($issues).Count | Should be 2
}
$issues = Get-GitHubIssue -Uri $repo.svn_url -State All
It 'Should return all issues' {
@($issues).Count | Should be 4
}
$createdOnOrAfterDate = Get-Date -Date $newIssues[0].created_at
$createdOnOrBeforeDate = Get-Date -Date $newIssues[2].created_at
$issues = (Get-GitHubIssue -Uri $repo.svn_url) | Where-Object { ($_.created_at -ge $createdOnOrAfterDate) -and ($_.created_at -le $createdOnOrBeforeDate) }
It 'Smart object date conversion works for comparing dates' {
@($issues).Count | Should be 2
}
$createdDate = Get-Date -Date $newIssues[1].created_at
$issues = Get-GitHubIssue -Uri $repo.svn_url -State All | Where-Object { ($_.created_at -ge $createdDate) -and ($_.state -eq 'closed') }
It 'Able to filter based on date and state' {
@($issues).Count | Should be 1
}
}
Context 'When issues are retrieved with a specific MediaTypes' {
$newIssue = New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Title ([guid]::NewGuid()) -Body ([guid]::NewGuid())
$issues = @(Get-GitHubIssue -Uri $repo.svn_url -Issue $newIssue.number -MediaType 'Html')
It 'Should return an issue with body_html' {
$issues[0].body_html | Should not be $null
}
}
$null = Remove-GitHubRepository -Uri ($repo.svn_url)
}
Describe 'Obtaining repository with biggest number of issues' {
$repo1 = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
$repo2 = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
Context 'When no addional conditions specified' {
for ($i = 0; $i -lt 3; $i++)
{
$null = New-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo1.name -Title ([guid]::NewGuid().Guid)
}
$repos = @(($repo1.svn_url), ($repo2.svn_url))
$issueCounts = @()
$repos | ForEach-Object { $issueCounts = $issueCounts + ([PSCustomObject]@{ 'Uri' = $_; 'Count' = (Get-GitHubIssue -Uri $_).Count }) }
$issueCounts = $issueCounts | Sort-Object -Property Count -Descending
It 'Should return expected number of issues for each repository' {
@($issueCounts[0].Count) | Should be 3
@($issueCounts[1].Count) | Should be 0
}
It 'Should return expected repository names' {
@($issueCounts[0].Uri) | Should be ($repo1.svn_url)
@($issueCounts[1].Uri) | Should be ($repo2.svn_url)
}
}
$null = Remove-GitHubRepository -Uri ($repo1.svn_url)
$null = Remove-GitHubRepository -Uri ($repo2.svn_url)
}
# TODO: Re-enable these tests once the module has sufficient support getting the repository into the
# required state for testing, and to recover back to the original state at the conclusion of the test.
# Describe 'Obtaining pull requests for repository' {
# Context 'When no addional conditions specified' {
# $pullRequests = Get-GitHubPullRequest -Uri $script:repositoryUrl
# It 'Should return expected number of PRs' {
# @($pullRequests).Count | Should be 2
# }
# }
# Context 'When state and time range specified' {
# $mergedStartDate = Get-Date -Date '2016-04-10'
# $mergedEndDate = Get-Date -Date '2016-05-07'
# $pullRequests = Get-GitHubPullRequest -Uri $script:repositoryUrl -State Closed |
# Where-Object { ($_.merged_at -ge $mergedStartDate) -and ($_.merged_at -le $mergedEndDate) }
# It 'Should return expected number of PRs' {
# @($pullRequests).Count | Should be 3
# }
# }
# }
# Describe 'Obtaining repository with biggest number of pull requests' {
# Context 'When no addional conditions specified' {
# @($script:repositoryUrl, $script:repositoryUrl2) |
# ForEach-Object {
# $pullRequestCounts += ([PSCustomObject]@{
# 'Uri' = $_;
# 'Count' = (Get-GitHubPullRequest -Uri $_).Count }) }
# $pullRequestCounts = $pullRequestCounts | Sort-Object -Property Count -Descending
# It 'Should return expected number of pull requests for each repository' {
# @($pullRequestCounts[0].Count) | Should be 2
# @($pullRequestCounts[1].Count) | Should be 0
# }
# It 'Should return expected repository names' {
# @($pullRequestCounts[0].Uri) | Should be $script:repositoryUrl
# @($pullRequestCounts[1].Uri) | Should be $script:repositoryUrl2
# }
# }
# Context 'When state and time range specified' {
# $mergedDate = Get-Date -Date '2015-04-20'
# $repos = @($script:repositoryUrl, $script:repositoryUrl2)
# $pullRequestCounts = @()
# $pullRequestSearchParams = @{
# 'State' = 'closed'
# }
# $repos |
# ForEach-Object {
# $pullRequestCounts += ([PSCustomObject]@{
# 'Uri' = $_;
# 'Count' = (
# (Get-GitHubPullRequest -Uri $_ @pullRequestSearchParams) |
# Where-Object { $_.merged_at -ge $mergedDate }
# ).Count
# }) }
# $pullRequestCounts = $pullRequestCounts | Sort-Object -Property Count -Descending
# $pullRequests = Get-GitHubTopPullRequestRepository -Uri @($script:repositoryUrl, $script:repositoryUrl2) -State Closed -MergedOnOrAfter
# It 'Should return expected number of pull requests for each repository' {
# @($pullRequests[0].Count) | Should be 3
# @($pullRequests[1].Count) | Should be 0
# }
# It 'Should return expected repository names' {
# @($pullRequests[0].Uri) | Should be $script:repositoryUrl
# @($pullRequests[1].Uri) | Should be $script:repositoryUrl2
# }
# }
# }
if ($script:accessTokenConfigured)
{
Describe 'Obtaining collaborators for repository' {
$repositoryName = [guid]::NewGuid().Guid
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
$repositoryUrl = "https://github.com/$script:ownerName/$repositoryName"
$collaborators = Get-GitHubRepositoryCollaborator -Uri $repositoryUrl
It 'Should return expected number of collaborators' {
@($collaborators).Count | Should be 1
}
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
}
}
Describe 'Obtaining contributors for repository' {
$repositoryName = [guid]::NewGuid().Guid
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
$repositoryUrl = "https://github.com/$script:ownerName/$repositoryName"
$contributors = Get-GitHubRepositoryContributor -Uri $repositoryUrl -IncludeStatistics
It 'Should return expected number of contributors' {
@($contributors).Count | Should be 1
}
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
}
if ($script:accessTokenConfigured)
{
# TODO: Re-enable these tests once the module has sufficient support getting the Organization
# and repository into the required state for testing, and to recover back to the original state
# at the conclusion of the test.
# Describe 'Obtaining organization members' {
# $members = Get-GitHubOrganizationMember -OrganizationName $script:organizationName
# It 'Should return expected number of organization members' {
# @($members).Count | Should be 1
# }
# }
# Describe 'Obtaining organization teams' {
# $teams = Get-GitHubTeam -OrganizationName $script:organizationName
# It 'Should return expected number of organization teams' {
# @($teams).Count | Should be 2
# }
# }
# Describe 'Obtaining organization team members' {
# $members = Get-GitHubTeamMember -OrganizationName $script:organizationName -TeamName $script:organizationTeamName
# It 'Should return expected number of organization team members' {
# @($members).Count | Should be 1
# }
# }
}
Describe 'Getting repositories from organization' {
$original = Get-GitHubRepository -OrganizationName $script:organizationName
$repositoryName = [guid]::NewGuid().Guid
$null = New-GitHubRepository -RepositoryName $repositoryName -OrganizationName $script:organizationName
$current = Get-GitHubRepository -OrganizationName $script:organizationName
It 'Should return expected number of organization repositories' {
(@($current).Count - @($original).Count) | Should be 1
}
$null = Remove-GitHubRepository -OwnerName $script:organizationName -RepositoryName $repositoryName
}
Describe 'Getting unique contributors from contributors array' {
$repositoryName = [guid]::NewGuid().Guid
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
$contributors = Get-GitHubRepositoryContributor -OwnerName $script:ownerName -RepositoryName $repositoryName -IncludeStatistics
$uniqueContributors = $contributors |
Select-Object -ExpandProperty author |
Select-Object -ExpandProperty login -Unique
Sort-Object
It 'Should return expected number of unique contributors' {
@($uniqueContributors).Count | Should be 1
}
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
}
Describe 'Getting repository name from url' {
$repositoryName = [guid]::NewGuid().Guid
$url = "https://github.com/$script:ownerName/$repositoryName"
$name = Split-GitHubUri -Uri $url -RepositoryName
It 'Should return expected repository name' {
$name | Should be $repositoryName
}
}
Describe 'Getting repository owner from url' {
$repositoryName = [guid]::NewGuid().Guid
$url = "https://github.com/$script:ownerName/$repositoryName"
$owner = Split-GitHubUri -Uri $url -OwnerName
It 'Should return expected repository owner' {
$owner | Should be $script:ownerName
}
}
Describe 'Getting branches for repository' {
$repositoryName = [guid]::NewGuid().Guid
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
$branches = Get-GitHubRepositoryBranch -OwnerName $script:ownerName -RepositoryName $repositoryName
It 'Should return expected number of repository branches' {
@($branches).Count | Should be 1
}
It 'Should return the name of the branches' {
@($branches[0].name) | Should be "master"
}
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
}
}
finally
{
# Restore the user's configuration to its pre-test state
Restore-GitHubConfiguration -Path $configFile
}