/*
 * Helper class for cleaning nedb state
 */
"use strict";
var Promise = require("bluebird");
var fs = require("fs");

/**
 * Reset the database, wiping all data.
 * @param {String} databaseUri : The database URI to wipe all data from.
 * @return {Promise} Which is resolved when the database has been cleared.
 */
module.exports._reset = function(databaseUri) {
    if (databaseUri.indexOf("nedb://") !== 0) {
        return Promise.reject("Must be nedb:// URI");
    }
    var baseDbName = databaseUri.substring("nedb://".length);

    function delDatabase(name) {
        var dbPath = baseDbName + name;
        return new Promise(function(resolve, reject) {
            // nuke the world
            fs.unlink(dbPath, function(err) {
                if (err) {
                    if (err.code == "ENOENT") { // already deleted
                        resolve();
                    }
                    else {
                        reject(err);
                    }
                }
                else {
                    resolve();
                }
            });
        });
    }

    return Promise.all([
        delDatabase("/rooms.db"),
        delDatabase("/users.db"),
    ]);
};

/*
 * Helper class for cleaning nedb state
 */
"use strict";
var Promise = require("bluebird");
var fs = require("fs");

/**
 * Reset the database, wiping all data.
 * @param {String} databaseUri : The database URI to wipe all data from.
 * @return {Promise} Which is resolved when the database has been cleared.
 */
module.exports._reset = function(databaseUri) {
    if (databaseUri.indexOf("nedb://") !== 0) {
        return Promise.reject("Must be nedb:// URI");
    }
    var baseDbName = databaseUri.substring("nedb://".length);

    function delDatabase(name) {
        var dbPath = baseDbName + name;
        return new Promise(function(resolve, reject) {
            try {
                // nuke the world
                fs.unlinkSync(dbPath);
                resolve();
            }
            catch (err) {
                if (err.code === "ENOENT") {
                    resolve(); // already deleted
                }
                else {
                    reject(err);
                }
            }
        });
    }

    return Promise.all([
        delDatabase("/rooms.db"),
        delDatabase("/users.db"),
    ]);
};

Use the sync version of unlink to try to resolve spurious Travis failures
Use the sync version of unlink to try to resolve spurious Travis failures

Travis can fail random tests with:

  Error: ENOENT: no such file or directory, rename 'spec-db/users.db~' -> 'spec-db/users.db'
    at Error (native)

This isn't due to double `unlink()` calls (else the operation would be "unlink"
and not "rename"). We don't ever call `fs.rename`. It looks like Travis is
doing something Weird, or maybe a later version of Node is? A quick look through
the Node source didn't show anything interesting. We don't ever use the tilde
suffix either.

The error is **impossible** to catch because it's on a different turn of the
event loop and we didn't call `rename()`, so we can't make it not throw by
adding an `errback`, nor can we `try/catch` because it's async.

Swap to using `unlinkSync` in the vain hope that it will mean that `rename`
will also be called syncly and therefore can be `try/catch`d.
