<commit_before>'use strict';

var fs = require('fs'),
    exec = require('child_process').exec,
    grunt = require('grunt');

// Load grunt configuration
require('../Gruntfile.js');

// Cache path to the directory hosting the Gruntfile
// As tests are run via Grunt, this will be the current working directory
var gruntfileDirectory = process.cwd();

function getHookPath(testID, hookName) {
  return 'tmp/' + testID + '/' + (hookName || 'pre-commit');
}

function testHookPermissions(hookPath, test) {
  test.ok(fs.statSync(hookPath).mode.toString(8).match(/755$/), 'Should generate hook file with appropriate permissions (755)');
}

function testHookContent(hookPath, testID, test, hookName) {
  var expected = grunt.file.read('test/expected/' + (hookName || 'pre-commit') + '.' + testID);
  expected = expected.replace('{{expectedWorkingDir}}', gruntfileDirectory.replace('\\','\\\\')); // Paths should have backslashes escaped
  var actual = grunt.file.read(hookPath);
  test.equal(actual, expected, 'Should create hook with appropriate content');
}

function addTest(testSuite, testID) {

  testSuite[testID] = function (test) {

    test.expect(2);
    var hookPath = getHookPath(testID);

    testHookPermissions(hookPath, test);
    testHookContent(hookPath, testID, test);

    test.done();
  };
}

/* Most of the testing is done by matching files generated by the `grunt githooks` calls 
 * to expected files stored in the `test/expected` folder of the project. 
 * Use the following naming conventions:
 *  - name the test tasks `test.<testID>`
 *  - name expected hook `pre-commit.<testID>`
 *  - set `dest` option of your task to `tmp/<testId>`
 */
exports.githooks = {

  'generatedHookExecution': function (test) {

    test.expect(1);
    exec(getHookPath('default'), function (err, stdout) {

      test.notEqual(stdout.indexOf('Boom! Running a task!'), -1);
      test.done();
    });
  },

  'logs.defaultLogging': function (test) {

    test.expect(1);
    exec('grunt githooks:logs.defaultLogging', function(err, stdout) {

      test.notEqual(stdout.indexOf('Binding `aTask` to `pre-commit` Git hook'), -1);
      test.done();
    });  
  },

  'logs.warnIfNotValidHook': function (test) {
    
    test.expect(1);
    exec('grunt githooks:logs.warnIfNotValidHook', function(err, stdout){

      test.notEqual(stdout.indexOf('`definitelyNotTheNameOfAGitHook` is not the name of a Git hook.'), -1);
      test.done();
    });  
  },

  'fails.invalidScriptingLanguage': function (test) {

    test.expect(3);
    exec('grunt githooks:fails.invalidScriptingLanguage', function(err, stdout, stderr){

      test.ok(!!err);
      test.notEqual(stdout.indexOf("doesn't seem to be written in the same language"), -1);
      testHookContent(getHookPath('invalidScriptingLanguage'), 'invalidScriptingLanguage', test);
      test.done();
    });
  },

  'fails.customHashbangInvalidScriptingLanguage': function (test) {
    test.expect(3);
    exec('grunt githooks:fails.customHashbangInvalidScriptingLanguage', function(err, stdout, stderr){

      test.ok(!!err);
      test.notEqual(stdout.indexOf("doesn't seem to be written in the same language"), -1);
      testHookContent(getHookPath('customHashbangInvalidScriptingLanguage'), 'customHashbangInvalidScriptingLanguage', test);
      test.done();
    });
  },

  'test.multipleHooks--commit-msg': function (test) {

    test.expect(2);

    var hookPath = getHookPath('multipleHooks','commit-msg');
    testHookPermissions(hookPath, test);
    testHookContent(hookPath,'multipleHooks', test, 'commit-msg');

    test.done();
  }
};

for (var target in grunt.config.data.githooks) {
  
  var TEST_TARGET = /^test.(.*)$/;
  var match = TEST_TARGET.exec(target);
  if (match) {
    addTest(exports.githooks, match[1]);
  }
}<commit_msg>Removed testing for generated file permissions, as there is now a test actually running the hook<commit_after>'use strict';

