Skip to content

Commit e2871b5

Browse files
committed
Convert remaining prune tests
1 parent aabccec commit e2871b5

File tree

4 files changed

+86
-73
lines changed

4 files changed

+86
-73
lines changed

git-tool-audit-prune.ps1

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ foreach ($branch in $configuredBranches) {
5252
}
5353
}
5454

55-
Write-Host (ConvertTo-Json $resultUpstreams)
56-
5755

5856
# For all keys (downstream) in the upstreams:
5957
# - Remove entire branch configuration if the branch does not exist
@@ -89,16 +87,14 @@ foreach ($branch in $configuredBranches) {
8987
}
9088
Assert-Diagnostics $diagnostics
9189

92-
Write-Host (ConvertTo-Json $resultUpstreams)
93-
9490
# Set upstream branch
9591

9692
if ($resultUpstreams.Count -ne 0) {
9793
$upstreamHash = Invoke-LocalAction @commonParams @{
9894
type = 'set-upstream'
9995
parameters = @{
10096
upstreamBranches = $resultUpstreams
101-
message = "Applied changes from 'simplify' audit"
97+
message = "Applied changes from 'prune' audit"
10298
}
10399
}
104100
Assert-Diagnostics $diagnostics

git-tool-audit-prune.tests.ps1

Lines changed: 80 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Describe 'Invoke-PruneAudit' {
44
Import-Module -Scope Local "$PSScriptRoot/utils/framework.mocks.psm1"
55
Import-Module -Scope Local "$PSScriptRoot/utils/input.mocks.psm1"
66
Import-Module -Scope Local "$PSScriptRoot/utils/query-state.mocks.psm1"
7+
Import-Module -Scope Local "$PSScriptRoot/utils/actions.mocks.psm1"
78

89
function Initialize-ValidDownstreamBranchNames {
910
$upstreams = Select-AllUpstreamBranches
@@ -22,16 +23,6 @@ Describe 'Invoke-PruneAudit' {
2223
BeforeEach {
2324
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification='This is put in scope and used in the tests below')]
2425
$fw = Register-Framework
25-
26-
27-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUserDeclaredVarsMoreThanAssignments', '', Justification='This is put in scope and used in the tests below')]
28-
$initialCommits = @{
29-
'rc/2022-07-14' = 'rc/2022-07-14-commitish'
30-
'main' = 'main-commitish'
31-
'feature/FOO-123' = 'feature/FOO-123-commitish'
32-
'feature/XYZ-1-services' = 'feature/XYZ-1-services-commitish'
33-
'infra/shared' = 'infra/shared-commitish'
34-
}
3526
}
3627

3728
function Add-StandardTests() {
@@ -43,73 +34,103 @@ Describe 'Invoke-PruneAudit' {
4334
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
4435
}
4536

46-
It 'does nothing when existing branches are configured correctly' -Pending {
47-
Initialize-AllUpstreamBranches @{
48-
'rc/2022-07-14' = @("feature/FOO-123")
49-
'feature/FOO-123' = @('infra/shared')
50-
'infra/shared' = @('main')
37+
Context 'with branches' {
38+
BeforeEach {
39+
Initialize-SelectBranches @(
40+
'rc/2022-07-14',
41+
'feature/FOO-123',
42+
'infra/shared',
43+
'main'
44+
)
5145
}
52-
Initialize-ValidDownstreamBranchNames
5346

54-
& $PSScriptRoot/git-tool-audit-prune.ps1
55-
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
56-
}
47+
It 'does nothing when existing branches are configured correctly' {
48+
Initialize-AllUpstreamBranches @{
49+
'rc/2022-07-14' = @("feature/FOO-123")
50+
'feature/FOO-123' = @('infra/shared')
51+
'infra/shared' = @('main')
52+
}
53+
Initialize-ValidDownstreamBranchNames
5754

58-
It 'does not apply with a dry run' -Pending {
59-
Initialize-AllUpstreamBranches @{
60-
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
61-
'feature/FOO-123' = @('infra/shared')
62-
'infra/shared' = @('main')
63-
'feature/XYZ-1-services' = @('infra/shared') # intentionally have an extra configured branch here for removal
55+
& $PSScriptRoot/git-tool-audit-prune.ps1
56+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
6457
}
65-
Initialize-ValidDownstreamBranchNames
66-
67-
& $PSScriptRoot/git-tool-audit-prune.ps1 -dryRun
68-
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
69-
}
7058

71-
It 'prunes configuration of extra branches' -Pending {
72-
Initialize-AllUpstreamBranches @{
73-
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
74-
'feature/FOO-123' = @('infra/shared')
75-
'feature/XYZ-1-services' = @('infra/shared') # intentionally have an extra configured branch here for removal
76-
'infra/shared' = @('main')
59+
It 'does not apply with a dry run' {
60+
Initialize-AllUpstreamBranches @{
61+
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
62+
'feature/FOO-123' = @('infra/shared')
63+
'infra/shared' = @('main')
64+
'feature/XYZ-1-services' = @() # intentionally have an extra configured branch here for removal
65+
}
66+
Initialize-ValidDownstreamBranchNames
67+
Initialize-LocalActionSetUpstream @{
68+
'feature/XYZ-1-services' = $null
69+
'rc/2022-07-14' = @("feature/FOO-123")
70+
} "Applied changes from 'prune' audit" 'new-commit'
71+
Initialize-AssertValidBranchName '_upstream'
72+
73+
& $PSScriptRoot/git-tool-audit-prune.ps1 -dryRun
74+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
7775
}
78-
Initialize-ValidDownstreamBranchNames
7976

80-
$mock = @(
81-
# TODO - save
82-
)
83-
84-
& $PSScriptRoot/git-tool-audit-prune.ps1
85-
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
77+
It 'prunes configuration of extra branches' {
78+
Initialize-AllUpstreamBranches @{
79+
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
80+
'feature/FOO-123' = @('infra/shared')
81+
'infra/shared' = @('main')
82+
'feature/XYZ-1-services' = @() # intentionally have an extra configured branch here for removal
83+
}
84+
Initialize-ValidDownstreamBranchNames
85+
86+
$mock = @(
87+
Initialize-LocalActionSetUpstream @{
88+
'feature/XYZ-1-services' = $null
89+
'rc/2022-07-14' = @("feature/FOO-123")
90+
} "Applied changes from 'prune' audit" 'new-commit'
91+
Initialize-FinalizeActionSetBranches @{
92+
'_upstream' = 'new-commit'
93+
}
94+
)
8695

87-
Invoke-VerifyMock $mock -Times 1
88-
}
96+
& $PSScriptRoot/git-tool-audit-prune.ps1
97+
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
8998

90-
It 'consolidates removed branches' -Pending {
91-
# TODO -setup
99+
Invoke-VerifyMock $mock -Times 1
100+
}
92101

93-
$mock = @(
94-
# TODO: save
95-
)
102+
It 'consolidates removed branches' {
103+
Initialize-AllUpstreamBranches @{
104+
'rc/2022-07-14' = @("feature/FOO-123","feature/XYZ-1-services")
105+
'feature/FOO-123' = @('infra/shared')
106+
'infra/shared' = @('main')
107+
'feature/XYZ-1-services' = @('infra/shared') # intentionally have an extra configured branch here for removal
108+
}
109+
Initialize-ValidDownstreamBranchNames
110+
111+
$mock = @(
112+
Initialize-LocalActionSetUpstream @{
113+
'feature/XYZ-1-services' = $null
114+
'rc/2022-07-14' = @("feature/FOO-123")
115+
} "Applied changes from 'prune' audit" 'new-commit'
116+
Initialize-FinalizeActionSetBranches @{
117+
'_upstream' = 'new-commit'
118+
}
119+
)
96120

97-
& $PSScriptRoot/git-tool-audit-prune.ps1
98-
$fw.assertDiagnosticOutput | Should -BeNullOrEmpty
121+
& $PSScriptRoot/git-tool-audit-prune.ps1
122+
$fw.assertDiagnosticOutput.Count | Should -Be 1
123+
$fw.assertDiagnosticOutput | Should -Contain "WARN: Removing 'infra/shared' from upstream branches of 'rc/2022-07-14'; it is redundant via the following: feature/FOO-123"
99124

100-
Invoke-VerifyMock $mock -Times 1
125+
Invoke-VerifyMock $mock -Times 1
126+
}
101127
}
102128
}
103129

104130
Context 'with no remote' {
105131
BeforeAll {
106132
Initialize-ToolConfiguration -noRemote
107-
Initialize-SelectBranches @(
108-
'rc/2022-07-14',
109-
'feature/FOO-123',
110-
'infra/shared',
111-
'main'
112-
)
133+
Initialize-NoCurrentBranch
113134
}
114135

115136
Add-StandardTests
@@ -119,12 +140,6 @@ Describe 'Invoke-PruneAudit' {
119140
BeforeAll {
120141
Initialize-ToolConfiguration
121142
Initialize-UpdateGitRemote
122-
Initialize-SelectBranches @(
123-
'rc/2022-07-14',
124-
'feature/FOO-123',
125-
'infra/shared',
126-
'main'
127-
)
128143
}
129144

130145
Add-StandardTests

utils/query-state/Select-Branches.mocks.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ function Invoke-MockGit([string] $gitCli, [object] $MockWith) {
99
function Initialize-SelectBranches([string[]] $branches) {
1010
$remote = $(Get-Configuration).remote
1111
if ($remote -ne $nil) {
12-
Invoke-MockGit 'branch -r' -MockWith ($branches | Select-Object { "$remote/$_" })
12+
$result = ($branches | ForEach-Object { "$remote/$_" })
13+
Invoke-MockGit 'branch -r' -MockWith $result
1314
} else {
1415
Invoke-MockGit 'branch' -MockWith $branches
1516
}

utils/query-state/Select-Branches.psm1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ Import-Module -Scope Local "$PSScriptRoot/Configuration.psm1"
22

33
function Select-Branches() {
44
$remote = $(Get-Configuration).remote
5-
$temp = $remote -eq $nil ? (git branch) : (git branch -r)
6-
return $temp | Foreach-Object { $_.split("`n") } | Foreach-Object {
5+
[string[]]$temp = $remote -eq $nil ? (git branch) : (git branch -r)
6+
return $temp | Foreach-Object {
7+
if ($null -eq $temp) { return $null }
78
if ($remote -eq $nil) {
89
$branchName = $_.Trim()
910
} else {

0 commit comments

Comments
 (0)