<commit_before>require 'which_works'

module RSpec::PageRegression

  RSpec::Matchers.define :match_reference_screenshot do |reference_screenshot_path|

    match do |page|
      @filepaths = FilePaths.new(RSpec.current_example, reference_screenshot_path)
      Renderer.render(page, @filepaths.test_screenshot)
      @comparison = ImageComparison.new(@filepaths)
      @comparison.result == :match
    end

    failure_message do |page|
      msg = case @comparison.result
            when :missing_reference_screenshot then "Missing reference screenshot #{@filepaths.reference_screenshot}"
            when :missing_test_screenshot then "Missing test screenshot #{@filepaths.test_screenshot}"
            when :size_mismatch then "Test screenshot size #{@comparison.test_size.join('x')} does not match reference screenshot size #{@comparison.expected_size.join('x')}"
            when :difference then "Test screenshot does not match reference screenshot"
            end

      msg += "\n    $ #{viewer} #{@filepaths.all.select(&:exist?).join(' ')}"

      case @comparison.result
      when :missing_reference_screenshot
        msg += "\nCreate it via:\n    $ mkdir -p #{@filepaths.reference_screenshot.dirname} && cp #{@filepaths.test_screenshot} #{@filepaths.reference_screenshot}"
      end

      msg
    end

    failure_message_when_negated do |page|
      "Test screenshot expected to not match reference screenshot"
    end

    def viewer
      File.basename(Which.which("open", "feh", "display", :array => true).first || "viewer")
    end
  end<commit_msg>cd to current working dir in failure message;simplifies evaluation of failures on CI system<commit_after>require 'which_works'

module RSpec::PageRegression

  RSpec::Matchers.define :match_reference_screenshot do |reference_screenshot_path|

    match do |page|
      @filepaths = FilePaths.new(RSpec.current_example, reference_screenshot_path)
      Renderer.render(page, @filepaths.test_screenshot)
      @comparison = ImageComparison.new(@filepaths)
      @comparison.result == :match
    end

    failure_message do |page|
      msg = case @comparison.result
            when :missing_reference_screenshot then "Missing reference screenshot #{@filepaths.reference_screenshot}"
            when :missing_test_screenshot then "Missing test screenshot #{@filepaths.test_screenshot}"
            when :size_mismatch then "Test screenshot size #{@comparison.test_size.join('x')} does not match reference screenshot size #{@comparison.expected_size.join('x')}"
            when :difference then "Test screenshot does not match reference screenshot"
            end

      msg += "\n    $ cd #{Pathname.getwd}; #{viewer} #{@filepaths.all.select(&:exist?).join(' ')}"

      case @comparison.result
      when :missing_reference_screenshot
        msg += "\nCreate it via:\n    $ mkdir -p #{@filepaths.reference_screenshot.dirname} && cp #{@filepaths.test_screenshot} #{@filepaths.reference_screenshot}"
      end

      msg
    end

    failure_message_when_negated do |page|
      "Test screenshot expected to not match reference screenshot"
    end

    def viewer
      File.basename(Which.which("open", "feh", "display", :array => true).first || "viewer")
    end
  end
{'fraction_non_alphanumeric': 0.10549043800123381, 'fraction_numerical': 0.0, 'mean_word_length': 3.6527977044476327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2998572', 'n_tokens_mistral': 963, 'n_tokens_neox': 910, 'n_words': 232}