1 var grunt = require('../../lib/grunt');
3 var fs = require('fs');
4 var path = require('path');
8 // compare - to effectively compare Buffers, we would need something like
9 // bnoordhuis/buffertools, but I'd rather not add a new dependency for the sake
12 // So we're relying on comparisons between the `hex` of buffers to do that,
13 // seems to be reliant enough to cover our test needs with file copy.
14 function compare(actual, expected, encoding) {
15 encoding = encoding || 'hex';
16 return fs.readFileSync(actual, encoding) === fs.readFileSync(expected, encoding);
20 'isPathAbsolute': function(test) {
22 test.ok(grunt.file.isPathAbsolute(path.resolve('test/fixtures/a.js')), 'should return true');
23 test.equal(grunt.file.isPathAbsolute('test/fixtures/a.js'), false, 'should return false');
26 'read': function(test) {
28 test.strictEqual(grunt.file.read('test/fixtures/a.js'), fs.readFileSync('test/fixtures/a.js', 'utf8'));
29 test.strictEqual(grunt.file.read('test/fixtures/octocat.png'), fs.readFileSync('test/fixtures/octocat.png', 'utf8'));
32 'write': function(test) {
34 var content = 'var a = "foobar";';
35 grunt.file.write('test/fixtures/test_write.js', content);
36 test.strictEqual(fs.readFileSync('test/fixtures/test_write.js', 'utf8'), content);
37 test.strictEqual(grunt.file.read('test/fixtures/test_write.js'), content);
39 var octocat = fs.readFileSync('test/fixtures/octocat.png');
40 grunt.file.write('test/fixtures/test_write.png', octocat);
41 test.strictEqual(fs.readFileSync('test/fixtures/test_write.png', 'utf8'), fs.readFileSync('test/fixtures/octocat.png', 'utf8'));
42 test.ok(compare('test/fixtures/test_write.png', 'test/fixtures/octocat.png'), 'both buffers should match');
44 ['test/fixtures/test_write.js', 'test/fixtures/test_write.png'].forEach(fs.unlinkSync);
47 'copy': function(test) {
49 grunt.file.copy('test/fixtures/a.js', 'test/fixtures/test_copy.js');
50 test.strictEqual(fs.readFileSync('test/fixtures/test_copy.js', 'utf8'), fs.readFileSync('test/fixtures/a.js', 'utf8'));
52 var tmpltest = '// should src be a string and template process be all good.';
53 grunt.file.copy('test/fixtures/a.js', 'test/fixtures/test_copy.js', {process: function(src) {
54 test.equal(Buffer.isBuffer(src), false);
55 test.equal(typeof src, 'string');
56 return grunt.template.process(src + '<%= tmpltest %>', {tmpltest: tmpltest});
58 test.strictEqual(fs.readFileSync('test/fixtures/test_copy.js', 'utf8'), grunt.utils.normalizelf(fs.readFileSync('test/fixtures/a.js', 'utf8')) + tmpltest);
60 grunt.file.copy('test/fixtures/octocat.png', 'test/fixtures/test_copy.png');
61 test.strictEqual(fs.readFileSync('test/fixtures/test_copy.png', 'utf8'), fs.readFileSync('test/fixtures/octocat.png', 'utf8'));
62 test.ok(compare('test/fixtures/test_copy.png', 'test/fixtures/octocat.png'), 'both buffers should match');
64 ['test/fixtures/test_copy.js', 'test/fixtures/test_copy.png'].forEach(fs.unlinkSync);