File: cache_persistent.js

package info (click to toggle)
node-module-deps 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 764 kB
  • sloc: javascript: 2,403; makefile: 19; sh: 2
file content (122 lines) | stat: -rw-r--r-- 3,544 bytes parent folder | download | duplicates (3)
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
var parser = require('../');
var test = require('tap').test;
var path = require('path');
var fs = require('fs');

var files = {
    foo: path.join(__dirname, '/files/foo.js'),
    bar: path.join(__dirname, '/files/bar.js')
};

test('uses persistent cache', function (t) {
    t.plan(1);
    var p = parser({
        persistentCache: function (file, id, pkg, fallback, cb) {
            if (file === files.bar) {
                return fallback(null, cb);
            }
            cb(null, {
                source: 'file at ' + file + '@' + id,
                package: pkg,
                deps: { './bar': files.bar }
            });
        }
    });
    p.end({ id: 'foo', file: files.foo, entry: false });

    var rows = [];
    p.on('data', function (row) { rows.push(row) });
    p.on('end', function () {
        t.same(rows.sort(cmp), [
            {
                id: files.bar,
                file: files.bar,
                source: fs.readFileSync(files.bar, 'utf8'),
                deps: {}
            },
            {
                id: 'foo',
                file: files.foo,
                source: 'file at ' + files.foo + '@' + files.foo,
                deps: { './bar': files.bar }
            }
        ].sort(cmp));
    });
});

test('passes persistent cache error through', function (t) {
    t.plan(1);
    var p = parser({
        persistentCache: function (file, id, pkg, fallback, cb) {
            cb(new Error('foo'));
        }
    });
    p.end({ id: 'foo', file: files.foo, entry: false });
    p.on('error', function (err) { t.equals(err.message, 'foo') });
});

test('allow passing of the raw source as string', function (t) {
    t.plan(1);
    var p = parser({
        persistentCache: function (file, id, pkg, fallback, cb) {
            fallback(fs.readFileSync(files.bar, 'utf8'), cb);
        }
    });
    p.end({ id: 'foo', file: files.foo, entry: false });

    var rows = [];
    p.on('data', function (row) { rows.push(row) });
    p.on('end', function () {
        t.same(rows.sort(cmp), [
            {
                id: 'foo',
                file: files.foo,
                source: fs.readFileSync(files.bar, 'utf8'),
                deps: {}
            }
        ].sort(cmp));
    });
});

test('send file event with persistent cache', function (t) {
    t.plan(2);
    var p = parser({
        persistentCache: function (file, id, pkg, fallback, cb) {
            cb(null, {
                source: 'file at ' + file + '@' + id,
                package: pkg,
                deps: {}
            });
        }
    });
    p.end({ id: 'foo', file: files.foo, entry: false });
    p.on('file', function (file, id) {
        t.same(file, path.resolve(files.foo));
        t.same(id, path.resolve(files.foo));
    });
});

test('errors of transforms occur in the correct order with a persistent cache', function (t) {
    t.plan(3);
    var p = parser({
        transform: [
            path.join(__dirname, 'cache_persistent', 'error_transform')
        ],
        persistentCache: function (file, id, pkg, fallback, cb) {
            fallback(fs.readFileSync(files.foo, 'utf8'), cb);
        }
    });
    p.end({ id: 'foo', file: files.foo, entry: false });
    var order = 0;
    p.on('file', function (file, id) {
        t.same(order, 0);
        order += 1;
    });
    p.on('error', function (err) {
        t.same(order, 1);
        t.same(err.message, 'rawr while parsing file: ' + path.resolve(files.foo));
    });
});


function cmp (a, b) { return a.id < b.id ? -1 : 1 }