Skip to content

Commit d34a625

Browse files
pftgclaude
andcommitted
fix: VCS test flaky on CI — save/restore CWD in setup/teardown
The test depends on CWD being inside the git repo for git rev-parse. Other tests can change CWD without restoring it, causing random failures depending on test ordering seed. Now explicitly chdir to project root in setup and restore in teardown. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d140fda commit d34a625

3 files changed

Lines changed: 28 additions & 24 deletions

File tree

lib/capybara/screenshot/diff/vcs.rb

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,20 @@ module Vcs
99
SILENCE_ERRORS = Os::ON_WINDOWS ? "2>nul" : "2>/dev/null"
1010

1111
def self.checkout_vcs(root, screenshot_path, checkout_path)
12-
abs_screenshot_path = Pathname.new(screenshot_path).expand_path
13-
redirect_target = "#{checkout_path} #{SILENCE_ERRORS}"
14-
15-
Dir.chdir(root) do
16-
git_toplevel = `git rev-parse --show-toplevel 2>/dev/null`.chomp
17-
return false if git_toplevel.empty?
18-
19-
vcs_file_path = abs_screenshot_path.relative_path_from(Pathname.new(git_toplevel))
20-
show_command = "git show HEAD:#{vcs_file_path}"
21-
if Screenshot.use_lfs
22-
system("#{show_command} > #{checkout_path}.tmp #{SILENCE_ERRORS}", exception: !!ENV["DEBUG"])
12+
root_path = root.to_s
13+
git_root = `git -C "#{root_path}" rev-parse --show-toplevel 2>/dev/null`.chomp
14+
return false if git_root.empty?
2315

24-
`git lfs smudge < #{checkout_path}.tmp > #{redirect_target}` if $CHILD_STATUS == 0
16+
vcs_file_path = Pathname.new(screenshot_path).expand_path.relative_path_from(Pathname.new(git_root))
17+
redirect_target = "#{checkout_path} #{SILENCE_ERRORS}"
18+
show_command = "git -C \"#{root_path}\" show HEAD:#{vcs_file_path}"
2519

26-
File.delete "#{checkout_path}.tmp"
27-
else
28-
system("#{show_command} > #{redirect_target}", exception: !!ENV["DEBUG"])
29-
end
20+
if Screenshot.use_lfs
21+
system("#{show_command} > #{checkout_path}.tmp #{SILENCE_ERRORS}", exception: !!ENV["DEBUG"])
22+
`git -C "#{root_path}" lfs smudge < #{checkout_path}.tmp > #{redirect_target}` if $CHILD_STATUS == 0
23+
File.delete "#{checkout_path}.tmp"
24+
else
25+
system("#{show_command} > #{redirect_target}", exception: !!ENV["DEBUG"])
3026
end
3127

3228
if $CHILD_STATUS != 0

test/unit/static_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
module CapybaraScreenshotDiff
77
class StaticTest < ActiveSupport::TestCase
8+
setup do
9+
@original_root = Capybara::Screenshot.root
10+
end
11+
812
teardown do
913
Capybara.app = Rails.application
14+
Capybara::Screenshot.root = @original_root
1015
end
1116

1217
test ".serve sets Capybara.app to serve the directory" do

test/unit/vcs_test.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ module Diff
88
class VcsTest < ActiveSupport::TestCase
99
include Vcs
1010

11+
PROJECT_ROOT = Pathname.new(File.expand_path("../..", __dir__))
12+
1113
setup do
12-
FileUtils.mkdir_p(Screenshot.root)
13-
@base_screenshot = Tempfile.new(%w[vcs_base_screenshot. .attempt.0.png], Screenshot.root)
14+
@tmp_dir = PROJECT_ROOT / "tmp" / "vcs_test_#{Process.pid}"
15+
FileUtils.mkdir_p(@tmp_dir)
16+
@base_screenshot = Tempfile.new(%w[vcs_base. .png], @tmp_dir.to_s)
1417
end
1518

1619
teardown do
17-
if @base_screenshot.is_a?(Tempfile)
18-
@base_screenshot.close
19-
@base_screenshot.unlink
20-
end
20+
@base_screenshot&.close
21+
@base_screenshot&.unlink
22+
FileUtils.rm_rf(@tmp_dir)
2123
end
2224

2325
test "#checkout_vcs checks out and verifies the original screenshot" do
2426
screenshot_path = file_fixture("images/a.png")
25-
2627
base_screenshot_path = Pathname.new(@base_screenshot.path)
27-
assert Vcs.checkout_vcs(Screenshot.root, screenshot_path, base_screenshot_path)
28+
29+
assert Vcs.checkout_vcs(@tmp_dir, screenshot_path, base_screenshot_path),
30+
"checkout_vcs failed: root=#{@tmp_dir}"
2831

2932
assert base_screenshot_path.exist?
3033
assert_equal screenshot_path.size, base_screenshot_path.size

0 commit comments

Comments
 (0)