bundler-auto-update 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,7 @@ module Bundler
11
11
  Updater.new(test_command).auto_update!
12
12
  end
13
13
 
14
+ # @return [String] Test command from @argv
14
15
  def test_command
15
16
  if @argv.first == '-c'
16
17
  @argv[1..-1].join(' ')
@@ -33,6 +34,8 @@ module Bundler
33
34
  end
34
35
  end
35
36
 
37
+ private
38
+
36
39
  def gemfile
37
40
  @gemfile ||= Gemfile.new
38
41
  end
@@ -45,6 +48,7 @@ module Bundler
45
48
  @gem, @gemfile, @test_command = gem, gemfile, test_command
46
49
  end
47
50
 
51
+ # Attempt to update to patch, then to minor then to major versions.
48
52
  def auto_update
49
53
  if updatable?
50
54
  Logger.log "Updating #{gem.name}"
@@ -54,6 +58,10 @@ module Bundler
54
58
  end
55
59
  end
56
60
 
61
+ # Update current gem to latest :version_type:, run test suite and commit new Gemfile
62
+ # if successful.
63
+ #
64
+ # @param version_type :patch or :minor or :major
57
65
  # @return [Boolean] true on success or when already at latest version
58
66
  def update(version_type)
59
67
  new_version = gem.last_version(version_type)
@@ -68,11 +76,19 @@ module Bundler
68
76
 
69
77
  gem.version = new_version
70
78
 
71
- (update_gemfile and run_test_suite and commit_new_version) or revert_to_previous_version
79
+ if update_gemfile and run_test_suite and commit_new_version
80
+ true
81
+ else
82
+ revert_to_previous_version
83
+ false
84
+ end
72
85
  end
73
86
 
74
87
  private
75
88
 
89
+ # Update gem version in Gemfile.
90
+ #
91
+ # @return true on success, false on failure.
76
92
  def update_gemfile
77
93
  if gemfile.update_gem(gem)
78
94
  Logger.log_indent "Gemfile updated successfully."
@@ -83,6 +99,7 @@ module Bundler
83
99
  end
84
100
  end
85
101
 
102
+ # @return true on success, false on failure
86
103
  def run_test_suite
87
104
  Logger.log_indent "Running test suite"
88
105
  if CommandRunner.system test_command
@@ -94,8 +111,9 @@ module Bundler
94
111
  end
95
112
  end
96
113
 
114
+ # @return true when the gem has a fixed version.
97
115
  def updatable?
98
- gem.version =~ /^\d+\.\d+\.\d+$/ && gem.options.nil?
116
+ gem.version =~ /^\d+\.\d+\.\d+$/
99
117
  end
100
118
 
101
119
  def commit_new_version
@@ -108,10 +126,13 @@ module Bundler
108
126
  CommandRunner.system "git checkout Gemfile Gemfile.lock"
109
127
  gemfile.reload!
110
128
  end
111
- end # class Updater
129
+ end # class GemUpdater
112
130
 
113
131
  class Gemfile
114
132
 
133
+ # Regex that matches a gem definition line.
134
+ #
135
+ # @return [RegEx] matching [_, name, _, version, _, options]
115
136
  def gem_line_regex(gem_name = '(\w+)')
