Skip to content

Commit

Permalink
Small refactor of Gitlab::Help, Gitlab::Shell & Gitlab::CLI::Helpers.…
Browse files Browse the repository at this point in the history
… Add some new tests and refactor ones recently added.
  • Loading branch information
asedge committed Dec 29, 2014
1 parent bffd84f commit e2bd91c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 58 deletions.
42 changes: 29 additions & 13 deletions lib/gitlab/cli_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
require 'yaml'

class Gitlab::CLI
# Defines methods related to CLI output and formatting.
module Helpers
extend self

# Returns actions available to CLI & Shell
#
# @return [Array]
def actions
@actions ||= Gitlab.actions
end

# Returns Gitlab::Client instance
#
# @return [Gitlab::Client]
def client
@client ||= Gitlab::Client.new(endpoint: (Gitlab.endpoint || ''))
end

# Returns method names and their owners
#
# @return [Array<Hash>]
def method_owners
@method_owners ||= actions.map do |action|
{
name: action.to_s,
owner: client.method(action).owner.to_s
}
end
end

# Returns filtered required fields.
#
# @return [Array]
Expand Down Expand Up @@ -53,19 +80,8 @@ def confirm_command(cmd)
#
# @return [String]
def actions_table
client = Gitlab::Client.new(endpoint: '')
actions = Gitlab.actions
methods = []

actions.each do |action|
methods << {
name: action,
owner: client.method(action).owner.to_s.gsub('Gitlab::Client::', '')
}
end

owners = methods.map {|m| m[:owner]}.uniq.sort
methods_c = methods.group_by {|m| m[:owner]}
owners = method_owners.map {|m| m[:owner].gsub('Gitlab::Client::','')}.uniq.sort
methods_c = method_owners.group_by {|m| m[:owner]}
methods_c = methods_c.map {|_, v| [_, v.sort_by {|hv| hv[:name]}] }
methods_c = Hash[methods_c.sort_by(&:first).map {|k, v| [k, v]}]
max_column_length = methods_c.values.max_by(&:size).size
Expand Down
19 changes: 6 additions & 13 deletions lib/gitlab/help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,19 @@ def get_help(cmd)

def ri_cmd
@ri_cmd if @ri_cmd

which_ri = `which ri`.chomp
if which_ri.length < 1
if which_ri.empty?
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
method_owners.select { |method| method[:name] === cmd }.
map { |method| method[:owner] + '.' + method[:name] }.
shift
end

def change_help_output! cmd, output_str
Expand Down
11 changes: 4 additions & 7 deletions lib/gitlab/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Gitlab::Shell
extend Gitlab::CLI::Helpers

class << self
attr_reader :client, :actions, :arguments, :command
attr_reader :arguments, :command

def start
setup
Expand Down Expand Up @@ -55,16 +55,13 @@ def setup

Readline.completion_proc = completion
Readline.completion_append_character = ' '

@client = Gitlab::Client.new(endpoint: '')
@actions = Gitlab.actions
end

def completion
proc { |str| actions.map(&:to_s).grep(/^#{Regexp.escape(str)}/) }
end

def help cmd
def help cmd=nil
cmd.nil? ? actions_table : Gitlab::Help.get_help(cmd)
end

Expand All @@ -73,8 +70,8 @@ def execute cmd = command, args = arguments
confirm_command(cmd)
gitlab_helper(cmd, args)
else
"Unknown command: #{cmd}. " +
"See the 'help' for a list of valid commands."
raise "Unknown command: #{cmd}. " +
"See the 'help' for a list of valid commands."
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/gitlab/cli_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
require 'spec_helper'

describe Gitlab::CLI::Helpers do
describe ".method_owners" do
before do
@methods = Gitlab::CLI::Helpers.method_owners
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 ".valid_command?" do
it "should return true when command is valid" do
expect(Gitlab::CLI::Helpers.valid_command? 'merge_requests').to be_truthy
Expand All @@ -9,6 +20,7 @@
expect(Gitlab::CLI::Helpers.valid_command? 'mmmmmerge_requests').to be_falsy
end
end

describe ".symbolize_keys" do
context "when input is a Hash" do
it "should return a Hash with symbols for keys" do
Expand Down
28 changes: 17 additions & 11 deletions spec/gitlab/help_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

describe Gitlab::Help do

describe ".ri_cmd" do
context "ri command found" do
it "should return the path to RI" do
allow(Gitlab::Help).to receive(:`).with(/which ri/).and_return('/usr/bin/ri')
expect(Gitlab::Help.ri_cmd).to eq('/usr/bin/ri')
end
end

context "ri command NOT found" do
it "should raise" do
allow(Gitlab::Help).to receive(:`).with(/which ri/).and_return('')
expect{Gitlab::Help.ri_cmd}.to raise_error
end
end

end

describe ".change_help_output!" do
before do
@cmd = "create_branch"
Expand All @@ -13,17 +30,6 @@
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'
Expand Down
51 changes: 37 additions & 14 deletions spec/gitlab/shell_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
require 'spec_helper'

describe Gitlab::Shell do
describe ".history" do
it "should return a Gitlab::Shell::History instance" do
history = Gitlab::Shell.history
expect(history).to be_a Gitlab::Shell::History
before do
Gitlab::Shell.setup
end

describe ".execute" do
context "invalid command" do
it "should raise" do
expect{Gitlab::Shell.execute 'foobar', []}.to raise_error(RuntimeError)
end
end
end

describe ".setup" do
before(:all) do
Gitlab::Shell.setup
describe ".history" do
before do
@history = Gitlab::Shell.history
end
it "should create an instance of Gitlab::Client in class variable 'client'" do
expect(Gitlab::Shell.client).to be_a Gitlab::Client

it "should return a Gitlab::Shell::History instance" do
expect(@history).to be_a Gitlab::Shell::History
end
it "should set array of @actions" do
expect(Gitlab::Shell.actions).to be_a Array
expect(Gitlab::Shell.actions.sort).to eq(Gitlab.actions.sort)
it "should respond to :save" do
expect(@history).to respond_to :save
end
it "should respond to :load" do
expect(@history).to respond_to :load
end
it "should respond to :<<" do
expect(@history).to respond_to :<<
end
end

describe ".setup" do
it "should set the Readline completion_proc" do
completion = Readline.completion_proc
expect(completion).to be_truthy
Expand All @@ -31,9 +45,18 @@
end

describe ".completion" do
before do
@comp = Gitlab::Shell.completion
end
it "should return a Proc object" do
comp = Gitlab::Shell.completion
expect(comp).to be_a Proc
expect(@comp).to be_a Proc
end
context "called with an argument" do
it "should return an Array of matching commands" do
completed_cmds = @comp.call 'group'
expect(completed_cmds).to be_a Array
expect(completed_cmds.sort).to eq(['group', 'group_members', 'groups'])
end
end
end

Expand Down

0 comments on commit e2bd91c

Please sign in to comment.