Skip to content

Commit

Permalink
Partial code coverage of DirectoryWatcher
Browse files Browse the repository at this point in the history
Partial code coverage of DirectoryWatcher
  • Loading branch information
videlais committed Jul 28, 2019
1 parent b61c3ee commit 0ec812e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
56 changes: 24 additions & 32 deletions DirectoryWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,48 @@ class DirectoryWatcher {
constructor (directory, callback) {

this.directory = directory;
this.watcher = null;
this.callback = callback;

// Test if callback is a function or not
if( !(callback instanceof Function) ) {
if( !(this.callback instanceof Function) ) {

throw new Error("Error: Expected function!");

}

// Verify that directory exists before watching it
if(this.check() ) {
try {

this.directory = fs.realpathSync(this.directory);

console.info("Watching " + this.directory + " for changes.");
console.info("Press CTRL+C to stop.");
} catch(event) {

// Setup the Chokidar watcher
let watcher = chokidar.watch(this.directory, {
ignored: /.html/,
persistent: true,
ignoreInitial: true
});
throw new Error("Error: Directory does not exist!");

}

console.info("Watching " + this.directory + " for changes.");
console.info("Press CTRL+C to stop.");

// Setup the Chokidar watcher
this.watcher = chokidar.watch(this.directory, {
ignored: /.html/,
persistent: true,
ignoreInitial: false
});

// Catch change events
watcher.on('change', (path) => {
this.watcher.on('change', (path) => {
console.info("Change detected on " + path);
callback();
});

// Catch add events
watcher.on('add', (path) => {
this.watcher.on('add', (path) => {
console.info("Addition detected on " + path);
callback();
});

}

}

check() {

// Resolve symbolics
this.directory = fs.realpathSync(this.directory);

// Does it exist?
if(fs.existsSync(this.directory) ) {

return true;

} else {
throw new Error("Error: Directory does not exist!");
return false;
}

}

}
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const Story = require('../Story.js');
const Passage = require('../Passage.js');
const DirectoryReader = require('../DirectoryReader.js');
const DirectoryWatcher = require('../DirectoryWatcher.js');
const shelljs = require('shelljs');
const fs = require("fs");

describe('FileReader', function() {

Expand Down Expand Up @@ -584,3 +586,23 @@ describe('DirectoryReader', function() {
});

});

describe('DirectoryWatcher', function() {

describe('#constructor()', function() {

it("Should throw error if not a real path", function() {

assert.throws( () => new DirectoryWatcher("#", function(){}), Error );

});

it("Should throw error if callback not a function", function() {

assert.throws( () => new DirectoryWatcher("test/DirectoryWatcher/", 2), Error );

});

});

});

0 comments on commit 0ec812e

Please sign in to comment.