<commit_before>"use strict";
var fs = require("fs");
var resolve = require("path").resolve;
var rmdir = require("rimraf");

var sandboxDir = process.env.SANDBOX_DIR;

var mochaCwd = process.cwd();

function writeFiles() {
  fs.writeFileSync("package.json", "{\n\"version\": \"1.2.3\"\n}");
}

before(function () {
  if (!sandboxDir) {
    sandboxDir = process.env.SANDBOX_DIR = resolve(__dirname, "test_sandbox");
  }
});

beforeEach(function () {
  fs.mkdirSync(sandboxDir);
  process.chdir(resolve(sandboxDir));
  writeFiles();
});

afterEach(function (done) {
  process.chdir(mochaCwd);
  rmdir(resolve(sandboxDir), done);
});

after(function(done){
  if(fs.statSync(resolve(sandboxDir)).isDirectory()) {
    rmdir(resolve(sandboxDir), done);
  } else {
    done();
  }
});<commit_msg>feat(tests): Add support for mockery, truly remove tests directory, even if something brokes<commit_after>"use strict";
var fs = require("fs");
var resolve = require("path").resolve;
var rmdir = require("rimraf");
var mockery = require("mockery");

var sandboxDir = process.env.SANDBOX_DIR;

var mochaCwd = process.cwd();

function writeFiles() {
  fs.writeFileSync("package.json", "{\n\"version\": \"1.2.3\"\n}");
}

before(function () {
  if (!sandboxDir) {
    sandboxDir = process.env.SANDBOX_DIR = resolve(__dirname, "test_sandbox");
  }
});

beforeEach(function () {
  fs.mkdirSync(sandboxDir);
  process.chdir(resolve(sandboxDir));
  writeFiles();

  mockery.enable({
    warnOnUnregistered: false
  });
});

afterEach(function (done) {
  mockery.disable();
  process.chdir(mochaCwd);
  rmdir(resolve(sandboxDir), done);
});

after(function(done){
  // If something gone wrong we force-removal of the test directory, we don't want this directory messing up future tests.
  try {
    rmdir(resolve(sandboxDir), done);
    throw new Error("Test sandbox directory has been forced deleted");
  } catch(err) {
    done();
  }
});
{'fraction_non_alphanumeric': 0.17144346013548722, 'fraction_numerical': 0.003126628452318916, 'mean_word_length': 4.818181818181818, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '203655', 'n_tokens_mistral': 689, 'n_tokens_neox': 656, 'n_words': 135}