main.tests.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. var assert = require("assert");
  2. var child_process = require("child_process"); // eslint-disable-line camelcase
  3. var path = require("path");
  4. var fs = require("fs");
  5. var temp = require('temp').track();
  6. var promises = require("../lib/promises");
  7. var test = require("./test")(module);
  8. var testPath = require("./testing").testPath;
  9. test("HTML is printed to stdout if output file is not set", function() {
  10. return runMammoth(testPath("single-paragraph.docx")).then(function(result) {
  11. assert.equal(result.stderrOutput, "");
  12. assert.equal(result.output, "<p>Walking on imported air</p>");
  13. });
  14. });
  15. test("HTML is written to file if output file is set", function() {
  16. return createTempDir().then(function(tempDir) {
  17. var outputPath = path.join(tempDir, "output.html");
  18. return runMammoth(testPath("single-paragraph.docx"), outputPath).then(function(result) {
  19. assert.equal(result.stderrOutput, "");
  20. assert.equal(result.output, "");
  21. assert.equal(fs.readFileSync(outputPath, "utf8"), "<p>Walking on imported air</p>");
  22. });
  23. });
  24. });
  25. var imageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=";
  26. test("inline images are included in output if writing to single file", function() {
  27. return runMammoth(testPath("tiny-picture.docx")).then(function(result) {
  28. assert.equal(result.stderrOutput, "");
  29. assert.equal(result.output, '<p><img src="data:image/png;base64,' + imageBase64 + '" /></p>');
  30. });
  31. });
  32. test("images are written to separate files if output dir is set", function() {
  33. return createTempDir().then(function(tempDir) {
  34. var outputPath = path.join(tempDir, "tiny-picture.html");
  35. var imagePath = path.join(tempDir, "1.png");
  36. return runMammoth(testPath("tiny-picture.docx"), "--output-dir", tempDir).then(function(result) {
  37. assert.equal(result.stderrOutput, "");
  38. assert.equal(result.output, "");
  39. assert.equal(fs.readFileSync(outputPath, "utf8"), '<p><img src="1.png" /></p>');
  40. assert.equal(fs.readFileSync(imagePath, "base64"), imageBase64);
  41. });
  42. });
  43. });
  44. test("style map is used if set", function() {
  45. return createTempDir().then(function(tempDir) {
  46. var styleMapPath = path.join(tempDir, "style-map");
  47. fs.writeFileSync(styleMapPath, "p => span:fresh");
  48. return runMammoth(testPath("single-paragraph.docx"), "--style-map", styleMapPath).then(function(result) {
  49. assert.equal(result.stderrOutput, "");
  50. assert.equal(result.output, "<span>Walking on imported air</span>");
  51. });
  52. });
  53. });
  54. test("--output-format=markdown option generate markdown output", function() {
  55. return runMammoth(testPath("single-paragraph.docx"), "--output-format=markdown").then(function(result) {
  56. assert.equal(result.stderrOutput, "");
  57. assert.equal(result.output, "Walking on imported air\n\n");
  58. });
  59. });
  60. function runMammoth() {
  61. var args = Array.prototype.slice.call(arguments, 0);
  62. var deferred = promises.defer();
  63. var processArgs = ["node", "bin/mammoth"].concat(args);
  64. // TODO: proper escaping of args
  65. var command = processArgs.join(" ");
  66. child_process.exec(command, function(error, stdout, stderr) { // eslint-disable-line camelcase
  67. console.log(stderr); // eslint-disable-line no-console
  68. assert.equal(error, null);
  69. deferred.resolve({output: stdout, stderrOutput: stderr});
  70. });
  71. return deferred.promise;
  72. }
  73. function createTempDir() {
  74. return promises.nfcall(temp.mkdir, null);
  75. }