<commit_before>var buster = require("buster");
var helper = require("./helper");
var fs = require("fs");
var rmrf = require("rimraf");
var path = require("path");

var treeWatcher = require("../lib/tree-watcher");

function p(name) {
    return path.join(helper.ROOT, name);
}

function rootTest(options) {
    return function (done) {
        this.watcher.on(options.event, this.listener);

        options.action.call(this);

        this.triggerWatch(helper.ROOT).then(done(function () {
            assert.calledOnce(this.listener);
        }.bind(this)));
    };
}

buster.testCase('treeWatcher', {
    setUp: function (done) {
        fs.mkdirSync(helper.ROOT, "0755");
        helper.mktree({
            subdir: { "nested.txt": "" },
            deleteme: {},
            ignored: {},
            "exists.txt": ""
        });

        this.stub(fs, "watch");
        this.triggerWatch = function (file) {
            return fs.watch.withArgs(file).args[0][1]();
        };

        this.listener = this.spy();

        this.watcher = treeWatcher.create(helper.ROOT, ["ignored"]);
        this.watcher.init().then(done);
    },

    tearDown: function (done) {
        rmrf(helper.ROOT, done);
    },

    "emits 'file:change'": function () {
        this.watcher.on("file:change", this.listener);

        fs.watch.withArgs(p("exists.txt")).yield("change");

        assert.calledOnce(this.listener);
    },

    "emits 'file:change' for nested files": function () {
        this.watcher.on("file:change", this.listener);

        fs.watch.withArgs(p("subdir/nested.txt")).yield("change");

        assert.calledOnce(this.listener);
    },

    "emits 'file:create'": rootTest({
        event: "file:create",
        action: function () {
            fs.writeFileSync(p("new.txt"), "stuff");
        }
    }),<commit_msg>Added some watch tests in preparation for watcher-abstraction.<commit_after>var buster = require("buster");
var helper = require("./helper");
var fs = require("fs");
var rmrf = require("rimraf");
var path = require("path");

var treeWatcher = require("../lib/tree-watcher");

function p(name) {
    return path.join(helper.ROOT, name);
}

function rootTest(options) {
    return function (done) {
        this.watcher.on(options.event, this.listener);

        options.action.call(this);

        this.triggerWatch(helper.ROOT).then(done(function () {
            assert.calledOnce(this.listener);
        }.bind(this)));
    };
}

buster.testCase('treeWatcher', {
    setUp: function (done) {
        fs.mkdirSync(helper.ROOT, "0755");
        helper.mktree({
            subdir: { "nested.txt": "", "ignored.txt": "" },
            deleteme: {},
            ignored: {},
            "exists.txt": ""
        });

        this.closeWatch = this.spy();
        this.stub(fs, "watch").returns({ close: this.closeWatch });
        this.triggerWatch = function (file) {
            return fs.watch.withArgs(file).args[0][1]();
        };

        this.listener = this.spy();

        this.watcher = treeWatcher.create(helper.ROOT, ["ignored"]);
        this.watcher.init().then(done);
    },

    tearDown: function (done) {
        rmrf(helper.ROOT, done);
    },

    "watches files and directories that aren't excluded": function () {
        assert.called(fs.watch.withArgs(helper.ROOT));
        assert.called(fs.watch.withArgs(p("subdir")));
        assert.called(fs.watch.withArgs(p("subdir/nested.txt")));
        assert.called(fs.watch.withArgs(p("deleteme")));
        assert.called(fs.watch.withArgs(p("exists.txt")));

        refute.called(fs.watch.withArgs(p("subdir/ignored.txt")));
        refute.called(fs.watch.withArgs(p("ignored")));

        assert.equals(fs.watch.callCount, 5);
    },

    "end closes all the watches": function () {
        this.watcher.end();
        assert.equals(this.closeWatch.callCount, 5);
    },

    "emits 'file:change'": function () {
        this.watcher.on("file:change", this.listener);

        fs.watch.withArgs(p("exists.txt")).yield("change");

        assert.calledOnce(this.listener);
    },

    "emits 'file:change' for nested files": function () {
        this.watcher.on("file:change", this.listener);

        fs.watch.withArgs(p("subdir/nested.txt")).yield("change");

        assert.calledOnce(this.listener);
    },

    "emits 'file:create'": rootTest({
        event: "file:create",
        action: function () {
            fs.writeFileSync(p("new.txt"), "stuff");
        }
    }),
{'fraction_non_alphanumeric': 0.17901785714285715, 'fraction_numerical': 0.003125, 'mean_word_length': 2.5676751592356686, 'pattern_counts': {'":': 13, '<': 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': '4707596', 'n_tokens_mistral': 1433, 'n_tokens_neox': 1396, 'n_words': 218}