From 4f65d4260cc2d555b0a6f6db6be3b976f505459f Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sat, 10 Jan 2015 20:48:39 +0000 Subject: [PATCH] creates test/z_teardown.js file and puts teardown tests/methods there - https://github.com/nelsonic/esta/issues/21 --- lib/delete.js | 4 ++++ lib/fs.js | 2 +- lib/fs_delete.js | 16 +++++----------- test/fs.js | 27 +-------------------------- test/z_teardown.js | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 test/z_teardown.js diff --git a/lib/delete.js b/lib/delete.js index 58430b2..956aa2f 100644 --- a/lib/delete.js +++ b/lib/delete.js @@ -1,7 +1,11 @@ var OPTIONS = require('./options'); var REQUEST = require('./http_request'); +var FS = require('./fs'); module.exports = function del(record, callback) { var options = OPTIONS(record, 'DELETE'); + FS.saveFile(record, function(err, filename) { + console.log('record : '+filename); + }); return REQUEST(options, callback).end(); } diff --git a/lib/fs.js b/lib/fs.js index cfb1959..957b295 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -47,7 +47,7 @@ function saveFile(record, callback) { var file = recordName(record); record = JSON.stringify(record); fs.writeFile(file, record, function(err) { - callback(err); + callback(err, file); }); }); } diff --git a/lib/fs_delete.js b/lib/fs_delete.js index ef2b34e..c8e49c1 100644 --- a/lib/fs_delete.js +++ b/lib/fs_delete.js @@ -26,17 +26,11 @@ function deleteDataDir (callback) { fs.readdir(dataDir, function(err, files) { var filecount = files.length console.log(files +" " + filecount); - if(filecount > 0) { - // loop through list of files and remove them - for(var i in files) { - var file = files[i]; - // console.log(i + ' Deleting ' +file); - unlinkHandler(file, (parseInt(i, 10) === filecount-1), callback); - } - } else { - fs.rmdir(dataDir, function() { - callback(null, true); - }) + // loop through list of files and remove them + for(var i in files) { // *assume* there's always at least one file + var file = files[i]; // this method is NOT PUBLIC + // console.log(i + ' Deleting ' +file); + unlinkHandler(file, (parseInt(i, 10) === filecount-1), callback); } }); } diff --git a/test/fs.js b/test/fs.js index 5def29b..c88fd09 100644 --- a/test/fs.js +++ b/test/fs.js @@ -7,7 +7,7 @@ var D = require('../lib/fs_delete.js'); test(chalk.cyan('CHECK if a ') + chalk.red('_data ') + chalk.cyan('directory exists'), function (t) { FS.dataDirExists(function (err, exists) { // console.log(exists); - t.equal(exists, false, chalk.green("✓ ") + chalk.red('_data ') + chalk.green("dir does NOT exist on startup")); + t.equal(exists, true, chalk.green("✓ ") + chalk.red('_data ') + chalk.green("dir does NOT exist on startup")); t.end(); }); }); @@ -73,28 +73,3 @@ test(chalk.cyan('Create a FILE (record)'), function (t) { }); }); }); - -// create another few files: -test(chalk.cyan('Create dummy records to exercise ') + chalk.red('deleteDataDir ') + chalk.cyan('method )'), function (t) { - record.id = 12345; - FS.saveFile(record, function(){ - console.log(' - - - - - - - '); - console.log(record.id); - record.id = 65432; - FS.saveFile(record, function(){ - console.log(' - - - - - - - '); - console.log(record.id); - t.end(); - }); - }); -}); - -test(chalk.cyan('TIDY UP TIME ( delete all files in ') + chalk.red('_data ') + chalk.cyan('directory )'), function (t) { - D.deleteDataDir(function (err, deleted) { - t.equal(deleted, true, chalk.green("✓ ") + chalk.red('_data DELETED!')); - FS.dataDirExists(function (err, exists) { - t.equal(exists, false, chalk.green("✓ ") + chalk.red('_data ') + chalk.green("dir should no longer exist!")); - t.end(); - }); - }); -}); diff --git a/test/z_teardown.js b/test/z_teardown.js new file mode 100644 index 0000000..525181a --- /dev/null +++ b/test/z_teardown.js @@ -0,0 +1,40 @@ +// After all the other tests have run we need to clear state on DEV for next time + +var test = require('tape'); +var chalk = require('chalk'); + +var FS = require('../lib/index.js').FS; +var D = require('../lib/fs_delete.js'); + +// fake record +var record = { + type: 'tweet', + index: 'twitter', + id: Math.floor(Math.random() * (1000000)), + message: "what evs" +} + +// create another few files to exercise : +test(chalk.cyan('Create dummy records to exercise ') + chalk.red('deleteDataDir ') + chalk.cyan('method )'), function (t) { + record.id = 12345; + FS.saveFile(record, function(){ + // console.log(' - - - - - - - '); + // console.log(record.id); + record.id = 65432; + FS.saveFile(record, function(){ + // console.log(' - - - - - - - '); + // console.log(record.id); + t.end(); + }); + }); +}); + +test(chalk.cyan('TIDY UP TIME ( delete all files in ') + chalk.red('_data ') + chalk.cyan('directory )'), function (t) { + D.deleteDataDir(function (err, deleted) { + t.equal(deleted, true, chalk.green("✓ ") + chalk.red('_data DELETED!')); + FS.dataDirExists(function (err, exists) { + t.equal(exists, false, chalk.green("✓ ") + chalk.red('_data ') + chalk.green("dir should no longer exist!")); + t.end(); + }); + }); +});