You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.6 KiB

  1. 'use strict';
  2. var grunt = require('grunt');
  3. var path = require('path');
  4. var fs = require('fs');
  5. var helper = require('./helper');
  6. var fixtures = helper.fixtures;
  7. var useFixtures = ['multiTargets', 'oneTarget'];
  8. function cleanUp() {
  9. useFixtures.forEach(function(fixture) {
  10. helper.cleanUp(fixture + '/node_modules');
  11. });
  12. }
  13. exports.watchConfig = {
  14. setUp: function(done) {
  15. cleanUp();
  16. useFixtures.forEach(function(fixture) {
  17. fs.symlinkSync(path.join(__dirname, '../../node_modules'), path.join(fixtures, fixture, 'node_modules'));
  18. });
  19. done();
  20. },
  21. tearDown: function(done) {
  22. cleanUp();
  23. done();
  24. },
  25. oneTarget: function(test) {
  26. test.expect(2);
  27. var cwd = path.resolve(fixtures, 'oneTarget');
  28. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  29. assertWatch(function() {
  30. var write = 'var test = true;';
  31. grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
  32. }, function(result) {
  33. helper.verboseLog(result);
  34. test.ok(result.indexOf('File "lib' + path.sep + 'one.js" changed') !== -1, 'Watch should have fired when oneTarget/lib/one.js has changed.');
  35. test.ok(result.indexOf('I do absolutely nothing.') !== -1, 'echo task should have fired.');
  36. test.done();
  37. });
  38. },
  39. multiTargetsTriggerOneNotTwo: function(test) {
  40. test.expect(2);
  41. var cwd = path.resolve(fixtures, 'multiTargets');
  42. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  43. assertWatch(function() {
  44. var write = 'var test = true;';
  45. grunt.file.write(path.join(cwd, 'lib', 'one.js'), write);
  46. }, function(result) {
  47. helper.verboseLog(result);
  48. test.ok(result.indexOf('one has changed') !== -1, 'Only task echo:one should of emit.');
  49. test.ok(result.indexOf('two has changed') === -1, 'Task echo:two should NOT emit.');
  50. test.done();
  51. });
  52. },
  53. multiTargetsSequentialFilesChangeTriggerBoth: function(test) {
  54. test.expect(2);
  55. var cwd = path.resolve(fixtures, 'multiTargets');
  56. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  57. assertWatch([function() {
  58. grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
  59. }, function() {
  60. grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var test = true;');
  61. }], function(result) {
  62. helper.verboseLog(result);
  63. test.ok(result.indexOf('one has changed') !== -1, 'Task echo:one should of emit.');
  64. test.ok(result.indexOf('two has changed') !== -1, 'Task echo:two should of emit.');
  65. test.done();
  66. });
  67. },
  68. multiTargetsSimultaneousFilesChangeTriggerBoth: function(test) {
  69. test.expect(2);
  70. var cwd = path.resolve(fixtures, 'multiTargets');
  71. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  72. assertWatch([function() {
  73. grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
  74. grunt.file.write(path.join(cwd, 'lib', 'two.js'), 'var test = true;');
  75. }], function(result) {
  76. helper.verboseLog(result);
  77. test.ok(result.indexOf('one has changed') !== -1, 'Task echo:one should of emit.');
  78. test.ok(result.indexOf('two has changed') !== -1, 'Task echo:two should of emit.');
  79. test.done();
  80. });
  81. },
  82. spawnOneAtATime: function(test) {
  83. test.expect(1);
  84. var cwd = path.resolve(fixtures, 'multiTargets');
  85. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  86. assertWatch(function() {
  87. grunt.file.write(path.join(cwd, 'lib', 'wait.js'), 'var wait = false;');
  88. setTimeout(function() {
  89. grunt.file.write(path.join(cwd, 'lib', 'wait.js'), 'var wait = true;');
  90. }, 500);
  91. }, function(result) {
  92. helper.verboseLog(result);
  93. test.ok(result.indexOf('I waited 2s') !== -1, 'Task should have waited 2s and only spawned once.');
  94. test.done();
  95. });
  96. },
  97. interrupt: function(test) {
  98. test.expect(1);
  99. var cwd = path.resolve(fixtures, 'multiTargets');
  100. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  101. assertWatch(function() {
  102. grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = false;');
  103. setTimeout(function() {
  104. grunt.file.write(path.join(cwd, 'lib', 'interrupt.js'), 'var interrupt = true;');
  105. }, 1000);
  106. }, function(result) {
  107. helper.verboseLog(result);
  108. test.ok(result.indexOf('have been interrupted') !== -1, 'Task should have been interrupted.');
  109. test.done();
  110. });
  111. },
  112. failingTask: function(test) {
  113. test.expect(2);
  114. var cwd = path.resolve(fixtures, 'multiTargets');
  115. var assertWatch = helper.assertTask('watch', {cwd:cwd});
  116. assertWatch(function() {
  117. grunt.file.write(path.join(cwd, 'lib', 'fail.js'), 'var fail = false;');
  118. }, function(result) {
  119. helper.verboseLog(result);
  120. test.ok(result.toLowerCase().indexOf('fatal') !== -1, 'Task should have been fatal.');
  121. test.equal(grunt.util._(result).count('Waiting...'), 2, 'Should have displayed "Wating..." twice');
  122. test.done();
  123. });
  124. },
  125. runAgainNotInterrupted: function(test) {
  126. test.expect(1);
  127. var cwd = path.resolve(fixtures, 'multiTargets');
  128. var assertWatch = helper.assertTask(['watch:one', '--wait=4000'], {cwd:cwd});
  129. assertWatch([function() {
  130. grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
  131. setTimeout(function() {
  132. grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
  133. }, 2000);
  134. setTimeout(function() {
  135. grunt.file.write(path.join(cwd, 'lib', 'one.js'), 'var test = true;');
  136. }, 3000);
  137. }], function(result) {
  138. helper.verboseLog(result);
  139. test.equal(grunt.util._(result).count('one has changed'), 2, 'Should have ran the one task twice.');
  140. test.done();
  141. });
  142. },
  143. };