drj
drj

Reputation: 573

How to cleanup files after multiple nested tests in Mocha, pass or fail

I just started using Mocha. I'm writing a test that requires file cleanup, whether the test succeeds or not. Here's what I'm kind of looking for:

describe('example',function(){
      fs.writeFile(file,'foo', function(){
          it('should succeed', function(done){
             done();
             describe('example2', function(){
                it('should do something awesome', function(done){
                  done();
                });
             })
          }

      }
    });
  });
});
afterAllTests(function(){
  fs.unlink(file);
})

What I'm looking for is to delete all my created files during the test. Right now, I delete them after my last test completes. But if any test fails, the files don't get deleted. I did look into after(), but it only runs after my first test. The only other option that I can see is doing afterEvery(), and setting a variable like var testFinished = false; at the beginning and setting the value to true once completed. Is this the best way?

Thanks!

Upvotes: 0

Views: 1255

Answers (1)

user128511
user128511

Reputation:

I wrote a file system wrapper that tracks the files written and can then delete all of them. Note: I only wrapped the functions I was using. You can easily add more.

// trackedfs.js

var fs = require('fs');

function deleteNoFail(filePath) {
  if (filePath && fs.existsSync(filePath)) {
    if (fs.lstatSync(filePath).isDirectory()) {
      fs.rmdirSync(filePath);
    } else {
      fs.unlinkSync(filePath);
    }
  }
};

function makeTrackedFS(options) {
  options = options || {};
  var f = options.fs || fs;

  var files = [];
  var folders = [];
  var links = [];

  function addFile(file) {
    files.push(file);
  }

  function addFolder(folder) {
    folders.push(folder);
  }

  function addLink(link) {
    links.push(link);
  };

  // Expose these in case we want to manually add stuff
  // like if we shell out to another program we can add
  // its output
  f.addFile = addFile;
  f.addFolder = addFolder;
  f.addLink = addLink;

  f.mkdirSync = function(origFn) {
    return function() {
      addFolder(arguments[0]);
      return origFn.apply(f, arguments);
    };
  }(f.mkdirSync);

  f.symlinkSync = function(origFn) {
    return function() {
      addLink(arguments[1]);
      return origFn.apply(f, arguments);
    };
  }(f.symlinkSync);

  f.writeFileSync = function(origFn) {
    return function() {
      addFile(arguments[0]);
      return origFn.apply(f, arguments);
    };
  }(f.writeFileSync);

  function deleteList(list) {
    list.reverse();  // because we want files before dirs
    list.forEach(function(entry) {
      deleteNoFail(entry);
    });
  };

  // call to delete everything
  f.cleanup = function(options) {
    deleteList(links);
    deleteList(files);
    deleteList(folders);
    links = [];
    files = [];
    folders = [];
  };
};

exports.makeTrackedFS = makeTrackedFS;

I can now wrap fs with

var trackedfs = require('trackedfs');     
trackedfs.makeTrackedFS();

and set for cleanup with this

function cleanUpOnExit() {
  fs.cleanup();
};

function cleanUpOnExitAndExit() {
  cleanUpOnExit();
  process.exit();
}

process.on('exit', cleanUpEncodersOnExit);
process.on('SIGINT', cleanUpEncodersOnExitAndExit);
process.on('uncaughtException', cleanUpEncodersOnExitAndExit);

Here's a test

var trackedfs = require('../lib/trackedfs');
var fs = require('fs');

trackedfs.makeTrackedFS();

function cleanUpOnExit() {
  fs.cleanup();
};

function cleanUpOnExitAndExit() {
  cleanUpOnExit();
  process.exit();
}

process.on('exit', cleanUpOnExit);
process.on('SIGINT', cleanUpOnExitAndExit);
process.on('uncaughtException', cleanUpOnExitAndExit);


describe("trackedfs", function() {

  it('makes a directory', function() {
    fs.mkdirSync('foo');
  });

  it('writes a file', function() {
    fs.writeFileSync("foo/bar", "hello");
  });

});

Run it and there will be no foo folder or foo/bar file when done.

The one issue is that it probably can't delete files that are still opened so if you open a file and then crash your test it might not be able to delete it.

Upvotes: 1

Related Questions