How to use the diff.createPatch function in diff

To help you get started, we’ve selected a few diff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github juhovh / mocha-jenkins-reporter / lib / jenkins.js View on Github external
var actual = err.actual,
        expected = err.expected;

    var lines, msg = '';

    if (err.actual && err.expected) {
      // make sure actual and expected are strings
      if (!(typeof actual === 'string' || actual instanceof String)) {
        actual = JSON.stringify(err.actual);
      }

      if (!(typeof expected === 'string' || expected instanceof String)) {
        expected = JSON.stringify(err.expected);
      }

      var diffstr = diff.createPatch('string', actual, expected);
      lines = diffstr.split('\n').splice(4);
      msg += lines.map(cleanUp).filter(notBlank).join('\n');
    }

    if (options.junit_report_stack && err.stack) {
      if (msg) msg += '\n';
      lines = err.stack.split('\n').slice(1);
      msg += lines.map(cleanUp).filter(notBlank).join('\n');
    }

    return msg;
  }
github olov / defs / run-tests.js View on Github external
function diffOutput(correct, got, name) {
        if (got !== correct) {
            const patch = diff.createPatch(name, correct, got);
            process.stdout.write(patch);
            process.stdout.write("\n\n");
        }
    }
github fimbullinter / wotan / scripts / api-guard.ts View on Github external
function compareFile(actual: string, fileName: string) {
    let expected = '';
    try {
        expected = fs.readFileSync(fileName, 'utf8');
    } catch {
    }
    if (expected === actual)
        return;
    const output = [
        chalk.underline(fileName),
        chalk.red('Expected'),
        chalk.green('Actual'),
    ];
    const lines = diff.createPatch('', expected, actual, '', '').split(/\n(?!\\)/g).slice(4);
    for (const line of lines) {
        switch (line[0]) {
            case '@':
                break;
            case '+':
                output.push(chalk.green(line));
                break;
            case '-':
                output.push(chalk.red(line));
                break;
            default:
                output.push(line);
        }
    }
    console.log(output.join('\n'));
    process.exitCode = 1;
github morishitter / scssfmt / cli.js View on Github external
function handleDiff (file, original, formatted) {
  let diff
  if (original === formatted) {
    diff = noDiffsWords
  }

  if (chalk.supportsColor) {
    file = chalk.blue(file)
    if (diff) {
      diff = chalk.gray(diff)
    } else {
      diff = JsDiff.createPatch(file, original, formatted)
      diff = diff.split('\n').splice(4).map(line => {
        if (line[0] === '+') {
          line = chalk.green(line)
        } else if (line[0] === '-') {
          line = chalk.red(line)
        } else if (line.match(/^@@\s+.+?\s+@@/) || '\\ No newline at end of file' === line) {
          line = ''
        }
        return chalk.gray(line)
      })
      diff = diff.join('\n').trim()
    }
  } else if (!diff) {
    diff = formatted
  }
  return file + '\n' + diff
github publiclab / Leaflet.DistortableImage / node_modules / mocha / lib / reporters / base.js View on Github external
}
    if (line[0] === '-') {
      return indent + colorLines('diff removed', line);
    }
    if (line.match(/@@/)) {
      return '--';
    }
    if (line.match(/\\ No newline/)) {
      return null;
    }
    return indent + line;
  }
  function notBlank(line) {
    return typeof line !== 'undefined' && line !== null;
  }
  var msg = diff.createPatch('string', actual, expected);
  var lines = msg.split('\n').splice(5);
  return (
    '\n      ' +
    colorLines('diff added', '+ expected') +
    ' ' +
    colorLines('diff removed', '- actual') +
    '\n\n' +
    lines
      .map(cleanUp)
      .filter(notBlank)
      .join('\n')
  );
}
github morishitter / stylefmt / bin / cli.js View on Github external
function handleDiff (file, original, formatted) {
  var diff
  var color = require('colorette')

  if (original === formatted) {
    diff = 'There is no difference with the original file.'
  }

  if (color.enabled) {
    file = color.blue(file)
    if(diff) {
      diff = color.gray(diff)
    } else {
      var JsDiff = require('diff')
      diff = JsDiff.createPatch(file, original, formatted)
      diff = diff.split('\n').splice(4).map(function (line) {
        if (line[0] === '+') {
          line = color.green(line)
        } else if (line[0] === '-') {
          line = color.red(line)
        } else if (line.match(/^@@\s+.+?\s+@@/) || '\\ No newline at end of file' === line) {
          line = ''
        }
        return color.gray(line)
      })
      diff = diff.join('\n').trim()
    }
  } else if (!diff) {
    diff = formatted
  }
  return file + '\n' + diff
github arangodb / arangodb / js / node / node_modules / mocha / lib / reporters / base.js View on Github external
}
    if (line[0] === '-') {
      return indent + colorLines('diff removed', line);
    }
    if (line.match(/@@/)) {
      return '--';
    }
    if (line.match(/\\ No newline/)) {
      return null;
    }
    return indent + line;
  }
  function notBlank(line) {
    return typeof line !== 'undefined' && line !== null;
  }
  var msg = diff.createPatch('string', actual, expected);
  var lines = msg.split('\n').splice(5);
  return (
    '\n      ' +
    colorLines('diff added', '+ expected') +
    ' ' +
    colorLines('diff removed', '- actual') +
    '\n\n' +
    lines
      .map(cleanUp)
      .filter(notBlank)
      .join('\n')
  );
}
github Qihoo360 / wayne / src / frontend / src / app / shared / diff / diff.component.ts View on Github external
let oldstr: string, newStr: string;
    try {
      if (this.inputType === 'json') {
        oldstr = this.formatString(this.diffTpl.oldStr);
        newStr = this.formatString(this.diffTpl.newStr);
      } else {
        oldstr = YAML.dump(this.formatJson(this.diffTpl.oldStr));
        newStr = YAML.dump(this.formatJson(this.diffTpl.newStr));
      }
    } catch {
      oldstr = '';
      newStr = '';
      this.messageService.showError('SHARED.DIFF.CHECK_DATA');
    }
    try {
      const dd = createPatch(
        this.diffTpl.fileName,
        oldstr,
        newStr,
        this.diffTpl.newHeader,
        this.diffTpl.oldHeader
      );
      const outStr = Diff2Html.getJsonFromDiff(
        dd,
        { inputFormat: this.inputType, outputFormat: this.outStyle, showFiles: false, matching: 'lines' }
      );
      const html = Diff2Html.getPrettyHtml(
        outStr, { inputFormat: 'json', outputFormat: this.outStyle, showFiles: false, matching: 'lines' }
      );
      this.html = html;
    } catch {
      this.messageService.showError('SHARED.DIFF.TRANS_ERROR');
github NREL / api-umbrella / lib / config_reloader / worker.js View on Github external
fs.readFile(nginxPath, function(error, data) {
          var oldContent = data.toString();
          if(oldContent === newContent) {
            logger.info('nginx backend config already up-to-date - skipping');
            writeNginxConfigCallback(null, nginxConfig);
          } else {
            logger.info(diff.createPatch('backends.conf', oldContent, newContent, 'old', 'new'));
            write();
          }
        });
      } else {