Skip to content

Commit abb0efb

Browse files
committed
test: verify deprecated Git::Log methods emit a deprecation warning
1 parent 9da1e91 commit abb0efb

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

tests/test_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
$stderr.sync = true
1414

1515
# Make tests that emit a deprecation warning fail
16-
16+
#
1717
# Deprecation warnings should not be ignored.
18-
18+
#
1919
# This is important so that:
2020
# * when a user sees a deprecation warning, they can be confident it is coming from
2121
# their code and not this gem
2222
# * test output is clean and does not contain noisey deprecation warnings
23-
23+
#
2424
# Tests whose purpose is to test that a deprecation warning is issued in the right
2525
# circumstance should mock Git::Deprecation#warn to avoid raising an error.
2626
#

tests/units/test_log_deprecations.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require 'git'
5+
require 'fileutils'
6+
require 'tmpdir'
7+
8+
# A test case to verify the deprecation warnings for methods on the Git::Log class.
9+
class LogDeprecationsTest < Test::Unit::TestCase
10+
# Set up a temporary Git repository with a single commit before each test.
11+
def setup
12+
@repo_path = Dir.mktmpdir('git_test')
13+
@repo = Git.init(@repo_path)
14+
15+
# Create a commit so the log has an entry to work with.
16+
Dir.chdir(@repo_path) do
17+
File.write('file.txt', 'content')
18+
@repo.add('file.txt')
19+
@repo.commit('Initial commit')
20+
end
21+
22+
@log = @repo.log
23+
@first_commit = @repo.gcommit('HEAD')
24+
end
25+
26+
# Clean up the temporary repository after each test.
27+
def teardown
28+
FileUtils.rm_rf(@repo_path)
29+
end
30+
31+
# Test the deprecation warning and functionality of Git::Log#each
32+
def test_each_deprecation
33+
Git::Deprecation.expects(:warn).with(
34+
'Calling Git::Log#each is deprecated. Call #execute and then #each on the result object.'
35+
)
36+
37+
commits = @log.map { |c| c }
38+
39+
assert_equal(1, commits.size, 'The #each method should still yield the correct number of commits.')
40+
assert_equal(@first_commit.sha, commits.first.sha, 'The yielded commit should have the correct SHA.')
41+
end
42+
43+
# Test the deprecation warning and functionality of Git::Log#size
44+
def test_size_deprecation
45+
Git::Deprecation.expects(:warn).with(
46+
'Calling Git::Log#size is deprecated. Call #execute and then #size on the result object.'
47+
)
48+
assert_equal(1, @log.size, 'The #size method should still return the correct number of commits.')
49+
end
50+
51+
# Test the deprecation warning and functionality of Git::Log#to_s
52+
def test_to_s_deprecation
53+
Git::Deprecation.expects(:warn).with(
54+
'Calling Git::Log#to_s is deprecated. Call #execute and then #to_s on the result object.'
55+
)
56+
assert_equal(@first_commit.sha, @log.to_s, 'The #to_s method should return the commit SHA.')
57+
end
58+
59+
# Test the deprecation warning and functionality of Git::Log#first
60+
def test_first_deprecation
61+
Git::Deprecation.expects(:warn).with(
62+
'Calling Git::Log#first is deprecated. Call #execute and then #first on the result object.'
63+
)
64+
assert_equal(@first_commit.sha, @log.first.sha, 'The #first method should return the correct commit.')
65+
end
66+
67+
# Test the deprecation warning and functionality of Git::Log#last
68+
def test_last_deprecation
69+
Git::Deprecation.expects(:warn).with(
70+
'Calling Git::Log#last is deprecated. Call #execute and then #last on the result object.'
71+
)
72+
assert_equal(@first_commit.sha, @log.last.sha, 'The #last method should return the correct commit.')
73+
end
74+
75+
# Test the deprecation warning and functionality of Git::Log#[]
76+
def test_indexer_deprecation
77+
Git::Deprecation.expects(:warn).with(
78+
'Calling Git::Log#[] is deprecated. Call #execute and then #[] on the result object.'
79+
)
80+
assert_equal(@first_commit.sha, @log[0].sha, 'The #[] method should return the correct commit at the specified index.')
81+
end
82+
end

0 commit comments

Comments
 (0)