<commit_before>#!/usr/bin/env node

var sys = require('sys')
  , assert = require('assert')
  , zfs = require('./zfs').zfs
  , zpool = require('./zfs').zpool;

// assert test is running as root

TestPlanner = function (testCount) {
  this.count = 0;
  var self = this;
  var aborted = false;
  var onExit = function (error) {
    if (aborted) return;
    aborted = true;

    if (error) { 
      puts(error.stack);
    }
    if (self.teardown) {
      puts("Tearing down...");
      self.teardown();
    }
    assert.equal(testCount, self.count,
                 'Number of tests run (' + self.count
                 + ') didn\'t match number of tests planned (' + testCount + ')');
    puts("All tests successful!");
  };

  process.addListener('exit', onExit);
  process.addListener('uncaughtException', onExit);
};

TestPlanner.prototype.track = function (fn) {
  var self = this;
  return function () {
    puts("test-");
    self.count++;
    return fn.apply(undefined, arguments);
  };
};

var testPlan = 18;
var tp = new TestPlanner(testPlan);

tp.teardown = function () {
  puts("tearing down");
  zfs.destroyAll(zfsName, function () {
    puts("destroyed " + zfsName + inspect(arguments));
  });
}

ok = tp.track(assert.ok);
equal = tp.track(assert.equal);

var puts = sys.puts;
var inspect = sys.inspect;

var zfsName = process.argv[2] || 'node-zfs-test/test';
var zpoolName = zfsName.split('/')[0];

function preCheck() {
  // check zpool exists
  zpool.list(function (err, fields, list) {
    ok(list, 'zpools list was empty or did not have a value');
    ok(list.length > 0, "zpool list is empty");
    ok(list.some(function (i) { return i[0] == zpoolName; }),
       "zpool doesn't exist");

    zfs.list(function (err, fields, list) {
      ok(list, 'zfs list was empty or did not have a value');
      ok(list.length > 0, "zfs list is empty");
      ok(!list.some(function (i) { return i[0] == zfsName; }),
         "zfs dataset already exists");

      runTests();
    });
  });
}

function assertDatasetExists(name, callback) {
  zfs.list(name, function (err, fields, list) {
    ok(list.length > 0, "zfs list is empty");
    ok(list.some(function (i) { return i[0] == name; }),
       "zfs dataset doesn't exist");
    callback();
  });
}

function assertDatasetDoesNotExist(name, callback) {
  zfs.list(name, function (err, fields, list) {
    ok(err);
    ok(err.msg.match(/does not exist/));
    ok(!list, "zfs list is empty");
    callback();
  });
}

assertDatasetExists = tp.track(assertDatasetExists);
assertDatasetDoesNotExist = tp.track(assertDatasetDoesNotExist);

function runTests() {
  var tp = 0;

  var tests =
    [ function () {
        zfs.create(zfsName, function () {
          assertDatasetExists(zfsName, next);
        });
      }
    , function () {
      var snapshotName = zfsName + '@mysnapshot';
      zfs.snapshot(snapshotName, function (err, stdout, stderr) {
        assertDatasetExists(snapshotName, next);
      });
    }
    , function () {
      var snapshotName = zfsName + '@mysnapshot';
      assertDatasetExists(snapshotName, function () {
        zfs.destroy(snapshotName, function (err, stdout, stderr) {
          assertDatasetDoesNotExist(snapshotName, next);
        });
      });
    }

    
//     , function () {}
//     , function () {}
    ];

  function next() {
    if (tp >= tests.length) return;

    puts("Starting new test");
    try {
      tests[tp++]();
    }

    catch(e) {
      puts("Error: " + e.toString());
      puts(e.back);
      next();
    }
  }

  next();
}

function test() {
  preCheck();
}

test();<commit_msg>add some negative tests around non-existant datasets<commit_after>#!/usr/bin/env node

var sys = require('sys')
  , assert = require('assert')
  , zfs = require('./zfs').zfs
  , zpool = require('./zfs').zpool;

// assert test is running as root

TestPlanner = function (testCount) {
  this.count = 0;
  var self = this;
  var aborted = false;
  var onExit = function (error) {
//     if (aborted) return;
//     aborted = true;
    if (error) { 
      puts(error.stack);
    }

    if (self.teardown) {
      puts("Tearing down...");
      self.teardown();
    }
    assert.equal(testCount, self.count,
                 'Number of tests run (' + self.count
                 + ') didn\'t match number of tests planned (' + testCount + ')');
    puts("All tests successful!");
  };

  process.addListener('exit', onExit);
  process.addListener('uncaughtException', onExit);
};

