<commit_before>var fs = require('fs'),
  exec = require('child_process').exec,
  swig = require('../lib/swig'),
  expect = require('expect.js'),
  _ = require('lodash'),
  path = require('path'),
  bin = path.normalize(__dirname + '/../bin/swig.js');

function isTest(f) {
  return (/\.test\.html$/).test(f);
}

function isExpectation(f) {
  return (/\.expectation\.html$/).test(f);
}

var casefiles = fs.readdirSync(__dirname + '/cases/'),
  tests = _.filter(casefiles, isTest),
  expectations = _.filter(casefiles, isExpectation),
  cases = _.groupBy(tests.concat(expectations), function (f) {
    return f.split('.')[0];
  });

describe('bin/swig -v', function () {
  it('shows the version number', function (done) {
    exec(bin + ' -v', function (err, stdout, stderr) {
      expect((/^\d+\.\d+\.\d+/).test(stdout)).to.equal(true);
      done();
    });
  });
});

describe('bin/swig render', function () {
  _.each(cases, function (files, c) {
    var test = _.find(files, isTest),
      expectation = fs.readFileSync(__dirname + '/cases/' + _.find(files, isExpectation), 'utf8');

    it(c, function (done) {
      exec(bin + ' render ./tests/cases/' + test + ' -j ' + __dirname + '/bin.locals.json', function (err, stdout, stderr) {
        expect(stdout.replace(/\n$/, '')).to.equal(expectation);
        done();
      });
    });
  });
});

describe('bin/swig compile + run', function () {
  _.each(cases, function (files, c) {
    var test = _.find(files, isTest),
      expectation = fs.readFileSync(__dirname + '/cases/' + _.find(files, isExpectation), 'utf8');

    it(c, function (done) {
      exec(bin + ' compile ./tests/cases/' + test + ' -j ' + __dirname + '/bin.locals.json -o ../tmp', function (err, stdout, stderr) {
        exec(bin + ' run ../tmp/' + test + ' -c ' + __dirname + '/bin.locals.js', function (err, stdout, stdrr) {
          expect(stdout.replace(/\n$/, '')).to.equal(expectation);
          done();
        });
      });
    });
  });
});

describe('bin/swig compile -m', function () {
  it('minifies output', function (done) {
    exec(bin + ' compile ./tests/cases/extends_1.test.html -m', function (err, stdout, stderr) {
      expect(stdout).to.equal('var tpl=function(n){var e=(n.extensions,"");return e+="Hi,\\n\\n",e+="This is the body.",e+="\\n\\nSincerely,\\nMe\\n"};\n');
      done();
    });
  });
});

describe('bin/swig compile & run from swig', function () {
  it('can be run', function (done) {
    var expectation = fs.readFileSync(__dirname + '/cases/extends_1.expectation.html', 'utf8');
    exec(bin + ' compile ./tests/cases/extends_1.test.html -o ../tmp --wrap-start="var foo = "', function (err, stdout, stderr) {
      fs.readFile(__dirname + '/../../tmp/extends_1.test.html', 'utf8', function (err, stdout) {
        var foo;
        eval(stdout);
        expect(swig.run(foo)).to.equal(expectation);
        done();
      });
    });
  });
});<commit_msg>fix bin tests on windows<commit_after>var fs = require('fs'),
  exec = require('child_process').exec,
  swig = require('../lib/swig'),
  expect = require('expect.js'),
  _ = require('lodash'),
  path = require('path'),
  bin = __dirname + '/../bin/swig.js';

function fixPath(p) {
  p = path.normalize(p);
  return (/[A-Z]\:\\/).test(p) ? '"' + p + '"' : p;
}

bin = fixPath(bin);

function isTest(f) {
  return (/\.test\.html$/).test(f);
}

function isExpectation(f) {
  return (/\.expectation\.html$/).test(f);
}

var casefiles = fs.readdirSync(__dirname + '/cases/'),
  tests = _.filter(casefiles, isTest),
  expectations = _.filter(casefiles, isExpectation),
  cases = _.groupBy(tests.concat(expectations), function (f) {
    return f.split('.')[0];
  });

describe('bin/swig -v', function () {
  it('shows the version number', function (done) {
    exec('node ' + bin + ' -v', function (err, stdout, stderr) {
      expect((/^\d+\.\d+\.\d+/).test(stdout)).to.equal(true);
      done();
    });
  });
});

describe('bin/swig render', function () {
  var locals = fixPath(__dirname + '/bin.locals.json');
  _.each(cases, function (files, c) {
    var test = fixPath(__dirname + '/cases/' + _.find(files, isTest)),
      expectation = fs.readFileSync(path.normalize(__dirname + '/cases/' + _.find(files, isExpectation)), 'utf8');

    it(c, function (done) {
      exec('node ' + bin + ' render ' + test + ' -j ' + locals, function (err, stdout, stderr) {
        expect(stdout.replace(/\n$/, '')).to.equal(expectation);
        done();
      });
    });
  });
});

describe('bin/swig compile + run', function () {
  var locals = fixPath(__dirname + '/bin.locals.json');
  _.each(cases, function (files, c) {
    var test = _.find(files, isTest),
      p = fixPath(__dirname + '/cases/' + test),
      expectation = fs.readFileSync(path.normalize(__dirname + '/cases/' + _.find(files, isExpectation)), 'utf8');

    it(c, function (done) {
      var tmp = fixPath(__dirname + '/../tmp');
      exec('node ' + bin + ' compile ' + p + ' -j ' + locals + ' -o ' + tmp, function (err, stdout, stderr) {
        var p = fixPath(tmp + '/' + test);
        exec('node ' + bin + ' run ' + p + ' -c ' + locals, function (err, stdout, stdrr) {
          expect(stdout.replace(/\n$/, '')).to.equal(expectation);
          done();
        });
      });
    });
  });
});

describe('bin/swig compile -m', function () {
  it('minifies output', function (done) {
    var p = fixPath(__dirname + '/cases/extends_1.test.html');
    exec('node ' + bin + ' compile ' + p + ' -m', function (err, stdout, stderr) {
      expect(stdout).to.equal('var tpl=function(n){var e=(n.extensions,"");return e+="Hi,\\n\\n",e+="This is the body.",e+="\\n\\nSincerely,\\nMe\\n"};\n');
      done();
    });
  });
});

describe('bin/swig compile & run from swig', function () {
  it('can be run', function (done) {
    var expectation = fs.readFileSync(__dirname + '/cases/extends_1.expectation.html', 'utf8'),
      p = fixPath(__dirname + '/cases/extends_1.test.html'),
      tmp = fixPath(__dirname + '/../tmp');
    exec('node ' + bin + ' compile ' + p + ' -o ' + tmp + ' --wrap-start="var foo = "', function (err, stdout, stderr) {
      fs.readFile(path.normalize(__dirname + '/../tmp/extends_1.test.html'), 'utf8', function (err, stdout, stderr) {
        var foo;
        eval(stdout);
        expect(swig.run(foo)).to.equal(expectation);
        done();
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.2215775442998275, 'fraction_numerical': 0.0028226438764309236, 'mean_word_length': 2.9321824907521576, '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': '1602420', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2193, 'n_words': 447}