Skip to content

Commit

Permalink
Refactor Gitlab::Help. Add tests some.
Browse files Browse the repository at this point in the history
  • Loading branch information
asedge committed Dec 28, 2014
1 parent 9bd4f7a commit bffd84f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/gitlab/cli_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def actions_table
end
end

# Decides which table to use.
# Outputs a nicely formatted table or error msg.
#
# @return [String]
def output_table(cmd, args, data)
Expand Down
76 changes: 47 additions & 29 deletions lib/gitlab/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,56 @@
module Gitlab::Help
extend Gitlab::CLI::Helpers

def self.get_help(methods,cmd)
help = ''

ri_cmd = `which ri`.chomp

if $? == 0
namespace = methods.select {|m| m[:name] === cmd }.
map {|m| m[:owner]+'.'+m[:name] }.shift

if namespace
begin
ri_output = `#{ri_cmd} -T #{namespace} 2>&1`.chomp

if $? == 0
ri_output.gsub!(/#{cmd}\((.*?)\)/m, cmd+' \1')
ri_output.gsub!(/Gitlab\./, 'gitlab> ')
ri_output.gsub!(/Gitlab\..+$/, '')
ri_output.gsub!(/\,[\s]*/, ' ')
help = ri_output
else
help = "Ri docs not found for #{namespace}, please install the docs to use 'help'."
end
rescue => e
puts e.message
class << self

def get_help(cmd)
cmd_namespace = namespace cmd

if cmd_namespace
ri_output = `#{ri_cmd} -T #{cmd_namespace} 2>&1`.chomp

if $? == 0
change_help_output! cmd, ri_output
ri_output
else
"Ri docs not found for #{cmd}, please install the docs to use 'help'."
end
else
help = "Unknown command: #{cmd}."
"Unknown command: #{cmd}."
end
end

def ri_cmd
@ri_cmd if @ri_cmd
which_ri = `which ri`.chomp
if which_ri.length < 1
raise "'ri' tool not found in your PATH, please install it to use the help."
end
@ri_cmd = which_ri
end

def namespace cmd
help_methods.select { |method| method[:name] === cmd }.
map { |method| method[:owner] + '.' + method[:name] }.
shift
end

def help_methods
@help_methods ||= Gitlab.actions.map do |action|
{
name: action.to_s,
owner: Gitlab.client.method(action).owner.to_s
}
end
else
help = "'ri' tool not found in your PATH, please install it to use the help."
end

help
end
def change_help_output! cmd, output_str
output_str.gsub!(/#{cmd}\((.*?)\)/m, cmd+' \1')
output_str.gsub!(/Gitlab\./, 'gitlab> ')
output_str.gsub!(/Gitlab\..+$/, '')
output_str.gsub!(/\,[\s]*/, ' ')
end

end # class << self
end

18 changes: 2 additions & 16 deletions lib/gitlab/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def start

data = execute command, arguments
output_table command, arguments, data
rescue ArgumentError => e
rescue => e
puts e.message
next
rescue
next
end
end
end
Expand Down Expand Up @@ -67,19 +65,7 @@ def completion
end

def help cmd
if cmd.nil?
actions_table
else
methods = []
actions.each do |action|
methods << {
name: action.to_s,
owner: client.method(action).owner.to_s
}
end

Gitlab::Help.get_help(methods, arguments[0])
end
cmd.nil? ? actions_table : Gitlab::Help.get_help(cmd)
end

def execute cmd = command, args = arguments
Expand Down
38 changes: 38 additions & 0 deletions spec/gitlab/help_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'spec_helper'

describe Gitlab::Help do

describe ".change_help_output!" do
before do
@cmd = "create_branch"
@help_output = "Gitlab.#{@cmd}(4, 'new-branch', 'master')"
end
it "should return a String of modified output" do
Gitlab::Help.change_help_output! @cmd, @help_output
expect(@help_output).to eq("gitlab> create_branch 4 'new-branch' 'master'")
end
end

describe ".help_methods" do
before do
@methods = Gitlab::Help.help_methods
end
it "should return Array of Hashes containing method names and owners" do
expect(@methods).to be_a Array
expect(@methods.all? { |m| m.is_a? Hash} ).to be true
expect(@methods.all? { |m| m.keys.sort === [:name, :owner]} ).to be true
end
end

describe ".namespace" do
before do
@cmd = 'create_tag'
@namespace = Gitlab::Help.namespace @cmd
end
it "should return the full namespace for a command" do
expect(@namespace).to be_a String
expect(@namespace).to eq("Gitlab::Client::Repositories.#{@cmd}")
end
end

end

0 comments on commit bffd84f

Please sign in to comment.