Tizen 2.1 base
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / test / grunt / file_test.js
1 var grunt = require('../../lib/grunt');
2
3 var fs = require('fs');
4 var path = require('path');
5
6 // test helper
7 //
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
10 // of testing.
11 //
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);
17 }
18
19 exports['file'] = {
20   'isPathAbsolute': function(test) {
21     test.expect(2);
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');
24     test.done();
25   },
26   'read': function(test) {
27     test.expect(2);
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'));
30     test.done();
31   },
32   'write': function(test) {
33     test.expect(4);
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);
38
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');
43
44     ['test/fixtures/test_write.js', 'test/fixtures/test_write.png'].forEach(fs.unlinkSync);
45     test.done();
46   },
47   'copy': function(test) {
48     test.expect(6);
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'));
51
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});
57     }});
58     test.strictEqual(fs.readFileSync('test/fixtures/test_copy.js', 'utf8'), grunt.utils.normalizelf(fs.readFileSync('test/fixtures/a.js', 'utf8')) + tmpltest);
59
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');
63
64     ['test/fixtures/test_copy.js', 'test/fixtures/test_copy.png'].forEach(fs.unlinkSync);
65     test.done();
66   }
67 };