<commit_before>const fs = require('fs-extra');
const looksSame = require('looks-same');

function getImageDiff(path1, path2, diffPath, tolerance) {
  return new Promise(async (resolve, reject) => {
    const [reference, current] = (await Promise.all([
      fs.readFile(path1),
      fs.readFile(path2),
    ])).map(Buffer.from);

    if (current.equals(reference)) {
      return resolve(true);
    }

    return looksSame(reference, current, (err, isSame) => {
      if (err) {
        reject(err);
      } else if (isSame) {
        resolve(isSame);
      } else {
        looksSame.createDiff(
          {
            reference,
            current,
            diff: diffPath,
            tolerance,<commit_msg>Fix bug where looks-same would choke on empty images<commit_after>const fs = require('fs-extra');
const looksSame = require('looks-same');

function getImageDiff(path1, path2, diffPath, tolerance) {
  return new Promise(async (resolve, reject) => {
    const [reference, current] = (await Promise.all([
      fs.readFile(path1),
      fs.readFile(path2),
    ])).map(Buffer.from);

    if (current.equals(reference)) {
      return resolve(true);
    }
    if (reference.length === 0) {
      return reject(new Error('Reference image is empty'));
    }
    if (current.length === 0) {
      return reject(new Error('Current image is empty'));
    }

    return looksSame(reference, current, (err, isSame) => {
      if (err) {
        reject(err);
      } else if (isSame) {
        resolve(isSame);
      } else {
        looksSame.createDiff(
          {
            reference,
            current,
            diff: diffPath,
            tolerance,
{'fraction_non_alphanumeric': 0.13377324535092983, 'fraction_numerical': 0.00599880023995201, 'mean_word_length': 2.3902439024390243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2455923', 'n_tokens_mistral': 494, 'n_tokens_neox': 492, 'n_words': 119}