diff --git a/lib/homesick/actions/git_actions.rb b/lib/homesick/actions/git_actions.rb index 464bba7..c7fb85b 100644 --- a/lib/homesick/actions/git_actions.rb +++ b/lib/homesick/actions/git_actions.rb @@ -3,6 +3,25 @@ module Homesick module Actions # Git-related helper methods for Homesick module GitActions + # Information on the minimum git version required for Homesick + MIN_VERSION = { + major: 1, + minor: 8, + patch: 0 + } + STRING = MIN_VERSION.values.join('.') + + 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) ] + 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 = {}) config ||= {} diff --git a/lib/homesick/cli.rb b/lib/homesick/cli.rb index fdb5fba..8754031 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_correct? + 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 + 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..9df3e2a 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,26 @@ describe Homesick::CLI do expect(@output.length).to be > 0 end end + + context 'when git is not installed' do + before do + expect_any_instance_of(Homesick::Actions::GitActions).to receive(:`).and_return("git version 1.0.0") + end + it 'should raise an exception when' do + output = Capture.stdout{ expect{Homesick::CLI.new}.to raise_error SystemExit } + 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 @@ -684,7 +704,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