file_task.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. var assert = require('assert')
  2. , fs = require('fs')
  3. , path = require('path')
  4. , exec = require('child_process').exec
  5. , h = require('./helpers')
  6. , utils = require('utilities');
  7. var cleanUpAndNext = function (callback) {
  8. utils.file.rmRf('./foo', {
  9. silent: true
  10. });
  11. callback();
  12. };
  13. var tests = {
  14. 'before': function (next) {
  15. process.chdir('./test');
  16. cleanUpAndNext(next);
  17. }
  18. , 'after': function () {
  19. process.chdir('../');
  20. }
  21. , 'test concating two files': function (next) {
  22. h.exec('../bin/cli.js fileTest:foo/concat.txt', function (out) {
  23. var data;
  24. assert.equal('fileTest:foo/src1.txt task\ndefault task\nfileTest:foo/src2.txt task\n' +
  25. 'fileTest:foo/concat.txt task', out);
  26. // Check to see the two files got concat'd
  27. data = fs.readFileSync(process.cwd() + '/foo/concat.txt');
  28. assert.equal('src1src2', data.toString());
  29. cleanUpAndNext(next);
  30. });
  31. }
  32. , 'test where a file-task prereq does not change': function (next) {
  33. h.exec('../bin/cli.js fileTest:foo/from-src1.txt', function (out) {
  34. assert.equal('fileTest:foo/src1.txt task\nfileTest:foo/from-src1.txt task', out);
  35. h.exec('../bin/cli.js fileTest:foo/from-src1.txt', function (out) {
  36. // Second time should be a no-op
  37. assert.equal('', out);
  38. next(); // Don't clean up
  39. });
  40. });
  41. }
  42. , 'file-task where prereq file is modified': function (next) {
  43. setTimeout(function () {
  44. fs.writeFile('./foo/src1.txt', '', function (err, data) {
  45. if (err) {
  46. throw err;
  47. }
  48. h.exec('../bin/cli.js fileTest:foo/from-src1.txt', function (out) {
  49. assert.equal('fileTest:foo/from-src1.txt task', out);
  50. cleanUpAndNext(next);
  51. });
  52. });
  53. }, 1000); // Wait to do the mod to ensure mod-time is different
  54. }
  55. , 'test where a file-task prereq does not change with --always-make': function (next) {
  56. h.exec('../bin/cli.js fileTest:foo/from-src1.txt', function (out) {
  57. assert.equal('fileTest:foo/src1.txt task\nfileTest:foo/from-src1.txt task',
  58. out);
  59. h.exec('../bin/cli.js -B fileTest:foo/from-src1.txt', function (out) {
  60. assert.equal('fileTest:foo/src1.txt task\nfileTest:foo/from-src1.txt task',
  61. out);
  62. cleanUpAndNext(next);
  63. });
  64. });
  65. }
  66. , 'test a preexisting file': function (next) {
  67. var prereqData = 'howdy';
  68. utils.file.mkdirP('foo');
  69. fs.writeFileSync('foo/prereq.txt', prereqData);
  70. h.exec('../bin/cli.js fileTest:foo/from-prereq.txt', function (out) {
  71. var data;
  72. assert.equal('fileTest:foo/from-prereq.txt task', out);
  73. data = fs.readFileSync(process.cwd() + '/foo/from-prereq.txt');
  74. assert.equal(prereqData, data.toString());
  75. h.exec('../bin/cli.js fileTest:foo/from-prereq.txt', function (out) {
  76. // Second time should be a no-op
  77. assert.equal('', out);
  78. cleanUpAndNext(next);
  79. });
  80. });
  81. }
  82. , 'test a preexisting file with --always-make flag': function (next) {
  83. var prereqData = 'howdy';
  84. utils.file.mkdirP('foo');
  85. fs.writeFileSync('foo/prereq.txt', prereqData);
  86. h.exec('../bin/cli.js fileTest:foo/from-prereq.txt', function (out) {
  87. var data;
  88. assert.equal('fileTest:foo/from-prereq.txt task', out);
  89. data = fs.readFileSync(process.cwd() + '/foo/from-prereq.txt');
  90. assert.equal(prereqData, data.toString());
  91. h.exec('../bin/cli.js -B fileTest:foo/from-prereq.txt', function (out) {
  92. assert.equal('fileTest:foo/from-prereq.txt task', out);
  93. cleanUpAndNext(next);
  94. });
  95. });
  96. }
  97. , 'test nested directory-task': function (next) {
  98. h.exec('../bin/cli.js fileTest:foo/bar/baz/bamf.txt', function (out) {
  99. var data = fs.readFileSync(process.cwd() + '/foo/bar/baz/bamf.txt');
  100. assert.equal('w00t', data);
  101. cleanUpAndNext(next);
  102. });
  103. }
  104. };
  105. module.exports = tests;