class NoRegrets

Attributes

screenshot_name[R]

Public Class Methods

check_screenshot_for_regressions(screenshot_name) click to toggle source
# File lib/no_regrets.rb, line 11
def self.check_screenshot_for_regressions(screenshot_name)
  new(screenshot_name).check_screenshot_for_regressions
end
new(screenshot_name) click to toggle source
# File lib/no_regrets.rb, line 36
def initialize(screenshot_name)
  @screenshot_name = screenshot_name
end

Public Instance Methods

check_screenshot_for_regressions() click to toggle source
# File lib/no_regrets.rb, line 15
def check_screenshot_for_regressions
  path = Capybara.current_session.save_screenshot("#{screenshot_name}.png")
  sha1 = Digest::SHA1.hexdigest(File.read(path))
  if !File.exists?(cache_file_path(sha1))
    FileUtils.mkdir_p(cache_dir)
    FileUtils.cp(path, cache_file_path(sha1))
  end

  if old_sha1 && old_sha1 != sha1
    raise_error(path, sha1)
  end

  FileUtils.rm(path)
  if !fingerprints[screenshot_name]
    puts "Saving a new screenshot fingerprint for \"#{screenshot_name}\" in #{fingerprint_file_path}"
  end
  fingerprints[screenshot_name] = sha1

  write_fingerprints_file(fingerprints)
end

Private Instance Methods

cache_dir() click to toggle source
# File lib/no_regrets.rb, line 98
def cache_dir
  "#{Capybara.save_path.to_s}/no_regrets"
end
cache_file_path(sha1) click to toggle source
# File lib/no_regrets.rb, line 94
def cache_file_path(sha1)
  "#{cache_dir}/#{sha1}.png"
end
diff_dir() click to toggle source
# File lib/no_regrets.rb, line 102
def diff_dir
  "#{cache_dir}/diffs"
end
diffable?(new_sha1) click to toggle source
# File lib/no_regrets.rb, line 90
def diffable?(new_sha1)
  File.exists?(cache_file_path(old_sha1)) && File.exists?(cache_file_path(new_sha1))
end
fingerprint_file_path() click to toggle source
# File lib/no_regrets.rb, line 86
def fingerprint_file_path
  'spec/support/no_regret_fingerprints.yml'
end
fingerprints() click to toggle source
# File lib/no_regrets.rb, line 44
def fingerprints
  @fingerprints ||= (File.exists?(fingerprint_file_path) && YAML.load(File.read(fingerprint_file_path))) || {}
end
old_sha1() click to toggle source
# File lib/no_regrets.rb, line 74
def old_sha1
  fingerprints[screenshot_name]
end
raise_error(new_screenshot_path, new_sha1) click to toggle source
# File lib/no_regrets.rb, line 48
  def raise_error(new_screenshot_path, new_sha1)
    error_message = <<~ERROR_MESSAGE
      The screenshot \"#{screenshot_name}\" has changed.
      You can view the new screenshot by opening #{new_screenshot_path}.

      If this was expected, you can remove this line:
      #{screenshot_name}: #{fingerprints[screenshot_name]}
      from the file #{fingerprint_file_path}, run this test again, and commit the result.
    ERROR_MESSAGE

    if diffable?(new_sha1)
      error_message << "You can see the diff"

      diff_path = "#{diff_dir}/#{screenshot_name}.png"
      FileUtils.mkdir_p(diff_dir)
      ImageDiff.generate_diff(
        cache_file_path(old_sha1),
        cache_file_path(new_sha1),
        diff_path
      )
      Launchy.open(diff_path)
    end

    raise ScreenshotMismatchError, error_message
  end
write_fingerprints_file(fingerprints) click to toggle source
# File lib/no_regrets.rb, line 78
def write_fingerprints_file(fingerprints)
  dirname = File.dirname(fingerprint_file_path)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end
  File.write(fingerprint_file_path, fingerprints.to_yaml)
end