Skip to content

Commit

Permalink
references #378, tests file/clean, references #316, convert to js
Browse files Browse the repository at this point in the history
  • Loading branch information
dbashford committed Feb 3, 2015
1 parent 29f5e09 commit 31836fc
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 76 deletions.
70 changes: 36 additions & 34 deletions lib/modules/file/clean.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
"use strict";
var _clean;

_clean = function(config, options, next) {
var dir, directories, done, fs, i, path, wrench, _;
fs = require('fs');
path = require('path');
_ = require('lodash');
wrench = require('wrench');
dir = config.watch.compiledDir;
directories = wrench.readdirSyncRecursive(dir).filter(function(f) {

// This module removes empty directories after the files
// have been removed from them

var fs = require( "fs" )
, path = require( "path" )
, _ = require( "lodash" )
, wrench = require( "wrench")
;

var _clean = function( config, options, next ) {

var dir = config.watch.compiledDir;
var directories = wrench.readdirSyncRecursive(dir).filter( function( f ) {
return fs.statSync(path.join(dir, f)).isDirectory();
});
if (directories.length === 0) {

if ( directories.length === 0 ) {
return next();
}
i = 0;
done = function() {
if (++i === directories.length) {
return next();

var doneCount = 0;
var done = function(){
if ( ++doneCount === directories.length ) {
next();
}
};
return _.sortBy(directories, 'length').reverse().forEach(function(dir) {
var dirPath, err;
dirPath = path.join(config.watch.compiledDir, dir);
if (fs.existsSync(dirPath)) {
if (config.log.isDebug()) {
config.log.debug("Deleting directory [[ " + dirPath + " ]]");
}

_.sortBy( directories, "length" ).reverse().forEach( function( dir ) {
var dirPath = path.join( config.watch.compiledDir, dir );
if( fs.existsSync( dirPath ) ) {
try {
fs.rmdirSync(dirPath);
config.log.success("Deleted empty directory [[ " + dirPath + " ]]");
} catch (_error) {
err = _error;
if (err.code === 'ENOTEMPTY') {
config.log.info("Unable to delete directory [[ " + dirPath + " ]] because directory not empty");
fs.rmdirSync( dirPath );
config.log.success( "Deleted empty directory [[ " + dirPath + " ]]" );
} catch ( err ) {
if ( err.code === "ENOTEMPTY" ) {
config.log.info( "Unable to delete directory [[ " + dirPath + " ]] because directory not empty." );
} else {
config.log.error("Unable to delete directory, [[ " + dirPath + " ]]");
config.log.error(err);
config.log.error( "Unable to delete directory, [[ " + dirPath + " ]]" );
config.log.error( err );
}
}
}
return done();
done();
});
};

exports.registration = function(config, register) {
return register(['postClean'], 'complete', _clean);
};
exports.registration = function( config, register ) {
register( ["postClean"], "complete", _clean );
};
2 changes: 1 addition & 1 deletion lib/modules/file/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _delete = function( config, options, next ) {
if ( err ) {
config.log.error( "Failed to delete file [[ " + fileName + " ]]" );
} else {
config.log.error( "Deleted file [[ " + fileName + " ]]", options );
config.log.success( "Deleted file [[ " + fileName + " ]]", options );
}
next();
});
Expand Down
35 changes: 0 additions & 35 deletions src/modules/file/clean.coffee

This file was deleted.

51 changes: 51 additions & 0 deletions src/modules/file/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"use strict";

// This module removes empty directories after the files
// have been removed from them

var fs = require( "fs" )
, path = require( "path" )
, _ = require( "lodash" )
, wrench = require( "wrench")
;

var _clean = function( config, options, next ) {

var dir = config.watch.compiledDir;
var directories = wrench.readdirSyncRecursive(dir).filter( function( f ) {
return fs.statSync(path.join(dir, f)).isDirectory();
});

if ( directories.length === 0 ) {
return next();
}

var doneCount = 0;
var done = function(){
if ( ++doneCount === directories.length ) {
next();
}
};

_.sortBy( directories, "length" ).reverse().forEach( function( dir ) {
var dirPath = path.join( config.watch.compiledDir, dir );
if( fs.existsSync( dirPath ) ) {
try {
fs.rmdirSync( dirPath );
config.log.success( "Deleted empty directory [[ " + dirPath + " ]]" );
} catch ( err ) {
if ( err.code === "ENOTEMPTY" ) {
config.log.info( "Unable to delete directory [[ " + dirPath + " ]] because directory not empty." );
} else {
config.log.error( "Unable to delete directory, [[ " + dirPath + " ]]" );
config.log.error( err );
}
}
}
done();
});
};

exports.registration = function( config, register ) {
register( ["postClean"], "complete", _clean );
};
3 changes: 2 additions & 1 deletion test/unit-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require("./units/modules/file/write-test");
require("./units/modules/file/read-test");
require("./units/modules/file/init-test");
require("./units/modules/file/delete-test");
require("./units/modules/file/delete-test");
require("./units/modules/file/clean-test");
93 changes: 93 additions & 0 deletions test/units/modules/file/clean-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var fs = require( "fs" )
, path = require( "path" )
, sinon = require( "sinon" )
, wrench = require( "wrench" )
, utils = require( path.join(process.cwd(), "test", "utils") )
, cleanModule = require( path.join(process.cwd(), "lib", "modules", "file", "clean") )
, fakeMimosaConfig = utils.fakeMimosaConfig();
;

describe( "Mimosa file cleaning workflow module", function(){
var cleanFunction
, spy
, wrenchStub
;

var createWrenchStub = function( directories ) {
sinon.stub(wrench, "readdirSyncRecursive", function(){
return directories;
});
}

before(function(done) {
spy = sinon.spy();
utils.testRegistration( cleanModule, function( func ) {
cleanFunction = func;
done();
}, true);
});

afterEach(function() {
spy.reset();
wrench.readdirSyncRecursive.restore();
});

it("will call lifecycle callback if no directories in project", function() {
createWrenchStub([]);
cleanFunction( fakeMimosaConfig, {}, spy );
expect(spy.calledOnce).to.be.true;
});

var testCallbackWithFiles = function( files, exists ) {
createWrenchStub(files);
var rmdirSyncStub = sinon.stub( fs, "rmdirSync");
var existsSyncStub = sinon.stub( fs, "existsSync", function(){
if( exists === true ) {
return true
} else if( exists === false ) {
return false;
} else {
return Math.random() >= 0.5;
}
});
var statSyncStub = sinon.stub( fs, "statSync", function() {
return {
isDirectory: function() {
return true;
}
}
});
cleanFunction( fakeMimosaConfig, {}, spy );

// if not random, then all directories "do not exist"
// so no attempt to remove the directory will take place
if( exists === false) {
expect(rmdirSyncStub.callCount).to.eql(0);
} else if ( exists === true) {
expect(rmdirSyncStub.callCount).to.eql(files.length);
}

expect(spy.calledOnce).to.be.true;
expect(existsSyncStub.callCount).to.eql(files.length);
expect(statSyncStub.callCount).to.eql(files.length);
fs.existsSync.restore();
fs.statSync.restore();
fs.rmdirSync.restore();
};

it("will call lifecycle callback if one directory in project", function() {
testCallbackWithFiles(["foo"]);
});

it("will call lifecycle callback if multiple directories in project", function() {
testCallbackWithFiles(["foo", "foooooo", "bar", "foo/bar", "baarrrrerrrrr"]);
});

it("will call lifecycle callback and not attempt to remove directories if files do not exist ", function(){
testCallbackWithFiles(["foo", "foooooo"], false);
});

it("will call lifecycle callback and attempt to remove directories if they do exist", function(){
testCallbackWithFiles(["foo", "foooooo"], true);
});
});
3 changes: 1 addition & 2 deletions test/units/modules/file/delete-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ var fs = require( "fs" )
, sinon = require( "sinon" )
, utils = require( path.join(process.cwd(), "test", "utils") )
, deleteModule = require( path.join(process.cwd(), "lib", "modules", "file", "delete") )
, fileUtils = require( path.join(process.cwd(), "lib", "util", "file" ) )
, fakeMimosaConfig = utils.fakeMimosaConfig();
;

describe( "Mimosa file reading workflow module", function(){
describe( "Mimosa file deleting workflow module", function(){
var deleteFunction;

before(function(done) {
Expand Down
1 change: 0 additions & 1 deletion test/units/modules/file/read-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var fs = require( "fs" )
, sinon = require( "sinon" )
, utils = require( path.join(process.cwd(), "test", "utils") )
, readModule = require( path.join(process.cwd(), "lib", "modules", "file", "read") )
, fileUtils = require( path.join(process.cwd(), "lib", "util", "file" ) )
, fakeMimosaConfig = utils.fakeMimosaConfig();
;

Expand Down
10 changes: 8 additions & 2 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ var crypto = require( "crypto" )
, path = require( "path" )
, _ = require( 'lodash' )
, fakeMimosaConfigObj = {
watch: {
compiledDir:"foo",
sourceDir:"bar"
},
extensions: {
javascript:["js", "coffee"],
css:["less"],
Expand Down Expand Up @@ -40,7 +44,7 @@ var fakeMimosaConfig = function() {
return _.cloneDeep(fakeMimosaConfigObj);
};

var testRegistration = function( mod, cb ) {
var testRegistration = function( mod, cb, noExtensions ) {
var workflows, step, writeFunction, extensions;

mod.registration( fakeMimosaConfig(), function( _workflows, _step , _writeFunction, _extensions ) {
Expand All @@ -52,7 +56,9 @@ var testRegistration = function( mod, cb ) {
expect( workflows ).to.be.instanceof( Array );
expect( step ).to.be.a( "string" );
expect( writeFunction ).to.be.instanceof( Function )
expect( extensions ).to.be.instanceof( Array );
if ( !noExtensions ) {
expect( extensions ).to.be.instanceof( Array );
}

cb( writeFunction );
});
Expand Down

0 comments on commit 31836fc

Please sign in to comment.