Skip to content

Commit

Permalink
Fix exception when Git is autoloaded
Browse files Browse the repository at this point in the history
Signed-off-by: James Couball <jcouball@yahoo.com>
  • Loading branch information
jcouball committed Aug 19, 2022
1 parent e58cd29 commit 04cc2e6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/git/lib.rb
Expand Up @@ -63,6 +63,8 @@ def initialize(base = nil, logger = nil)
@git_work_dir = base[:working_directory]
end
@logger = logger

Git::Lib.warn_if_old_command(self)
end

# creates or reinitializes the repository
Expand Down Expand Up @@ -1027,6 +1029,13 @@ def meets_required_version?
(self.current_command_version <=> self.required_command_version) >= 0
end

def self.warn_if_old_command(lib)
return true if @version_checked
unless lib.meets_required_version?
$stderr.puts "[WARNING] The git gem requires git #{lib.required_command_version.join('.')} or later, but only found #{lib.current_command_version.join('.')}. You should probably upgrade."
end
@version_checked = true
end

private

Expand Down
7 changes: 5 additions & 2 deletions tests/all_tests.rb
@@ -1,5 +1,8 @@
Dir.chdir(File.dirname(__FILE__)) do
Dir.glob('**/test_*.rb') do |test_case|
require "#{File.expand_path(File.dirname(__FILE__))}/#{test_case}"
Dir.glob('**/test_*.rb') do |test_case|
require "#{File.expand_path(File.dirname(__FILE__))}/#{test_case}"
end
end

# To run a single test:
# require_relative 'units/test_lib_meets_required_version'
24 changes: 24 additions & 0 deletions tests/units/test_lib_meets_required_version.rb
@@ -0,0 +1,24 @@
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'

class TestLibMeetsRequiredVersion < Test::Unit::TestCase
def test_with_supported_command_version
lib = Git::Lib.new(nil, nil)
major_version, minor_version = lib.required_command_version
lib.define_singleton_method(:current_command_version) { [major_version, minor_version] }
assert lib.meets_required_version?
end

def test_with_old_command_version
lib = Git::Lib.new(nil, nil)
major_version, minor_version = lib.required_command_version

# Set the major version to be returned by #current_command_version to be an
# earlier version than required
major_version = major_version - 1

lib.define_singleton_method(:current_command_version) { [major_version, minor_version] }
assert !lib.meets_required_version?
end
end

0 comments on commit 04cc2e6

Please sign in to comment.