Permalink
Browse files

Finish up the request review command

  • Loading branch information...
1 parent a9d71ae commit 1b1270cd5f8c7af830e118738fd9bb0057e4b441 Ryan Burrows committed Apr 8, 2010
View
6 bin/reviewr
@@ -0,0 +1,6 @@
+#!/usr/bin/ruby
+
+$LOAD_PATH << 'lib'
+require 'reviewr'
+
+Reviewr::CLI::Main.new(ARGV).run
View
12 features/reviewr_gets_user_info.feature
@@ -34,7 +34,19 @@ Feature: Reviewr gets user information
Then the email password should be set to "asdfasdf"
Scenario: Reviewr asks for email server but sets a default
+ Given my git email is "name@wrongsite.com"
+ And I enter "me@site.com" for my email
+ When I run a reviewr command that requires email
+ Then I should see "Email server (default site.com): "
Scenario: Reviewr defaults the email server if I don't set one
+ Given my git email is "name@site.com"
+ And I use the default email server
+ When I run a reviewr command that requires email
+ Then the email server should be set to "site.com"
Scenario: Reviewr accepts the email server that I enter
+ Given my git email is "name@wrongsite.com"
+ And I enter "site.com" for my email server
+ When I run a reviewr command that requires email
+ Then the email server should be set to "site.com"
View
2 features/step_definitions/reviewr_steps.rb
@@ -29,5 +29,5 @@ def last_commit_msg
end
def reviewr(opts = ['command'])
- @reviewr ||= Reviewr::CLI::Main.new(opts)
+ @reviewr ||= Reviewr::CLI::Main.new(opts, input, output)
end
View
25 features/step_definitions/user_steps.rb
@@ -1,4 +1,7 @@
Given /^assuming I enter valid email account information$/ do
+ Given 'I enter "coder@site.com" for my email'
+ Given 'I enter "asdfasdf" for my email password'
+ Given 'I enter "site.com" for my email server'
end
Given /^I use the default email$/ do
@@ -13,18 +16,34 @@
input.password = pass
end
+Given /^I use the default email server$/ do
+ Given "I enter \"\n\" for my email server"
+end
+
+Given /^I enter "([^\"]*)" for my email server$/ do |server|
+ input.email_server = server
+end
+
When /^I run a reviewr command that requires email$/ do
- @project = reviewr.prompt_for_user(input, output)
+ project = reviewr.prompt_for_user(input, output)
end
Then /^I should see "([^\"]*)"$/ do |msg|
output.messages.should include(msg)
end
Then /^the email should be set to "([^\"]*)"$/ do |email|
- @project.user_email.should == email
+ project.user_email.should == email
end
Then /^the email password should be set to "([^\"]*)"$/ do |pass|
- @project.email_password.should == pass
+ project.email_password.should == pass
+end
+
+Then /^the email server should be set to "([^\"]*)"$/ do |server|
+ project.email_server.should == server
+end
+
+def project
+ reviewr.project
end
View
6 features/support/fake_io.rb
@@ -1,7 +1,7 @@
class Input
def initialize
# default to entering nothing for each step
- @messages = ["", ""]
+ @messages = (0..2).map{ '' }
end
def email=(email)
@@ -12,6 +12,10 @@ def password=(password)
@messages[1] = password
end
+ def email_server=(server)
+ @messages[2] = server
+ end
+
def gets
@messages.shift
end
View
24 features/support/reviewr_env.rb
@@ -0,0 +1,24 @@
+# Override a few methods in Termios
+# These methods are just used for hiding the user's input when
+# reviewr prompts them for a password, but I don't want to deal
+# with this in cucumber so I manually stubbed out the methods
+module Termios
+ class << self
+ def tcgetattr(a)
+ FakeAttr.new
+ end
+
+ def tcsetattr(a, b, c)
+ end
+ end
+
+ class FakeAttr
+ def lflag
+ 1
+ end
+
+ def lflag=(v)
+ end
+ end
+end
+
View
31 lib/reviewr/cli/main.rb
@@ -1,32 +1,55 @@
+require 'termios'
+require 'ruby-debug'
+
module Reviewr
module CLI
class Main
attr_reader :command
attr_accessor :project
- def initialize(args)
+ def initialize(args, input = STDIN, output = STDOUT)
@command = args.shift
@project = Project.new(args.shift)
+ @input, @output = input, output
end
def run
case command
when "request"
+ prompt_for_user(@input, @output)
Request.new(project).call
else
Help.new.call
end
end
- def prompt_for_user(input, output)
+ def prompt_for_user(input = STDIN, output = STDOUT)
output.puts("Email (default #{project.user_email}): ")
email = input.gets.chomp
project.user_email = email unless email.empty?
output.puts("Email password: ")
- project.email_password = input.gets.chomp
+ no_echo(input) do
+ project.email_password = input.gets.chomp
+ end
+
+ output.puts("Email server (default #{project.email_server}): ")
+ server = input.gets.chomp
+ project.email_server = server unless server.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
- project
+ Termios.tcsetattr(input, Termios::TCSANOW, oldt)
end
end
end
View
3 lib/reviewr/git.rb
@@ -23,7 +23,8 @@ def commit(msg)
end
def user_email
- execute('git config user.email')
+ email = execute('git config user.email')
+ email && email.chomp
end
def push_branch(branch_name)
View
6 lib/reviewr/project.rb
@@ -5,7 +5,7 @@ class Project
extend Forwardable
attr_reader :from, :to, :git
- attr_accessor :user_email, :email_password
+ attr_accessor :user_email, :email_password, :email_server
def_delegators :git, :push_branch, :origin_location
@@ -40,5 +40,9 @@ def review_branch
def user_email
@user_email ||= git.user_email
end
+
+ def email_server
+ @email_server ||= user_email.split('@')[1]
+ end
end
end
View
39 spec/reviewr/cli/main_spec.rb
@@ -2,6 +2,11 @@
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"])
@@ -23,12 +28,12 @@ module Reviewr::CLI
it "creates a Reviewr::Request" do
Request.should_receive(:new)
- Main.new(["request", "test@site.com"]).run
+ new_main(["request", "test@site.com"]).run
end
it "calls #call on the request" do
@request.should_receive(:call)
- Main.new(["request"]).run
+ new_main(["request"]).run
end
end
@@ -40,14 +45,20 @@ module Reviewr::CLI
it "creates a help command" do
Help.should_receive(:new)
- Main.new(["asdf"]).run
+ new_main(["asdf"]).run
end
it "calls #call on the help command" do
@help.should_receive(:call)
- Main.new(["ff"]).run
+ 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
@@ -80,7 +91,25 @@ module Reviewr::CLI
it "Sets the entered email password into the project" do
main.project.should_receive(:email_password=).with("asdf")
- input.stub(:gets).and_return("email", "asdf")
+ input.stub(:gets).and_return("email@s.com", "asdf")
+ main.prompt_for_user(input, output)
+ end
+
+ it "Asks for the user's email server with the default based on email" do
+ main.project.stub(:email_server).and_return("site.com")
+ output.should_receive(:puts).with("Email server (default site.com): ")
+ main.prompt_for_user(input, output)
+ end
+
+ it "Sets the entered email server into the project" do
+ main.project.should_receive(:email_server=).with("site.com")
+ input.stub(:gets).and_return("site.com")
+ main.prompt_for_user(input, output)
+ end
+
+ it "Uses the default email if an empty string is entered" do
+ main.project.should_not_receive(:email_server=)
+ input.stub(:gets).and_return("\n")
main.prompt_for_user(input, output)
end
end
View
7 spec/reviewr/project_spec.rb
@@ -19,6 +19,13 @@ module Reviewr
end
end
+ describe "#email_server" do
+ it "pulls the domain from the user's email" do
+ project.user_email = "email@site.com"
+ project.email_server.should == "site.com"
+ end
+ end
+
describe "#review_branch" do
it "appends the review_sha to 'review_'" do
project.stub!(:review_sha).and_return('12345678')

0 comments on commit 1b1270c

Please sign in to comment.