Skip to content

Commit 36b1a02

Browse files
committed
Further cleanup of git specs. Moved branch related tests to branch_spec.rb
1 parent 3ffdb8e commit 36b1a02

3 files changed

Lines changed: 111 additions & 118 deletions

File tree

‎spec/code/branch_spec.rb‎

Lines changed: 78 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,22 @@ module Code
77

88
describe Branch do
99

10-
def setup_test_repo(options = {})
11-
12-
defaults = {
13-
name: "test_repo",
14-
origins: [],
15-
branches: []
16-
}
17-
18-
options = defaults.merge(options)
19-
20-
repo_path = Dir.mktmpdir(options[:name])
21-
Dir.chdir repo_path
22-
23-
System.call "init"
24-
System.call "config user.email \"test@hotmail.com\""
25-
System.call "config user.name \"Chuck Norris\""
26-
System.exec "touch README"
27-
System.call "add -A"
28-
System.call "commit -m \"initial commit\""
29-
30-
options[:origins].each do |origin|
31-
System.call "remote add #{origin.name} #{origin.url}"
32-
end
33-
34-
options[:branches].each do |branch|
35-
System.call "branch #{branch}"
36-
end
37-
end
38-
39-
def checkout_branch(name)
40-
System.call("checkout #{name}")
10+
before(:all) do
11+
Branch.send :include, GitExtras
4112
end
4213

4314
before do
4415
allow(System).to receive(:puts)
16+
Branch.setup_test_repo
4517
end
4618

4719
describe "self.all_names" do
48-
it "returns names of all branches in the repo as strings" do
49-
setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"])
5020

21+
before do
22+
Branch.add_branches(["test-branch-1", "test-branch-2"])
23+
end
24+
25+
it "returns names of all branches in the repo as strings" do
5126
branch_names = Branch.all_names
5227

5328
expect(branch_names.length).to eq 3
@@ -58,8 +33,12 @@ def checkout_branch(name)
5833
end
5934

6035
describe "self.all" do
36+
37+
before do
38+
Branch.add_branches(["test-branch-1", "test-branch-2"])
39+
end
40+
6141
it "returns Branch instances for all branches in the repo" do
62-
setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"])
6342

6443
branches = Branch.all
6544

@@ -85,45 +64,88 @@ def checkout_branch(name)
8564
end
8665

8766
describe "self.matching" do
88-
it "finds the branch matching the patterns" do
89-
setup_test_repo(name: "test_repo", branches: ["test-branch-1"])
9067

91-
expect(Branch.matching('test', 'branch', '1').name).to eq 'test-branch-1'
68+
before do
69+
Branch.add_branches(["test-branch-1", "test-branch-2"])
9270
end
9371

9472
it "returns the first branch that matches the patterns" do
95-
setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"])
96-
9773
expect(Branch.matching('test', 'branch').name).to eq 'test-branch-1'
74+
expect(Branch.matching('test', '2').name).to eq 'test-branch-2'
9875
end
9976
end
10077

10178
describe "self.exists?" do
102-
it "returns true for a localy known branch, false otherwise" do
103-
setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"])
10479

80+
before do
81+
Branch.add_branches(["test-branch-1", "test-branch-2"])
82+
end
83+
84+
it "returns true for a localy known branch, false otherwise" do
10585
expect(Branch.exists? "master").to eq true
10686
expect(Branch.exists? "test-branch-1").to eq true
10787
expect(Branch.exists? "test-branch-2").to eq true
10888
expect(Branch.exists? "test-branch-3").to eq false
10989
end
11090
end
11191

112-
#TODO: "self.merged"
113-
11492
describe "self.current" do
115-
it "returns a Branch instance for the current git branch" do
116-
setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"])
93+
let(:branch) { Branch.current }
94+
95+
subject { branch }
96+
97+
it "should currently be on the master branch" do
98+
expect(branch.name).to eq 'master'
99+
end
100+
101+
context "when creating a new branch" do
102+
103+
before do
104+
Branch.create "test_branch"
105+
end
106+
107+
it "should have the right branches" do
108+
expect(Branch.all.count).to eq 2
109+
end
110+
111+
it "should have created the branch" do
112+
expect(Branch.new("test_branch")).to exist
113+
end
117114

118-
current_branch = Branch.current
119-
expect(current_branch).to be_a Branch
120-
expect(current_branch.name).to eq "master"
115+
context "when deleting a branch" do
116+
before do
117+
Branch.matching("test_branch").delete!
118+
end
121119

122-
checkout_branch "test-branch-1"
120+
it "should only have one branch left" do
121+
expect(Branch.all.count).to eq 1
122+
end
123123

