Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract a command superclass to prepare for adding more commands

Signed-off-by: Ryan Burrows <rhburrows@gmail.com>
  • Loading branch information...
commit 43fb04bac3fe599f85451d6d030447927d2edeb7 1 parent c2c645c
@rhburrows authored
View
5 features/step_definitions/user_steps.rb
@@ -25,7 +25,10 @@
end
When /^I run a reviewr command that requires email$/ do
- project = reviewr.prompt_for_user(input, output)
+ command = reviewr.build_command
+ command.input = input
+ command.output = output
+ command.prompt_for_user
end
Then /^I should see "([^\"]*)"$/ do |msg|
View
1  lib/reviewr/cli.rb
@@ -1,4 +1,5 @@
require 'reviewr/cli/main'
+require 'reviewr/cli/command'
require 'reviewr/cli/help'
require 'reviewr/cli/request'
View
42 lib/reviewr/cli/command.rb
@@ -0,0 +1,42 @@
+module Reviewr
+ module CLI
+ class Command
+ attr_reader :project
+ attr_accessor :input, :output
+
+ def initialize(project, input = STDIN, output = STDOUT)
+ @project = project
+ @input, @output = input, output
+ end
+
+ def prompt_for_user
+ output.print("Email (default #{project.user_email}): ")
+ email = input.gets.chomp
+ project.user_email = email unless email.empty?
+
+ output.print("Email password: ")
+ no_echo(input) do
+ project.email_password = input.gets.chomp
+ end
+ output.print("\n")
+
+ output.print("Remote repository (default origin): ")
+ repo = input.gets.chomp
+ project.remote_repo = repo unless repo.empty?
+ end
+
+ private
+
+ def no_echo(input)
+ oldt = Termios.tcgetattr(input)
+ newt = oldt.dup
+ newt.lflag &= ~Termios::ECHO
+ Termios.tcsetattr(input, Termios::TCSANOW, newt)
+
+ yield
+
+ Termios.tcsetattr(input, Termios::TCSANOW, oldt)
+ end
+ end
+ end
+end
View
2  lib/reviewr/cli/help.rb
@@ -1,6 +1,6 @@
module Reviewr
module CLI
- class Help
+ class Help < Command
def call(command)
if command.nil?
usage
View
40 lib/reviewr/cli/main.rb
@@ -13,44 +13,16 @@ def initialize(args, input = STDIN, output = STDOUT)
end
def run
+ build_command.call
+ end
+
+ def build_command
case command
when "request"
- prompt_for_user(@input, @output)
- Request.new(project).call
+ Request.new(project, @input, @output)
else
- #FIXME
- Help.new.call(project.to)
- end
- end
-
- def prompt_for_user(input = STDIN, output = STDOUT)
- output.print("Email (default #{project.user_email}): ")
- email = input.gets.chomp
- project.user_email = email unless email.empty?
-
- output.print("Email password: ")
- no_echo(input) do
- project.email_password = input.gets.chomp
+ Help.new(project, @input, @output)
end
- output.print("\n")
-
- output.print("Remote repository (default origin): ")
- repo = input.gets.chomp
- project.remote_repo = repo unless repo.empty?
- end
-
- private
-
- #TODO, should this be somewhere better?
- def no_echo(input)
- oldt = Termios.tcgetattr(input)
- newt = oldt.dup
- newt.lflag &= ~Termios::ECHO
- Termios.tcsetattr(input, Termios::TCSANOW, newt)
-
- yield
-
- Termios.tcsetattr(input, Termios::TCSANOW, oldt)
end
end
end
View
9 lib/reviewr/cli/request.rb
@@ -3,14 +3,9 @@
module Reviewr
module CLI
- class Request
- attr_reader :project
-
- def initialize(project)
- @project = project
- end
-
+ class Request < Command
def call
+ prompt_for_user
original_branch = project.current_branch
project.create_review_branch
project.create_review_commit(commit_msg)
View
66 spec/reviewr/cli/command_spec.rb
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+module Reviewr
+ module CLI
+ describe Command do
+ let(:project){ double("Project").as_null_object }
+ let(:input){ double("Input").as_null_object }
+ let(:output){ double("Output").as_null_object }
+ let(:command){ Command.new(project, input, output) }
+
+ describe "#prompt_for_user" do
+ before do
+ Termios.stub(:tcgetattr).and_return(mock("Attr").as_null_object)
+ Termios.stub(:tcsetattr)
+ end
+
+ it "Asks for the user's email with the default user email from git" do
+ command.project.stub(:user_email).and_return('e@s.com')
+ output.should_receive(:print).with("Email (default e@s.com): ")
+ command.prompt_for_user
+ end
+
+ it "Sets the entered email into the project" do
+ command.project.should_receive(:user_email=).with("myemail@site.com")
+ input.stub(:gets).and_return("myemail@site.com")
+ command.prompt_for_user
+ end
+
+ it "Uses the default email if an empty string is entered" do
+ command.project.should_not_receive(:user_email=)
+ input.stub(:gets).and_return("\n")
+ command.prompt_for_user
+ end
+
+ it "Asks for the user's email password" do
+ output.should_receive(:print).with("Email password: ")
+ command.prompt_for_user
+ end
+
+ it "Sets the entered email password into the project" do
+ command.project.should_receive(:email_password=).with("asdf")
+ input.stub(:gets).and_return("email@s.com", "asdf")
+ command.prompt_for_user
+ end
+
+ it "Asks for the remote repository name" do
+ output.should_receive(:print).
+ with("Remote repository (default origin): ")
+ command.prompt_for_user
+ end
+
+ it "Sets the entered remote repository into the project" do
+ command.project.should_receive(:remote_repo=).with("remote_name")
+ input.stub(:gets).and_return("remote_name\n")
+ command.prompt_for_user
+ end
+
+ it "Uses the default remote repository if an empty string is entered" do
+ command.project.should_not_receive(:remote_repo=)
+ input.stub(:gets).and_return("\n")
+ command.prompt_for_user
+ end
+ end
+ end
+ end
+end
View
107 spec/reviewr/cli/main_spec.rb
@@ -2,11 +2,6 @@
module Reviewr::CLI
describe Main do
- before do
- Termios.stub(:tcgetattr).and_return(mock("Attr").as_null_object)
- Termios.stub(:tcsetattr)
- end
-
describe "#initialize" do
it "takes the first argument as the command name" do
main = Main.new(["a", "b"])
@@ -20,97 +15,41 @@ module Reviewr::CLI
end
describe "#run" do
- context "command name is 'request'"do
- before do
- @request = mock("Request").as_null_object
- Request.stub!(:new).and_return(@request)
- end
-
- it "creates a Reviewr::Request" do
- Request.should_receive(:new)
- new_main(["request", "test@site.com"]).run
- end
-
- it "calls #call on the request" do
- @request.should_receive(:call)
- new_main(["request"]).run
- end
- end
-
- context "when passed a non-existant command" do
- before do
- @help = mock("Help").as_null_object
- Help.stub!(:new).and_return(@help)
- end
-
- it "creates a help command" do
- Help.should_receive(:new)
- new_main(["asdf"]).run
- end
-
- it "calls #call on the help command" do
- @help.should_receive(:call)
- new_main(["ff"]).run
- end
- end
-
- def new_main(args)
- m = Main.new(args)
- m.stub(:prompt_for_user)
- m
- end
- end
-
- describe "#prompt_for_user" do
- let(:input){ double("Input").as_null_object }
- let(:output){ double("Output").as_null_object }
let(:main) { Main.new(['a']) }
+ let(:command) { mock('Command').as_null_object }
- it "Asks for the user's email with the default user email from git" do
- Reviewr::Git.instance.stub(:user_email).and_return('e@s.com')
- output.should_receive(:print).with("Email (default e@s.com): ")
- main.prompt_for_user(input, output)
- end
-
- it "Sets the entered email into the project" do
- main.project.should_receive(:user_email=).with("myemail@site.com")
- input.stub(:gets).and_return("myemail@site.com")
- main.prompt_for_user(input, output)
+ before do
+ main.stub(:build_command).and_return(command)
end
- it "Uses the default email if an empty string is entered" do
- main.project.should_not_receive(:user_email=)
- input.stub(:gets).and_return("\n")
- main.prompt_for_user(input, output)
+ it "builds a command object" do
+ main.should_receive(:build_command)
+ main.run
end
- it "Asks for the user's email password" do
- output.should_receive(:print).with("Email password: ")
- main.prompt_for_user(input, output)
+ it "calls the command" do
+ command.should_receive(:call)
+ main.run
end
+ end
- it "Sets the entered email password into the project" do
- main.project.should_receive(:email_password=).with("asdf")
- input.stub(:gets).and_return("email@s.com", "asdf")
- main.prompt_for_user(input, output)
- end
+ describe "#build_command" do
+ context "command name is 'request'"do
+ let(:main) { Main.new(['request']) }
- it "Asks for the remote repository name" do
- output.should_receive(:print).
- with("Remote repository (default origin): ")
- main.prompt_for_user(input, output)
+ it "creates a Reviewr::Request" do
+ Request.should_receive(:new)
+ main.build_command
+ end
end
- it "Sets the entered remote repository into the project" do
- main.project.should_receive(:remote_repo=).with("remote_name")
- input.stub(:gets).and_return("remote_name\n")
- main.prompt_for_user(input, output)
- end
+ context "when passed a non-existant command" do
+ let(:main) { Main.new(['blah']) }
- it "Uses the default remote repository if an empty string is entered" do
- main.project.should_not_receive(:remote_repo=)
- input.stub(:gets).and_return("\n")
- main.prompt_for_user(input, output)
+ it "creates a help command" do
+ Help.should_receive(:new)
+ main.build_command
+ end
end
end
end
View
8 spec/reviewr/cli/request_spec.rb
@@ -5,11 +5,17 @@ module CLI
describe Request do
let(:project) { double("Project").as_null_object }
let(:mailer) { double("Mailer").as_null_object }
- let(:request) { Request.new(project) }
+ let(:request){ Request.new(project) }
describe "#call" do
before do
Mailer.stub!(:new).and_return(mailer)
+ request.stub(:prompt_for_user)
+ end
+
+ it "prompts for user information" do
+ request.should_receive(:prompt_for_user)
+ request.call
end
it "creates a branch for review" do
Please sign in to comment.
Something went wrong with that request. Please try again.