test.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var fileSyncCmp = require('../');
  3. var assert = require('assert');
  4. var fs = require('fs');
  5. var tmp = require('tmp');
  6. tmp.setGracefulCleanup();
  7. var Q = require('q');
  8. var fsWrite = Q.nfbind(fs.write);
  9. var tmpFile = Q.nfbind(tmp.file);
  10. function write (fd, buf) {
  11. return fsWrite(fd, buf, 0, buf.length, null);
  12. }
  13. describe('equalFiles', function () {
  14. var pathA, pathB;
  15. var fdA, fdB;
  16. beforeEach(function () {
  17. return Q.all([tmpFile(), tmpFile()]).spread(function (a, b) {
  18. pathA = a[0];
  19. pathB = b[0];
  20. fdA = a[1];
  21. fdB = b[1];
  22. });
  23. });
  24. it('should handle empty files', function () {
  25. assert(fileSyncCmp.equalFiles(pathA, pathB));
  26. });
  27. it('should handle equal content', function () {
  28. var buf = new Buffer('File content\n');
  29. var writes = [write(fdA, buf), write(fdB, buf)];
  30. return Q.all(writes).then(function () {
  31. assert(fileSyncCmp.equalFiles(pathA, pathB));
  32. });
  33. });
  34. it('should handle non-equal content', function () {
  35. var bufA = new Buffer('Some text\n');
  36. var bufB = new Buffer('Other text\n');
  37. var writes = [write(fdA, bufA), write(fdB, bufB)];
  38. return Q.all(writes).then(function () {
  39. assert(!fileSyncCmp.equalFiles(pathA, pathB));
  40. });
  41. });
  42. });