UNPKG

3.2 kBJavaScriptView Raw
1var grunt = require('../../lib/grunt');
2var file = grunt.file;
3var template = grunt.template;
4var utils = grunt.utils;
5
6var fs = require('fs');
7var path = require('path');
8
9// test helper
10//
11// compare - to effectively compare Buffers, we would need something like
12// bnoordhuis/buffertools, but I'd rather not add a new dependency for the sake
13// of testing.
14//
15// So we're relying on comparisons between the `hex` of buffers to do that,
16// seems to be reliant enough to cover our test needs with file copy.
17function compare(actual, expected, encoding) {
18 encoding = encoding || 'hex';
19 return fs.readFileSync(actual, encoding) === fs.readFileSync(expected, encoding);
20}
21
22exports['file'] = {
23 'isPathAbsolute': function(test) {
24 test.expect(2);
25 test.ok(file.isPathAbsolute(path.resolve('test/fixtures/a.js')), 'should return true');
26 test.equal(file.isPathAbsolute('test/fixtures/a.js'), false, 'should return false');
27 test.done();
28 },
29 'read': function(test) {
30 test.expect(2);
31 test.strictEqual(file.read('test/fixtures/a.js'), fs.readFileSync('test/fixtures/a.js', 'utf8'));
32 test.strictEqual(file.read('test/fixtures/octocat.png'), fs.readFileSync('test/fixtures/octocat.png', 'utf8'));
33 test.done();
34 },
35 'write': function(test) {
36 test.expect(4);
37 var content = 'var a = "foobar";';
38 file.write('test/fixtures/test_write.js', content);
39 test.strictEqual(fs.readFileSync('test/fixtures/test_write.js', 'utf8'), content);
40 test.strictEqual(file.read('test/fixtures/test_write.js'), content);
41
42 var octocat = fs.readFileSync('test/fixtures/octocat.png');
43 file.write('test/fixtures/test_write.png', octocat);
44 test.strictEqual(fs.readFileSync('test/fixtures/test_write.png', 'utf8'), fs.readFileSync('test/fixtures/octocat.png', 'utf8'));
45 test.ok(compare('test/fixtures/test_write.png', 'test/fixtures/octocat.png'), 'both buffers should match');
46
47 ['test/fixtures/test_write.js', 'test/fixtures/test_write.png'].forEach(fs.unlinkSync);
48 test.done();
49 },
50 'copy': function(test) {
51 test.expect(6);
52 file.copy('test/fixtures/a.js', 'test/fixtures/test_copy.js');
53 test.strictEqual(fs.readFileSync('test/fixtures/test_copy.js', 'utf8'), fs.readFileSync('test/fixtures/a.js', 'utf8'));
54
55 var tmpltest = '// should src be a string and template process be all good.';
56 file.copy('test/fixtures/a.js', 'test/fixtures/test_copy.js', {process: function(src) {
57 test.equal(Buffer.isBuffer(src), false);
58 test.equal(typeof src, 'string');
59 return template.process(src + '<%= tmpltest %>', {tmpltest: tmpltest});
60 }});
61 test.strictEqual(fs.readFileSync('test/fixtures/test_copy.js', 'utf8'), utils.normalizelf(fs.readFileSync('test/fixtures/a.js', 'utf8')) + tmpltest);
62
63 file.copy('test/fixtures/octocat.png', 'test/fixtures/test_copy.png');
64 test.strictEqual(fs.readFileSync('test/fixtures/test_copy.png', 'utf8'), fs.readFileSync('test/fixtures/octocat.png', 'utf8'));
65 test.ok(compare('test/fixtures/test_copy.png', 'test/fixtures/octocat.png'), 'both buffers should match');
66
67 ['test/fixtures/test_copy.js', 'test/fixtures/test_copy.png'].forEach(fs.unlinkSync);
68 test.done();
69 }
70};