Skip to content

Commit

Permalink
Merge pull request #9 from Designory/tests/create-initial-tests
Browse files Browse the repository at this point in the history
Creates initial tests
  • Loading branch information
romellem authored Aug 31, 2019
2 parents 69c720d + ec6c685 commit 210be09
Show file tree
Hide file tree
Showing 31 changed files with 924 additions and 5 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build-diff": "index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha",
"prepublishOnly": "yarn test"
},
"repository": {
"type": "git",
Expand All @@ -33,5 +34,7 @@
"globby": "^10.0.1",
"lodash": "^4.17.15"
},
"devDependencies": {}
"devDependencies": {
"mocha": "^6.2.0"
}
}
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/folders-to-compare/all-combinations/new/updated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new
Empty file.
Empty file.
1 change: 1 addition & 0 deletions test/folders-to-compare/all-combinations/old/updated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
old
1 change: 1 addition & 0 deletions test/folders-to-compare/blacklist-file/new/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new
1 change: 1 addition & 0 deletions test/folders-to-compare/blacklist-file/old/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
old
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions test/folders-to-compare/paths-lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { readdirSync, statSync } = require('fs');
const { basename, join, relative } = require('path');

const getListOfDirectories = path => readdirSync(path).filter(f => statSync(join(path, f)).isDirectory());
const relative_path = relative(process.cwd(), __dirname);

const test_directories = getListOfDirectories(relative_path);
const PATHS_LOOKUP = {};
test_directories.forEach(dir => (PATHS_LOOKUP[dir] = { old_dir: join(relative_path, dir, 'old'), new_dir: join(relative_path, dir, 'new') }));

/**
* Exports an object with keys that match all directories immediately
* within this 'folders-to-compare' directory. The values on this object
* are also objects with an `old_dir` and `new_dir` key, that are relative (to
* this directory) paths to the 'old' and 'new directories.
*
* This is a bit overkill but the idea is that I can destructure these
* within my tests and pass that to `diffDirectories`.
*
* @example PATHS_LOOKUP = { 'no-change': { old_dir: 'folders-to-compare/no-change/old', new_dir: 'folders-to-compare/no-change/new' } }
*/
module.exports = PATHS_LOOKUP;
1 change: 1 addition & 0 deletions test/folders-to-compare/updated-file/new/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new
1 change: 1 addition & 0 deletions test/folders-to-compare/updated-file/old/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
old
158 changes: 158 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
const assert = require('assert');
const diffDirectories = require('../src/gen-diff-object');
const DEFAULT_BLACKLIST = require('../src/diff-blacklist');

// Ensure tests are run on mac operating system
if (process.platform !== 'darwin') {
console.error('Tests are only supported on macOS at this time');
process.exit(1);
}

const PATHS_LOOKUP = require('./folders-to-compare/paths-lookup');

const FILES_ADDED = 'filesAdded';
const FILES_DELETED = 'filesDeleted';
const FILES_UPDATED = 'filesUpdated';
const RESULTS_KEYS = [FILES_ADDED, FILES_DELETED, FILES_UPDATED];

describe('`diffDirectories` method', function() {
it('should return three keys: `filesAdded`, `filesDeleted`, and `filesUpdated`', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['no-change'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
let keys = Object.keys(result);
keys.sort();

assert.deepEqual(keys, RESULTS_KEYS);
done();
})
.catch(done);
});

it('should return empty arrays when no differences are found', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['no-change'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
RESULTS_KEYS.forEach(key => {
assert.strictEqual(result[key].length, 0);
});

done();
})
.catch(done);
});

it('should find new files', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['new-file'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
assert.strictEqual(result[FILES_ADDED].length, 1);
assert.strictEqual(result[FILES_DELETED].length, 0);
assert.strictEqual(result[FILES_UPDATED].length, 0);

done();
})
.catch(done);
});

it('should find new folders', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['new-folder'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
assert.strictEqual(result[FILES_ADDED].length, 1);
assert.strictEqual(result[FILES_DELETED].length, 0);
assert.strictEqual(result[FILES_UPDATED].length, 0);

done();
})
.catch(done);
});

// @todo Remove this test once we _do_ return all files within new folders
it('should find new folders but not report all new files within that new folder', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['new-folder-with-many-new-files'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
// New folder contains 3 files, but just gets reported as one new folder
assert.strictEqual(result[FILES_ADDED].length, 1);
assert.strictEqual(result[FILES_DELETED].length, 0);
assert.strictEqual(result[FILES_UPDATED].length, 0);

done();
})
.catch(done);
});

it('should find updated files', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['updated-file'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
assert.strictEqual(result[FILES_ADDED].length, 0);
assert.strictEqual(result[FILES_DELETED].length, 0);
assert.strictEqual(result[FILES_UPDATED].length, 1);

done();
})
.catch(done);
});

it('should find deleted files', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['deleted-file'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
assert.strictEqual(result[FILES_ADDED].length, 0);
assert.strictEqual(result[FILES_DELETED].length, 1);
assert.strictEqual(result[FILES_UPDATED].length, 0);

done();
})
.catch(done);
});

it('should handle new/updated/deleted all at once', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['all-combinations'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
// 1 new file + 1 new folder
assert.strictEqual(result[FILES_ADDED].length, 2);
assert.strictEqual(result[FILES_DELETED].length, 1);
assert.strictEqual(result[FILES_UPDATED].length, 1);

done();
})
.catch(done);
});

// @todo support regex/globs instead of exact matches
it('should ignore updated files on blacklist', function(done) {
let { old_dir, new_dir } = PATHS_LOOKUP['blacklist-file'];

diffDirectories(old_dir, new_dir, { log: false })
.then(result => {
assert.strictEqual(result[FILES_ADDED].length, 0);
assert.strictEqual(result[FILES_DELETED].length, 0);
assert.strictEqual(result[FILES_UPDATED].length, 1);
})
.then(() =>
// Run diff again, but ignore 'file.txt'
diffDirectories(old_dir, new_dir, { log: false, blacklist: [...DEFAULT_BLACKLIST, 'file.txt'] })
)
.then(result => {
// With blacklist, we shouldn't have any diffs
assert.strictEqual(result[FILES_ADDED].length, 0);
assert.strictEqual(result[FILES_DELETED].length, 0);
assert.strictEqual(result[FILES_UPDATED].length, 0);

done();
})
.catch(done);
});
});
Loading

0 comments on commit 210be09

Please sign in to comment.