aboutsummaryrefslogtreecommitdiff
path: root/tests/main.tests.js
blob: 3b56b0b8d2dd568e5d1c71783d594651e8ecfe36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var assert = require("assert");
var child_process = require("child_process");
var path = require("path");
var fs = require("fs");

var temp = require('temp').track();

var promises = require("../lib/promises");
var test = require("./testing").test;
var testPath = require("./testing").testPath;

describe("CLI", function() {
    test("HTML is printed to stdout if output file is not set", function() {
        return runMammoth(testPath("single-paragraph.docx")).then(function(result) {
            assert.equal(result.stderrOutput, "")
            assert.equal(result.output, "<p>Walking on imported air</p>")
        });
    });
    
    test("HTML is written to file if output file is set", function() {
        return createTempDir().then(function(tempDir) {
            var outputPath = path.join(tempDir, "output.html");
            return runMammoth(testPath("single-paragraph.docx"), outputPath).then(function(result) {
                assert.equal(result.stderrOutput, "")
                assert.equal(result.output, "")
                assert.equal(fs.readFileSync(outputPath), "<p>Walking on imported air</p>");
            });
        });
    });
    
    test("inline images are included in output if writing to single file", function() {
        return runMammoth(testPath("tiny-picture.docx")).then(function(result) {
            assert.equal(result.stderrOutput, "")
            assert.equal(result.output, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>')
        });
    });
});

function runMammoth() {
    var args = Array.prototype.slice.call(arguments, 0);
    var deferred = promises.defer();
    
    var processArgs = ["node", "bin/mammoth"].concat(args);
    // TODO: proper escaping of args
    var command = processArgs.join(" ");
    child_process.exec(command, function(error, stdout, stderr) {
        console.log(stderr);
        assert.equal(error, null);
        deferred.resolve({output: stdout, stderrOutput: stderr});
    });
    
    return deferred.promise;
}

function createTempDir() {
    return promises.nfcall(temp.mkdir, null);
}