<commit_before>'use strict';

var fs        = require('fs')
  , resolve   = require('path').resolve
  , deferred  = require('deferred')
  , delay     = deferred.delay
  , promisify = deferred.promisify
  , mkdir     = promisify(fs.mkdir)
  , open      = promisify(fs.open)
  , rename    = promisify(fs.rename)
  , write     = promisify(fs.write)
  , close     = promisify(fs.close)
  , writeFile = promisify(fs.writeFile)
  , unlink    = promisify(fs.unlink)
  , rmdir     = promisify(fs.rmdir)

  , pgPath = resolve(__dirname, '../__playground/_watch')

module.exports = function (t, a, d) {
	var ondirchange = 0, onfilechange = 0, ondirend = 0, onfileend = 0, tmpPath
	  , DELAY = 100
	  , tmpFilePath, alt
	  , dirPath = resolve(pgPath, 'tmpdir')
	  , filePath = resolve(dirPath, 'tmpfile')
	  , dirCurrent, fileCurrent

	a.throws(function () {
		t(filePath);
	}, "Not existing");

	delay(function () {
		return mkdir(dirPath);
	}, DELAY)()(delay(function () {
		t(dirPath).on('change', function (e) { ++ondirchange; });
		t(dirPath).on('end', function (e) { ++ondirend; });
	}, DELAY))(delay(function () {
		return writeFile(filePath, 'raz');
	}, DELAY))(delay(function () {
		a(ondirchange, 1, "Dir change: File created");
		a(ondirend, 0, "Dir end: File created");
		ondirchange = ondirend = 0;
		t(filePath).on('change', function () { ++onfilechange });
		t(filePath).on('end', function () { ++onfileend });
	}, DELAY))(delay(function () {
		return open(filePath, 'a')(function (fd) {
			return write(fd, new Buffer('dwatrzy'), 0, 3, null)(function () {
				return close(fd);
			});
		});
	}, DELAY))(delay(function () {
		a(ondirchange, 0, "Dir change: File changed");
		a(ondirend, 0, "Dir end: File changed");
		a(onfilechange, 1, "File change: File created");
		a(onfileend, 0, "File end: File created");
		ondirchange = ondirend = onfilechange = onfileend = 0;
		return rename(filePath, filePath + 'r');
	}, DELAY))(delay(function () {
		a(ondirchange, 1, "Dir change: File renamed");
		a(ondirend, 0, "Dir end: File renamed");<commit_msg>Be gentle environement specific bugs<commit_after>'use strict';

var fs        = require('fs')
  , resolve   = require('path').resolve
  , deferred  = require('deferred')
  , delay     = deferred.delay
  , promisify = deferred.promisify
  , mkdir     = promisify(fs.mkdir)
  , open      = promisify(fs.open)
  , rename    = promisify(fs.rename)
  , write     = promisify(fs.write)
  , close     = promisify(fs.close)
  , writeFile = promisify(fs.writeFile)
  , unlink    = promisify(fs.unlink)
  , rmdir     = promisify(fs.rmdir)

  , pgPath = resolve(__dirname, '../__playground/_watch')

module.exports = function (t, a, d) {
	var ondirchange = 0, onfilechange = 0, ondirend = 0, onfileend = 0, tmpPath
	  , DELAY = 100
	  , tmpFilePath, alt
	  , dirPath = resolve(pgPath, 'tmpdir')
	  , filePath = resolve(dirPath, 'tmpfile')
	  , dirCurrent, fileCurrent

	a.throws(function () {
		t(filePath);
	}, "Not existing");

	delay(function () {
		return mkdir(dirPath);
	}, DELAY)()(delay(function () {
		var emitter = t(dirPath);
		emitter.on('change', function (e) { ++ondirchange; });
		emitter.on('end', function (e) { ++ondirend; });
	}, DELAY))(delay(function () {
		return writeFile(filePath, 'raz');
	}, DELAY))(delay(function () {
		var emitter;
		a(ondirchange, 1, "Dir change: File created");
		a(ondirend, 0, "Dir end: File created");
		ondirchange = ondirend = 0;
		emitter = t(filePath);
		emitter.on('change', function () { ++onfilechange });
		emitter.on('end', function () { ++onfileend });
	}, DELAY))(delay(function () {
		return open(filePath, 'a')(function (fd) {
			return write(fd, new Buffer('dwatrzy'), 0, 3, null)(function () {
				return close(fd);
			});
		});
	}, DELAY))(delay(function () {
		a(ondirchange, 0, "Dir change: File changed");
		a(ondirend, 0, "Dir end: File changed");
		a(onfilechange, 1, "File change: File created");
		a(onfileend, 0, "File end: File created");
		ondirchange = ondirend = onfilechange = onfileend = 0;
		return rename(filePath, filePath + 'r');
	}, DELAY))(delay(function () {
		a(ondirchange, 1, "Dir change: File renamed");
		a(ondirend, 0, "Dir end: File renamed");
{'fraction_non_alphanumeric': 0.17779373055754966, 'fraction_numerical': 0.009093084469968891, 'mean_word_length': 3.9176470588235293, 'pattern_counts': {'":': 0, '<': 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': '7512350', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1496, 'n_words': 334}