rule.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var assert = require('assert')
  2. , fs = require('fs')
  3. , path = require('path')
  4. , exec = require('child_process').exec
  5. , h = require('./helpers')
  6. , Matcher = require('../lib/rule').Matcher
  7. , utils = require('utilities');
  8. var cleanUpAndNext = function (callback) {
  9. // Gotta add globbing to file utils rmRf
  10. var tmpFiles = [
  11. 'tmp'
  12. , 'tmp_ns'
  13. , 'tmp_cr'
  14. , 'tmp_p'
  15. , 'tmp_pf'
  16. , 'tmpbin'
  17. , 'tmpsrc'
  18. , 'tmp_dep1.c'
  19. , 'tmp_dep1.o'
  20. , 'tmp_dep1.oo'
  21. , 'tmp_dep2.c'
  22. , 'tmp_dep2.o'
  23. , 'tmp_dep2.oo'
  24. , 'foo'
  25. , 'foo.html'
  26. ];
  27. tmpFiles.forEach(function (f) {
  28. utils.file.rmRf(f, {
  29. silent: true
  30. });
  31. });
  32. callback();
  33. };
  34. var tests = {
  35. 'before': function (next) {
  36. process.chdir('./test');
  37. cleanUpAndNext(next);
  38. }
  39. , 'after': function () {
  40. process.chdir('../');
  41. }
  42. // - name foo:bin/main.o
  43. // - pattern bin/%.o
  44. // - source src/%.c
  45. //
  46. // return {
  47. // 'dep' : 'foo:src/main.c',
  48. // 'file': 'src/main.c'
  49. // };
  50. , 'test Matcher.getSource': function () {
  51. var src = Matcher.getSource('foo:bin/main.o', 'bin/%.o', 'src/%.c');
  52. assert.equal('foo:src/main.c', src);
  53. }
  54. , 'test rule w/o pattern': function (next) {
  55. h.exec( '../bin/cli.js -f Jakefile.rule tmp', function (out) {
  56. var output = [
  57. "tmp_dep2.c task"
  58. , "tmp_dep1.c task"
  59. , "cp tmp_dep1.c tmp_dep1.o task"
  60. , "cp tmp_dep2.c tmp_dep2.o task"
  61. , "tmp task" ];
  62. var data;
  63. assert.equal( output.join('\n') , out);
  64. data = fs.readFileSync(process.cwd() + '/tmp');
  65. assert.equal('src_1src_2', data.toString());
  66. cleanUpAndNext(next);
  67. });
  68. }
  69. , 'test rule w pattern w/o folder w/o namespace': function (next) {
  70. h.exec( '../bin/cli.js -f Jakefile.rule tmp_p', function (out) {
  71. var output = [
  72. "tmp_dep2.c task"
  73. , "tmp_dep1.c task"
  74. , "cp tmp_dep1.c tmp_dep1.oo task"
  75. , "cp tmp_dep2.c tmp_dep2.oo task"
  76. , "tmp pattern task" ];
  77. var data;
  78. assert.equal( output.join('\n') , out);
  79. data = fs.readFileSync(process.cwd() + '/tmp_p');
  80. assert.equal('src_1src_2 pattern', data.toString());
  81. cleanUpAndNext(next);
  82. });
  83. }
  84. , 'test rule w pattern w folder w/o namespace': function (next) {
  85. h.exec( '../bin/cli.js -f Jakefile.rule tmp_pf', function (out) {
  86. var output = [
  87. "tmpsrc/tmp_dep1.c task"
  88. , "cp tmpsrc/tmp_dep1.c tmpbin/tmp_dep1.oo task"
  89. , "tmpsrc/tmp_dep2.c task"
  90. , "cp tmpsrc/tmp_dep2.c tmpbin/tmp_dep2.oo task"
  91. , "tmp pattern folder task" ];
  92. var data;
  93. assert.equal( output.join('\n') , out);
  94. data = fs.readFileSync(process.cwd() + '/tmp_pf');
  95. assert.equal('src/src_1src/src_2 pattern folder', data.toString());
  96. cleanUpAndNext(next);
  97. });
  98. }
  99. , 'test rule w pattern w folder w namespace': function (next) {
  100. h.exec( '../bin/cli.js -f Jakefile.rule tmp_ns', function (out) {
  101. var output = [
  102. "tmpsrc/file2.c init task"
  103. , "tmpsrc/tmp_dep2.c task"
  104. , "cp tmpsrc/tmp_dep2.c tmpbin/tmp_dep2.oo task"
  105. , "tmpsrc/dep1.c task"
  106. , "cp tmpsrc/dep1.c tmpbin/dep1.oo ns task"
  107. , "cp tmpsrc/file2.c tmpbin/file2.oo ns task"
  108. , "tmp pattern folder namespace task" ];
  109. var data;
  110. assert.equal( output.join('\n') , out);
  111. data = fs.readFileSync(process.cwd() + '/tmp_ns');
  112. assert.equal('src/src_1src/src_2src/src_3 pattern folder namespace', data.toString());
  113. cleanUpAndNext(next);
  114. });
  115. }
  116. , 'test rule w chain w pattern w folder w namespace': function (next) {
  117. h.exec( '../bin/cli.js -f Jakefile.rule tmp_cr', function (out) {
  118. var output = [
  119. "chainrule init task"
  120. , "cp tmpsrc/file1.tex tmpbin/file1.dvi tex->dvi task"
  121. , "cp tmpbin/file1.dvi tmpbin/file1.pdf dvi->pdf task"
  122. , "cp tmpsrc/file2.tex tmpbin/file2.dvi tex->dvi task"
  123. , "cp tmpbin/file2.dvi tmpbin/file2.pdf dvi->pdf task"
  124. , "tmp chainrule namespace task" ];
  125. var data;
  126. assert.equal( output.join('\n') , out);
  127. data = fs.readFileSync(process.cwd() + '/tmp_cr');
  128. assert.equal('tex1 tex2 chainrule namespace', data.toString());
  129. cleanUpAndNext(next);
  130. });
  131. }
  132. };
  133. ['precedence', 'regexPattern', 'sourceFunction'].forEach(function (key) {
  134. tests['test rule with source file not created yet (' + key + ')'] = function (next) {
  135. utils.file.rmRf('foo.txt', {silent: true});
  136. utils.file.rmRf('foo.html', {silent: true});
  137. h.exec('../bin/cli.js -f Jakefile.rule ' + key + ':test', {breakOnError: false},
  138. function (out) {
  139. // foo.txt prereq doesn't exist yet
  140. assert.ok(out.toString().indexOf('Unknown task "foo.html"') > -1);
  141. next();
  142. });
  143. };
  144. tests['test rule with source file now created (' + key + ')'] = function (next) {
  145. fs.writeFileSync('foo.txt', '');
  146. h.exec('../bin/cli.js -f Jakefile.rule ' + key + ':test', function (out) {
  147. // Should run prereq and test task
  148. var output = [
  149. 'created html'
  150. , 'ran test'
  151. ];
  152. assert.equal(output.join('\n'), out);
  153. next();
  154. });
  155. };
  156. /*
  157. tests['test rule with objective file now created (' + key + ')'] = function (next) {
  158. fs.writeFileSync('foo.txt', '');
  159. h.exec('../bin/cli.js -f Jakefile.rule ' + key + ':test', function (out) {
  160. // Should only run test task
  161. var output = [
  162. 'ran test'
  163. ];
  164. assert.equal(output.join('\n'), out);
  165. next();
  166. });
  167. };
  168. */
  169. tests['test rule with source file modified (' + key + ')'] = function (next) {
  170. setTimeout(function () {
  171. fs.writeFile('foo.txt', '', function (err, data) {
  172. if (err) {
  173. throw err;
  174. }
  175. h.exec('../bin/cli.js -f Jakefile.rule ' + key + ':test', function (out) {
  176. // Should again run both prereq and test task
  177. var output = [
  178. 'created html'
  179. , 'ran test'
  180. ];
  181. assert.equal(output.join('\n'), out);
  182. //next();
  183. cleanUpAndNext(next);
  184. });
  185. });
  186. }, 1000); // Wait to do the touch to ensure mod-time is different
  187. };
  188. tests['test rule with existing objective file and no source ' +
  189. ' (should be normal file-task) (' + key + ')'] = function (next) {
  190. // Remove just the source file
  191. fs.writeFileSync('foo.html', '');
  192. utils.file.rmRf('foo.txt', {silent: true});
  193. h.exec('../bin/cli.js -f Jakefile.rule ' + key + ':test', function (out) {
  194. // Should treat existing objective file as plain file-task,
  195. // and just run test-task
  196. var output = [
  197. 'ran test'
  198. ];
  199. assert.equal(output.join('\n'), out);
  200. cleanUpAndNext(next);
  201. });
  202. };
  203. });
  204. module.exports = tests;