From 31d4249f662b8ac130dd2e6b5cab5eca67d7a0e7 Mon Sep 17 00:00:00 2001 From: Yusuke Sangenya Date: Sat, 9 Dec 2023 13:22:43 +0900 Subject: [PATCH] Fix visual regression spec --- app/lib/visual_regression_test/browser.rb | 10 ++-- lib/tasks/visual_regression_test.rake | 71 +++++++++-------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/app/lib/visual_regression_test/browser.rb b/app/lib/visual_regression_test/browser.rb index ffc859c7..edf9b4e4 100644 --- a/app/lib/visual_regression_test/browser.rb +++ b/app/lib/visual_regression_test/browser.rb @@ -43,11 +43,11 @@ def click_rotated(x:, y:) # rubocop:disable Naming/MethodParameterName end def wait_server_up(path) - loop do - sleep 1 - go_to(path) - break if @browser.network.status - end + go_to(path) + @browser.network.status + rescue Ferrum::StatusError + sleep 1 + retry end def go_to(path) diff --git a/lib/tasks/visual_regression_test.rake b/lib/tasks/visual_regression_test.rake index 75d5b459..b3522513 100644 --- a/lib/tasks/visual_regression_test.rake +++ b/lib/tasks/visual_regression_test.rake @@ -3,39 +3,36 @@ def _system(cmd, exception: true) system(cmd, exception:) end -def fork_run_block(&) - pid = fork(&) - - _, status = Process.wait2(pid) - raise "Process exited with status code: #{status.exitstatus}. at #{caller.first}" unless status.success? +def take_screenshot(output_dir:, freeze_time_at: Time.zone.now.to_i) + FileUtils.mkdir_p(output_dir) + scenario = VisualRegressionTest::Scenario.new(output_dir:, freeze_time_at:) + scenario.execute end -namespace :visual_regression_test do - task take_screenshot: :environment do - output_dir = ENV['OUTPUT_DIR'] || 'tmp/visual_regression_test' - freeze_time_at = ENV['FREEZE_TIME_AT'].to_i || Time.zone.now.to_i - FileUtils.mkdir_p(output_dir) - scenario = VisualRegressionTest::Scenario.new(output_dir:, freeze_time_at:) - scenario.execute - end +def compare_screenshot(before_dir:, after_dir:, compare_dir:) + FileUtils.mkdir_p(compare_dir) - task compare: :environment do - before_dir = ENV.fetch('BEFORE_DIR') - after_dir = ENV.fetch('AFTER_DIR') - compare_dir = ENV.fetch('OUTPUT_DIR') - FileUtils.mkdir_p(compare_dir) + Dir.glob(File.join(after_dir, '*')).each do |entry| + filename = File.basename(entry) + before_file = File.join(before_dir, filename) + after_file = File.join(after_dir, filename) + next if File.read(before_file) == File.read(after_file) - Dir.glob(File.join(after_dir, '*')).each do |entry| - filename = File.basename(entry) - before_file = File.join(before_dir, filename) - after_file = File.join(after_dir, filename) - next if File.read(before_file) == File.read(after_file) + compare_file = File.join(compare_dir, filename) + _system("compare #{after_file} #{before_file} #{compare_file}", exception: false) + end +end - compare_file = File.join(compare_dir, filename) - _system("compare #{after_file} #{before_file} #{compare_file}", exception: false) - end +def combine(before_dir:, after_dir:, compare_dir:, combined_dir:) + FileUtils.mkdir_p(combined_dir) + + Dir.glob(File.join(compare_dir, '*')).each do |path| + entry = File.basename path + _system("convert +append #{File.join(before_dir, entry)} #{File.join(after_dir, entry)} #{File.join(compare_dir, entry)} #{File.join(combined_dir, entry)}", exception: false) end +end +namespace :visual_regression_test do task run: :environment do raise 'git diff exists' unless `git status --porcelain`.strip.lines.empty? @@ -48,32 +45,22 @@ namespace :visual_regression_test do before_dir = File.join('tmp', 'visual_regression_test_auto', base_ref.to_s) after_dir = File.join('tmp', 'visual_regression_test_auto', current_ref.to_s) compare_dir = File.join('tmp', 'visual_regression_test_auto', 'compare') + combined_dir = File.join('tmp', 'visual_regression_test_auto', 'combined') _system('rm -rf tmp/visual_regression_test_auto') - fork_run_block do - ENV['OUTPUT_DIR'] = after_dir - ENV['FREEZE_TIME_AT'] = freeze_time_at.to_s - Rake::Task['visual_regression_test:take_screenshot'].invoke - end + take_screenshot(output_dir: after_dir, freeze_time_at:) - fork_run_block do + begin _system("git checkout #{base_branch}") _system('bundle install') - - ENV['OUTPUT_DIR'] = before_dir - ENV['FREEZE_TIME_AT'] = freeze_time_at.to_s - Rake::Task['visual_regression_test:take_screenshot'].invoke + take_screenshot(output_dir: before_dir, freeze_time_at:) ensure _system("git checkout #{current_branch}") end - fork_run_block do - ENV['BEFORE_DIR'] = before_dir - ENV['AFTER_DIR'] = after_dir - ENV['OUTPUT_DIR'] = compare_dir + compare_screenshot(before_dir:, after_dir:, compare_dir:) - Rake::Task['visual_regression_test:compare'].invoke - end + combine(before_dir:, after_dir:, compare_dir:, combined_dir:) end end