forked from microsoft/AL-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVerifyPRChanges.Action.Test.ps1
147 lines (123 loc) · 7.34 KB
/
VerifyPRChanges.Action.Test.ps1
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
Get-Module TestActionsHelper | Remove-Module -Force
Import-Module (Join-Path $PSScriptRoot 'TestActionsHelper.psm1')
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0
Describe 'VerifyPRChanges Action Tests' {
BeforeAll {
$actionName = "VerifyPRChanges"
$scriptRoot = Join-Path $PSScriptRoot "..\Actions\$actionName" -Resolve
$scriptName = "$actionName.ps1"
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'scriptPath', Justification = 'False positive.')]
$scriptPath = Join-Path $scriptRoot $scriptName
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'actionScript', Justification = 'False positive.')]
$actionScript = GetActionScript -scriptRoot $scriptRoot -scriptName $scriptName
}
It 'should fail if the PR is from a fork and changes a script' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename": "Scripts/BuildScript.ps1", "status": "modified"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'should fail if the PR is from a fork and adds a script' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":"Scripts/BuildScript.ps1", "status": "added"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'should fail if the PR is from a fork and removes a script' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":"Scripts/BuildScript.ps1","status":"removed"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'should fail if the PR is from a fork and changes the CODEOWNERS file' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":"CODEOWNERS","status":"modified"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'should fail if the PR is from a fork and changes anything in the .github folder' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":".github/Settings.json","status":"modified"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'should fail if the PR is from a fork and changes a yml file' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":".github/workflows/test.yaml","status":"modified"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'should succeed if the PR is from a fork and changes an .al file' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":"ALModule/Test.Codeunit.al","status":"modified"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
Mock Write-Host {}
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "Verification completed successfully." }
}
It 'should succeed if the PR is from a fork and adds an .al file' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":"ALModule/Test.Codeunit.al","status":"added"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
Mock Write-Host {}
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "Verification completed successfully." }
}
It 'should succeed if the PR is from a fork and removes an .al file' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 1 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '[{"filename":"ALModule/Test.Codeunit.al","status":"removed"}]' } } -ParameterFilter { $Uri -and $Uri -match "/files"}
Mock Write-Host {}
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
Assert-MockCalled Write-Host -Exactly 1 -Scope It -ParameterFilter { $Object -eq "Verification completed successfully." }
}
It 'should fail if the PR is from a fork and changes more than 3000 files' {
Mock -CommandName Invoke-WebRequest -MockWith { @{"Content" = '{ "changed_files": 5001 }' } } -ParameterFilter { $Uri -and $Uri -notmatch "/files"}
{
& $scriptPath `
-prBaseRepository "microsoft/AL-Go" `
-pullRequestId "123456" `
-token "ABC"
} | Should -Throw
}
It 'Compile Action' {
Invoke-Expression $actionScript
}
It 'Test action.yaml matches script' {
$permissions = [ordered]@{
}
$outputs = [ordered]@{
}
YamlTest -scriptRoot $scriptRoot -actionName $actionName -actionScript $actionScript -permissions $permissions -outputs $outputs
}
}