test.cli.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. var should = require('should'),
  2. fs = require('fs'),
  3. path = require('path'),
  4. spawn = require('child_process').spawn,
  5. exec = require('child_process').exec,
  6. rimraf = require('rimraf'),
  7. glob = require('glob'),
  8. mkdirp = require('mkdirp'),
  9. typogr = require('../typogr');
  10. describe('./bin/typogr', function () {
  11. // variables used throughout the tests
  12. var testText = '<h2>"Jayhawks" & KU fans act extremely obnoxiously</h2>',
  13. renderedText = '<h2><span class="dquo">&#8220;</span>Jayhawks&#8221; <span class="amp">&amp;</span> <span class=\"caps\">KU</span> fans act extremely<span class="widont">&nbsp;</span>obnoxiously</h2>',
  14. testDir = '/var/tmp/typogr/',
  15. inputDir = testDir + 'input/',
  16. inputFiles = [ 'file1.html',
  17. 'file2.html',
  18. 'file3.html',
  19. 'firstleveldir/file4.html',
  20. 'firstleveldir/secondleveldir/file5.html',
  21. 'firstleveldir/secondleveldir/file6.html'
  22. ],
  23. outputDir = testDir + '/output';
  24. // run before every test
  25. beforeEach(function () {
  26. // create input files
  27. inputFiles.forEach(function (fileName) {
  28. mkdirp.sync(path.dirname(inputDir + fileName));
  29. fs.writeFileSync(inputDir + fileName, testText);
  30. });
  31. });
  32. it('should create test files', function (done) {
  33. glob(inputDir + '**', { cwd: testDir }, function (error, files) {
  34. files.should.have.length(9);
  35. done();
  36. });
  37. });
  38. it('should have test files with expected contents', function (done) {
  39. var stats, contents;
  40. glob(inputDir + '**', { cwd: testDir }, function (error, files) {
  41. if(error) throw error;
  42. files.forEach(function (fileName) {
  43. stats = fs.statSync(fileName);
  44. if(stats.isFile()) {
  45. contents = fs.readFileSync(fileName, 'utf8');
  46. contents.should.equal(testText);
  47. }
  48. });
  49. done();
  50. });
  51. });
  52. it('# should take input from stdin and output to stdout', function (done) {
  53. var stdout = "";
  54. var typogr = spawn('./bin/typogr', [inputDir + inputFiles[0]]);
  55. typogr.stdin.write(testText);
  56. typogr.stdin.end();
  57. typogr.stdout.on('data', function (data) {
  58. stdout += data;
  59. });
  60. typogr.on('exit', function (code) {
  61. if(code) throw code;
  62. stdout.should.equal(renderedText + '\n');
  63. done();
  64. });
  65. });
  66. it('# should overwrite input file in place ( -if)' + inputDir + inputFiles[0],
  67. function (done) {
  68. exec('./bin/typogr -if ' + inputDir + inputFiles[0], function (error, stdout, stderr) {
  69. if(error) throw error;
  70. // make sure file now has rendered text
  71. var contents = fs.readFileSync(inputDir + inputFiles[0], 'utf8');
  72. contents.should.equal(renderedText);
  73. done();
  74. });
  75. });
  76. it('# should read from one file and write to another', function (done) {
  77. exec('./bin/typogr ' + inputDir + inputFiles[0] + ' ' + outputDir + inputFiles[0],
  78. function (error, stdout, stderr) {
  79. if(error) throw error;
  80. // make sure file now has rendered text
  81. var contents = fs.readFileSync(outputDir + inputFiles[0], 'utf8');
  82. contents.should.equal(renderedText);
  83. done();
  84. });
  85. });
  86. it('# should read from one directory and write to another', function (done) {
  87. exec('./bin/typogr ' + inputDir + ' ' + outputDir, function (error, stdout, stderr) {
  88. if(error) throw error;
  89. // make sure file now has rendered text in each file
  90. glob(outputDir + '**', { cwd: testDir }, function (error, files) {
  91. if(error) throw error;
  92. files.forEach(function (fileName) {
  93. if(fs.statSync(fileName).isFile()) {
  94. contents = fs.readFileSync(fileName, 'utf8');
  95. contents.should.equal(renderedText);
  96. }
  97. });
  98. done();
  99. });
  100. });
  101. });
  102. it('# should process glob: /**', function (done) {
  103. exec('./bin/typogr ' + inputDir + '** ' + outputDir, function (error, stdout, stderr) {
  104. if(error) throw error;
  105. // make sure file now has rendered text in each file
  106. glob(outputDir + '**', { cwd: testDir }, function (error, files) {
  107. if(error) throw error;
  108. files.forEach(function (fileName) {
  109. stats = fs.statSync(fileName);
  110. if(stats.isFile()) {
  111. contents = fs.readFileSync(fileName, 'utf8');
  112. contents.should.equal(renderedText);
  113. }
  114. });
  115. done();
  116. });
  117. });
  118. });
  119. it('# should prompt for confirmation to overwrite single file: negative response', function (done) {
  120. // in place modification of a single file
  121. // test negative response
  122. var buffer,
  123. response = false,
  124. typogr = spawn('./bin/typogr', ['-i', inputDir + inputFiles[0]]);
  125. typogr.stdout.on('readable', function () {
  126. buffer = typogr.stdout.read(null);
  127. if (/Confirm: overwrite file \(.+\)\? /.test(buffer))
  128. typogr.stdin.end('n\n');
  129. else {
  130. if (/Cancelling action, file wasn't modified\./.test(buffer)) {
  131. response = true;
  132. }
  133. else
  134. if (!response) {
  135. throw 'unexpected text found!';
  136. }
  137. }
  138. });
  139. typogr.on('exit', function (error) {
  140. if(error) throw error;
  141. response.should.equal(true);
  142. done();
  143. });
  144. });
  145. it('# should prompt for confirmation to overwrite single file: positive response', function (done) {
  146. // test positive response
  147. var buffer,
  148. response = false,
  149. typogr = spawn('./bin/typogr', ['-i', inputDir + inputFiles[0]]);
  150. typogr.stdout.on('readable', function () {
  151. buffer = typogr.stdout.read(null);
  152. if (/Confirm: overwrite file \(.+\)\? /.test(buffer)) {
  153. response = true;
  154. typogr.stdin.end('y\n');
  155. }
  156. else
  157. if (!response) {
  158. throw 'unexpected text found!';
  159. }
  160. });
  161. typogr.on('exit', function (error) {
  162. if(error) throw error;
  163. response.should.equal(true);
  164. done();
  165. });
  166. });
  167. it('# should prompt for confirmation of each overwritten file when writing from an input directory to an output directory', function (done) {
  168. // we should get prompted for each file overwrite
  169. var buffer,
  170. confirmCount = 0,
  171. typogr = spawn('./bin/typogr', [inputDir, inputDir]);
  172. typogr.stdout.on('readable', function () {
  173. buffer = typogr.stdout.read(null);
  174. if (/Confirm: overwrite file \(.+\)\? /.test(buffer)) {
  175. typogr.stdin.write('y\n');
  176. ++confirmCount;
  177. }
  178. else
  179. if (buffer)
  180. throw 'unexpected text found!';
  181. });
  182. // wait for cli interactions to complete
  183. setTimeout(function () {
  184. typogr.stdin.end();
  185. }, 1000);
  186. typogr.on('exit', function (code) {
  187. if(code) throw code;
  188. confirmCount.should.equal(6);
  189. done();
  190. });
  191. });
  192. it('# should prompt for confirmation of each overwritten file when writing from an input glob to an output directory', function (done) {
  193. // we should get prompted for each file overwrite
  194. var buffer,
  195. confirmCount = 0,
  196. typogr = spawn('./bin/typogr', [inputDir, inputDir]);
  197. typogr.stdout.on('readable', function () {
  198. buffer = typogr.stdout.read(null);
  199. if (/Confirm: overwrite file \(.+\)\? /.test(buffer)) {
  200. typogr.stdin.write('y\n');
  201. ++confirmCount;
  202. }
  203. else
  204. if (buffer)
  205. throw 'unexpected text found!';
  206. });
  207. // wait for cli interactions to complete
  208. setTimeout(function () {
  209. typogr.stdin.end();
  210. }, 1000);
  211. typogr.on('exit', function (code) {
  212. if(code) throw code;
  213. confirmCount.should.equal(6);
  214. done();
  215. });
  216. });
  217. // run after every test
  218. afterEach(function (done) {
  219. // remove all test files in testDir (equivalent to `rm -rf`)
  220. rimraf(testDir, function (error) {
  221. if(error) throw error;
  222. done();
  223. });
  224. });
  225. });