1 | 'use strict'; |
---|
2 | |
---|
3 | var path = require('path'); |
---|
4 | var grunt = require('grunt'); |
---|
5 | var jshint = require('../tasks/lib/jshint').init(grunt); |
---|
6 | |
---|
7 | var fixtures = path.join(__dirname, 'fixtures'); |
---|
8 | |
---|
9 | // Helper for testing stdout |
---|
10 | var hooker = grunt.util.hooker; |
---|
11 | var stdoutEqual = function(callback, done) { |
---|
12 | var actual = ''; |
---|
13 | // Hook process.stdout.write |
---|
14 | hooker.hook(process.stdout, 'write', { |
---|
15 | // This gets executed before the original process.stdout.write. |
---|
16 | pre: function(result) { |
---|
17 | // Concatenate uncolored result onto actual. |
---|
18 | actual += grunt.log.uncolor(result); |
---|
19 | // Prevent the original process.stdout.write from executing. |
---|
20 | return hooker.preempt(); |
---|
21 | } |
---|
22 | }); |
---|
23 | // Execute the logging code to be tested. |
---|
24 | callback(); |
---|
25 | // Restore process.stdout.write to its original value. |
---|
26 | hooker.unhook(process.stdout, 'write'); |
---|
27 | // Actually test the actually-logged stdout string to the expected value. |
---|
28 | done(actual); |
---|
29 | }; |
---|
30 | |
---|
31 | exports.jshint = { |
---|
32 | basic: function(test) { |
---|
33 | test.expect(1); |
---|
34 | var files = [path.join(fixtures, 'missingsemicolon.js')]; |
---|
35 | var options = {}; |
---|
36 | jshint.lint(files, options, function(results, data) { |
---|
37 | test.equal(results[0].error.reason, 'Missing semicolon.', 'Should reporter a missing semicolon.'); |
---|
38 | test.done(); |
---|
39 | }); |
---|
40 | }, |
---|
41 | jshintrc: function(test) { |
---|
42 | test.expect(1); |
---|
43 | var files = [path.join(fixtures, 'nodemodule.js')]; |
---|
44 | var options = { |
---|
45 | jshintrc: path.join(__dirname, '..', '.jshintrc') |
---|
46 | }; |
---|
47 | jshint.lint(files, options, function(results, data) { |
---|
48 | test.ok(results.length === 0, 'Should not have reported any errors with supplied .jshintrc'); |
---|
49 | test.done(); |
---|
50 | }); |
---|
51 | }, |
---|
52 | defaultReporter: function(test) { |
---|
53 | test.expect(2); |
---|
54 | grunt.log.muted = false; |
---|
55 | var files = [path.join(fixtures, 'nodemodule.js')]; |
---|
56 | var options = {}; |
---|
57 | stdoutEqual(function() { |
---|
58 | jshint.lint(files, options, function(results, data) {}); |
---|
59 | }, function(result) { |
---|
60 | test.ok(jshint.usingGruntReporter, 'Should be using the default grunt reporter.'); |
---|
61 | test.ok(result.indexOf('[L3:C1] W117: \'module\' is not defined.') !== -1, 'Should have reported errors with the default grunt reporter.'); |
---|
62 | test.done(); |
---|
63 | }); |
---|
64 | }, |
---|
65 | defaultReporterErrors: function(test) { |
---|
66 | test.expect(3); |
---|
67 | grunt.log.muted = false; |
---|
68 | var files = [path.join(fixtures, 'nodemodule.js'), path.join(fixtures, 'missingsemicolon.js')]; |
---|
69 | var options = {}; |
---|
70 | stdoutEqual(function() { |
---|
71 | jshint.lint(files, options, function(results, data) {}); |
---|
72 | }, function(result) { |
---|
73 | test.ok(jshint.usingGruntReporter, 'Should be using the default grunt reporter.'); |
---|
74 | test.ok(result.match(/nodemodule\.js\s\.\.\.ERROR/g).length === 2, 'Should have reported nodemodule.js once per error.'); |
---|
75 | test.ok(result.match(/missingsemicolon\.js\s\.\.\.ERROR/g).length === 1, 'Should have reported missingsemicolon.js once per error.'); |
---|
76 | test.done(); |
---|
77 | }); |
---|
78 | }, |
---|
79 | alternateReporter: function(test) { |
---|
80 | test.expect(2); |
---|
81 | var files = [path.join(fixtures, 'nodemodule.js')]; |
---|
82 | var options = { |
---|
83 | reporter: 'jslint' |
---|
84 | }; |
---|
85 | stdoutEqual(function() { |
---|
86 | jshint.lint(files, options, function(results, data) {}); |
---|
87 | }, function(result) { |
---|
88 | test.ok((jshint.usingGruntReporter === false), 'Should NOT be using the default grunt reporter.'); |
---|
89 | test.ok(result.indexOf('<jslint>') !== -1, 'Should have reported errors with the jslint reporter.'); |
---|
90 | test.done(); |
---|
91 | }); |
---|
92 | }, |
---|
93 | reporterOutput: function(test) { |
---|
94 | test.expect(1); |
---|
95 | var result = grunt.file.read(path.join('tmp', 'report.xml')); |
---|
96 | test.ok(result.indexOf('<file name="test/fixtures/missingsemicolon.js">') !== -1, 'Should have reported errors with the checkstyle reporter.'); |
---|
97 | test.done(); |
---|
98 | }, |
---|
99 | dontBlowUp: function(test) { |
---|
100 | test.expect(1); |
---|
101 | var files = [path.join(fixtures, 'lint.txt')]; |
---|
102 | jshint.lint(files, {}, function(results, data) { |
---|
103 | test.equal(results[0].error.code, 'W100', 'It should not blow up if an error occurs on character 0.'); |
---|
104 | test.done(); |
---|
105 | }); |
---|
106 | }, |
---|
107 | jshintignore: function(test) { |
---|
108 | test.expect(1); |
---|
109 | var files = [path.join(fixtures, 'dontlint.txt')]; |
---|
110 | jshint.lint(files, {}, function(results, data) { |
---|
111 | test.equal(data.length, 0, 'Should not have linted a file listed in the .jshintignore.'); |
---|
112 | test.done(); |
---|
113 | }); |
---|
114 | }, |
---|
115 | ignoresOption: function(test) { |
---|
116 | test.expect(1); |
---|
117 | var files = [path.join(fixtures, 'lint.txt')]; |
---|
118 | var options = { |
---|
119 | ignores: files |
---|
120 | }; |
---|
121 | jshint.lint(files, options, function(results, data) { |
---|
122 | test.equal(data.length, 0, 'Should not have linted a file listed in the ignores option.'); |
---|
123 | test.done(); |
---|
124 | }); |
---|
125 | }, |
---|
126 | singleReportCall: function(test) { |
---|
127 | test.expect(2); |
---|
128 | |
---|
129 | // stub jshint.reporter |
---|
130 | var reporterCallCount = 0; |
---|
131 | var _report = jshint.reporter; |
---|
132 | jshint.reporter = function() { reporterCallCount++; }; |
---|
133 | |
---|
134 | var files = [path.join(fixtures, 'dontlint.txt'), path.join(fixtures, 'lint.txt')]; |
---|
135 | jshint.lint(files, {}, function(results, data) { |
---|
136 | test.equal(data.length, 1, 'Should not have linted a file listed in the .jshintignore.'); |
---|
137 | test.equal(reporterCallCount, 1, 'Should have called the reporter once.'); |
---|
138 | test.done(); |
---|
139 | }); |
---|
140 | } |
---|
141 | }; |
---|