From c432b27c9291a8e2e60e1416f5cfbca82f143cd4 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Fri, 27 Dec 2013 16:59:03 -0500 Subject: [PATCH 1/3] Added guard to project to run tests automatically. --- Gemfile | 3 +++ Guardfile | 9 +++++++++ 2 files changed, 12 insertions(+) create mode 100644 Guardfile diff --git a/Gemfile b/Gemfile index 88b1506..ca71dcb 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,9 @@ gem "thor", ">= 0.14.0" group :development do gem "rake", ">= 0.8.7" gem "rspec", "~> 2.10" + gem "guard" + gem "guard-rspec" + gem "rb-readline", "~> 0.5.0" gem "jeweler", ">= 1.6.2" gem "rcov", :platforms => :mri_18 gem "simplecov", :platforms => :mri_19 diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..ee15012 --- /dev/null +++ b/Guardfile @@ -0,0 +1,9 @@ +guard :rspec do + watch(%r{^spec/.+_spec\.rb$}) + watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } + watch(%r{^lib/homesick/.*\.rb}) { "spec" } + watch('spec/spec_helper.rb') { "spec" } + + notification :tmux, display_message: true +end + From 8f67188c191213b3cdd4507b5444e8fa0f0ead10 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Fri, 27 Dec 2013 17:04:34 -0500 Subject: [PATCH 2/3] Added new tests for status and commit --- Gemfile | 1 + lib/homesick.rb | 12 ++++++------ lib/homesick/actions.rb | 6 +++++- spec/homesick_spec.rb | 31 +++++++++++++++++++++++-------- spec/spec_helper.rb | 3 +++ 5 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index ca71dcb..c4e733d 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ group :development do gem "rcov", :platforms => :mri_18 gem "simplecov", :platforms => :mri_19 gem "test-construct" + gem "capture-output", "~> 1.0.0" if RUBY_VERSION >= '1.9.2' gem "rubocop" end diff --git a/lib/homesick.rb b/lib/homesick.rb index 0616354..71abde8 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -90,9 +90,9 @@ class Homesick < Thor end - desc 'commit CASTLE', "Commit the specified castle's changes" - def commit(name = DEFAULT_CASTLE_NAME) - commit_castle name + desc 'commit CASTLE MESSAGE', "Commit the specified castle's changes" + def commit(name = DEFAULT_CASTLE_NAME, message = nil) + commit_castle name, message end @@ -289,10 +289,10 @@ class Homesick < Thor end end - def commit_castle(castle) + def commit_castle(castle, message) check_castle_existance(castle, 'commit') inside repos_dir.join(castle) do - git_commit_all + git_commit_all :message => message end end @@ -397,7 +397,7 @@ class Homesick < Thor home_path = home_dir.join(relative_dir).join(path) yield(absolute_path, home_path) - end + end end end diff --git a/lib/homesick/actions.rb b/lib/homesick/actions.rb index fe5719f..b079325 100644 --- a/lib/homesick/actions.rb +++ b/lib/homesick/actions.rb @@ -64,7 +64,11 @@ class Homesick def git_commit_all(config = {}) say_status 'git commit all', '', :green unless options[:quiet] - system 'git commit -v -a' unless options[:pretend] + if config[:message] + system "git commit -a -m '#{config[:message]}'" unless options[:pretend] + else + system 'git commit -v -a' unless options[:pretend] + end end def git_add(file, config = {}) diff --git a/spec/homesick_spec.rb b/spec/homesick_spec.rb index d05d735..852ffee 100644 --- a/spec/homesick_spec.rb +++ b/spec/homesick_spec.rb @@ -1,5 +1,6 @@ # -*- encoding : utf-8 -*- require 'spec_helper' +require 'capture-output' describe 'homesick' do let(:home) { create_construct } @@ -341,9 +342,19 @@ describe 'homesick' do end describe 'status' do + it 'should say "nothing to commit" when there are no changes' do + given_castle('castle_repo') + text = Capture.stdout { homesick.status('castle_repo') } + text.should =~ /nothing to commit \(create\/copy files and use "git add" to track\)$/ + end - xit 'needs testing' - + it 'should say "Changes to be committed" when there are changes' do + given_castle('castle_repo') + some_rc_file = home.file '.some_rc_file' + homesick.track(some_rc_file.to_s, 'castle_repo') + text = Capture.stdout { homesick.status('castle_repo') } + text.should =~ /Changes to be committed:.*new file:\s*home\/.some_rc_file/m + end end describe 'diff' do @@ -372,12 +383,6 @@ describe 'homesick' do end - describe 'commit' do - - xit 'needs testing' - - end - describe 'push' do xit 'needs testing' @@ -448,6 +453,16 @@ describe 'homesick' do end end + describe 'commit' do + it 'should have a commit message when the commit succeeds' do + given_castle('castle_repo') + some_rc_file = home.file '.a_random_rc_file' + homesick.track(some_rc_file.to_s, 'castle_repo') + text = Capture.stdout { homesick.commit('castle_repo', 'Test message') } + text.should =~ /^\[master \(root-commit\) \w+\] Test message/ + end + end + describe 'subdir_file' do it 'should add the nested files parent to the subdir_file' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d231ec8..22fd155 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require 'homesick' require 'rspec' require 'rspec/autorun' require 'construct' +require 'tempfile' RSpec.configure do |config| config.include Construct::Helpers @@ -21,6 +22,8 @@ RSpec.configure do |config| castles.directory(path) do |castle| Dir.chdir(castle) do system 'git init >/dev/null 2>&1' + system 'git config user.email "test@test.com"' + system 'git config user.name "Test Name"' system "git remote add origin git://github.com/technicalpickles/#{name}.git >/dev/null 2>&1" if subdirs subdir_file = castle.join(Homesick::SUBDIR_FILENAME) From 78271a9ed4a25f965f490a901f49ea3c9a888a92 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Sat, 28 Dec 2013 22:12:47 -0500 Subject: [PATCH 3/3] 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