UNPKG

3.49 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs'),
4 exec = require('child_process').exec,
5 grunt = require('grunt');
6
7// Load grunt configuration
8require('../Gruntfile.js');
9
10// Cache path to the directory hosting the Gruntfile
11// As tests are run via Grunt, this will be the current working directory
12var gruntfileDirectory = process.cwd();
13
14function getHookPath(testID, hookName) {
15 return 'tmp/' + testID + '/' + (hookName || 'pre-commit');
16}
17
18function testHookPermissions(hookPath, test) {
19 test.ok(fs.statSync(hookPath).mode.toString(8).match(/755$/), 'Should generate hook file with appropriate permissions (755)');
20}
21
22function testHookContent(hookPath, testID, test, hookName) {
23 var expected = grunt.file.read('test/expected/' + (hookName || 'pre-commit') + '.' + testID);
24 expected = expected.replace('{{expectedWorkingDir}}', gruntfileDirectory);
25 var actual = grunt.file.read(hookPath);
26 test.equal(actual, expected, 'Should create hook with appropriate content');
27}
28
29function addTest(testSuite, testID) {
30
31 testSuite[testID] = function (test) {
32
33 test.expect(2);
34 var hookPath = getHookPath(testID);
35
36 testHookPermissions(hookPath, test);
37 testHookContent(hookPath, testID, test);
38
39 test.done();
40 };
41}
42
43/* Most of the testing is done by matching files generated by the `grunt githooks` calls
44 * to expected files stored in the `test/expected` folder of the project.
45 * Use the following naming conventions:
46 * - name the test tasks `test.<testID>`
47 * - name expected hook `pre-commit.<testID>`
48 * - set `dest` option of your task to `tmp/<testId>`
49 */
50exports.githooks = {
51
52 'logs.defaultLogging': function (test) {
53
54 test.expect(1);
55 exec('grunt githooks:logs.defaultLogging', function(err, stdout) {
56
57 test.notEqual(stdout.indexOf('Binding `aTask` to `pre-commit` Git hook'), -1);
58 test.done();
59 });
60 },
61
62 'logs.warnIfNotValidHook': function (test) {
63
64 test.expect(1);
65 exec('grunt githooks:logs.warnIfNotValidHook', function(err, stdout){
66
67 test.notEqual(stdout.indexOf('`definitelyNotTheNameOfAGitHook` is not the name of a Git hook.'), -1);
68 test.done();
69 });
70 },
71
72 'fails.invalidScriptingLanguage': function (test) {
73
74 test.expect(3);
75 exec('grunt githooks:fails.invalidScriptingLanguage', function(err, stdout, stderr){
76
77 test.ok(!!err);
78 test.notEqual(stdout.indexOf("doesn't seem to be written in the same language"), -1);
79 testHookContent(getHookPath('invalidScriptingLanguage'), 'invalidScriptingLanguage', test);
80 test.done();
81 });
82 },
83
84 'fails.customHashbangInvalidScriptingLanguage': function (test) {
85 test.expect(3);
86 exec('grunt githooks:fails.customHashbangInvalidScriptingLanguage', function(err, stdout, stderr){
87
88 test.ok(!!err);
89 test.notEqual(stdout.indexOf("doesn't seem to be written in the same language"), -1);
90 testHookContent(getHookPath('customHashbangInvalidScriptingLanguage'), 'customHashbangInvalidScriptingLanguage', test);
91 test.done();
92 });
93 },
94
95 'test.multipleHooks--commit-msg': function (test) {
96
97 test.expect(2);
98
99 var hookPath = getHookPath('multipleHooks','commit-msg');
100 testHookPermissions(hookPath, test);
101 testHookContent(hookPath,'multipleHooks', test, 'commit-msg');
102
103 test.done();
104 }
105};
106
107for (var target in grunt.config.data.githooks) {
108
109 var TEST_TARGET = /^test.(.*)$/;
110 var match = TEST_TARGET.exec(target);
111 if (match) {
112 addTest(exports.githooks, match[1]);
113 }
114}
\No newline at end of file