TestPlanner.prototype.track = function (fn) {
  var self = this;
  return function () {
    puts("test-");
    self.count++;
    return fn.apply(undefined, arguments);
  };
};

var testPlan = 33;
var tp = new TestPlanner(testPlan);

tp.teardown = function () {
  puts("tearing down");
  zfs.destroyAll(zfsName, function () {
    puts("destroyed " + zfsName + inspect(arguments));
  });
}

ok = tp.track(assert.ok);
equal = tp.track(assert.equal);

var puts = sys.puts;
var inspect = sys.inspect;

var zfsName = process.argv[2] || 'node-zfs-test/test';
var zpoolName = zfsName.split('/')[0];

function preCheck() {
  // check zpool exists
  zpool.list(function (err, fields, list) {
    ok(list, 'zpools list was empty or did not have a value');
    ok(list.length > 0, "zpool list is empty");
    ok(list.some(function (i) { return i[0] == zpoolName; }),
       "zpool doesn't exist");

    zfs.list(function (err, fields, list) {
      ok(list, 'zfs list was empty or did not have a value');
      ok(list.length > 0, "zfs list is empty");
      ok(!list.some(function (i) { return i[0] == zfsName; }),
         "zfs dataset already exists");

      runTests();
    });
  });
}

function assertDatasetExists(name, callback) {
  zfs.list(name, function (err, fields, list) {
    ok(list.length > 0, "zfs list is empty");
    ok(list.some(function (i) { return i[0] == name; }),
       "zfs dataset doesn't exist");
    callback();
  });
}

function assertDatasetDoesNotExist(name, callback) {
  zfs.list(name, function (err, fields, list) {
    ok(err);
    ok(err.msg.match(/does not exist/));
    ok(!list, "zfs list is empty");
    callback();
  });
}

assertDatasetExists = tp.track(assertDatasetExists);
assertDatasetDoesNotExist = tp.track(assertDatasetDoesNotExist);

function runTests() {
  var tp = 0;

  var tests =
    [ function () {
        zfs.create(zfsName, function () {
          assertDatasetExists(zfsName, next);
        });
      }
    , function () {
      var snapshotName = zfsName + '@mysnapshot';
      zfs.snapshot(snapshotName, function (err, stdout, stderr) {
        assertDatasetExists(snapshotName, next);
      });
    }
    , function () {
      var snapshotName = zfsName + '@mysnapshot';
      assertDatasetExists(snapshotName, function () {
        zfs.destroy(snapshotName, function (err, stdout, stderr) {
          assertDatasetDoesNotExist(snapshotName, next);
        });
      });
    }
    , function () {
      var snapshotName = 'thisprobably/doesnotexist';
      assertDatasetDoesNotExist(snapshotName, function () {
        zfs.list(snapshotName, function (err, fields, list) {
          ok(err);
          ok(err.msg.match(/does not exist/),
             'Could list snashot that should not exist');
          next();
        });
      });
    }
    , function () {
      var snapshotName = 'thisprobably/doesnotexist';
      assertDatasetDoesNotExist(snapshotName, function () {
        zfs.destroy(snapshotName, function (err, stdout, stderr) {
          puts("got here");
          ok(err, "Expected an error deleting nonexistant dataset");
          ok(typeof(err.code) === 'number');
          ok(err.code !== 0, "Return code should be non-zero");
          ok(stderr.match(/does not exist/),
             'Error message did not indicate that dataset does not exist');
          next();
        });
      });
    }
  ];

  function next() {
    if (tp >= tests.length) return;

    puts("Starting new test");
    try {
      tests[tp++]();
    }

    catch(e) {
      puts("Error: " + e.toString());
      puts(e.back);
      next();
    }
  }

  next();
}

function test() {
  preCheck();
}

test();
{'fraction_non_alphanumeric': 0.15810179493484142, 'fraction_numerical': 0.003073518564052127, 'mean_word_length': 2.7010919017288444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '445480', 'n_tokens_mistral': 2666, 'n_tokens_neox': 2694, 'n_words': 637}