Skip to content

Commit f894089

Browse files
committed
Cleanup Branch specs
1 parent 36b1a02 commit f894089

2 files changed

Lines changed: 39 additions & 128 deletions

File tree

‎lib/code/branch.rb‎

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class Branch
1313
DEVELOPMENT_BRANCH_NAME = 'development'
1414
PROTECTED_BRANCH_NAMES = %w{master development}
1515

16-
1716
def self.all_names
1817
System.result('git branch').strip.lines.map { |line| line.gsub(/\s|\*/, '') }
1918
end
@@ -22,10 +21,15 @@ def self.all
2221
all_names.map { |name| new(name) }
2322
end
2423

24+
25+
# TODO: This should probably be moved to another class or module.
26+
# An identical method is used in match_scorer/base
2527
def self.coerce_patterns(*patterns)
2628
patterns.flatten.join(' ').split(' ')
2729
end
2830

31+
private_class_method :coerce_patterns
32+
2933
def self.matching(*patterns)
3034
patterns = coerce_patterns(patterns)
3135
all.find { |b| b.matches? *patterns }
@@ -124,10 +128,6 @@ def checkout
124128
self
125129
end
126130

127-
def authorize_delete!
128-
raise ProtectedBranchError, "The #{name} branch is protected" if protected?
129-
end
130-
131131
def message
132132
name.gsub('-', ' ').capitalize
133133
end
@@ -152,24 +152,12 @@ def mark_prs_as_hotfix
152152
label_prs('hotfix')
153153
end
154154

155-
def pull_request
156-
pull_requests.first
157-
end
158-
159-
def pull_requests
160-
@pull_request ||= PullRequest.for_branch(self)
161-
end
162-
163155
def pull_request_url
164156
# This could be a delegate, but it seems easier to read just by
165157
# calling it.
166158
pull_request.url
167159
end
168160

169-
def ensure_public!
170-
raise PrivateBranchError, "#{name} is a private branch" if private?
171-
end
172-
173161
attr_reader :name
174162
alias_method :to_s, :name
175163

@@ -183,5 +171,20 @@ def label_prs(label)
183171
end
184172
end
185173

174+
def authorize_delete!
175+
raise ProtectedBranchError, "The #{name} branch is protected" if protected?
176+
end
177+
178+
def pull_request
179+
pull_requests.first
180+
end
181+
182+
def pull_requests
183+
@pull_request ||= PullRequest.for_branch(self)
184+
end
185+
186+
def ensure_public!
187+
raise PrivateBranchError, "#{name} is a private branch" if private?
188+
end
186189
end
187190
end

‎spec/code/branch_spec.rb‎

Lines changed: 19 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ module Code
5252
end
5353
end
5454

55-
describe "self.coerce_patterns" do
56-
it "returns an array consisting of provided patterns" do
57-
patterns = Branch.coerce_patterns 'abc', 'def', '123'
58-
59-
expect(patterns.length).to eq 3
60-
expect(patterns[0]).to eq 'abc'
61-
expect(patterns[1]).to eq 'def'
62-
expect(patterns[2]).to eq '123'
63-
end
64-
end
65-
6655
describe "self.matching" do
6756

6857
before do
@@ -180,7 +169,7 @@ module Code
180169

181170
describe "self.find" do
182171
it "creates a new Branch instance with specified name, but makes no System.call" do
183-
expect(System).not_to receive(:call)
172+
expect(System).not_to receive(:call).with("branch test_branch")
184173

185174
branch = Branch.find "test_branch"
186175

@@ -189,7 +178,7 @@ module Code
189178
end
190179
end
191180

192-
describe "#==" do
181+
describe "equality comparer" do
193182
it "compares by branch name" do
194183
branch_a = Branch.new "branch-a"
195184
branch_b = Branch.new "branch-b"
@@ -362,15 +351,6 @@ module Code
362351
end
363352

364353
describe "#push" do
365-
it "checks if the branch is public before pushing it" do
366-
random_branch = Branch.new "random"
367-
368-
expect(random_branch).to receive(:ensure_public!).and_return(true)
369-
allow(System).to receive(:call)
370-
371-
random_branch.push
372-
end
373-
374354
it "makes a system call for 'push origin branch-name:branch-name'" do
375355
random_branch = Branch.new "random"
376356
allow(random_branch).to receive(:ensure_public!).and_return(true)
@@ -381,7 +361,7 @@ module Code
381361

382362
it "allows pushing of a public branch" do
383363
random_branch = Branch.new "random"
384-
allow(System).to receive(:call)
364+
allow(System).to receive(:call).with("push origin random:random")
385365
expect { random_branch.push }.not_to raise_error
386366
end
387367

@@ -413,22 +393,6 @@ module Code
413393
end
414394
end
415395

