UNPKG

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