116
137
  /^\s*gem\s*['"]#{gem_name}['"]\s*(,\s*['"](.+)['"])?\s*(,\s*(.*))?\n?$/
117
138
  end
@@ -130,15 +151,17 @@ module Bundler
130
151
  gems
131
152
  end
132
153
 
133
- # @todo spec
154
+ # Update Gemfile and run 'bundle update'
134
155
  def update_gem(gem)
135
156
  update_content(gem) and write and run_bundle_update(gem)
136
157
  end
137
158
 
159
+ # @return [String] Gemfile content
138
160
  def content
139
161
  @content ||= read
140
162
  end
141
163
 
164
+ # Reload Gemfile content
142
165
  def reload!
143
166
  @content = read
144
167
  end
@@ -158,16 +181,23 @@ module Bundler
158
181
  @content = new_content
159
182
  end
160
183
 
184
+ # @return [String] Gemfile content read from filesystem
161
185
  def read
162
186
  File.read('Gemfile')
163
187
  end
164
188
 
189
+ # Write content to Gemfile
165
190
  def write
166
191
  File.open('Gemfile', 'w') do |f|
167
192
  f.write(content)
168
193
  end
169
194
  end
170
195
 
196
+ # Attempt to run 'bundle install' and fall back on running 'bundle update :gem'.
197
+ #
198
+ # @param [Dependency] gem The gem to update
199
+ #
200
+ # @return true on success, false on failure
171
201
  def run_bundle_update(gem)
172
202
  CommandRunner.system("bundle install") or CommandRunner.system("bundle update #{gem.name}")
173
203
  end
@@ -178,10 +208,16 @@ module Bundler
178
208
  puts prefix + msg
179
209
  end
180
210
 
211
+ # Log with indentation:
212
+ # " - Log message"
213
+ #
181
214
  def self.log_indent(msg)
182
215
  log(msg, " - ")
183
216
  end
184
217
 
218
+ # Log command:
219
+ # " > bundle update"
220
+ #
185
221
  def self.log_cmd(msg)
186
222
  log(msg, " > ")
187
223
  end
@@ -197,6 +233,13 @@ module Bundler
197
233
  @major, @minor, @patch = version.split('.') if version
198
234
  end
199
235
 
236
+ # Return last version scoped at :version_type:.
237
+ #
238
+ # Example: last_version(:patch), returns the last patch version
239
+ # for the current major/minor version
240
+ #
241
+ # @return [String] last version. Ex: '1.2.3'
242
+ #
200
243
  def last_version(version_type)
201
244
  case version_type
202
245
  when :patch
@@ -205,26 +248,38 @@ module Bundler
205
248
  available_versions.select { |v| v =~ /^#{major}\./ }.first
206
249
  when :major
207
250
  available_versions.first
251
+ else
252
+ raise "Invalid version_type: #{version_type}"
208
253
  end
209
254
  end
210
255
 
256
+ # Return an ordered array of all available versions.
257
+ #
258
+ # @return [Array] of [String].
211
259
  def available_versions
212
260
  the_gem_line = gem_remote_list_output.scan(/^#{name}\s.*$/).first
213
261
  the_gem_line.scan /\d+\.\d+\.\d+/
214
262
  end
215
263
 
264
+ private
265
+
216
266
  def gem_remote_list_output
217
267
  CommandRunner.run "gem list #{name} -r -a"
218
268
  end
219
269
  end # class Dependency
220
270
 
221
271
  class CommandRunner
272
+
273
+ # Output the command about to run, and run it using system.
274
+ #
275
+ # @return true on success, false on failure
222
276
  def self.system(cmd)
223
277
  Logger.log_cmd cmd
224
278
 
225
279
  Kernel.system cmd
226
280
  end
227
281
 
282
+ # Run a system command and return its output.
228
283
  def self.run(cmd)
229
284
  `#{cmd}`
230
285
  end
@@ -1,5 +1,5 @@
1
1
  module Bundler
2
2
  module AutoUpdate
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -12,12 +12,16 @@ class Bundler::AutoUpdate::CommandRunner
12
12
 
13
13
  def self.run(cmd)
14
14
  puts "Stub! #{cmd}"
15
+
16
+ "command output"
15
17
  end
16
18
  end
17
19
 
18
20
  class Gemfile
19
21
  def read
20
22
  puts "Stub! read"
23
+
24
+ true
21
25
  end
22
26
 
23
27
  def write
@@ -7,7 +7,7 @@ describe GemUpdater do
7
7
  describe "auto_update" do
8
8
 
9
9
  context "when gem is updatable" do
10
- let(:gem_updater) { GemUpdater.new(Dependency.new('rails', '3.0.0', nil), gemfile, test_command) }
10
+ let(:gem_updater) { GemUpdater.new(Dependency.new('rails', '3.0.0'), gemfile, test_command) }
11
11
 
12
12
  it "should attempt to update to patch, minor and major" do
13
13
  gem_updater.should_receive(:update).with(:patch).and_return(true)
@@ -18,7 +18,7 @@ describe GemUpdater do
18
18
  end
19
19
  end
20
20
 
21
- context "when gem is not" do
21
+ context "when gem is not updatable" do
22
22
  let(:gem_updater) { GemUpdater.new(Dependency.new('rake', '<0.9'), gemfile, test_command) }
23
23
 
24
24
  it "should not attempt to update it" do
@@ -45,41 +45,42 @@ describe GemUpdater do
45
45
 
46
46
  context "when new version" do
47
47
  context "when tests pass" do
48
- it "should commit new version" do
48
+ it "should commit new version and return true" do
49
49
  gem.should_receive(:last_version).with(:patch) { gem.version.next }
50
50
  gem_updater.should_receive(:update_gemfile).and_return true
51
51
  gem_updater.should_receive(:run_test_suite).and_return true
52
52
  gem_updater.should_receive(:commit_new_version).and_return true
53
53
  gem_updater.should_not_receive(:revert_to_previous_version)
54
54
 
55
- gem_updater.update(:patch)
55
+ gem_updater.update(:patch).should == true
56
56
  end
57
57
  end
58
58
 
59
59
  context "when tests do not pass" do
60
- it "should revert to previous version" do
60
+ it "should revert to previous version and return false" do
61
61
  gem.should_receive(:last_version).with(:patch) { gem.version.next }
62
62
  gem_updater.should_receive(:update_gemfile).and_return true
63
63
  gem_updater.should_receive(:run_test_suite).and_return false
64
64
  gem_updater.should_not_receive(:commit_new_version)
65
65
  gem_updater.should_receive(:revert_to_previous_version)
66
66
 
67
- gem_updater.update(:patch)
67
+ gem_updater.update(:patch).should == false
68
68
  end
69
69
  end
70
70
 
71
71
  context "when it fails to upgrade gem" do
72
- it "should revert to previous version" do
72
+ it "should revert to previous version and return false" do
73
73
  gem.should_receive(:last_version).with(:patch) { gem.version.next }
74
74
  gem_updater.should_receive(:update_gemfile).and_return false
75
75
  gem_updater.should_not_receive(:run_test_suite)
76
76
  gem_updater.should_not_receive(:commit_new_version)
77
77
  gem_updater.should_receive(:revert_to_previous_version)
78
78
 
79
- gem_updater.update(:patch)
79
+ gem_updater.update(:patch).should == false
80
80
  end
81
81
  end
82
82
  end
83
- end
83
+ end # describe "#update"
84
+
84
85
  end
85
86
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bundler-auto-update
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Philippe Creux
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-06 00:00:00 +02:00
18
+ date: 2011-10-13 00:00:00 +02:00
19
19
  default_executable: bundle-auto-update
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency