Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ def local_branch?(branch)
end

def is_local_branch?(branch) # rubocop:disable Naming/PredicatePrefix
Git.deprecation('Git::Base#is_local_branch? is deprecated. Use Git::Base#local_branch? instead.')
Git::Deprecation.warn(
'Git::Base#is_local_branch? is deprecated and will be removed in a future version. ' \
'Use Git::Base#local_branch? instead.'
)
local_branch?(branch)
end

Expand All @@ -297,7 +300,10 @@ def remote_branch?(branch)
end

def is_remote_branch?(branch) # rubocop:disable Naming/PredicatePrefix
Git.deprecated('Git::Base#is_remote_branch? is deprecated. Use Git::Base#remote_branch? instead.')
Git::Deprecation.warn(
'Git::Base#is_remote_branch? is deprecated and will be removed in a future version. ' \
'Use Git::Base#remote_branch? instead.'
)
remote_branch?(branch)
end

Expand All @@ -308,7 +314,10 @@ def branch?(branch)
end

def is_branch?(branch) # rubocop:disable Naming/PredicatePrefix
Git.deprecated('Git::Base#is_branch? is deprecated. Use Git::Base#branch? instead.')
Git::Deprecation.warn(
'Git::Base#is_branch? is deprecated and will be removed in a future version. ' \
'Use Git::Base#branch? instead.'
)
branch?(branch)
end

Expand Down
5 changes: 4 additions & 1 deletion lib/git/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,10 @@ def diff_parent
end

def set_commit(data) # rubocop:disable Naming/AccessorMethodName
Git.deprecation('Git::Object::Commit#set_commit is deprecated. Use #from_data instead.')
Git::Deprecation.warn(
'Git::Object::Commit#set_commit is deprecated and will be removed in a future version. ' \
'Use #from_data instead.'
)
from_data(data)
end

Expand Down
187 changes: 187 additions & 0 deletions tests/units/test_deprecations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
# frozen_string_literal: true

require_relative '../test_helper'

# Consolidated deprecation tests to ensure all deprecated entry points emit
# Git::Deprecation warnings and still behave as expected.
class TestDeprecations < Test::Unit::TestCase
def setup
clone_working_repo
@git = Git.open(@wdir)
end

def teardown
# Cleanup handled by TestCase#git_teardown
end

# --- Git::Base deprecations ---

def test_base_is_local_branch_deprecation
Git::Deprecation.expects(:warn).with(
'Git::Base#is_local_branch? is deprecated and will be removed in a future version. ' \
'Use Git::Base#local_branch? instead.'
)

assert_equal(true, @git.is_local_branch?(@git.current_branch))
end

def test_base_is_remote_branch_deprecation
Git::Deprecation.expects(:warn).with(
'Git::Base#is_remote_branch? is deprecated and will be removed in a future version. ' \
'Use Git::Base#remote_branch? instead.'
)

# No remotes in fixture; method should return false
assert_equal(false, @git.is_remote_branch?('origin/master'))
end

def test_base_is_branch_deprecation
Git::Deprecation.expects(:warn).with(
'Git::Base#is_branch? is deprecated and will be removed in a future version. ' \
'Use Git::Base#branch? instead.'
)

assert_equal(true, @git.is_branch?(@git.current_branch))
end

def test_base_set_index_check_arg_deprecation
require 'tempfile'
tmp = Tempfile.new('index')
tmp.close

Git::Deprecation.expects(:warn).with(
'The "check" argument is deprecated and will be removed in a future version. ' \
'Use "must_exist:" instead.'
)

# Ensure must_exist is provided to avoid nil | check
@git.set_index(tmp.path, false, must_exist: true)
assert_instance_of(Git::Index, @git.index)
ensure
tmp&.unlink
end

def test_base_set_working_check_arg_deprecation
Dir.mktmpdir('git_work') do |dir|
Git::Deprecation.expects(:warn).with(
'The "check" argument is deprecated and will be removed in a future version. ' \
'Use "must_exist:" instead.'
)

@git.set_working(dir, false, must_exist: true)
assert_equal(dir, @git.dir.path)
end
end

# --- Git::Log deprecations ---

def test_log_each_deprecation
log = @git.log
first_commit = @git.gcommit('HEAD')

Git::Deprecation.expects(:warn).with(
'Calling Git::Log#each is deprecated. Call #execute and then #each on the result object.'
)

commits = log.map { |c| c }
assert_equal(first_commit.sha, commits.first.sha)
end

def test_log_size_deprecation
log = @git.log
Git::Deprecation.expects(:warn).with(
'Calling Git::Log#size is deprecated. Call #execute and then #size on the result object.'
)
assert_operator(log.size, :>=, 1)
end

def test_log_to_s_deprecation
log = @git.log
first_commit = @git.gcommit('HEAD')

Git::Deprecation.expects(:warn).with(
'Calling Git::Log#to_s is deprecated. Call #execute and then #to_s on the result object.'
)
assert_match(first_commit.sha, log.to_s)
end

def test_log_first_deprecation
log = @git.log
first_commit = @git.gcommit('HEAD')

Git::Deprecation.expects(:warn).with(
'Calling Git::Log#first is deprecated. Call #execute and then #first on the result object.'
)
assert_equal(first_commit.sha, log.first.sha)
end

def test_log_last_deprecation
log = @git.log
# Determine expected last via modern API to avoid assumptions about repo history
expected_last_sha = log.execute.last.sha

Git::Deprecation.expects(:warn).with(
'Calling Git::Log#last is deprecated. Call #execute and then #last on the result object.'
)
assert_equal(expected_last_sha, log.last.sha)
end

def test_log_indexer_deprecation
log = @git.log
first_commit = @git.gcommit('HEAD')

Git::Deprecation.expects(:warn).with(
'Calling Git::Log#[] is deprecated. Call #execute and then #[] on the result object.'
)
assert_equal(first_commit.sha, log[0].sha)
end

# --- Git::Object deprecations ---

def test_object_new_is_tag_deprecation
# The `objectish` here is the tag name, as was the old pattern.
tag_name = 'v2.8' # Present in fixtures

Git::Deprecation.expects(:warn).with(
'Git::Object.new with is_tag argument is deprecated. Use Git::Object::Tag.new instead.'
)

tag_object = Git::Object.new(@git, tag_name, nil, true)
assert_instance_of(Git::Object::Tag, tag_object)
assert(tag_object.tag?)
end

def test_commit_set_commit_deprecation_warns_and_delegates
commit = Git::Object::Commit.new(@git, 'deadbeef')

data = {
'sha' => 'deadbeef',
'committer' => { 'name' => 'C', 'email' => 'c@example.com', 'date' => Time.now },
'author' => { 'name' => 'A', 'email' => 'a@example.com', 'date' => Time.now },
'tree' => 'cafebabe',
'parent' => [],
'message' => 'message'
}

Git::Deprecation.expects(:warn).with(
'Git::Object::Commit#set_commit is deprecated and will be removed in a future version. ' \
'Use #from_data instead.'
)

commit.expects(:from_data).with(data)
commit.set_commit(data)
end

# --- Git::Lib deprecations ---

def test_lib_warn_if_old_command_deprecation
# Ensure class-level check does not short-circuit the call in this test
Git::Lib.instance_variable_set(:@version_checked, nil)

Git::Deprecation.expects(:warn).with(
'Git::Lib#warn_if_old_command is deprecated. Use meets_required_version?.'
)

assert_equal(true, Git::Lib.warn_if_old_command(@git.lib))
end
end
82 changes: 0 additions & 82 deletions tests/units/test_log_deprecations.rb

This file was deleted.

19 changes: 0 additions & 19 deletions tests/units/test_object_new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,4 @@ def test_new_creates_blob_object
assert_instance_of(Git::Object::Blob, object, 'Should create a Blob object.')
assert(object.blob?, 'Object#blob? should be true.')
end

# Test that using the deprecated `is_tag` argument creates a Tag object
# and issues a deprecation warning.
def test_new_with_is_tag_deprecation
# Set up the mock expectation for the deprecation warning.
Git::Deprecation.expects(:warn).with(
'Git::Object.new with is_tag argument is deprecated. Use Git::Object::Tag.new instead.'
)

# Action: Call the factory method with the deprecated argument.
# The `objectish` here is the tag name, as was the old pattern.
tag_object = Git::Object.new(@repo, 'v1.0', nil, true)

# Verification
assert_instance_of(Git::Object::Tag, tag_object, 'Should create a Tag object.')
assert(tag_object.tag?, 'Object#tag? should be true.')
assert_equal('v1.0', tag_object.name, 'Tag object should have the correct name.')
# Mocha automatically verifies the expectation at the end of the test.
end
end