416-
describe "#authorize_delete!" do
417-
it "raises a ProtectedBranchError for protected branches" do
418-
random_branch = Branch.new "random"
419-
420-
expect(random_branch).to receive(:protected?).and_return(true)
421-
expect { random_branch.authorize_delete!}.to raise_error Branch::ProtectedBranchError
422-
end
423-
424-
it "doesn't raise an error for unprotected branches" do
425-
random_branch = Branch.new "random"
426-
427-
expect(random_branch).to receive(:protected?).and_return(false)
428-
expect {random_branch.authorize_delete!}.not_to raise_error
429-
end
430-
end
431-
432396
describe "#message" do
433397
it "returns the capitalized branch name, where dashes are replaced with spaces" do
434398
random_branch = Branch.new "random"
@@ -457,12 +421,17 @@ module Code
457421
end
458422

459423
describe "#has_pull_request?" do
460-
it "returns true if #pull_request is not nil, false otherwise" do
461-
random_branch = Branch.new "random"
462-
expect(random_branch).to receive(:pull_request).and_return("something")
463-
expect(random_branch.has_pull_request?).to eq true
464-
expect(random_branch).to receive(:pull_request).and_return(nil)
465-
expect(random_branch.has_pull_request?).to eq false
424+
it "returns true if the branch has any pull requests, false otherwise" do
425+
random_branch_1 = Branch.new "random_1"
426+
pull_request_1 = PullRequest.new(pull_request_info: { html_url: "example.com" })
427+
pull_request_2 = PullRequest.new(pull_request_info: { html_url: "example2.com" })
428+
429+
expect(PullRequest).to receive(:for_branch).with(random_branch_1).and_return([pull_request_1, pull_request_2])
430+
expect(random_branch_1.has_pull_request?).to eq true
431+
432+
random_branch_2 = Branch.new "random_2"
433+
expect(PullRequest).to receive(:for_branch).with(random_branch_2).and_return([])
434+
expect(random_branch_2.has_pull_request?).to eq false
466435
end
467436
end
468437

@@ -494,76 +463,15 @@ module Code
494463
end
495464
end
496465

497-
describe "#pull_request" do
498-
it "returns the first item in #pull_requests" do
499-
random_branch = Branch.new "random"
500-
501-
expect(random_branch).to receive(:pull_requests).and_return(["first", "second"])
502-
expect(random_branch.pull_request).to eq "first"
503-
end
504-
505-
it "returns nil if #pull_requests is empty" do
506-
random_branch = Branch.new "random"
507-
508-
expect(random_branch).to receive(:pull_requests).and_return([])
509-
expect(random_branch.pull_request).to eq nil
510-
end
511-
end
512-
513-
describe "#pull_requests" do
514-
it "calls 'PullRequest.for_branch'" do
515-
random_branch = Branch.new "random"
516-
pull_requests = [ Code::PullRequest.new(pull_request_info: {}) ]
517-
518-
expect(PullRequest).to receive(:for_branch).with(random_branch).and_return(pull_requests)
519-
expect(random_branch.pull_requests).to eq pull_requests
520-
end
521-
end
522-
523466
describe "#pull_request_url" do
524-
it "returns #pull_request.url" do
467+
it "returns the URL of the first pull request" do
525468
random_branch = Branch.new "random"
526-
pull_request = Code::PullRequest.new(pull_request_info: { html_url: "example.com" })
469+
pull_request_1 = PullRequest.new(pull_request_info: { html_url: "example.com" })
470+
pull_request_2 = PullRequest.new(pull_request_info: { html_url: "example2.com" })
527471

528-
expect(random_branch).to receive(:pull_request).and_return(pull_request)
529-
expect(random_branch.pull_request_url).to eq "example.com"
530-
end
531-
end
532-
533-
describe "#ensure_public!" do
534-
it "raises error if branch is private" do
535-
random_branch = Branch.new "random-local"
536-
537-
expect { random_branch.ensure_public! }.to raise_error Branch::PrivateBranchError
538-
end
472+
expect(PullRequest).to receive(:for_branch).and_return([pull_request_1, pull_request_2])
539473

540-
it "raises no error if branch is public" do
541-
random_branch = Branch.new "random"
542-
543-
expect { random_branch.ensure_public! }.not_to raise_error
544-
end
545-
end
546-
547-
describe "#label_prs" do
548-
it "rases a NoPRError if there are no PRs" do
549-
random_branch = Branch.new "random"
550-
551-
expect(random_branch).to receive(:has_pull_request?).and_return(false)
552-
expect { random_branch.send(:label_prs, "label") }.to raise_error Branch::NoPRError
553-
end
554-
555-
it "calls PullRequest#add_label for each pull_request" do
556-
random_branch = Branch.new "random"
557-
558-
pull_request_a = Code::PullRequest.new(pull_request_info: {})
559-
pull_request_b = Code::PullRequest.new(pull_request_info: {})
560-
561-
expect(random_branch).to receive(:pull_requests).and_return([pull_request_a, pull_request_b]).twice
562-
563-
expect(pull_request_a).to receive(:add_label).with("label")
564-
expect(pull_request_b).to receive(:add_label).with("label")
565-
566-
expect { random_branch.send(:label_prs, "label") }.not_to raise_error
474+
expect(random_branch.pull_request_url).to eq "example.com"
567475
end
568476
end
569477
end

0 commit comments

Comments
 (0)