Skip to content

Commit bcd0a56

Browse files
Fix multi-result behavior across all versions of PowerShell (fixes CI UT's on all platforms) (#199)
Tests were failing on Mac and Linux, but not Windows ([recent test run](https://dev.azure.com/ms/PowerShellForGitHub/_build/results?buildId=83887&view=logs&j=0da5d1d9-276d-5173-c4c4-9d4d4ed14fdb)). That's because Windows CI was running against PoSh 5.x while Linux and Mac were running on PoSh 7.x. There's a slight difference in behavior for how those two treat arrays. The real root cause for this was the behavior of `Invoke-GHRestMethodMultipleResult`. When creating `$finalResult`, it was always blindly adding the result to the existing array: https://github.com/microsoft/PowerShellForGitHub/blob/587e2042621091c79cc06be2aa9cc6ea836561f4/GitHubCore.ps1#L648 `...` https://github.com/microsoft/PowerShellForGitHub/blob/587e2042621091c79cc06be2aa9cc6ea836561f4/GitHubCore.ps1#L670 Oddly enough, this created a difference in behavior between PowerShell versions when making the result an array on the caller side. Now I ensure that I don't add anything to `$finalResult` unless there's actually a value. With that change, we can now be sure that when we grab the result as an array, it'll be appropriately empty or populated (and not populated with a single `$null` entry, thus making `Count` 1, erroneously). I removed the attempt to force the results to be an array, because this is pointless. PowerShell will always unwrap an array of 0 or 1 in a return result. If you want to ensure that a result is always an array, you have to [wrap the result in an object](https://stackoverflow.com/a/60330501) or you have to do wrap the result in an array on the caller side. https://github.com/microsoft/PowerShellForGitHub/blob/587e2042621091c79cc06be2aa9cc6ea836561f4/GitHubCore.ps1#L684-L685 I also normalized some naming in all of the tests, so that when we're getting back a singular result (by querying for a specific item) that we use a singular variable name, and a plural variable name otherwise. With this change, we should now be passing CI on all OS platforms and across PowerShell 4+. Resolves #198
1 parent 587e204 commit bcd0a56

12 files changed

+175
-152
lines changed

GitHubCore.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,11 @@ function Invoke-GHRestMethodMultipleResult
667667
}
668668

669669
$result = Invoke-GHRestMethod @params
670-
$finalResult += $result.result
670+
if ($null -ne $result.result)
671+
{
672+
$finalResult += $result.result
673+
}
674+
671675
$nextLink = $result.nextLink
672676
$currentDescription = "$Description (getting additional results)"
673677
}
@@ -681,8 +685,7 @@ function Invoke-GHRestMethodMultipleResult
681685
Set-TelemetryEvent -EventName $TelemetryEventName -Properties $TelemetryProperties -Metrics $telemetryMetrics
682686
}
683687

684-
# Ensure we're always returning our results as an array, even if there is a single result.
685-
return @($finalResult)
688+
return $finalResult
686689
}
687690
catch
688691
{

Tests/GitHubAnalytics.tests.ps1

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ try
1616
$repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) -AutoInit
1717

1818
Context 'When initially created, there are no issues' {
19-
$issues = Get-GitHubIssue -Uri $repo.svn_url
19+
$issues = @(Get-GitHubIssue -Uri $repo.svn_url)
2020

2121
It 'Should return expected number of issues' {
22-
@($issues).Count | Should be 0
22+
$issues.Count | Should be 0
2323
}
2424
}
2525

@@ -34,29 +34,29 @@ try
3434
$newIssues[0] = Update-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $newIssues[0].number -State Closed
3535
$newIssues[-1] = Update-GitHubIssue -OwnerName $script:ownerName -RepositoryName $repo.name -Issue $newIssues[-1].number -State Closed
3636

