rule.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Jake JavaScript build tool
  3. * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. const PROJECT_DIR = process.env.PROJECT_DIR;
  19. const JAKE_CMD = `${PROJECT_DIR}/bin/cli.js`;
  20. let assert = require('assert');
  21. let exec = require('child_process').execSync;
  22. let fs = require('fs');
  23. let { Rule } = require(`${PROJECT_DIR}/lib/rule`);
  24. let { rmRf } = require(`${PROJECT_DIR}/lib/jake`);
  25. let cleanUpAndNext = function (callback) {
  26. // Gotta add globbing to file utils rmRf
  27. let tmpFiles = [
  28. 'tmp'
  29. , 'tmp_ns'
  30. , 'tmp_cr'
  31. , 'tmp_p'
  32. , 'tmp_pf'
  33. , 'tmpbin'
  34. , 'tmpsrc'
  35. , 'tmp_dep1.c'
  36. , 'tmp_dep1.o'
  37. , 'tmp_dep1.oo'
  38. , 'tmp_dep2.c'
  39. , 'tmp_dep2.o'
  40. , 'tmp_dep2.oo'
  41. , 'foo'
  42. , 'foo.html'
  43. ];
  44. tmpFiles.forEach(function (f) {
  45. rmRf(f, {
  46. silent: true
  47. });
  48. });
  49. callback && callback();
  50. };
  51. suite('rule', function () {
  52. this.timeout(7000);
  53. setup(function (next) {
  54. cleanUpAndNext(next);
  55. });
  56. // - name foo:bin/main.o
  57. // - pattern bin/%.o
  58. // - source src/%.c
  59. //
  60. // return {
  61. // 'dep' : 'foo:src/main.c',
  62. // 'file': 'src/main.c'
  63. // };
  64. test('Rule.getSource', function () {
  65. let src = Rule.getSource('foo:bin/main.o', 'bin/%.o', 'src/%.c');
  66. assert.equal('foo:src/main.c', src);
  67. });
  68. test('rule w/o pattern', function () {
  69. let out = exec( `${JAKE_CMD} -q tmp`).toString().trim();
  70. let output = [
  71. "tmp_dep2.c task"
  72. , "tmp_dep1.c task"
  73. , "cp tmp_dep1.c tmp_dep1.o task"
  74. , "cp tmp_dep2.c tmp_dep2.o task"
  75. , "tmp task"];
  76. assert.equal( output.join('\n'), out);
  77. let data = fs.readFileSync(process.cwd() + '/tmp');
  78. assert.equal('src_1src_2', data.toString());
  79. cleanUpAndNext();
  80. });
  81. test('rule w pattern w/o folder w/o namespace', function () {
  82. let out = exec( `${JAKE_CMD} -q tmp_p`).toString().trim();
  83. let output = [
  84. "tmp_dep2.c task"
  85. , "tmp_dep1.c task"
  86. , "cp tmp_dep1.c tmp_dep1.oo task"
  87. , "cp tmp_dep2.c tmp_dep2.oo task"
  88. , "tmp pattern task"];
  89. let data;
  90. assert.equal( output.join('\n'), out);
  91. data = fs.readFileSync(process.cwd() + '/tmp_p');
  92. assert.equal('src_1src_2 pattern', data.toString());
  93. cleanUpAndNext();
  94. });
  95. test('rule w pattern w folder w/o namespace', function () {
  96. let out = exec( `${JAKE_CMD} -q tmp_pf`).toString().trim();
  97. let output = [
  98. "tmpsrc/tmp_dep1.c task"
  99. , "cp tmpsrc/tmp_dep1.c tmpbin/tmp_dep1.oo task"
  100. , "tmpsrc/tmp_dep2.c task"
  101. , "cp tmpsrc/tmp_dep2.c tmpbin/tmp_dep2.oo task"
  102. , "tmp pattern folder task"];
  103. let data;
  104. assert.equal( output.join('\n'), out);
  105. data = fs.readFileSync(process.cwd() + '/tmp_pf');
  106. assert.equal('src/src_1src/src_2 pattern folder', data.toString());
  107. cleanUpAndNext();
  108. });
  109. test.skip('rule w pattern w folder w namespace', function () {
  110. let out = exec( `${JAKE_CMD} -q tmp_ns`).toString().trim();
  111. let output = [
  112. "tmpsrc/file2.c init task" // yes
  113. , "tmpsrc/tmp_dep2.c task" // no
  114. , "cp tmpsrc/tmp_dep2.c tmpbin/tmp_dep2.oo task" // no
  115. , "tmpsrc/dep1.c task" // no
  116. , "cp tmpsrc/dep1.c tmpbin/dep1.oo ns task" // no
  117. , "cp tmpsrc/file2.c tmpbin/file2.oo ns task" // yes
  118. , "tmp pattern folder namespace task"]; // yes
  119. let data;
  120. assert.equal( output.join('\n'), out);
  121. data = fs.readFileSync(process.cwd() + '/tmp_ns');
  122. assert.equal('src/src_1src/src_2src/src_3 pattern folder namespace', data.toString());
  123. cleanUpAndNext();
  124. });
  125. test.skip('rule w chain w pattern w folder w namespace', function () {
  126. let out = exec( `${JAKE_CMD} -q tmp_cr`).toString().trim();
  127. let output = [
  128. "chainrule init task"
  129. , "cp tmpsrc/file1.tex tmpbin/file1.dvi tex->dvi task"
  130. , "cp tmpbin/file1.dvi tmpbin/file1.pdf dvi->pdf task"
  131. , "cp tmpsrc/file2.tex tmpbin/file2.dvi tex->dvi task"
  132. , "cp tmpbin/file2.dvi tmpbin/file2.pdf dvi->pdf task"
  133. , "tmp chainrule namespace task"];
  134. let data;
  135. assert.equal( output.join('\n'), out);
  136. data = fs.readFileSync(process.cwd() + '/tmp_cr');
  137. assert.equal('tex1 tex2 chainrule namespace', data.toString());
  138. cleanUpAndNext();
  139. });
  140. ['precedence', 'regexPattern', 'sourceFunction'].forEach(function (key) {
  141. test('rule with source file not created yet (' + key + ')', function () {
  142. let write = process.stderr.write;
  143. process.stderr.write = () => {};
  144. rmRf('foo.txt', {silent: true});
  145. rmRf('foo.html', {silent: true});
  146. try {
  147. exec(`${JAKE_CMD} ` + key + ':test');
  148. }
  149. catch(err) {
  150. // foo.txt prereq doesn't exist yet
  151. assert.ok(err.message.indexOf('Unknown task "foo.html"') > -1);
  152. }
  153. process.stderr.write = write;
  154. });
  155. test('rule with source file now created (' + key + ')', function () {
  156. fs.writeFileSync('foo.txt', '');
  157. let out = exec(`${JAKE_CMD} -q ` + key + ':test').toString().trim();
  158. // Should run prereq and test task
  159. let output = [
  160. 'created html'
  161. , 'ran test'
  162. ];
  163. assert.equal(output.join('\n'), out);
  164. });
  165. test('rule with source file modified (' + key + ')', function (next) {
  166. setTimeout(function () {
  167. fs.writeFileSync('foo.txt', '');
  168. let out = exec(`${JAKE_CMD} -q ` + key + ':test').toString().trim();
  169. // Should again run both prereq and test task
  170. let output = [
  171. 'created html'
  172. , 'ran test'
  173. ];
  174. assert.equal(output.join('\n'), out);
  175. //next();
  176. cleanUpAndNext(next);
  177. }, 1000); // Wait to do the touch to ensure mod-time is different
  178. });
  179. test('rule with existing objective file and no source ' +
  180. ' (should be normal file-task) (' + key + ')', function () {
  181. // Remove just the source file
  182. fs.writeFileSync('foo.html', '');
  183. rmRf('foo.txt', {silent: true});
  184. let out = exec(`${JAKE_CMD} -q ` + key + ':test').toString().trim();
  185. // Should treat existing objective file as plain file-task,
  186. // and just run test-task
  187. let output = [
  188. 'ran test'
  189. ];
  190. assert.equal(output.join('\n'), out);
  191. cleanUpAndNext();
  192. });
  193. });
  194. });