Metalskin
Metalskin

Reputation: 4268

How to test if two files are identical via mocha

I have written a node.js script which scans a very large legacy code base and applies a set of changes.

The changes can be quite complex because this affects certain storage mechanisms and associated size definitions.

I've written tests that all the calculations and reporting is correct by using mocha, chai and sinon. '

Now I'm attempting to write a test that applies the changes to 20 samples of the legacy source. I can create a version of the 20 samples with the correct changes, I just want my test to compare the applied changes to the predicted changes.

So in essence the test is a diff between file A and file B, where the results should be that the files are identical. Google is showing a lot of results on array, string and object diffs. Does anyone have a solution of how to do this for files?

I should note that I'm not trying to reinvent the wheel. I've experimented with prettydiff, with something along the lines of:

prettydiff({source: processedFile, diff: expectedFile, lang: 'text'}).should.equal("Number of differences: 0 differences from 0 lines of text.");

I've also tried:

expect(prettydiff({source: processedFile, diff: expectedFile, lang: 'text'})).to.equal("Number of differences: 0 differences from 0 lines of text.");

However all I get in both cases is an error stating Uncaught TypeError: object is not a function.

What is the correct way to test a diff of two files using mocha?

Upvotes: 2

Views: 3918

Answers (3)

Austin Cheney
Austin Cheney

Reputation: 9

Have you tried running Pretty Diff from the command line with the diffcli option?

When I run:

node api/node-local.js source:"prettydiff.js" diff:"prettydiff.js" mode:"diff" diffcli:"true"

The command line reports:

Pretty Diff found 0 differences in 1 files out of 1 file. Executed in 0.058 seconds.

There is some documentation about this: http://prettydiff.com/guide/diffcli.xhtml

Upvotes: -1

Metalskin
Metalskin

Reputation: 4268

This turned out to be a tad more complex than I expected.

With the help of sctskw, we determined that the first problem was using the prettydiff api incorrectly. Unfortunately it didn't resolve how to diff two files within node.js.

The libs that I found only accept strings to perform a diff.

prettydiff adds a lot of html content to the results, such that you're forced to redirect the output to a file and view the results in a browser. Not really suitable if you wish to analyse the results programatically.

jsdiff reports on every single line, with a couple of variables per line to determine if it's a new line or a removed line.

In both cases the files must be read first. So common code for both approaches is as follows:

var fs = require ('fs');

var source = "";
var target = "";

try {
  source = fs.readFileSync('a.txt', 'utf8');
} catch(e) {
  console.error("error reading a.txt: " + e.message);
  return;
}

try {
  target = fs.readFileSync('b.txt', 'utf8');
} catch(e) {
  console.error("error reading a.txt: " + e.message);
  return;
}

To test the files in prettydiff perform the following:

var prettydiff = require('prettydiff');

output = prettydiff.api({
  source: source, 
  mode: 'diff',
  diff: target, 
  lang: 'text',
});

console.log(output[0]);

Just make sure that you redirect the output to a file when you run it, view the results in the browser of your choice.

To test the files in jsdff perform the following:

var diff = require('diff');
var results = diff.diffLines(source, target);
var identical = true;

results.forEach(function(part) {
  if(part.added) {
    console.log("added:   " + part.value);
    identical = false;
  }
  if(part.removed) {
    console.log("removed: " + part.value);
    identical = false;
  }
});

console.log("Identical? " + identical);

Obviously the code isn't the most efficient and a smarter approach could be taken in handling the files instead of using sync right now, but the examples clearly demonstrate how to diff files within node.js.

The second one is useful if you wish to process the results programatically and is the only simple way to determine if the files are identical.

Upvotes: 3

sctskw
sctskw

Reputation: 1598

Maybe this module? Or something like it?

https://www.npmjs.com/package/diff

From their api docs: https://www.npmjs.com/package/prettydiff

var prettydiff = require("prettydiff"),
    args       = {
        source: "asdf",
        diff  : "asdd",
        lang  : "text"
    },
    output     = prettydiff.api(args);

Upvotes: 1

Related Questions