1 | 'use strict';
|
2 |
|
3 | var fs = require('fs'),
|
4 | path = require('path'),
|
5 | exec = require('child_process').exec,
|
6 | grunt = require('grunt');
|
7 |
|
8 |
|
9 | require('../Gruntfile.js');
|
10 |
|
11 |
|
12 |
|
13 | var gruntfileDirectory = process.cwd();
|
14 |
|
15 | function getHookPath(testID, hookName) {
|
16 | return 'tmp/' + testID + '/' + (hookName || 'pre-commit');
|
17 | }
|
18 |
|
19 |
|
20 | function escapedPath(path, testID) {
|
21 |
|
22 | if (testID !== 'shellScript') {
|
23 | return path.replace(/\\/g,'\\\\');
|
24 | }
|
25 |
|
26 | return path;
|
27 | }
|
28 |
|
29 | function 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));
|
32 | var actual = grunt.file.read(hookPath);
|
33 |
|
34 |
|
35 | test.equal(actual.replace(/[\n,\r]/g,''), expected.replace(/[\n,\r]/g,''), 'Should create hook with appropriate content');
|
36 | }
|
37 |
|
38 | function 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 |
|
51 | exports.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 |
|
63 | if (process.platform !== 'win32') {
|
64 |
|
65 |
|
66 |
|
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 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | for (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 |