Skip to content

Commit ee11137

Browse files
committed
test: add tests for Git::Base#set_working including deprecation
1 parent e6ccb11 commit ee11137

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/units/test_set_working.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 demonstrate the use of Git::Base#set_working
9+
class SetWorkingTest < Test::Unit::TestCase
10+
# Set up a temporary Git repository before each test.
11+
def setup
12+
# Create a temporary directory for the repository
13+
@repo_path = Dir.mktmpdir('git_test')
14+
15+
# Initialize a new Git repository in the temporary directory
16+
@repo = Git.init(@repo_path)
17+
end
18+
19+
attr_reader :repo_path, :repo
20+
21+
# Clean up the temporary repository after each test.
22+
def teardown
23+
FileUtils.rm_rf(repo_path)
24+
end
25+
26+
# Tests that `set_working` can point to a new, non-existent directory
27+
# when `must_exist: false` is specified.
28+
def test_set_working_with_must_exist_false_for_new_path
29+
custom_working_path = File.join(repo_path, 'custom_work_dir')
30+
assert(!File.exist?(custom_working_path), 'Precondition: Custom working directory should not exist.')
31+
32+
# Action: Set the working directory to a new path, allowing it to not exist.
33+
repo.set_working(custom_working_path, must_exist: false)
34+
35+
# Verification: The repo object should now point to the new working directory path.
36+
assert_equal(custom_working_path, repo.dir.path, 'Working directory path should be updated to the custom path.')
37+
end
38+
39+
# Tests that `set_working` successfully points to an existing directory
40+
# when `must_exist: true` is specified.
41+
def test_set_working_with_must_exist_true_for_existing_path
42+
original_working_path = repo.dir.path
43+
assert(File.exist?(original_working_path), 'Precondition: Original working directory should exist.')
44+
45+
# Action: Set the working directory to the same, existing path, explicitly requiring it to exist.
46+
repo.set_working(original_working_path, must_exist: true)
47+
48+
# Verification: The working directory path should remain unchanged.
49+
assert_equal(original_working_path, repo.dir.path, 'Working directory path should still be the original path.')
50+
end
51+
52+
# Tests that `set_working` raises an ArgumentError when trying to point to a
53+
# non-existent directory with the default behavior (`must_exist: true`).
54+
def test_set_working_with_must_exist_true_raises_error_for_new_path
55+
non_existent_path = File.join(repo_path, 'no_such_dir')
56+
assert(!File.exist?(non_existent_path), 'Precondition: The target working directory path should not exist.')
57+
58+
# Action & Verification: Assert that an ArgumentError is raised.
59+
assert_raise(ArgumentError, 'Should raise ArgumentError for a non-existent working directory path.') do
60+
repo.set_working(non_existent_path) # must_exist defaults to true
61+
end
62+
end
63+
64+
# Tests that using the deprecated `check` argument issues a warning via mocking.
65+
def test_set_working_with_deprecated_check_argument
66+
custom_working_path = File.join(repo_path, 'custom_work_dir')
67+
assert(!File.exist?(custom_working_path), 'Precondition: Custom working directory should not exist.')
68+
69+
# Set up the mock expectation.
70+
# We expect Git::Deprecation.warn to be called once with a message
71+
# matching the expected deprecation warning.
72+
Git::Deprecation.expects(:warn).with(
73+
regexp_matches(/The "check" argument is deprecated/)
74+
)
75+
76+
# Action: Use the deprecated positional argument `check = false`
77+
repo.set_working(custom_working_path, false)
78+
79+
# Verification: The repo object should still point to the new working directory path.
80+
assert_equal(custom_working_path, repo.dir.path, 'Working directory path should be updated even with deprecated argument.')
81+
# Mocha automatically verifies the expectation at the end of the test.
82+
end
83+
end

0 commit comments

Comments
 (0)