var fs = require('fs'),
    exec = require('child_process').exec,
    grunt = require('grunt');

// Load grunt configuration
require('../Gruntfile.js');

// Cache path to the directory hosting the Gruntfile
// As tests are run via Grunt, this will be the current working directory
var gruntfileDirectory = process.cwd();

function getHookPath(testID, hookName) {
  return 'tmp/' + testID + '/' + (hookName || 'pre-commit');
}

function testHookContent(hookPath, testID, test, hookName) {
  var expected = grunt.file.read('test/expected/' + (hookName || 'pre-commit') + '.' + testID);
  expected = expected.replace('{{expectedWorkingDir}}', gruntfileDirectory.replace('\\','\\\\')); // Paths should have backslashes escaped
  var actual = grunt.file.read(hookPath);
  test.equal(actual, expected, 'Should create hook with appropriate content');
}

function addTest(testSuite, testID) {

  testSuite[testID] = function (test) {

    test.expect(1);
    var hookPath = getHookPath(testID);

    testHookContent(hookPath, testID, test);

    test.done();
  };
}

/* Most of the testing is done by matching files generated by the `grunt githooks` calls 
 * to expected files stored in the `test/expected` folder of the project. 
 * Use the following naming conventions:
 *  - name the test tasks `test.<testID>`
 *  - name expected hook `pre-commit.<testID>`
 *  - set `dest` option of your task to `tmp/<testId>`
 */
exports.githooks = {

  'generatedHookExecution': function (test) {

    test.expect(1);
    exec(getHookPath('default'), function (err, stdout) {

      test.notEqual(stdout.indexOf('Boom! Running a task!'), -1);
      test.done();
    });
  },

  'logs.defaultLogging': function (test) {

    test.expect(1);
    exec('grunt githooks:logs.defaultLogging', function(err, stdout) {

      test.notEqual(stdout.indexOf('Binding `aTask` to `pre-commit` Git hook'), -1);
      test.done();
    });  
  },

  'logs.warnIfNotValidHook': function (test) {
    
    test.expect(1);
    exec('grunt githooks:logs.warnIfNotValidHook', function(err, stdout){

      test.notEqual(stdout.indexOf('`definitelyNotTheNameOfAGitHook` is not the name of a Git hook.'), -1);
      test.done();
    });  
  },

  'fails.invalidScriptingLanguage': function (test) {

    test.expect(3);
    exec('grunt githooks:fails.invalidScriptingLanguage', function(err, stdout, stderr){

      test.ok(!!err);
      test.notEqual(stdout.indexOf("doesn't seem to be written in the same language"), -1);
      testHookContent(getHookPath('invalidScriptingLanguage'), 'invalidScriptingLanguage', test);
      test.done();
    });
  },

  'fails.customHashbangInvalidScriptingLanguage': function (test) {
    test.expect(3);
    exec('grunt githooks:fails.customHashbangInvalidScriptingLanguage', function(err, stdout, stderr){

      test.ok(!!err);
      test.notEqual(stdout.indexOf("doesn't seem to be written in the same language"), -1);
      testHookContent(getHookPath('customHashbangInvalidScriptingLanguage'), 'customHashbangInvalidScriptingLanguage', test);
      test.done();
    });
  },

  'test.multipleHooks--commit-msg': function (test) {

    test.expect(1);

    var hookPath = getHookPath('multipleHooks','commit-msg');
    testHookContent(hookPath,'multipleHooks', test, 'commit-msg');

    test.done();
  }
};

for (var target in grunt.config.data.githooks) {
  
  var TEST_TARGET = /^test.(.*)$/;
  var match = TEST_TARGET.exec(target);
  if (match) {
    addTest(exports.githooks, match[1]);
  }
}
{'fraction_non_alphanumeric': 0.14970382337102853, 'fraction_numerical': 0.0044426494345718905, 'mean_word_length': 3.9926075268817205, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5157859', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2325, 'n_words': 553}