1 | // Load in dependencies |
---|
2 | var fs = require('fs'), |
---|
3 | cp = require('child_process'), |
---|
4 | exec = cp.exec, |
---|
5 | chai = require('chai'), |
---|
6 | expect = chai.expect; |
---|
7 | |
---|
8 | module.exports = { |
---|
9 | // Utilities |
---|
10 | 'execute task': function (done) { |
---|
11 | // Relocate to test directory |
---|
12 | process.chdir(__dirname); |
---|
13 | |
---|
14 | // Execute the cmd and task combination |
---|
15 | var that = this; |
---|
16 | exec(this.cmd + this.task, function (err, stdout, stderr) { |
---|
17 | // Save results for later |
---|
18 | that.err = err; |
---|
19 | that.stdout = stdout; |
---|
20 | that.stderr = stderr; |
---|
21 | |
---|
22 | // Callback |
---|
23 | done(); |
---|
24 | }); |
---|
25 | }, |
---|
26 | |
---|
27 | // Cleaning tasks |
---|
28 | 'A clean test directory': [function () { |
---|
29 | this.cmd = 'grunt clean'; |
---|
30 | this.task = ''; |
---|
31 | }, 'execute task'], |
---|
32 | 'is clean': function () {}, |
---|
33 | |
---|
34 | // Grunt commands |
---|
35 | 'grunt curl': function () { |
---|
36 | this.cmd = 'grunt curl:'; |
---|
37 | }, |
---|
38 | 'grunt curl-dir': function () { |
---|
39 | this.cmd = 'grunt curl-dir:'; |
---|
40 | }, |
---|
41 | |
---|
42 | // curl tasks |
---|
43 | 'downloading a js (utf16) file': [function () { |
---|
44 | this.task = 'js'; |
---|
45 | this.filenames = ['file.js']; |
---|
46 | }, 'execute task'], |
---|
47 | 'downloading a zip (binary) file': [function () { |
---|
48 | this.task = 'zip'; |
---|
49 | this.filenames = ['file.zip']; |
---|
50 | this.timeout(5000); |
---|
51 | }, 'execute task'], |
---|
52 | 'downloading a file from an invalid domain': [function () { |
---|
53 | this.task = 'nonExistingDomain'; |
---|
54 | this.filenames = ['nonexistent-domain']; |
---|
55 | }, 'execute task'], |
---|
56 | 'downloading a nonexistant file': [function () { |
---|
57 | this.task = 'nonExistingFile'; |
---|
58 | this.filenames = ['nonexistent-file']; |
---|
59 | }, 'execute task'], |
---|
60 | |
---|
61 | // curl-dir tasks |
---|
62 | 'downloading multiple files': [function () { |
---|
63 | this.task = 'multi'; |
---|
64 | this.filenames = ['multi/LAB.min.js', 'multi/cookiejar.js']; |
---|
65 | }, 'execute task'], |
---|
66 | 'downloading brace expanded files': [function () { |
---|
67 | this.task = 'braceExpansion'; |
---|
68 | this.filenames = ['braceExpansion/LAB.min.js', 'braceExpansion/cookiejar.js']; |
---|
69 | }, 'execute task'], |
---|
70 | 'using a custom router': [function () { |
---|
71 | this.task = 'router'; |
---|
72 | this.filenames = ['router/ajax/libs/labjs/2.0.3/LAB.min.js', 'router/ajax/libs/cookiejar/0.5/cookiejar.js']; |
---|
73 | }, 'execute task'], |
---|
74 | |
---|
75 | // curl and curl-dir results |
---|
76 | 'is successful': function () { |
---|
77 | // Assert no error |
---|
78 | expect(this.err).to.equal(null); |
---|
79 | |
---|
80 | // and file(s) match as expected |
---|
81 | this.filenames.forEach(function assertFilenames (filename) { |
---|
82 | var expectedContent = fs.readFileSync('expected/' + filename, 'binary'), |
---|
83 | actualContent = fs.readFileSync('actual/' + filename, 'binary'); |
---|
84 | expect(actualContent).to.equal(expectedContent); |
---|
85 | }); |
---|
86 | }, |
---|
87 | 'throws an error': function () { |
---|
88 | expect(this.err).to.not.equal(null); |
---|
89 | }, |
---|
90 | 'does not create the file': function () { |
---|
91 | // Loop over the files |
---|
92 | this.filenames.forEach(function lookupFile(filename) { |
---|
93 | // Grab the file |
---|
94 | var filepath = 'actual/' + filename; |
---|
95 | try { |
---|
96 | fs.statSync(filepath); |
---|
97 | throw new Error('File "' + filepath + '" was properly located'); |
---|
98 | } catch (e) { |
---|
99 | expect(e).to.have.property('code', 'ENOENT'); |
---|
100 | } |
---|
101 | }); |
---|
102 | } |
---|
103 | }; |
---|