conflicter.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*global describe, before, it */
  2. var fs = require('fs');
  3. var events = require('events');
  4. var assert = require('assert');
  5. var proxyquire = require('proxyquire');
  6. var conflicter = require('../lib/util/conflicter');
  7. describe('conflicter', function () {
  8. beforeEach(conflicter.reset.bind(conflicter));
  9. conflicter.on('conflict', function () {
  10. process.nextTick(function () {
  11. process.stdin.write('Y\n');
  12. process.stdin.emit('data', 'Y\n');
  13. });
  14. });
  15. it('is an event emitter', function () {
  16. assert.ok(conflicter instanceof events.EventEmitter);
  17. });
  18. it('conflicter#add(conflict)`', function () {
  19. conflicter.add(__filename);
  20. var conflict = conflicter.conflicts.pop();
  21. assert.deepEqual(conflict.file, __filename);
  22. assert.deepEqual(conflict.content, fs.readFileSync(__filename, 'utf8'));
  23. });
  24. describe('conflicter#resolve(cb)', function (done) {
  25. it('wihout conflict', function (done) {
  26. conflicter.resolve(done);
  27. });
  28. it('with at least one, without async callback handling', function (done) {
  29. var conflicts = 0;
  30. var callbackExecuted = false;
  31. conflicter.add(__filename);
  32. conflicter.add({
  33. file: 'foo.js',
  34. content: 'var foo = "foo";\n'
  35. });
  36. // called.
  37. conflicter.once('resolved:' + __filename, function (config) {
  38. conflicts++;
  39. });
  40. // not called.
  41. conflicter.once('resolved:foo.js', function (config) {
  42. conflicts++;
  43. });
  44. conflicter.resolve(function(){
  45. callbackExecuted = true;
  46. });
  47. assert(conflicts, 1);
  48. assert(!callbackExecuted);
  49. done();
  50. });
  51. it('with at least one, with async callback handling', function (done) {
  52. var called = 0;
  53. conflicter.add(__filename);
  54. conflicter.add({
  55. file: 'foo.js',
  56. content: 'var foo = "foo";\n'
  57. });
  58. conflicter.once('resolved:' + __filename, function (config) {
  59. called++;
  60. config.callback();
  61. });
  62. conflicter.once('resolved:foo.js', function (config) {
  63. called++;
  64. config.callback();
  65. });
  66. conflicter.resolve(function(){
  67. assert(called, 2);
  68. done();
  69. });
  70. });
  71. });
  72. describe.skip('conflicter#collision(filepath, content, cb)', function (done) {
  73. var me = fs.readFileSync(__filename, 'utf8');
  74. it('identical status', function(done) {
  75. conflicter.collision(__filename, me, function (status) {
  76. assert.equal(status, 'identical');
  77. done();
  78. });
  79. });
  80. it('create status', function (done) {
  81. conflicter.collision('foo.js', '', function (status) {
  82. assert.equal(status, 'create');
  83. done();
  84. });
  85. });
  86. it('conflict status', function (done) {
  87. conflicter.collision(__filename, '', function (status) {
  88. assert.equal(status, 'force');
  89. done();
  90. });
  91. });
  92. });
  93. it('conflicter#diff(actual, expected)', function () {
  94. var diff = conflicter.diff('var', 'let');
  95. assert.equal(diff, '\n\u001b[41mremoved\u001b[49m \u001b[42madded\u001b[49m\n\n\u001b[42mlet\u001b[49m\u001b[41mvar\u001b[49m\n');
  96. });
  97. describe('conflicter#_ask', function () {
  98. var promptMock = {
  99. prompt: function (config, cb) {
  100. cb({ overwrite: this.answer });
  101. },
  102. err: null,
  103. answer: null
  104. };
  105. before(function () {
  106. this.conflicter = proxyquire('../lib/util/conflicter', {
  107. '../actions/prompt': promptMock.prompt.bind(promptMock)
  108. });
  109. });
  110. it('Calls answer related function and pass a callback', function (done) {
  111. var callCount = 0;
  112. promptMock.answer = function (cb) { callCount++; cb(); };
  113. this.conflicter._ask('/tmp/file', 'my file contents', function (result) {
  114. assert(callCount, 1);
  115. done();
  116. });
  117. });
  118. });
  119. });