Skip to content

Commit 63be6c8

Browse files
committed
fix: ignore git env in vcs checkout
1 parent fbabb1c commit 63be6c8

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

lib/capybara/screenshot/diff/vcs.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ module Diff
99
module Vcs
1010
def self.checkout_vcs(root, screenshot_path, checkout_path)
1111
root_path = root.to_s
12-
git_root, _, status = Open3.capture3("git", "-C", root_path, "rev-parse", "--show-toplevel")
12+
git_env = { "GIT_DIR" => nil, "GIT_WORK_TREE" => nil, "GIT_INDEX_FILE" => nil }
13+
git_root, _, status = Open3.capture3(git_env, "git", "-C", root_path, "rev-parse", "--show-toplevel")
1314
return false unless status.success?
1415

1516
git_root = git_root.chomp
1617
vcs_file_path = Pathname.new(screenshot_path).expand_path.relative_path_from(Pathname.new(git_root)).to_s
1718

1819
if Screenshot.use_lfs
1920
tmp_path = "#{checkout_path}.tmp"
20-
success = system("git", "-C", root_path, "show", "HEAD:#{vcs_file_path}", out: tmp_path, err: File::NULL)
21+
success = system(git_env, "git", "-C", root_path, "show", "HEAD:#{vcs_file_path}", out: tmp_path, err: File::NULL)
2122
if success
22-
system("git", "-C", root_path, "lfs", "smudge", in: tmp_path, out: checkout_path.to_s, err: File::NULL)
23+
system(git_env, "git", "-C", root_path, "lfs", "smudge", in: tmp_path, out: checkout_path.to_s, err: File::NULL)
2324
end
2425
File.delete(tmp_path) if File.exist?(tmp_path)
2526
else
26-
success = system("git", "-C", root_path, "show", "HEAD:#{vcs_file_path}", out: checkout_path.to_s, err: File::NULL)
27+
success = system(git_env, "git", "-C", root_path, "show", "HEAD:#{vcs_file_path}", out: checkout_path.to_s, err: File::NULL)
2728
end
2829

2930
unless success

test/unit/vcs_test.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@ class VcsTest < ActiveSupport::TestCase
3232
assert base_screenshot_path.exist?
3333
assert_equal screenshot_path.size, base_screenshot_path.size
3434
end
35+
36+
test "#checkout_vcs ignores external git env overrides" do
37+
screenshot_path = file_fixture("images/a.png")
38+
base_screenshot_path = Pathname.new(@base_screenshot.path)
39+
40+
with_env("GIT_DIR" => "/tmp/does-not-exist", "GIT_WORK_TREE" => "/tmp/does-not-exist") do
41+
assert Vcs.checkout_vcs(@tmp_dir, screenshot_path, base_screenshot_path),
42+
"checkout_vcs failed with overridden git env: root=#{@tmp_dir}"
43+
end
44+
45+
assert base_screenshot_path.exist?
46+
assert_equal screenshot_path.size, base_screenshot_path.size
47+
end
48+
49+
private
50+
51+
def with_env(vars)
52+
previous = vars.transform_values { |_, _| nil }
53+
vars.each_key { |key| previous[key] = ENV[key] }
54+
vars.each { |key, value| ENV[key] = value }
55+
yield
56+
ensure
57+
previous.each { |key, value| ENV[key] = value }
58+
end
3559
end
3660
end
3761
end

0 commit comments

Comments
 (0)