124-
current_branch = Branch.current
125-
expect(current_branch).to be_a Branch
126-
expect(current_branch.name).to eq "test-branch-1"
124+
it "should have deleted the branch" do
125+
expect(Branch.new("test_branch")).to_not exist
126+
end
127+
end
128+
129+
context "when checking out a branch" do
130+
before do
131+
Branch.matching('test_branch').checkout
132+
end
133+
134+
it "should have changed the branch" do
135+
expect(Branch.current.name).to eq 'test_branch'
136+
end
137+
end
138+
end
139+
140+
context "when trying to delete a protected branch" do
141+
142+
before do
143+
Branch.create "development"
144+
end
145+
146+
it "should now be allowed" do
147+
expect { Branch.matching("development").delete! }.to raise_error Branch::ProtectedBranchError
148+
end
127149
end
128150
end
129151

@@ -200,6 +222,10 @@ def checkout_branch(name)
200222

201223
describe "#exists" do
202224

225+
before do
226+
Branch.add_branches(["test-branch-1", "test-branch-2"])
227+
end
228+
203229
it "calls System.result and returns the result" do
204230
test_branch_1 = Branch.new "test-branch-1"
205231

@@ -212,8 +238,6 @@ def checkout_branch(name)
212238
end
213239

214240
it "returns true if the branch with the provided instance name exists in the git repo" do
215-
setup_test_repo(name: "test_repo", branches: ["test-branch-1", "test-branch-2"])
216-
217241
test_branch_1 = Branch.new "test-branch-1"
218242
test_branch_2 = Branch.new "test-branch-2"
219243
test_branch_3 = Branch.new "test-branch-3"
@@ -540,7 +564,6 @@ def checkout_branch(name)
540564
expect(pull_request_b).to receive(:add_label).with("label")
541565

542566
expect { random_branch.send(:label_prs, "label") }.not_to raise_error
543-
544567
end
545568
end
546569
end

‎spec/code/git_spec.rb‎

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -21,69 +21,6 @@ module Code
2121
Git.setup_test_repo
2222
end
2323

24-
describe "#current_branch" do
25-
let(:branch) { Branch.current }
26-
27-
subject { branch }
28-
29-
it "should currently be on the master branch" do
30-
expect(branch.name).to eq 'master'
31-
end
32-
33-
context "when creating a new branch" do
34-
35-
before do
36-
Branch.create "test_branch"
37-
end
38-
39-
it "should have the right branches" do
40-
expect(Branch.all.count).to eq 2
41-
end
42-
43-
it "should have created the branch" do
44-
expect(Branch.new("test_branch")).to exist
45-
end
46-
47-
context "when deleting a branch" do
48-
before do
49-
Branch.matching("test_branch").delete!
50-
end
51-
52-
it "should only have one branch left" do
53-
expect(Branch.all.count).to eq 1
54-
end
55-
56-
it "should have deleted the branch" do
57-
expect(Branch.new("test_branch")).to_not exist
58-
end
59-
end
60-
61-
context "when checking out a branch" do
62-
before do
63-
Branch.matching('test_branch').checkout
64-
end
65-
66-
it "should have changed the branch" do
67-
expect(Branch.current.name).to eq 'test_branch'
68-
end
69-
end
70-
71-
end
72-
73-
context "when trying to delete a protected branch" do
74-
75-
before do
76-
Branch.create "development"
77-
end
78-
79-
it "should now be allowed" do
80-
expect { Branch.matching("development").delete! }.to raise_error Branch::ProtectedBranchError
81-
end
82-
83-
end
84-
85-
end
86-
8724
describe "#search" do
8825

8926
before do
@@ -176,6 +113,29 @@ module Code
176113
end
177114
end
178115

116+
describe "#switch" do
117+
it "should switch to the first branch (alphabetically) matching the provided pattern" do
118+
Branch.create("some-other-branch")
119+
Branch.create("some-random-branch")
120+
121+
expect(System).to receive(:call).with("checkout some-other-branch")
122+
git.switch("some")
123+
124+
expect(System).to receive(:call).with("checkout some-random-branch")
125+
git.switch("some", "random")
126+
127+
expect(System).to receive(:call).with("checkout some-other-branch")
128+
git.switch("some", "other")
129+
130+
expect(System).to receive(:call).with("checkout some-other-branch")
131+
git.switch("some", "branch")
132+
end
133+
134+
it "should raise error if no matching branch was found" do
135+
expect{ git.switch("some") }.to raise_error
136+
end
137+
end
138+
179139
describe "#finish" do
180140
it "should switch to development, perform a pull and delete the previous branch" do
181141
branch = Branch.create("test-branch")

‎spec/support/git_extras.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ def setup_test_repo
2121
System.call 'commit -m "initial commit"'
2222
System.call "remote add origin #{test_repo_origin}"
2323
end
24+
25+
def checkout_branch(name)
26+
System.call "checkout #{name}"
27+
end
28+
29+
def add_branches(names)
30+
names.each do |name|
31+
System.call "branch #{name}"
32+
end
33+
end
2434
end
2535

2636
def self.included(base)

0 commit comments

Comments
 (0)