Skip to content

Commit

Permalink
creates test/z_teardown.js file and puts teardown tests/methods there -
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Jan 10, 2015
1 parent 84efdad commit 4f65d42
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
4 changes: 4 additions & 0 deletions 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();
}
2 changes: 1 addition & 1 deletion lib/fs.js
Expand Up @@ -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);
});
});
}
Expand Down
16 changes: 5 additions & 11 deletions lib/fs_delete.js
Expand Up @@ -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);
}
});
}
Expand Down
27 changes: 1 addition & 26 deletions test/fs.js
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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();
});
});
});
40 changes: 40 additions & 0 deletions 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();
});
});
});

0 comments on commit 4f65d42

Please sign in to comment.