-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
143 lines (115 loc) · 3.93 KB
/
test.js
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
135
136
137
138
139
140
141
142
143
/*!
* to-exports <https://github.com/jonschlinkert/to-exports>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
/* deps:mocha */
var path = require('path');
var exportFiles = require('./');
require('should');
/**
* Support functions
*/
function rename(fp) {
return path.basename(fp);
}
function filter(re) {
return function(fp) {
return re.test(fp);
}
}
// path must be absolute for requires to work
var fixtures = path.join(process.cwd(), 'fixtures');
/**
* Tests
*/
describe('export-files:', function() {
it('should recurse when `true` is passed', function() {
var files = exportFiles(fixtures, true);
files.should.have.properties('a', 'b', 'c', 'd', 'i', 'j', 'k');
});
it('should return the modules from the given cwd', function() {
var files = exportFiles(fixtures);
files.should.have.properties('a', 'b', 'c', 'd');
files.d.should.have.properties('foo', 'bar', 'baz');
});
it('should not recurse by default', function() {
var files = exportFiles(fixtures);
files.should.have.properties('a', 'b', 'c', 'd');
files.should.not.have.properties('i', 'j', 'k');
});
});
describe('glob/regex patterns', function() {
it('should support glob patterns for filtering files:', function() {
var files = exportFiles(fixtures, '**/*.json');
files.should.eql({ a: { desc: 'aaa', name: 'a.json' }, b: { bb: 'bbb' } });
});
it('should support regex for filtering files:', function() {
var re = /\.json/;
var files = exportFiles(fixtures, re);
files.should.eql({ a: { desc: 'aaa', name: 'a.json' }, b: { bb: 'bbb' } });
});
});
describe('callback function', function() {
it('should allow a callback function to read files', function() {
var files = exportFiles(fixtures, function (fp) {
return require(fp)
});
files.should.have.properties('a', 'b', 'c', 'd');
files.should.not.have.properties('i', 'j', 'k');
});
it('should use the callback to read files and rename keys from options:', function() {
var files = exportFiles(fixtures, {renameKey: rename}, function (fp) {
return require(fp);
});
files.should.have.properties('a.js', 'b.js', 'c.js', 'd.js');
files.should.not.have.properties('i.js', 'j.js', 'k.js');
});
});
describe('custom filters', function() {
it('`.css` filter with recurse `true`', function() {
var files = exportFiles(fixtures, true, {filter: filter(/\.css$/)});
files.should.have.properties('a', 'e');
files.a.should.equal('div span {\n color: red;\n}');
files.e.should.equal('#navbar {\n color: black;\n}');
});
it('should dynamically choose the reader:', function() {
var files = exportFiles(fixtures, false, {
read: require('file-reader').file
});
files.should.have.properties('a', 'b');
});
it('should dynamically choose the reader:', function() {
var files = exportFiles(fixtures + '/nested', false, {
read: require('file-reader').file
});
files.should.have.properties('e', 'f', 'g');
});
it('should use a read function and `.yml` filter with recurse `false`', function() {
var files = exportFiles(fixtures, false, {
read: function(fp) {
return require('read-yaml').sync(fp);
},
filter: filter(/\.ya?ml$/)
});
files.should.have.properties('a', 'b');
files.a.should.have.properties('name','desc');
files.b.should.have.property('bb');
files.b.bb.should.equal('bbb');
});
it('should use a `.json` filter with recurse `true`', function() {
var files = exportFiles(fixtures, true, {
filter: filter(/\.json$/)
});
files.should.have.properties('a', 'b', 'e', 'k');
files.a.should.have.properties('name','desc');
files.b.should.have.property('bb');
files.b.bb.should.equal('bbb');
files.e.should.have.property('ee');
files.e.ee.should.equal('eee');
files.k.should.have.property('kk');
files.k.kk.should.equal('kkk');
});
});