@@ -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
0 commit comments