From 7bd9759e81e98e6cbee90d8454af8c9d84e15cbe Mon Sep 17 00:00:00 2001 From: Jeremy Cook Date: Sun, 23 Nov 2014 22:22:44 -0500 Subject: [PATCH] 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