37-
$issues = Get-GitHubIssue -Uri $repo.svn_url
37+
$issues = @(Get-GitHubIssue -Uri $repo.svn_url)
3838
It 'Should return only open issues' {
39-
@($issues).Count | Should be 2
39+
$issues.Count | Should be 2
4040
}
4141

42-
$issues = Get-GitHubIssue -Uri $repo.svn_url -State All
42+
$issues = @(Get-GitHubIssue -Uri $repo.svn_url -State All)
4343
It 'Should return all issues' {
44-
@($issues).Count | Should be 4
44+
$issues.Count | Should be 4
4545
}
4646

4747
$createdOnOrAfterDate = Get-Date -Date $newIssues[0].created_at
4848
$createdOnOrBeforeDate = Get-Date -Date $newIssues[2].created_at
49-
$issues = (Get-GitHubIssue -Uri $repo.svn_url) | Where-Object { ($_.created_at -ge $createdOnOrAfterDate) -and ($_.created_at -le $createdOnOrBeforeDate) }
49+
$issues = @((Get-GitHubIssue -Uri $repo.svn_url) | Where-Object { ($_.created_at -ge $createdOnOrAfterDate) -and ($_.created_at -le $createdOnOrBeforeDate) })
5050

5151
It 'Smart object date conversion works for comparing dates' {
52-
@($issues).Count | Should be 2
52+
$issues.Count | Should be 2
5353
}
5454

5555
$createdDate = Get-Date -Date $newIssues[1].created_at
56-
$issues = Get-GitHubIssue -Uri $repo.svn_url -State All | Where-Object { ($_.created_at -ge $createdDate) -and ($_.state -eq 'closed') }
56+
$issues = @(Get-GitHubIssue -Uri $repo.svn_url -State All | Where-Object { ($_.created_at -ge $createdDate) -and ($_.state -eq 'closed') })
5757

5858
It 'Able to filter based on date and state' {
59-
@($issues).Count | Should be 1
59+
$issues.Count | Should be 1
6060
}
6161
}
6262

@@ -88,18 +88,18 @@ try
8888
$issueCounts = $issueCounts | Sort-Object -Property Count -Descending
8989

9090
It 'Should return expected number of issues for each repository' {
91-
@($issueCounts[0].Count) | Should be 3
92-
@($issueCounts[1].Count) | Should be 0
91+
$issueCounts[0].Count | Should be 3
92+
$issueCounts[1].Count | Should be 0
9393
}
9494

9595
It 'Should return expected repository names' {
96-
@($issueCounts[0].Uri) | Should be ($repo1.svn_url)
97-
@($issueCounts[1].Uri) | Should be ($repo2.svn_url)
96+
$issueCounts[0].Uri | Should be $repo1.svn_url
97+
$issueCounts[1].Uri | Should be $repo2.svn_url
9898
}
9999
}
100100

101-
$null = Remove-GitHubRepository -Uri ($repo1.svn_url)
102-
$null = Remove-GitHubRepository -Uri ($repo2.svn_url)
101+
$null = Remove-GitHubRepository -Uri $repo1.svn_url
102+
$null = Remove-GitHubRepository -Uri $repo2.svn_url
103103
}
104104

105105

@@ -186,10 +186,10 @@ try
186186
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
187187
$repositoryUrl = "https://github.com/$script:ownerName/$repositoryName"
188188

189-
$collaborators = Get-GitHubRepositoryCollaborator -Uri $repositoryUrl
189+
$collaborators = @(Get-GitHubRepositoryCollaborator -Uri $repositoryUrl)
190190

191191
It 'Should return expected number of collaborators' {
192-
@($collaborators).Count | Should be 1
192+
$collaborators.Count | Should be 1
193193
}
194194

195195
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
@@ -201,10 +201,10 @@ try
201201
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
202202
$repositoryUrl = "https://github.com/$script:ownerName/$repositoryName"
203203

204-
$contributors = Get-GitHubRepositoryContributor -Uri $repositoryUrl -IncludeStatistics
204+
$contributors = @(Get-GitHubRepositoryContributor -Uri $repositoryUrl -IncludeStatistics)
205205

