file_task.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 fs = require('fs');
  22. let exec = require('child_process').execSync;
  23. let { rmRf } = require(`${PROJECT_DIR}/lib/jake`);
  24. let cleanUpAndNext = function (callback) {
  25. rmRf('./foo', {
  26. silent: true
  27. });
  28. callback && callback();
  29. };
  30. suite('fileTask', function () {
  31. this.timeout(7000);
  32. setup(function () {
  33. cleanUpAndNext();
  34. });
  35. test('where a file-task prereq does not change with --always-make', function () {
  36. let out;
  37. out = exec(`${JAKE_CMD} -q fileTest:foo/from-src1.txt`).toString().trim();
  38. assert.equal('fileTest:foo/src1.txt task\nfileTest:foo/from-src1.txt task',
  39. out);
  40. out = exec(`${JAKE_CMD} -q -B fileTest:foo/from-src1.txt`).toString().trim();
  41. assert.equal('fileTest:foo/src1.txt task\nfileTest:foo/from-src1.txt task',
  42. out);
  43. cleanUpAndNext();
  44. });
  45. test('concating two files', function () {
  46. let out;
  47. out = exec(`${JAKE_CMD} -q fileTest:foo/concat.txt`).toString().trim();
  48. assert.equal('fileTest:foo/src1.txt task\ndefault task\nfileTest:foo/src2.txt task\n' +
  49. 'fileTest:foo/concat.txt task', out);
  50. // Check to see the two files got concat'd
  51. let data = fs.readFileSync(process.cwd() + '/foo/concat.txt');
  52. assert.equal('src1src2', data.toString());
  53. cleanUpAndNext();
  54. });
  55. test('where a file-task prereq does not change', function () {
  56. let out;
  57. out = exec(`${JAKE_CMD} -q fileTest:foo/from-src1.txt`).toString().trim();
  58. assert.equal('fileTest:foo/src1.txt task\nfileTest:foo/from-src1.txt task', out);
  59. out = exec(`${JAKE_CMD} -q fileTest:foo/from-src1.txt`).toString().trim();
  60. // Second time should be a no-op
  61. assert.equal('', out);
  62. cleanUpAndNext();
  63. });
  64. test('where a file-task prereq does change, then does not', function (next) {
  65. exec('mkdir -p ./foo');
  66. exec('touch ./foo/from-src1.txt');
  67. setTimeout(() => {
  68. fs.writeFileSync('./foo/src1.txt', '-SRC');
  69. // Task should run the first time
  70. let out;
  71. out = exec(`${JAKE_CMD} -q fileTest:foo/from-src1.txt`).toString().trim();
  72. assert.equal('fileTest:foo/from-src1.txt task', out);
  73. // Task should not run on subsequent invocation
  74. out = exec(`${JAKE_CMD} -q fileTest:foo/from-src1.txt`).toString().trim();
  75. assert.equal('', out);
  76. cleanUpAndNext(next);
  77. }, 1000);
  78. });
  79. test('a preexisting file', function () {
  80. let prereqData = 'howdy';
  81. exec('mkdir -p ./foo');
  82. fs.writeFileSync('foo/prereq.txt', prereqData);
  83. let out;
  84. out = exec(`${JAKE_CMD} -q fileTest:foo/from-prereq.txt`).toString().trim();
  85. assert.equal('fileTest:foo/from-prereq.txt task', out);
  86. let data = fs.readFileSync(process.cwd() + '/foo/from-prereq.txt');
  87. assert.equal(prereqData, data.toString());
  88. out = exec(`${JAKE_CMD} -q fileTest:foo/from-prereq.txt`).toString().trim();
  89. // Second time should be a no-op
  90. assert.equal('', out);
  91. cleanUpAndNext();
  92. });
  93. test('a preexisting file with --always-make flag', function () {
  94. let prereqData = 'howdy';
  95. exec('mkdir -p ./foo');
  96. fs.writeFileSync('foo/prereq.txt', prereqData);
  97. let out;
  98. out = exec(`${JAKE_CMD} -q fileTest:foo/from-prereq.txt`).toString().trim();
  99. assert.equal('fileTest:foo/from-prereq.txt task', out);
  100. let data = fs.readFileSync(process.cwd() + '/foo/from-prereq.txt');
  101. assert.equal(prereqData, data.toString());
  102. out = exec(`${JAKE_CMD} -q -B fileTest:foo/from-prereq.txt`).toString().trim();
  103. assert.equal('fileTest:foo/from-prereq.txt task', out);
  104. cleanUpAndNext();
  105. });
  106. test('nested directory-task', function () {
  107. exec(`${JAKE_CMD} -q fileTest:foo/bar/baz/bamf.txt`);
  108. let data = fs.readFileSync(process.cwd() + '/foo/bar/baz/bamf.txt');
  109. assert.equal('w00t', data);
  110. cleanUpAndNext();
  111. });
  112. test('partially existing prereqs', function () {
  113. /*
  114. dependency graph:
  115. /-- foo/output2a.txt --\
  116. foo -- foo/output1.txt --+ +-- output3.txt
  117. \-- foo/output2b.txt --/
  118. */
  119. // build part of the prereqs
  120. exec(`${JAKE_CMD} -q fileTest:foo/output2a.txt`);
  121. // verify the final target gets built
  122. exec(`${JAKE_CMD} -q fileTest:foo/output3.txt`);
  123. let data = fs.readFileSync(process.cwd() + '/foo/output3.txt');
  124. assert.equal('w00t', data);
  125. cleanUpAndNext();
  126. });
  127. });