Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions lib/bundle_outdated_formatter/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Formatter
INSTALLED_REGEXP = /installed (?<installed>[\d.]+)/.freeze
REQUESTED_REGEXP = /requested (?<requested>.+)\)/.freeze
GROUPS_REGEXP = /in groups? "(?<groups>.+)"/.freeze
TABLE_FORMAT_REGEXP = /Gem +Current +Latest +Requested +Groups/.freeze

def initialize(options)
@pretty = options[:pretty]
Expand All @@ -19,10 +20,17 @@ def initialize(options)
def read_stdin
@outdated_gems = STDIN.each.to_a.map(&:strip).reject(&:empty?)

@outdated_gems.map! do |line|
find_gem(line)
if (header_index = find_table_header_index(@outdated_gems))
header = @outdated_gems[header_index]
pos = table_pos(header)
@outdated_gems.map!.with_index do |line, index|
find_gem_for_table_format(line, pos) if header_index < index
end
else
@outdated_gems.map! do |line|
find_gem(line)
end
end

@outdated_gems.compact!
end

Expand Down Expand Up @@ -57,6 +65,33 @@ def gem_text(text, name)
text ? text[name] : ''
end

def find_table_header_index(lines)
lines.find_index { |line| line =~ TABLE_FORMAT_REGEXP }
end

def table_pos(header)
current_pos = header.index('Current')
latest_pos = header.index('Latest')
requested_pos = header.index('Requested')
groups_pos = header.index('Groups')
{
'gem' => 0..current_pos.pred,
'newest' => latest_pos..requested_pos.pred,
'installed' => current_pos..latest_pos.pred,
'requested' => requested_pos..groups_pos.pred,
'groups' => groups_pos..-1
}
end

def find_gem_for_table_format(line, pos)
gems = {}
@columns.each do |column|
range = pos[column]
gems[column] = line[range].to_s.strip
end
gems
end

def xml_formatter
return REXML::Formatters::Default.new unless @pretty

Expand Down
84 changes: 84 additions & 0 deletions spec/bundle_outdated_formatter/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
EOS
end

let(:stdin_with_table_format) do
<<-EOS
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies....

Gem Current Latest Requested Groups
faker 1.6.5 1.6.6 ~> 1.4 development, test
hashie 1.2.0 3.4.6 = 1.2.0 default
headless 2.2.3 2.3.1
EOS
end

let(:stdin_without_outdated) do
<<-EOS
Fetching gem metadata from https://rubygems.org/..........
Expand Down Expand Up @@ -453,6 +465,12 @@

it_behaves_like 'terminal format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'terminal format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_terminal_unicode) { stdout_terminal_unicode_without_outdated }
Expand Down Expand Up @@ -504,6 +522,12 @@

it_behaves_like 'terminal format', style: :ascii

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'terminal format', style: :ascii
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_terminal_ascii) { stdout_terminal_ascii_without_outdated }
Expand Down Expand Up @@ -535,6 +559,12 @@

it_behaves_like 'markdown format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'markdown format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_markdown) { stdout_markdown_without_outdated }
Expand Down Expand Up @@ -610,6 +640,12 @@

it_behaves_like 'json format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'json format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_json) { stdout_json_without_outdated }
Expand Down Expand Up @@ -637,6 +673,12 @@

it_behaves_like 'json format', pretty: true

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'json format', pretty: true
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_json_pretty) { stdout_json_pretty_without_outdated }
Expand Down Expand Up @@ -692,6 +734,12 @@

it_behaves_like 'yaml format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'yaml format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_yaml) { stdout_yaml_without_outdated }
Expand Down Expand Up @@ -767,6 +815,12 @@

it_behaves_like 'csv format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'csv format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_csv) { stdout_csv_without_outdated }
Expand Down Expand Up @@ -842,6 +896,12 @@

it_behaves_like 'tsv format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'tsv format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_tsv) { stdout_tsv_without_outdated }
Expand Down Expand Up @@ -917,6 +977,12 @@

it_behaves_like 'xml format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'xml format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_xml) { stdout_xml_without_outdated }
Expand Down Expand Up @@ -944,6 +1010,12 @@

it_behaves_like 'xml format', pretty: true

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'xml format', pretty: true
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_xml_pretty) { stdout_xml_pretty_without_outdated }
Expand Down Expand Up @@ -999,6 +1071,12 @@

it_behaves_like 'html format'

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'html format'
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_html) { stdout_html_without_outdated }
Expand Down Expand Up @@ -1026,6 +1104,12 @@

it_behaves_like 'html format', pretty: true

context 'with table format' do
let(:stdin) { stdin_with_table_format }

it_behaves_like 'html format', pretty: true
end

context 'without outdated' do
let(:stdin) { stdin_without_outdated }
let(:stdout_html_pretty) { stdout_html_pretty_without_outdated }
Expand Down