webmat-git_remote_branch 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ module ShouldaUnitHelpers
2
+ def self.included(base)
3
+ base.extend ClassMethods
4
+ add_param_checkers(base)
5
+ end
6
+
7
+ def self.add_param_checkers(base)
8
+ # Excuse my french but:
9
+ %w(action branch origin current_branch silent explain).each do |param|
10
+ base.instance_eval(%Q!
11
+ def self.should_set_#{param}_to(#{param}_value)
12
+ should "set #{param} to #{ '#{' }#{ param }_value}" do
13
+ assert_equal #{param}_value, @p[:#{param}]
14
+ end
15
+ end
16
+ !)
17
+ end
18
+ # In other words, create a bunch of helpers like:
19
+ #
20
+ # def self.should_set_explain_to(explain_value)
21
+ # should "set explain to #{explain_value}" do
22
+ # assert_equal explain_value, @p[:explain]
23
+ # end
24
+ # end
25
+
26
+ end
27
+
28
+ module ClassMethods
29
+ def should_return_help_for_parameters(params, context_explanation)
30
+ context context_explanation do
31
+ setup do
32
+ @p = grb.read_params params
33
+ end
34
+
35
+ should "not even get to checking the current_branch" do
36
+ grb.expects(:get_current_branch).never
37
+ grb.read_params ['help']
38
+ end
39
+
40
+ should "only return a hash specifying the action" do
41
+ assert_array_content [:action], @p.keys
42
+ end
43
+
44
+ should_set_action_to :help
45
+ end
46
+ end
47
+
48
+ def should_explain_with_current_branch(current_branch_value, current_branch_explanation)
49
+ context "on an 'explain' command" do
50
+ context "with no information provided other than the action" do
51
+ setup do
52
+ @p = grb.read_params %w{explain create}
53
+ end
54
+
55
+ should_set_explain_to true
56
+ should_set_action_to :create
57
+ should_set_origin_to 'origin'
58
+
59
+ context current_branch_explanation do
60
+ should_set_current_branch_to current_branch_value
61
+ end
62
+
63
+ should "set a dummy new branch name" do
64
+ assert @p[:branch]
65
+ end
66
+ end
67
+
68
+ context "with all information provided" do
69
+ setup do
70
+ @p = grb.read_params %w{explain create specific_branch specific_origin}
71
+ end
72
+
73
+ should_set_explain_to true
74
+ should_set_action_to :create
75
+ should_set_current_branch_to current_branch_value
76
+
77
+ should "set the origin to 'specific_origin'" do
78
+ assert_equal 'specific_origin', @p[:origin]
79
+ end
80
+
81
+ should "set the specified branch name" do
82
+ assert_equal 'specific_branch', @p[:branch]
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,38 @@
1
+ require 'fileutils'
2
+ require 'tmpdir'
3
+
4
+ class TempDirHelper
5
+ include FileUtils
6
+
7
+ attr_reader :directory
8
+
9
+ def initialize(namespace='temp_dir_helper')
10
+ @directory = get_temp_dir!(namespace)
11
+ end
12
+
13
+ def cleanup
14
+ rm_rf @directory
15
+ end
16
+
17
+ def to_s
18
+ directory
19
+ end
20
+
21
+ private
22
+ def get_temp_dir!(namespace='')
23
+ wd = File.expand_path( File.join( Dir::tmpdir, namespace) )
24
+ mkdir wd unless File.exists? wd
25
+
26
+ #Create new subdir with a random name
27
+ new_dir=''
28
+ begin
29
+ new_dir = File.join( wd, "#{rand(10000)}" )
30
+ mkdir new_dir
31
+
32
+ rescue
33
+ retry
34
+ end
35
+
36
+ new_dir
37
+ end
38
+ end
data/test/test_helper.rb CHANGED
@@ -3,19 +3,27 @@ require 'test/unit'
3
3
 
4
4
  test_dir = File.dirname(__FILE__)
5
5
 
6
- require 'redgreen'
6
+ # Install the non-Rails shoulda gem with 'gem install Shoulda'
7
+ # Notice the capitalization in the name.
8
+ require 'shoulda'
9
+ require 'mocha'
10
+
11
+ # Just load redgreen if not running tests from TextMate
12
+ IN_TM = !ENV['TM_DIRECTORY'].nil? unless defined?(IN_TM)
13
+ require 'redgreen' unless IN_TM
14
+
7
15
  require 'ruby-debug'
8
16
 
9
- require File.join(test_dir, 'git_helper')
10
17
  require File.join( [test_dir] + %w{ .. lib git_remote_branch} )
11
18
 
19
+ Dir[test_dir+'/helpers/**/*.rb'].each{|f| require f}
20
+
12
21
  class Test::Unit::TestCase
13
- include GitRemoteBranch
22
+ include MoreAssertions
14
23
 
15
- # Passes assertion if condition is false
16
- def assert_false(condition, message = nil)
17
- message = "assert_false failed" unless message
18
- assert condition == false, message
24
+ attr_reader :grb
25
+ def setup
26
+ @grb = Object.new
27
+ @grb.send :extend, GitRemoteBranch
19
28
  end
20
-
21
29
  end
@@ -0,0 +1,39 @@
1
+ require File.join( File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ class GitRemoteBranchTest < Test::Unit::TestCase
4
+ context 'help' do
5
+ should 'contain examples for all basic commands' do
6
+ GitRemoteBranch::COMMANDS.keys.each do |k|
7
+ assert_match "grb #{k} branch_name", grb.get_usage
8
+ end
9
+ end
10
+
11
+ should 'contain an example for explain' do
12
+ assert_match 'grb explain', grb.get_usage
13
+ end
14
+
15
+ should 'contain an enumeration of all aliases' do
16
+ GitRemoteBranch::COMMANDS.each_pair do |k,v|
17
+ assert_match "#{k}: #{v[:aliases].join(', ')}", grb.get_usage
18
+ end
19
+ end
20
+ end
21
+
22
+ context "the reverse mapping for aliases" do
23
+ GitRemoteBranch::COMMANDS.each_pair do |cmd, params|
24
+ params[:aliases].each do |alias_|
25
+ should "contain the alias #{alias_}" do
26
+ assert GitRemoteBranch::ALIAS_REVERSE_MAP[alias_]
27
+ end
28
+ end
29
+ end
30
+
31
+ context "upon creation" do
32
+ should "raise an exception when there are duplicates" do
33
+ assert_raise(RuntimeError) do
34
+ GitRemoteBranch.get_reverse_map( GitRemoteBranch::COMMANDS.merge(:new_command => {:aliases => ['create']}) )
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,261 @@
1
+ require File.join( File.dirname(__FILE__), '..', 'test_helper')
2
+
3
+ REGULAR_BRANCH_LISTING = <<-STR
4
+ other_user/master
5
+ * stubbed_current_branch
6
+ rubyforge
7
+ STR
8
+
9
+ BRANCH_LISTING_WHEN_NOT_ON_BRANCH = <<-STR
10
+ * (no branch)
11
+ other_user/master
12
+ master
13
+ rubyforge
14
+ STR
15
+
16
+ WHEN_NOT_ON_GIT_REPOSITORY = "fatal: Not a git repository\n"
17
+
18
+ class ParamReaderTest < Test::Unit::TestCase
19
+ include ShouldaUnitHelpers
20
+
21
+ context 'read_params' do
22
+ context "when on a valid branch" do
23
+ setup do
24
+ grb.stubs(:capture_process_output).returns([0, REGULAR_BRANCH_LISTING])
25
+ end
26
+
27
+ context "on a normal valid command without an origin" do
28
+ setup do
29
+ @p = grb.read_params %w{create the_branch}
30
+ end
31
+
32
+ should_set_action_to :create
33
+ should_set_branch_to 'the_branch'
34
+ should_set_origin_to 'origin'
35
+ should_set_current_branch_to 'stubbed_current_branch'
36
+ should_set_explain_to false
37
+ should_set_silent_to false
38
+ end
39
+
40
+ context "on a normal valid command" do
41
+ setup do
42
+ @p = grb.read_params %w{create the_branch the_origin}
43
+ end
44
+
45
+ should_set_action_to :create
46
+ should_set_branch_to 'the_branch'
47
+ should_set_origin_to 'the_origin'
48
+ should_set_current_branch_to 'stubbed_current_branch'
49
+ should_set_explain_to false
50
+ should_set_silent_to false
51
+ end
52
+
53
+ should_explain_with_current_branch 'stubbed_current_branch', "use real current branch"
54
+
55
+ should_return_help_for_parameters %w(help), "on a 'help' command"
56
+ should_return_help_for_parameters %w(create), "on an incomplete command"
57
+ should_return_help_for_parameters %w(decombobulate something), "on an invalid command"
58
+
59
+ context "understands the --silent parameter" do
60
+ context "at the beginning" do
61
+ setup do
62
+ @p = grb.read_params %w{--silent create some_branch some_origin}
63
+ end
64
+ should_set_silent_to true
65
+ should_set_action_to :create
66
+ should_set_branch_to 'some_branch'
67
+ should_set_origin_to 'some_origin'
68
+ end
69
+
70
+ context "at the end" do
71
+ setup do
72
+ @p = grb.read_params %w{create some_branch some_origin --silent}
73
+ end
74
+ should_set_silent_to true
75
+ should_set_action_to :create
76
+ should_set_branch_to 'some_branch'
77
+ should_set_origin_to 'some_origin'
78
+ end
79
+
80
+ context "in the freakin' middle" do
81
+ setup do
82
+ @p = grb.read_params %w{create --silent some_branch some_origin}
83
+ end
84
+ should_set_silent_to true
85
+ should_set_action_to :create
86
+ should_set_branch_to 'some_branch'
87
+ should_set_origin_to 'some_origin'
88
+ end
89
+ end
90
+ end
91
+
92
+ context "when on an invalid branch" do
93
+ setup do
94
+ grb.stubs(:capture_process_output).returns([0, BRANCH_LISTING_WHEN_NOT_ON_BRANCH])
95
+ end
96
+
97
+ GitRemoteBranch::COMMANDS.each_key do |action|
98
+ context "running the '#{action}' command" do
99
+ setup do
100
+ @command = [action.to_s, 'branch_name']
101
+ end
102
+
103
+ context "raising an exception" do
104
+ setup do
105
+ begin
106
+ grb.read_params(@command)
107
+ rescue => @ex
108
+ end
109
+ end
110
+
111
+ should "raise an InvalidBranchError" do
112
+ assert_kind_of GitRemoteBranch::InvalidBranchError, @ex
113
+ end
114
+
115
+ should "give a clear error message" do
116
+ assert_match(/identify.*branch/, @ex.message)
117
+ end
118
+
119
+ should "display git's branch listing" do
120
+ assert_match(/\(no branch\)/, @ex.message)
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ should_explain_with_current_branch 'current_branch', "use a dummy value for the current branch"
127
+
128
+ should_return_help_for_parameters %w(help), "on a 'help' command"
129
+ should_return_help_for_parameters %w(create), "on an incomplete command"
130
+ should_return_help_for_parameters %w(decombobulate something), "on an invalid command"
131
+ end
132
+
133
+ context "when not on a git repository" do
134
+ setup do
135
+ grb.stubs(:capture_process_output).returns([128, WHEN_NOT_ON_GIT_REPOSITORY])
136
+ end
137
+
138
+ should_explain_with_current_branch 'current_branch', "use a dummy value for the current branch"
139
+
140
+ should_return_help_for_parameters %w(help), "on a 'help' command"
141
+ should_return_help_for_parameters %w(create), "on an incomplete command"
142
+ should_return_help_for_parameters %w(decombobulate something), "on an invalid command"
143
+
144
+ GitRemoteBranch::COMMANDS.each_key do |action|
145
+ context "running the '#{action}' command" do
146
+ setup do
147
+ @command = [action.to_s, 'branch_name']
148
+ end
149
+
150
+ context "raising an exception" do
151
+ setup do
152
+ begin
153
+ grb.read_params(@command)
154
+ rescue => @ex
155
+ end
156
+ end
157
+
158
+ should "raise an NotOnGitRepositoryError" do
159
+ assert_kind_of GitRemoteBranch::NotOnGitRepositoryError, @ex
160
+ end
161
+
162
+ should "give a clear error message" do
163
+ assert_match(/fatal/, @ex.message)
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ context 'get_current_branch' do
172
+ context "when not on a git repository" do
173
+ setup do
174
+ grb.stubs(:capture_process_output).returns([128, WHEN_NOT_ON_GIT_REPOSITORY])
175
+ end
176
+
177
+ should "raise an exception" do
178
+ assert_raise(GitRemoteBranch::NotOnGitRepositoryError) { grb.get_current_branch }
179
+ end
180
+ end
181
+
182
+ context "when on an invalid branch" do
183
+ setup do
184
+ grb.stubs(:capture_process_output).returns([0, BRANCH_LISTING_WHEN_NOT_ON_BRANCH])
185
+ end
186
+
187
+ should "raise an exception" do
188
+ assert_raise(GitRemoteBranch::InvalidBranchError) { grb.get_current_branch }
189
+ end
190
+ end
191
+
192
+ context "when on a valid branch" do
193
+ setup do
194
+ grb.stubs(:capture_process_output).returns([0, REGULAR_BRANCH_LISTING])
195
+ end
196
+
197
+ should "return the current branch name" do
198
+ assert_equal 'stubbed_current_branch', grb.get_current_branch
199
+ end
200
+ end
201
+ end
202
+
203
+ context 'explain_mode!' do
204
+ context "when it receives an array beginning with 'explain'" do
205
+ setup do
206
+ @array = ['explain', 'create', 'some_branch']
207
+ end
208
+
209
+ should "return true" do
210
+ assert grb.explain_mode!(@array)
211
+ end
212
+
213
+ should 'accept symbol arrays as well' do
214
+ assert grb.explain_mode!( @array.map{|e| e.to_sym} )
215
+ end
216
+
217
+ should "remove 'explain' from the argument array" do
218
+ grb.explain_mode!(@array)
219
+ assert_equal ['create', 'some_branch'], @array
220
+ end
221
+ end
222
+
223
+ context "when it receives an array that doesn't begin with 'explain'" do
224
+ setup do
225
+ @array = ['create', 'some_branch']
226
+ end
227
+
228
+ should "return false" do
229
+ assert_false grb.explain_mode!(@array)
230
+ end
231
+
232
+ should "not modify the argument array" do
233
+ grb.explain_mode!(@array)
234
+ assert_equal ['create', 'some_branch'], @array
235
+ end
236
+ end
237
+ end
238
+
239
+ context 'get_action' do
240
+ GitRemoteBranch::COMMANDS.each_pair do |cmd, params|
241
+ should "recognize all #{cmd} aliases" do
242
+ params[:aliases].each do |alias_|
243
+ assert cmd, grb.get_action(alias_)
244
+ end
245
+ end
246
+ end
247
+ should 'return nil on unknown aliases' do
248
+ assert_nil grb.get_action('please_dont_create_an_alias_named_like_this')
249
+ end
250
+ end
251
+
252
+ context 'get_origin' do
253
+ should "default to 'origin' when the param is nil" do
254
+ assert_equal 'origin', grb.get_origin(nil)
255
+ end
256
+ should "return the unchanged param if it's not nil" do
257
+ assert_equal 'someword', grb.get_origin('someword')
258
+ end
259
+ end
260
+
261
+ end