-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce BundleOutdatedParser to handle bundler’s output
- Loading branch information
1 parent
52428c7
commit 50738d2
Showing
7 changed files
with
128 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This class runs `bundle outdated` and parses the output | ||
# into a workable data structure in Ruby. | ||
module SafeUpdate | ||
class BundleOutdatedParser | ||
def call | ||
@outdated_gems = [] | ||
# Yes, I know about `bundle outdated --parseable` but old versions | ||
# don't support it and it's really not THAT much more parseable anyway | ||
# and parseable still sometimes has lines that aren't relevant | ||
@output = `bundle outdated` | ||
@output.split(/\n+/).each do |line| | ||
process_single_line(line) | ||
end | ||
return @outdated_gems | ||
end | ||
|
||
private | ||
|
||
def process_single_line(line) | ||
# guard clause for output that's not an outdated gem | ||
return if !line.include?(' (newest') | ||
# get rid of leading *, eg in ' * poltergeist (newest 1.9.0, installed 1.8.1)' | ||
line.strip! | ||
line.gsub!(/^\*/, '') | ||
line.strip! | ||
|
||
@outdated_gems << OutdatedGem.new( | ||
gem_name: gem_name(line), | ||
newest: newest(line), | ||
installed: installed(line), | ||
requested: requested(line) | ||
) | ||
end | ||
|
||
def gem_name(line) | ||
string_between(line, '', ' (newest') | ||
end | ||
|
||
def newest(line) | ||
string_between(line, ' (newest ', ', installed') | ||
end | ||
|
||
def requested(line) | ||
string_between(line, ', requested ', ')') | ||
end | ||
|
||
def installed(line) | ||
if line.index('requested') | ||
string_between(line, ', installed ', ', requested') | ||
else | ||
string_between(line, ', installed ', ')') | ||
end | ||
end | ||
|
||
# returns the section of string that resides between marker1 and marker2 | ||
def string_between(string, marker1, marker2) | ||
string[/#{Regexp.escape(marker1)}(.*?)#{Regexp.escape(marker2)}/m, 1] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'spec_helper' | ||
|
||
sample_output = <<eot | ||
The git source `git://github.com/ianfleeton/paypal-express.git` uses the `git` protocol, which transmits data without encryption. Disable this warning with `bundle config git.allow_insecure true`, or switch to the `https` protocol to keep your data secure. | ||
some other example line that should be discarded | ||
Fetching git://github.com/activerecord-hackery/ransack.git | ||
Fetching gem metadata from https://rubygems.org/.......... | ||
Fetching version metadata from https://rubygems.org/... | ||
Fetching dependency metadata from https://rubygems.org/.. | ||
Resolving dependencies................................... | ||
Outdated gems included in the bundle: | ||
* rails-footnotes (newest 4.1.8 4e6f69f, installed 4.1.8 a179ce0) | ||
* rspec-rails (newest 3.4.2, installed 3.4.0, requested ~> 3.4) | ||
* poltergeist (newest 1.9.0, installed 1.8.1) | ||
* unf_ext (newest 0.0.7.2, installed 0.0.7.1) | ||
eot | ||
|
||
describe SafeUpdate::GitRepo do | ||
it 'Gets rid of irrelevant lines' do | ||
parser = SafeUpdate::BundleOutdatedParser.new | ||
allow(parser).to( | ||
receive(:`).with('bundle outdated') | ||
.and_return(sample_output) | ||
) | ||
|
||
outdated_gems = parser.call | ||
# Just check two results... if it works for those two, | ||
# safe to assume it works for the others with slightly different formats | ||
|
||
expect(outdated_gems[0].gem_name).to eq('rails-footnotes') | ||
expect(outdated_gems[0].newest).to eq('4.1.8 4e6f69f') | ||
expect(outdated_gems[0].installed).to eq('4.1.8 a179ce0') | ||
expect(outdated_gems[0].requested).to eq(nil) | ||
|
||
expect(outdated_gems[1].gem_name).to eq('rspec-rails') | ||
expect(outdated_gems[1].newest).to eq('3.4.2') | ||
expect(outdated_gems[1].installed).to eq('3.4.0') | ||
expect(outdated_gems[1].requested).to eq('~> 3.4') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters