From b7e2b45e69db5405f0ce7d81d3ef409dc3c0c4dc Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Sat, 8 Nov 2014 13:28:22 -0500 Subject: [PATCH 1/5] Added simple implementation to check if git is installed before executing commands. --- lib/homesick/cli.rb | 7 ++++++- spec/homesick_cli_spec.rb | 13 ++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index fdb5fba..3a308a9 100644 --- a/lib/homesick/cli.rb +++ b/lib/homesick/cli.rb @@ -19,8 +19,13 @@ module Homesick def initialize(args = [], options = {}, config = {}) super - self.shell = Thor::Shell::Color.new + # Check if git is installed + unless `git --version`.size > 0 + say_status :error, 'Git must be installed to use Homesick', :red + exit(1) + end # Hack in support for diffing symlinks + self.shell = Thor::Shell::Color.new class << shell def show_diff(destination, content) destination = Pathname.new(destination) diff --git a/spec/homesick_cli_spec.rb b/spec/homesick_cli_spec.rb index fc0fa3f..9a710f0 100644 --- a/spec/homesick_cli_spec.rb +++ b/spec/homesick_cli_spec.rb @@ -13,7 +13,7 @@ describe Homesick::CLI do before { allow(homesick).to receive(:repos_dir).and_return(castles) } - describe 'smoke test' do + describe 'smoke tests' do context 'when running bin/homesick' do before do bin_path = Pathname.new(__FILE__).parent.parent @@ -23,6 +23,17 @@ describe Homesick::CLI do expect(@output.length).to be > 0 end end + + context 'when git is not installed' do + before do + # The following line suppresses Thor warnings about overriding methods. + allow($stdout).to receive(:write).at_least(:once) + expect_any_instance_of(Homesick::CLI).to receive(:`).and_return('') + end + it 'should raise an exception when Git is not installed' do + Capture.stdout{ expect{Homesick::CLI.new}.to raise_error SystemExit } + end + end end describe 'clone' do From a808f56cafb6e2ae35de5abb4ddebd716d13d365 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Sun, 23 Nov 2014 14:32:47 -0500 Subject: [PATCH 2/5] Tightened up git checking to check for a minimum installed version of Git. --- lib/homesick.rb | 1 + lib/homesick/actions/git_actions.rb | 6 ++++++ lib/homesick/cli.rb | 4 ++-- spec/homesick_cli_spec.rb | 18 ++++++++++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/homesick.rb b/lib/homesick.rb index 1e32c91..4f56f90 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -8,6 +8,7 @@ require 'homesick/cli' # Homesick's top-level module module Homesick GITHUB_NAME_REPO_PATTERN = /\A([A-Za-z0-9_-]+\/[A-Za-z0-9_-]+)\Z/ + GIT_VERSION_PATTERN = /[#{Homesick::Actions::GitActions::MAJOR}-9]\.[#{Homesick::Actions::GitActions::MINOR}-9]\.[#{Homesick::Actions::GitActions::PATCH}-9]/ SUBDIR_FILENAME = '.homesick_subdir' DEFAULT_CASTLE_NAME = 'dotfiles' diff --git a/lib/homesick/actions/git_actions.rb b/lib/homesick/actions/git_actions.rb index 464bba7..56db2ea 100644 --- a/lib/homesick/actions/git_actions.rb +++ b/lib/homesick/actions/git_actions.rb @@ -3,6 +3,12 @@ module Homesick module Actions # Git-related helper methods for Homesick module GitActions + # Information on the minimum git version required for Homesick + MAJOR = 2 + MINOR = 0 + PATCH = 0 + STRING = [MAJOR, MINOR, PATCH].compact.join('.') + # TODO: move this to be more like thor's template, empty_directory, etc def git_clone(repo, config = {}) config ||= {} diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index 3a308a9..99c524c 100644 --- a/lib/homesick/cli.rb +++ b/lib/homesick/cli.rb @@ -20,8 +20,8 @@ module Homesick def initialize(args = [], options = {}, config = {}) super # Check if git is installed - unless `git --version`.size > 0 - say_status :error, 'Git must be installed to use Homesick', :red + unless `git --version` =~ GIT_VERSION_PATTERN + say_status :error, "Git version >= #{Homesick::Actions::GitActions::STRING} must be installed to use Homesick", :red exit(1) end # Hack in support for diffing symlinks diff --git a/spec/homesick_cli_spec.rb b/spec/homesick_cli_spec.rb index 9a710f0..fa218d7 100644 --- a/spec/homesick_cli_spec.rb +++ b/spec/homesick_cli_spec.rb @@ -24,6 +24,19 @@ describe Homesick::CLI do end end + context 'when git is installed' do + before do + # The following line suppresses Thor warnings about overriding methods. + allow($stdout).to receive(:write).at_least(:once) + expect_any_instance_of(Homesick::CLI).to receive(:`).and_return(Homesick::Actions::GitActions::STRING) + end + it 'should not raise an exception when Git is installed' do + output = Capture.stdout{ expect{Homesick::CLI.new}.not_to raise_error } + expect(output.chomp).not_to match(/#{Regexp.escape(Homesick::Actions::GitActions::STRING)}/) + end + + end + context 'when git is not installed' do before do # The following line suppresses Thor warnings about overriding methods. @@ -31,7 +44,8 @@ describe Homesick::CLI do expect_any_instance_of(Homesick::CLI).to receive(:`).and_return('') end it 'should raise an exception when Git is not installed' do - Capture.stdout{ expect{Homesick::CLI.new}.to raise_error SystemExit } + output = Capture.stdout{ expect{Homesick::CLI.new}.to raise_error SystemExit } + expect(output.chomp).to match(/#{Regexp.escape(Homesick::Actions::GitActions::STRING)}/) end end end @@ -695,7 +709,7 @@ describe Homesick::CLI do describe 'version' do it 'prints the current version of homesick' do text = Capture.stdout { homesick.version } - expect(text.chomp).to match(/\d+\.\d+\.\d+/) + expect(text.chomp).to match(/#{Regexp.escape(Homesick::Version::STRING)}/) end end From 7bd9759e81e98e6cbee90d8454af8c9d84e15cbe Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Sun, 23 Nov 2014 22:22:44 -0500 Subject: [PATCH 3/5] Added tests for a minimumGit version of 1.8.0. --- lib/homesick.rb | 1 - lib/homesick/actions/git_actions.rb | 21 +++++++++++++++++---- lib/homesick/cli.rb | 2 +- spec/homesick_cli_spec.rb | 12 ++++++------ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/homesick.rb b/lib/homesick.rb index 4f56f90..1e32c91 100644 --- a/lib/homesick.rb +++ b/lib/homesick.rb @@ -8,7 +8,6 @@ require 'homesick/cli' # Homesick's top-level module module Homesick GITHUB_NAME_REPO_PATTERN = /\A([A-Za-z0-9_-]+\/[A-Za-z0-9_-]+)\Z/ - GIT_VERSION_PATTERN = /[#{Homesick::Actions::GitActions::MAJOR}-9]\.[#{Homesick::Actions::GitActions::MINOR}-9]\.[#{Homesick::Actions::GitActions::PATCH}-9]/ SUBDIR_FILENAME = '.homesick_subdir' DEFAULT_CASTLE_NAME = 'dotfiles' diff --git a/lib/homesick/actions/git_actions.rb b/lib/homesick/actions/git_actions.rb index 56db2ea..0e2a746 100644 --- a/lib/homesick/actions/git_actions.rb +++ b/lib/homesick/actions/git_actions.rb @@ -4,10 +4,23 @@ module Homesick # Git-related helper methods for Homesick module GitActions # Information on the minimum git version required for Homesick - MAJOR = 2 - MINOR = 0 - PATCH = 0 - STRING = [MAJOR, MINOR, PATCH].compact.join('.') + MIN_VERSION = { + major: 1, + minor: 8, + patch: 0 + } + STRING = MIN_VERSION.values.join('.') + + def version_check + info = `git --version`.scan(/(\d+)\.(\d+)\.(\d+)/).flatten.map(&:to_i) + return false unless info.count == 3 + current_version = Hash[ [:major, :minor, :patch].zip(info) ] + return true if current_version.eql?(MIN_VERSION) + current_version.each do |k,v| + return true if v > MIN_VERSION[k] + end + false + end # TODO: move this to be more like thor's template, empty_directory, etc def git_clone(repo, config = {}) diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index 99c524c..2e2eeab 100644 --- a/lib/homesick/cli.rb +++ b/lib/homesick/cli.rb @@ -20,7 +20,7 @@ module Homesick def initialize(args = [], options = {}, config = {}) super # Check if git is installed - unless `git --version` =~ GIT_VERSION_PATTERN + unless version_check say_status :error, "Git version >= #{Homesick::Actions::GitActions::STRING} must be installed to use Homesick", :red exit(1) end diff --git a/spec/homesick_cli_spec.rb b/spec/homesick_cli_spec.rb index fa218d7..c796a4f 100644 --- a/spec/homesick_cli_spec.rb +++ b/spec/homesick_cli_spec.rb @@ -28,11 +28,11 @@ describe Homesick::CLI do before do # The following line suppresses Thor warnings about overriding methods. allow($stdout).to receive(:write).at_least(:once) - expect_any_instance_of(Homesick::CLI).to receive(:`).and_return(Homesick::Actions::GitActions::STRING) + expect_any_instance_of(Homesick::Actions::GitActions).to receive(:`).and_return("git version #{Homesick::Actions::GitActions::STRING}") end - it 'should not raise an exception when Git is installed' do + it 'should not raise an exception' do output = Capture.stdout{ expect{Homesick::CLI.new}.not_to raise_error } - expect(output.chomp).not_to match(/#{Regexp.escape(Homesick::Actions::GitActions::STRING)}/) + expect(output.chomp).not_to include(Homesick::Actions::GitActions::STRING) end end @@ -41,11 +41,11 @@ describe Homesick::CLI do before do # The following line suppresses Thor warnings about overriding methods. allow($stdout).to receive(:write).at_least(:once) - expect_any_instance_of(Homesick::CLI).to receive(:`).and_return('') + expect_any_instance_of(Homesick::Actions::GitActions).to receive(:`).and_return("git version 1.0.0") end - it 'should raise an exception when Git is not installed' do + it 'should raise an exception when' do output = Capture.stdout{ expect{Homesick::CLI.new}.to raise_error SystemExit } - expect(output.chomp).to match(/#{Regexp.escape(Homesick::Actions::GitActions::STRING)}/) + expect(output.chomp).to include(Homesick::Actions::GitActions::STRING) end end end From 03490531d8d827addabb503722e4be41f8011741 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Mon, 24 Nov 2014 08:49:17 -0500 Subject: [PATCH 4/5] Changed name of git check method to be more descriptive. --- lib/homesick/actions/git_actions.rb | 2 +- lib/homesick/cli.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/homesick/actions/git_actions.rb b/lib/homesick/actions/git_actions.rb index 0e2a746..c7fb85b 100644 --- a/lib/homesick/actions/git_actions.rb +++ b/lib/homesick/actions/git_actions.rb @@ -11,7 +11,7 @@ module Homesick } STRING = MIN_VERSION.values.join('.') - def version_check + def git_version_correct? info = `git --version`.scan(/(\d+)\.(\d+)\.(\d+)/).flatten.map(&:to_i) return false unless info.count == 3 current_version = Hash[ [:major, :minor, :patch].zip(info) ] diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index 2e2eeab..8754031 100644 --- a/lib/homesick/cli.rb +++ b/lib/homesick/cli.rb @@ -20,7 +20,7 @@ module Homesick def initialize(args = [], options = {}, config = {}) super # Check if git is installed - unless version_check + unless git_version_correct? say_status :error, "Git version >= #{Homesick::Actions::GitActions::STRING} must be installed to use Homesick", :red exit(1) end From 2c920100936475727a937e55672913ddb7569da8 Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Tue, 25 Nov 2014 21:18:13 -0500 Subject: [PATCH 5/5] Fix to make tests pass in Ruby 1.9.3 --- spec/homesick_cli_spec.rb | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/spec/homesick_cli_spec.rb b/spec/homesick_cli_spec.rb index c796a4f..9df3e2a 100644 --- a/spec/homesick_cli_spec.rb +++ b/spec/homesick_cli_spec.rb @@ -24,23 +24,8 @@ describe Homesick::CLI do end end - context 'when git is installed' do - before do - # The following line suppresses Thor warnings about overriding methods. - allow($stdout).to receive(:write).at_least(:once) - expect_any_instance_of(Homesick::Actions::GitActions).to receive(:`).and_return("git version #{Homesick::Actions::GitActions::STRING}") - end - it 'should not raise an exception' do - output = Capture.stdout{ expect{Homesick::CLI.new}.not_to raise_error } - expect(output.chomp).not_to include(Homesick::Actions::GitActions::STRING) - end - - end - context 'when git is not installed' do before do - # The following line suppresses Thor warnings about overriding methods. - allow($stdout).to receive(:write).at_least(:once) expect_any_instance_of(Homesick::Actions::GitActions).to receive(:`).and_return("git version 1.0.0") end it 'should raise an exception when' do @@ -48,6 +33,16 @@ describe Homesick::CLI do expect(output.chomp).to include(Homesick::Actions::GitActions::STRING) end end + + context 'when git is installed' do + before do + expect_any_instance_of(Homesick::Actions::GitActions).to receive(:`).at_least(:once).and_return("git version #{Homesick::Actions::GitActions::STRING}") + end + it 'should not raise an exception' do + output = Capture.stdout{ expect{Homesick::CLI.new}.not_to raise_error } + expect(output.chomp).not_to include(Homesick::Actions::GitActions::STRING) + end + end end describe 'clone' do