206206
It 'Should return expected number of contributors' {
207-
@($contributors).Count | Should be 1
207+
$contributors.Count | Should be 1
208208
}
209209

210210
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
@@ -242,15 +242,13 @@ try
242242
}
243243

244244
Describe 'Getting repositories from organization' {
245-
<# Temporary hack due to issues with this test in ADO #> . (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Config\Settings.ps1')
246-
247-
$original = Get-GitHubRepository -OrganizationName $script:organizationName
245+
$original = @(Get-GitHubRepository -OrganizationName $script:organizationName)
248246

249247
$repo = New-GitHubRepository -RepositoryName ([guid]::NewGuid().Guid) -OrganizationName $script:organizationName
250-
$current = Get-GitHubRepository -OrganizationName $script:organizationName
248+
$current = @(Get-GitHubRepository -OrganizationName $script:organizationName)
251249

252250
It 'Should return expected number of organization repositories' {
253-
(@($current).Count - @($original).Count) | Should be 1
251+
($current.Count - $original.Count) | Should be 1
254252
}
255253

256254
$null = Remove-GitHubRepository -Uri $repo.svn_url
@@ -260,15 +258,15 @@ try
260258
$repositoryName = [guid]::NewGuid().Guid
261259
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
262260

263-
$contributors = Get-GitHubRepositoryContributor -OwnerName $script:ownerName -RepositoryName $repositoryName -IncludeStatistics
261+
$contributors = @(Get-GitHubRepositoryContributor -OwnerName $script:ownerName -RepositoryName $repositoryName -IncludeStatistics)
264262

265263
$uniqueContributors = $contributors |
266264
Select-Object -ExpandProperty author |
267265
Select-Object -ExpandProperty login -Unique
268266
Sort-Object
269267

270268
It 'Should return expected number of unique contributors' {
271-
@($uniqueContributors).Count | Should be 1
269+
$uniqueContributors.Count | Should be 1
272270
}
273271

274272
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName
@@ -298,14 +296,14 @@ try
298296
$repositoryName = [guid]::NewGuid().Guid
299297
$null = New-GitHubRepository -RepositoryName $repositoryName -AutoInit
300298

301-
$branches = Get-GitHubRepositoryBranch -OwnerName $script:ownerName -RepositoryName $repositoryName
299+
$branches = @(Get-GitHubRepositoryBranch -OwnerName $script:ownerName -RepositoryName $repositoryName)
302300

303301
It 'Should return expected number of repository branches' {
304-
@($branches).Count | Should be 1
302+
$branches.Count | Should be 1
305303
}
306304

307305
It 'Should return the name of the branches' {
308-
@($branches[0].name) | Should be "master"
306+
$branches[0].name | Should be 'master'
309307
}
310308

311309
$null = Remove-GitHubRepository -OwnerName $script:ownerName -RepositoryName $repositoryName

Tests/GitHubAssignees.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ try
4444
Context 'For adding an assignee to an issue'{
4545
$assigneeList = @(Get-GitHubAssignee -Uri $repo.svn_url)
4646
$assigneeUserName = $assigneeList[0].login
47-
$assignees = @($assigneeUserName)
47+
$assignees = $assigneeUserName
4848
New-GithubAssignee -Uri $repo.svn_url -Issue $issue.number -Assignee $assignees
4949
$issue = Get-GitHubIssue -Uri $repo.svn_url -Issue $issue.number
5050

Tests/GitHubLabels.tests.ps1

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ try
7979
Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels
8080

8181
Context 'When querying for all labels' {
82-
$labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName
82+
$labels = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName)
8383

8484
It 'Should return expected number of labels' {
85-
$($labels).Count | Should be $:defaultLabels.Count
85+
$labels.Count | Should be $:defaultLabels.Count
8686
}
8787
}
8888

@@ -123,17 +123,17 @@ try
123123

124124
$labelName = [Guid]::NewGuid().Guid
125125
New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB
126-
$labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName
126+
$labels = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName)
127127

128128
It 'Should return increased number of labels' {
129-
$($labels).Count | Should be ($defaultLabels.Count + 1)
129+
$labels.Count | Should be ($defaultLabels.Count + 1)
130130
}
131131

132132
Remove-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName
133-
$labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName
133+
$labels = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName)
134134

135135
It 'Should return expected number of labels' {
136-
$($labels).Count | Should be $defaultLabels.Count
136+
$labels.Count | Should be $defaultLabels.Count
137137
}
138138

139139
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
@@ -187,7 +187,7 @@ try
187187

188188
# Add new label
189189
New-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name $labelName -Color BBBBBB
190-
$labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName
190+
$labels = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName)
191191

192192
# Change color of existing label
193193
Update-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "bug" -NewName "bug" -Color BBBBBB
@@ -200,10 +200,10 @@ try
200200
}
201201

202202
Set-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Label $defaultLabels
203-
$labels = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName
203+
$labels = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName)
204204

205205
It 'Should return expected number of labels' {
206-
$($labels).Count | Should be $defaultLabels.Count
206+
$labels.Count | Should be $defaultLabels.Count
207207
$bugLabel = $labels | Where-Object {$_.name -eq "bug"}
208208
$bugLabel.color | Should be "fc2929"
209209
}
@@ -269,7 +269,7 @@ try
269269
Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "discussion" -Issue $issue.number
270270
Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "question" -Issue $issue.number
271271
Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Name "bug" -Issue $issue.number
272-
$labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number
272+
$labelIssues = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number)
273273

274274
It 'Should have removed three labels from the issue' {
275275
$labelIssues.Count | Should be ($defaultLabels.Count - 3)
@@ -278,7 +278,7 @@ try
278278

279279
Context 'For removing all issues'{
280280
Remove-GitHubIssueLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number
281-
$labelIssues = Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number
281+
$labelIssues = @(Get-GitHubLabel -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number)
282282

283283
It 'Should have removed all labels from the issue' {
284284
$labelIssues.Count | Should be 0
@@ -312,7 +312,7 @@ try
312312
$labelIssues.Count | Should be $defaultLabels.Count
313313
}
314314

315-
$updatedIssueLabels = @($labelsToAdd[0])
315+
$updatedIssueLabels = $labelsToAdd[0]
316316
$updatedIssue = Update-GitHubIssue -OwnerName $ownerName -RepositoryName $repositoryName -Issue $issue.number -Label $updatedIssueLabels
317317

318318
It 'Should have 1 label after updating the issue' {

Tests/GitHubMilestones.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ try
7171
}
7272

7373
Context 'For getting milestones from a repo' {
74-
$existingMilestones = @(Get-GitHubMilestone -Uri $repo.svn_url -State Closed)
74+
$existingMilestones =@(Get-GitHubMilestone -Uri $repo.svn_url -State Closed)
7575
$issue = Get-GitHubIssue -Uri $repo.svn_url -Issue $issue.number
7676

7777
It 'Should have the expected number of milestones' {
@@ -110,11 +110,11 @@ try
110110
$existingMilestones.Count | Should be 4
111111
}
112112

113-
foreach($milestone in $existingMilestones) {
113+
foreach ($milestone in $existingMilestones) {
114114
Remove-GitHubMilestone -Uri $repo.svn_url -Milestone $milestone.number
115115
}
116116

117-
$existingMilestones = @(Get-GitHubMilestone -Uri $repo.svn_url)
117+
$existingMilestones = @(Get-GitHubMilestone -Uri $repo.svn_url -State All)
118118
$issue = Get-GitHubIssue -Uri $repo.svn_url -Issue $issue.number
119119

120120
It 'Should have no milestones' {

0 commit comments

Comments
 (0)