summaryrefslogtreecommitdiff
path: root/node_modules/mmmagic/test/test.js
blob: 934b011e3e9b96bf0c612382ca0eb3a2039f42a4 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var mmm = require('../lib/index');

var path = require('path');
var assert = require('assert');
var fs = require('fs');
var format = require('util').format;

var t = -1;
var group = path.basename(__filename, '.js') + '/';
var timeout;

var tests = [
  { run: function() {
      var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
      magic.detectFile(path.join(__dirname, '..', 'src', 'binding.cc'),
                       function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(result, 'text/x-c++');
        next();
      });
    },
    what: 'detectFile - Normal operation, mime type'
  },
  { run: function() {
      var magic = new mmm.Magic(mmm.MAGIC_MIME_ENCODING);
      magic.detectFile(path.join(__dirname, '..', 'src', 'binding.cc'),
                       function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(result, 'us-ascii');
        next();
      });
    },
    what: 'detectFile - Normal operation, encoding'
  },
  { run: function() {
      var magic = new mmm.Magic(mmm.MAGIC_MIME);
      magic.detectFile(path.join(__dirname, '..', 'src', 'binding.cc'),
                       function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(result, 'text/x-c++; charset=us-ascii');
        next();
      });
    },
    what: 'detectFile - Normal operation, mime type + encoding'
  },
  { run: function() {
      var magic = new mmm.Magic();
      magic.detectFile(path.join(__dirname, '..', 'src', 'binding.cc'),
                       function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(/^C\+\+ source, ASCII text/.test(result), true);
        next();
      });
    },
    what: 'detectFile - Normal operation, description'
  },
  { run: function() {
      var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE | mmm.MAGIC_CONTINUE);
      magic.detectFile(path.join(__dirname, '..', 'src', 'binding.cc'),
                       function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(Array.isArray(result), true);
        assert.strictEqual(result[0], 'text/x-c++');
        next();
      });
    },
    what: 'detectFile - Normal operation, find all matches'
  },
  { run: function() {
      var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
      magic.detectFile('/no/such/path1234567', function(err, result) {
        assert(err);
        assert.strictEqual(result, undefined);
        next();
      });
    },
    what: 'detectFile - Nonexistent file'
  },
  { run: function() {
      var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
      magic.detectFile(path.join(__dirname, 'fixtures', 'tést.txt'),
                       function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(result, 'text/x-c++');
        next();
      });
    },
    what: 'detectFile - UTF-8 filename'
  },
  { run: function() {
      var buf = fs.readFileSync(path.join(__dirname, '..', 'src', 'binding.cc'));
      var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
      magic.detect(buf, function(err, result) {
        assert.strictEqual(err, null);
        assert.strictEqual(result, 'text/x-c++');
        next();
      });
    },
    what: 'detect - Normal operation, mime type'
  },
];

function next() {
  clearTimeout(timeout);
  if (t > -1)
    console.log('Finished  %j', tests[t].what)
  if (t === tests.length - 1)
    return;
  var v = tests[++t];
  timeout = setTimeout(function() {
    throw new Error(format('Test case %j timed out', v.what));
  }, 10 * 1000);
  console.log('Executing %j', v.what);
  v.run.call(v);
}

function makeMsg(msg) {
  var fmtargs = ['[%s]: ' + msg, tests[t].what];
  for (var i = 1; i < arguments.length; ++i)
    fmtargs.push(arguments[i]);
  return format.apply(null, fmtargs);
}

process.once('uncaughtException', function(err) {
  if (t > -1 && !/(?:^|\n)AssertionError: /i.test(''+err))
    console.error(makeMsg('Unexpected Exception:'));

  throw err;
}).once('exit', function() {
  assert(t === tests.length - 1,
         makeMsg('Only finished %d/%d tests', (t + 1), tests.length));
});

next();