1
1
import { Octokit } from "octokit" ;
2
2
import { MockInstance , describe , expect , it , vi } from "vitest" ;
3
3
4
+ import { Options } from "../shared/types.js" ;
4
5
import { initializeBranchProtectionSettings } from "./initializeGitHubRepository/initializeBranchProtectionSettings.js" ;
5
6
6
7
const createMockOctokit = ( request : MockInstance ) =>
7
8
( {
8
9
request,
9
10
} ) as unknown as Octokit ;
10
11
11
- const stubValues = { owner : "" , repository : "" } ;
12
+ const stubOptions = {
13
+ access : "public" ,
14
+ description : "" ,
15
+ directory : "" ,
16
+ email : {
17
+ github : "" ,
18
+ npm : "" ,
19
+ } ,
20
+ mode : "create" ,
21
+ owner : "" ,
22
+ repository : "" ,
23
+ title : "" ,
24
+ } satisfies Options ;
12
25
13
26
describe ( "migrateBranchProtectionSettings" , ( ) => {
14
27
it ( "does not throw when the request receives a non-error response" , async ( ) => {
@@ -17,17 +30,72 @@ describe("migrateBranchProtectionSettings", () => {
17
30
await expect (
18
31
initializeBranchProtectionSettings (
19
32
createMockOctokit ( mockRequest ) ,
20
- stubValues ,
33
+ stubOptions ,
21
34
) ,
22
35
) . resolves . not . toThrow ( ) ;
36
+
37
+ expect ( mockRequest . mock . calls ) . toMatchInlineSnapshot ( `
38
+ [
39
+ [
40
+ "PUT /repos///branches/main/protection",
41
+ {
42
+ "allow_deletions": false,
43
+ "allow_force_pushes": true,
44
+ "allow_fork_pushes": false,
45
+ "allow_fork_syncing": true,
46
+ "block_creations": false,
47
+ "branch": "main",
48
+ "enforce_admins": false,
49
+ "owner": "",
50
+ "repo": "",
51
+ "required_conversation_resolution": true,
52
+ "required_linear_history": false,
53
+ "required_pull_request_reviews": null,
54
+ "required_status_checks": {
55
+ "checks": [
56
+ {
57
+ "context": "build",
58
+ },
59
+ {
60
+ "context": "lint",
61
+ },
62
+ {
63
+ "context": "prettier",
64
+ },
65
+ {
66
+ "context": "compliance",
67
+ },
68
+ {
69
+ "context": "lint_knip",
70
+ },
71
+ {
72
+ "context": "lint_markdown",
73
+ },
74
+ {
75
+ "context": "lint_packages",
76
+ },
77
+ {
78
+ "context": "lint_spelling",
79
+ },
80
+ {
81
+ "context": "test",
82
+ },
83
+ ],
84
+ "strict": false,
85
+ },
86
+ "restrictions": null,
87
+ },
88
+ ],
89
+ ]
90
+ ` ) ;
23
91
} ) ;
24
92
25
93
it ( "returns false when the request receives a 403 response" , async ( ) => {
26
94
const mockRequest = vi . fn ( ) . mockRejectedValue ( { status : 403 } ) ;
27
95
28
96
const actual = await initializeBranchProtectionSettings (
29
97
createMockOctokit ( mockRequest ) ,
30
- stubValues ,
98
+ stubOptions ,
31
99
) ;
32
100
33
101
expect ( actual ) . toBe ( false ) ;
@@ -40,8 +108,59 @@ describe("migrateBranchProtectionSettings", () => {
40
108
await expect ( ( ) =>
41
109
initializeBranchProtectionSettings (
42
110
createMockOctokit ( mockRequest ) ,
43
- stubValues ,
111
+ stubOptions ,
44
112
) ,
45
113
) . rejects . toBe ( error ) ;
46
114
} ) ;
115
+
116
+ it ( "doesn't create workflows for excluded options when specified" , async ( ) => {
117
+ const mockRequest = vi . fn ( ) . mockResolvedValue ( { status : 200 } ) ;
118
+
119
+ await initializeBranchProtectionSettings ( createMockOctokit ( mockRequest ) , {
120
+ ...stubOptions ,
121
+ excludeCompliance : true ,
122
+ excludeLintKnip : true ,
123
+ excludeLintMd : true ,
124
+ excludeLintPackages : true ,
125
+ excludeLintSpelling : true ,
126
+ excludeTests : true ,
127
+ } ) ;
128
+
129
+ expect ( mockRequest . mock . calls ) . toMatchInlineSnapshot ( `
130
+ [
131
+ [
132
+ "PUT /repos///branches/main/protection",
133
+ {
134
+ "allow_deletions": false,
135
+ "allow_force_pushes": true,
136
+ "allow_fork_pushes": false,
137
+ "allow_fork_syncing": true,
138
+ "block_creations": false,
139
+ "branch": "main",
140
+ "enforce_admins": false,
141
+ "owner": "",
142
+ "repo": "",
143
+ "required_conversation_resolution": true,
144
+ "required_linear_history": false,
145
+ "required_pull_request_reviews": null,
146
+ "required_status_checks": {
147
+ "checks": [
148
+ {
149
+ "context": "build",
150
+ },
151
+ {
152
+ "context": "lint",
153
+ },
154
+ {
155
+ "context": "prettier",
156
+ },
157
+ ],
158
+ "strict": false,
159
+ },
160
+ "restrictions": null,
161
+ },
162
+ ],
163
+ ]
164
+ ` ) ;
165
+ } ) ;
47
166
} ) ;
0 commit comments