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