From 78271a9ed4a25f965f490a901f49ea3c9a888a92 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Sat, 28 Dec 2013 22:12:47 -0500 Subject: [PATCH] Added commands to cd into a castle and to open a shell and to open the default editor in the root of a given castle. --- lib/homesick.rb | 25 +++++++++++++++++++++++++ spec/homesick_spec.rb | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/lib/homesick.rb b/lib/homesick.rb index 71abde8..eb19073 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -239,6 +239,31 @@ class Homesick < Thor end + desc "cd CASTLE", "Open a new shell in the root of the given castle" + def cd(castle = DEFAULT_CASTLE_NAME) + check_castle_existance castle, "cd" + castle_dir = repos_dir.join(castle) + say_status "cd #{castle_dir.realpath}", "Opening a new shell in castle '#{castle}'. To return to the original one exit from the new shell.", :green + inside castle_dir do + system(ENV['SHELL']) + end + end + + desc "open CASTLE", "Open your default editor in the root of the given castle" + def open(castle = DEFAULT_CASTLE_NAME) + if ! ENV['EDITOR'] + say_status :error,"The $EDITOR environment variable must be set to use this command", :red + + exit(1) + end + check_castle_existance castle, "open" + castle_dir = repos_dir.join(castle) + say_status "#{ENV['EDITOR']} #{castle_dir.realpath}", "Opening the root directory of castle '#{castle}' in editor '#{ENV['EDITOR']}'.", :green + inside castle_dir do + system(ENV['EDITOR']) + end + end + protected def home_dir diff --git a/spec/homesick_spec.rb b/spec/homesick_spec.rb index 852ffee..830f2ca 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -528,4 +528,41 @@ describe 'homesick' do castle.should_not be_exist end end + + describe "cd" do + it "cd's to the root directory of the given castle" do + given_castle('castle_repo') + homesick.should_receive("inside").once.with(kind_of(Pathname)).and_yield + homesick.should_receive("system").once.with(ENV["SHELL"]) + Capture.stdout { homesick.cd 'castle_repo' } + end + + it "returns an error message when the given castle does not exist" do + homesick.should_receive("say_status").once.with(:error, /Could not cd castle_repo, expected \/tmp\/construct_container.* exist and contain dotfiles/, :red) + expect { homesick.cd "castle_repo" }.to raise_error(SystemExit) + end + end + + describe "open" do + it "opens the system default editor in the root of the given castle" do + ENV.stub(:[]).and_call_original # Make sure calls to ENV use default values for most things... + ENV.stub(:[]).with('EDITOR').and_return('vim') # Set a default value for 'EDITOR' just in case none is set + given_castle 'castle_repo' + homesick.should_receive("inside").once.with(kind_of(Pathname)).and_yield + homesick.should_receive("system").once.with('vim') + Capture.stdout { homesick.open 'castle_repo' } + end + + it "returns an error message when the $EDITOR environment variable is not set" do + ENV.stub(:[]).with('EDITOR').and_return(nil) # Set the default editor to make sure it fails. + homesick.should_receive("say_status").once.with(:error,"The $EDITOR environment variable must be set to use this command", :red) + expect { homesick.open "castle_repo" }.to raise_error(SystemExit) + end + + it "returns an error message when the given castle does not exist" do + ENV.stub(:[]).with('EDITOR').and_return('vim') # Set a default just in case none is set + homesick.should_receive("say_status").once.with(:error, /Could not open castle_repo, expected \/tmp\/construct_container.* exist and contain dotfiles/, :red) + expect { homesick.open "castle_repo" }.to raise_error(SystemExit) + end + end end