git_helper 3.6.1 → 3.6.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,270 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'git_helper'
5
-
6
- describe GitHelper::LocalCode do
7
- let(:response) { double(:response, readline: true, to_i: 5) }
8
- let(:local_codeent) { double(:local_code, ask: response) }
9
- let(:project_name) { Faker::Lorem.word }
10
- let(:owner) { Faker::Name.first_name }
11
- let(:ssh_remote) { "origin\tgit@github.com:#{owner}/#{project_name}.git (fetch)" }
12
- let(:https_remote) { "origin\thttps://github.com/#{owner}/#{project_name}.git (fetch)" }
13
-
14
- let(:github_remotes) do
15
- [
16
- "origin\tgit@github.com:#{owner}/#{project_name}.git (fetch)",
17
- "origin\thttps://github.com/#{owner}/#{project_name}.git (fetch)"
18
- ]
19
- end
20
-
21
- let(:gitlab_remotes) do
22
- [
23
- "origin\tgit@gitlab.com:#{owner}/#{project_name}.git (fetch)",
24
- "origin\thttps://gitlab.com/#{owner}/#{project_name}.git (fetch)"
25
- ]
26
- end
27
-
28
- subject { GitHelper::LocalCode.new }
29
-
30
- before do
31
- allow(subject).to receive(:system).and_return(nil)
32
- end
33
-
34
- describe '#checkout_default' do
35
- it 'should make a system call' do
36
- expect(subject).to receive(:system)
37
- subject.checkout_default
38
- end
39
- end
40
-
41
- describe '#forget_local_commits' do
42
- it 'should make a system call' do
43
- expect(subject).to receive(:system).exactly(2).times
44
- subject.forget_local_commits
45
- end
46
-
47
- it 'should return nil' do
48
- expect(subject.forget_local_commits).to eq(nil)
49
- end
50
- end
51
-
52
- describe '#empty_commit' do
53
- it 'should make a system call' do
54
- expect(subject).to receive(:system)
55
- subject.empty_commit
56
- end
57
- end
58
-
59
- describe '#clean_branches' do
60
- it 'should make a system call' do
61
- expect(subject).to receive(:system).exactly(4).times
62
- subject.clean_branches
63
- end
64
- end
65
-
66
- describe '#new_branch' do
67
- it 'should make a system call' do
68
- expect(subject).to receive(:system).exactly(4).times
69
- subject.new_branch(Faker::Lorem.word)
70
- end
71
- end
72
-
73
- describe '#change_remote' do
74
- it 'should return a string' do
75
- allow(subject).to receive(:`).and_return(Faker::Lorem.word)
76
- expect(subject.change_remote(Faker::Lorem.word, Faker::Internet.url)).to be_a(String)
77
- end
78
- end
79
-
80
- describe '#remotes' do
81
- it 'should return an array of strings' do
82
- expect(subject.remotes).to be_a(Array)
83
- expect(subject.remotes.first).to be_a(String)
84
- end
85
- end
86
-
87
- describe '#remote_name' do
88
- it 'should be a string' do
89
- expect(subject.remote_name(ssh_remote)).to be_a(String)
90
- end
91
- end
92
-
93
- describe '#ssh_remote' do
94
- it 'should come out true if ssh' do
95
- expect(subject.ssh_remote?(ssh_remote)).to eq(true)
96
- end
97
-
98
- it 'should come out false if https' do
99
- expect(subject.ssh_remote?(https_remote)).to eq(false)
100
- end
101
- end
102
-
103
- describe '#https_remote' do
104
- it 'should come out false if ssh' do
105
- expect(subject.https_remote?(ssh_remote)).to eq(false)
106
- end
107
-
108
- it 'should come out true if https' do
109
- expect(subject.https_remote?(https_remote)).to eq(true)
110
- end
111
- end
112
-
113
- describe '#remote_project' do
114
- it 'should return just the plain project if ssh' do
115
- expect(subject.remote_project(ssh_remote)).to eq(project_name)
116
- end
117
-
118
- it 'should return just the plain project if https' do
119
- expect(subject.remote_project(https_remote)).to eq(project_name)
120
- end
121
- end
122
-
123
- describe '#remote_source' do
124
- it 'should return just the plain project if ssh' do
125
- expect(subject.remote_source(ssh_remote)).to eq('github.com')
126
- end
127
-
128
- it 'should return just the plain project if https' do
129
- expect(subject.remote_source(https_remote)).to eq('github.com')
130
- end
131
- end
132
-
133
- describe '#github_repo' do
134
- it 'should return true if github' do
135
- allow(subject).to receive(:remotes).and_return(github_remotes)
136
- expect(subject.github_repo?).to eq(true)
137
- end
138
-
139
- it 'should return false if gitlab' do
140
- allow(subject).to receive(:remotes).and_return(gitlab_remotes)
141
- expect(subject.github_repo?).to eq(false)
142
- end
143
- end
144
-
145
- describe '#gitlab_project' do
146
- it 'should return true if gitlab' do
147
- allow(subject).to receive(:remotes).and_return(gitlab_remotes)
148
- expect(subject.gitlab_project?).to eq(true)
149
- end
150
-
151
- it 'should return false if github' do
152
- allow(subject).to receive(:remotes).and_return(github_remotes)
153
- expect(subject.gitlab_project?).to eq(false)
154
- end
155
- end
156
-
157
- describe '#project_name' do
158
- it 'should return a string' do
159
- expect(subject.project_name).to be_a(String)
160
- end
161
-
162
- it 'should equal this project name' do
163
- allow_any_instance_of(String).to receive(:scan).and_return([["#{owner}/#{project_name}"]])
164
- expect(subject.project_name).to eq("#{owner}/#{project_name}")
165
- end
166
- end
167
-
168
- describe '#branch' do
169
- it 'should return a string' do
170
- expect(subject.branch).to be_a(String)
171
- end
172
- end
173
-
174
- describe '#default_branch' do
175
- it 'should return a string' do
176
- expect(subject.default_branch).to be_a(String)
177
- end
178
- end
179
-
180
- describe '#template_options' do
181
- let(:template_identifiers) do
182
- {
183
- template_directory: '.github',
184
- nested_directory_name: 'PULL_REQUEST_TEMPLATE',
185
- non_nested_file_name: 'pull_request_template'
186
- }
187
- end
188
-
189
- it 'should return an array' do
190
- expect(subject.template_options(template_identifiers)).to be_a(Array)
191
- end
192
-
193
- it 'should call Dir.glob and File.join' do
194
- expect(Dir).to receive(:glob).and_return(['.github/pull_request_template.md']).at_least(:once)
195
- expect(File).to receive(:join).at_least(:once)
196
- subject.template_options(template_identifiers)
197
- end
198
- end
199
-
200
- describe '#read_template' do
201
- it 'should call File.open' do
202
- expect(File).to receive(:open).and_return(double(read: true))
203
- subject.read_template('.gitignore')
204
- end
205
- end
206
-
207
- describe '#generate_title' do
208
- it 'should return a title based on the branch' do
209
- prefix = Faker::Lorem.word
210
- word1 = Faker::Lorem.word
211
- word2 = Faker::Lorem.word
212
- description = [word1, word2].join('-')
213
- branch = "#{prefix}-123-#{description}"
214
- expect(subject.generate_title(branch)).to eq("#{prefix.upcase}-123 #{[word1.capitalize, word2].join(' ')}")
215
- end
216
-
217
- it 'should return a title based on the branch' do
218
- prefix = Faker::Lorem.word
219
- word1 = Faker::Lorem.word
220
- word2 = Faker::Lorem.word
221
- description = [word1, word2].join('_')
222
- branch = "#{prefix}_123_#{description}"
223
- expect(subject.generate_title(branch)).to eq("#{prefix.upcase}-123 #{[word1.capitalize, word2].join(' ')}")
224
- end
225
-
226
- it 'should return a title based on the branch' do
227
- prefix = Faker::Lorem.word
228
- word1 = Faker::Lorem.word
229
- word2 = Faker::Lorem.word
230
- description = [word1, word2].join('_')
231
- branch = "#{prefix}-123_#{description}"
232
- expect(subject.generate_title(branch)).to eq("#{prefix.upcase}-123 #{[word1.capitalize, word2].join(' ')}")
233
- end
234
-
235
- it 'should return a title based on the branch' do
236
- word1 = Faker::Lorem.word
237
- word2 = Faker::Lorem.word
238
- branch = [word1, word2].join('_')
239
- expect(subject.generate_title(branch)).to eq([word1.capitalize, word2].join(' '))
240
- end
241
-
242
- it 'should return a title based on the branch' do
243
- word1 = Faker::Lorem.word
244
- word2 = Faker::Lorem.word
245
- branch = [word1, word2].join('-')
246
- expect(subject.generate_title(branch)).to eq([word1.capitalize, word2].join(' '))
247
- end
248
-
249
- it 'should return a title based on the branch' do
250
- branch = Faker::Lorem.word
251
- expect(subject.generate_title(branch)).to eq(branch.capitalize)
252
- end
253
-
254
- it 'should return a title based on the branch' do
255
- word1 = Faker::Lorem.word
256
- word2 = Faker::Lorem.word
257
- word3 = Faker::Lorem.word
258
- branch = [word1, word2, word3].join('_')
259
- expect(subject.generate_title(branch)).to eq([word1.capitalize, word2, word3].join(' '))
260
- end
261
-
262
- it 'should return a title based on the branch' do
263
- word1 = Faker::Lorem.word
264
- word2 = Faker::Lorem.word
265
- word3 = Faker::Lorem.word
266
- branch = [word1, word2, word3].join('-')
267
- expect(subject.generate_title(branch)).to eq([word1.capitalize, word2, word3].join(' '))
268
- end
269
- end
270
- end
@@ -1,278 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'git_helper'
5
-
6
- describe GitHelper::GitLabMergeRequest do
7
- let(:local_code) { double(:local_code, read_template: Faker::Lorem.word) }
8
- let(:highline_wrapper) { double(:highline_wrapper) }
9
- let(:gitlab_client) { double(:gitlab_client) }
10
-
11
- let(:merge_request) do
12
- double(:merge_request,
13
- message: Faker::Lorem.sentence,
14
- diff_refs: { base_sha: Faker::Lorem.word, head_sha: Faker::Lorem.word },
15
- web_url: Faker::Internet.url,
16
- merge_commit_sha: Faker::Internet.password)
17
- end
18
-
19
- let(:options) do
20
- {
21
- local_project: Faker::Lorem.word,
22
- local_branch: Faker::Lorem.word,
23
- local_code: local_code,
24
- highline: highline_wrapper
25
- }
26
- end
27
-
28
- subject { described_class.new(options) }
29
-
30
- before do
31
- allow(GitHelper::GitLabClient).to receive(:new).and_return(gitlab_client)
32
- allow(subject).to receive(:puts)
33
- end
34
-
35
- describe '#create' do
36
- it 'should call the gitlab client to create' do
37
- allow(subject).to receive(:squash_merge_request).and_return(true)
38
- allow(subject).to receive(:remove_source_branch).and_return(false)
39
- allow(subject).to receive(:new_mr_body).and_return('')
40
- expect(gitlab_client).to receive(:create_merge_request).and_return(merge_request)
41
- subject.create({ base_branch: Faker::Lorem.word, new_title: Faker::Lorem.word })
42
- end
43
-
44
- it 'should call various other methods' do
45
- expect(subject).to receive(:squash_merge_request).and_return(true)
46
- expect(subject).to receive(:remove_source_branch).and_return(false)
47
- expect(subject).to receive(:new_mr_body).and_return('')
48
- allow(gitlab_client).to receive(:create_merge_request).and_return(merge_request)
49
- subject.create({ base_branch: Faker::Lorem.word, new_title: Faker::Lorem.word })
50
- end
51
-
52
- it 'should catch the raised error if the creation does not work' do
53
- allow(subject).to receive(:squash_merge_request).and_return(true)
54
- allow(subject).to receive(:remove_source_branch).and_return(false)
55
- allow(subject).to receive(:new_mr_body).and_return('')
56
- allow(gitlab_client).to receive(:create_merge_request).and_raise(StandardError)
57
- expect(subject.create({ base_branch: Faker::Lorem.word, new_title: Faker::Lorem.word })).to eq(nil)
58
- end
59
- end
60
-
61
- describe '#merge' do
62
- it 'should call the gitlab client to merge' do
63
- allow(subject).to receive(:existing_mr).and_return(double(should_remove_source_branch: true, squash: false, title: 'title'))
64
- allow(subject).to receive(:mr_id).and_return(Faker::Number.number)
65
- expect(gitlab_client).to receive(:accept_merge_request).and_return(merge_request)
66
- subject.merge
67
- end
68
-
69
- it 'should call various other methods' do
70
- expect(subject).to receive(:existing_mr).and_return(double(should_remove_source_branch: true, squash: false, title: 'title')).at_least(:once)
71
- expect(subject).to receive(:mr_id).and_return(Faker::Number.number).at_least(:once)
72
- allow(gitlab_client).to receive(:accept_merge_request).and_return(merge_request)
73
- subject.merge
74
- end
75
-
76
- it 'should catch the raised error if the merge does not work' do
77
- allow(subject).to receive(:existing_mr).and_return(double(should_remove_source_branch: true, squash: false, title: 'title'))
78
- allow(subject).to receive(:mr_id).and_return(Faker::Number.number)
79
- allow(gitlab_client).to receive(:accept_merge_request).and_raise(StandardError)
80
- expect(subject.merge).to eq(nil)
81
- end
82
-
83
- it 'should try to merge multiple times if the first merge errors' do
84
- allow(subject).to receive(:existing_mr).and_return(double(should_remove_source_branch: true, squash: false, title: 'title'))
85
- allow(subject).to receive(:mr_id).and_return(Faker::Number.number)
86
- expect(gitlab_client).to receive(:accept_merge_request).and_return(double(merge_commit_sha: nil, merge_error: Faker::Lorem.word, message: Faker::Lorem.sentence)).exactly(2).times
87
- expect(subject.merge).to eq(nil)
88
- end
89
- end
90
-
91
- describe '#new_mr_body' do
92
- it 'should call the local code if the template to apply exists' do
93
- allow(subject).to receive(:template_name_to_apply).and_return('')
94
- expect(local_code).to receive(:read_template)
95
- subject.send(:new_mr_body)
96
- end
97
-
98
- it 'should not call the local code if the template is nil' do
99
- allow(subject).to receive(:template_name_to_apply).and_return(nil)
100
- expect(local_code).not_to receive(:read_template)
101
- subject.send(:new_mr_body)
102
- end
103
-
104
- it 'should return an empty string if the template is nil' do
105
- allow(subject).to receive(:template_name_to_apply).and_return(nil)
106
- expect(subject.send(:new_mr_body)).to eq('')
107
- end
108
- end
109
-
110
- describe '#template_name_to_apply' do
111
- context 'if MR template options are empty' do
112
- it 'should return nil' do
113
- allow(subject).to receive(:mr_template_options).and_return([])
114
- expect(subject.send(:template_name_to_apply)).to eq(nil)
115
- end
116
- end
117
-
118
- context 'if there is one template option' do
119
- let(:template) { Faker::Lorem.word }
120
-
121
- it 'should call the CLI to ask about a single template' do
122
- allow(subject).to receive(:mr_template_options).and_return([template])
123
- expect(highline_wrapper).to receive(:ask_yes_no).and_return(true)
124
- subject.send(:template_name_to_apply)
125
- end
126
-
127
- it 'should return the single template if the user says yes' do
128
- allow(subject).to receive(:mr_template_options).and_return([template])
129
- allow(highline_wrapper).to receive(:ask_yes_no).and_return(true)
130
- expect(subject.send(:template_name_to_apply)).to eq(template)
131
- end
132
-
133
- it 'should return nil if the user says no' do
134
- allow(subject).to receive(:mr_template_options).and_return([template])
135
- allow(highline_wrapper).to receive(:ask_yes_no).and_return(false)
136
- expect(subject.send(:template_name_to_apply)).to eq(nil)
137
- end
138
- end
139
-
140
- context 'if there are multiple template options' do
141
- let(:template1) { Faker::Lorem.word }
142
- let(:template2) { Faker::Lorem.word }
143
-
144
- it 'should call the CLI to ask which of multiple templates to apply' do
145
- allow(subject).to receive(:mr_template_options).and_return([template1, template2])
146
- expect(highline_wrapper).to receive(:ask_options).and_return(template1)
147
- subject.send(:template_name_to_apply)
148
- end
149
-
150
- it 'should return the answer template if the user says yes' do
151
- allow(subject).to receive(:mr_template_options).and_return([template1, template2])
152
- allow(highline_wrapper).to receive(:ask_options).and_return(template1)
153
- expect(subject.send(:template_name_to_apply)).to eq(template1)
154
- end
155
-
156
- it 'should return nil if the user says no' do
157
- allow(subject).to receive(:mr_template_options).and_return([template1, template2])
158
- allow(highline_wrapper).to receive(:ask_options).and_return('None')
159
- expect(subject.send(:template_name_to_apply)).to eq(nil)
160
- end
161
- end
162
- end
163
-
164
- describe '#mr_template_options' do
165
- it 'should call the local code' do
166
- expect(local_code).to receive(:template_options)
167
- subject.send(:mr_template_options)
168
- end
169
- end
170
-
171
- describe '#mr_id' do
172
- it 'should ask the CLI for the code request ID' do
173
- expect(highline_wrapper).to receive(:ask).and_return(Faker::Number.number)
174
- subject.send(:mr_id)
175
- end
176
-
177
- it 'should equal an integer' do
178
- pr_id = Faker::Number.number
179
- expect(highline_wrapper).to receive(:ask).and_return(pr_id)
180
- expect(subject.send(:mr_id)).to eq(pr_id)
181
- end
182
- end
183
-
184
- describe '#squash_merge_request' do
185
- let(:existing_project) { double(squash_option: 'default_off') }
186
-
187
- it 'should return true if the squash is set to always on the project' do
188
- allow(subject).to receive(:existing_project).and_return(existing_project)
189
- allow(existing_project).to receive(:squash_option).and_return('always')
190
- expect(highline_wrapper).not_to receive(:ask_yes_no)
191
- expect(subject.send(:squash_merge_request)).to eq(true)
192
- end
193
-
194
- it 'should return true if the squash is set to default_on on the project' do
195
- allow(subject).to receive(:existing_project).and_return(existing_project)
196
- allow(existing_project).to receive(:squash_option).and_return('default_on')
197
- expect(highline_wrapper).not_to receive(:ask_yes_no)
198
- expect(subject.send(:squash_merge_request)).to eq(true)
199
- end
200
-
201
- it 'should return false if the squash is set to never on the project' do
202
- allow(subject).to receive(:existing_project).and_return(existing_project)
203
- allow(existing_project).to receive(:squash_option).and_return('never')
204
- expect(highline_wrapper).not_to receive(:ask_yes_no)
205
- expect(subject.send(:squash_merge_request)).to eq(false)
206
- end
207
-
208
- it 'should ask the user for their response to the squash question' do
209
- allow(subject).to receive(:existing_project).and_return(existing_project)
210
- allow(existing_project).to receive(:squash_option).and_return(nil)
211
- expect(highline_wrapper).to receive(:ask_yes_no).and_return(true)
212
- subject.send(:squash_merge_request)
213
- end
214
-
215
- it 'should be a boolean' do
216
- allow(subject).to receive(:existing_project).and_return(existing_project)
217
- allow(existing_project).to receive(:squash_option).and_return(nil)
218
- expect(highline_wrapper).to receive(:ask_yes_no).and_return(false)
219
- expect([true, false]).to include(subject.send(:squash_merge_request))
220
- end
221
- end
222
-
223
- describe '#remove_source_branch' do
224
- before do
225
- allow(subject).to receive(:existing_project).and_return(double(remove_source_branch_after_merge: nil))
226
- end
227
-
228
- context 'when the existing project has no setting' do
229
- it 'should ask the CLI for the code request ID' do
230
- expect(highline_wrapper).to receive(:ask_yes_no).and_return(true)
231
- subject.send(:remove_source_branch)
232
- end
233
-
234
- it 'should be a boolean' do
235
- allow(highline_wrapper).to receive(:ask_yes_no).and_return(false)
236
- expect([true, false]).to include(subject.send(:remove_source_branch))
237
- end
238
- end
239
-
240
- it 'should ask the existing project' do
241
- expect(subject).to receive(:existing_project).and_return(double(remove_source_branch_after_merge: true))
242
- subject.send(:remove_source_branch)
243
- end
244
-
245
- it 'should return the existing projects setting if it exists' do
246
- allow(subject).to receive(:existing_project).and_return(double(remove_source_branch_after_merge: true))
247
- expect(subject.send(:remove_source_branch)).to eq(true)
248
- end
249
-
250
- it 'should return the existing projects setting if it exists' do
251
- allow(subject).to receive(:existing_project).and_return(double(remove_source_branch_after_merge: false))
252
- allow(highline_wrapper).to receive(:ask_yes_no).and_return(true)
253
- expect(subject.send(:remove_source_branch)).to eq(true)
254
- end
255
- end
256
-
257
- describe '#existing_project' do
258
- it 'should call the gitlab client' do
259
- expect(gitlab_client).to receive(:project).and_return(:project)
260
- subject.send(:existing_project)
261
- end
262
- end
263
-
264
- describe '#existing_mr' do
265
- it 'should call the gitlab client' do
266
- allow(highline_wrapper).to receive(:ask).and_return(Faker::Number.number)
267
- expect(gitlab_client).to receive(:merge_request).and_return(:merge_request)
268
- subject.send(:existing_mr)
269
- end
270
- end
271
-
272
- describe '#gitlab_client' do
273
- it 'should call the gitlab client' do
274
- expect(GitHelper::GitLabClient).to receive(:new).and_return(gitlab_client)
275
- subject.send(:gitlab_client)
276
- end
277
- end
278
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'git_helper'
5
-
6
- describe GitHelper::NewBranch do
7
- let(:new_branch_name) { 'new-branch-name' }
8
- let(:local_code) { double(:local_code, new_branch: :commit) }
9
- let(:highline) { double(:highline_wrapper, ask: new_branch_name) }
10
-
11
- subject { GitHelper::NewBranch.new }
12
-
13
- before do
14
- allow(GitHelper::LocalCode).to receive(:new).and_return(local_code)
15
- allow(HighlineWrapper).to receive(:new).and_return(highline)
16
- allow(subject).to receive(:puts)
17
- end
18
-
19
- it 'should call GitHelper::LocalCode' do
20
- expect(GitHelper::LocalCode).to receive(:new).and_return(local_code)
21
- subject.execute
22
- end
23
-
24
- it 'should call the new_branch method from the local code class' do
25
- expect(local_code).to receive(:new_branch)
26
- subject.execute
27
- end
28
-
29
- context 'when no branch name is passed in' do
30
- it 'should call the highline client' do
31
- expect(HighlineWrapper).to receive(:new).and_return(highline)
32
- subject.execute
33
- end
34
-
35
- it 'should ask the highline client what the new branch name should be' do
36
- expect(highline).to receive(:ask)
37
- subject.execute
38
- end
39
- end
40
-
41
- context 'when there is a branch name passed in' do
42
- it 'should not create a highline_wrapper client' do
43
- expect(HighlineWrapper).not_to receive(:new)
44
- subject.execute(new_branch_name)
45
- end
46
- end
47
- end