-
Notifications
You must be signed in to change notification settings - Fork 2
/
onessg.js
169 lines (152 loc) · 4.69 KB
/
onessg.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
/* eslint no-console: "off" */
const fs = require('fs-extra');
const path = require('path-extra');
const assert = require('assert');
const suppose = require('suppose');
const resolve = require('autoresolve');
const onessg = require(resolve('index.js'));
// Expand the assert module:
assert.dirsEqual = require('assert-dir-equal');
assert.fixture = function (fixture) {
// Get layoutPath:
var layoutPath = path.join('test/fixtures/', fixture, 'layouts');
try {
fs.accessSync(layoutPath);
} catch (e) {
// Use a dummy directory if there isn't a local one:
layoutPath = 'test/fixtures/empty-dir';
}
// Clean dist:
var distPath = path.join('test/fixtures/', fixture, 'dist');
fs.removeSync(distPath);
// Run onessg:
return onessg({
src: path.join('test/fixtures/', fixture, 'src'),
dist: distPath,
layouts: layoutPath,
})
.then(() => {
// Assert that dist/ & expected/ are equal:
assert.dirsEqual(distPath, path.join('test/fixtures/', fixture, 'expected'));
});
};
suite('cli', function () {
this.timeout(5000);
this.slow(3000);
test('works', function (done) {
fs.removeSync('test/fixtures/cli/dist');
suppose(resolve('cli.js'), [
'ejs',
'-s', 'test/fixtures/cli/src',
'-d', 'test/fixtures/cli/dist',
'-l', 'test/fixtures/cli/layouts',
])
.on('error', err => {
console.error(err);
done(err);
})
.end(function (code) {
assert.dirsEqual('test/fixtures/cli/dist', 'test/fixtures/cli/expected');
assert.equal(code, 0, 'CLI exited with non-zero exit code');
done();
});
});
test('returns errors', function (done) {
var error;
// Run cli.js ejs -s noop:
suppose(resolve('cli.js'), ['ejs', '-s', 'noop'])
.on('error', function (err) {
error = err;
})
.end(function (code) {
assert.notEqual(code, 0, 'expected CLI to return non-zero exit code on error');
// Errors:
assert(error, 'expected CLI to print error message');
done();
});
});
});
suite('html & markdown', function () {
test('empty files', () => assert.fixture('empty-files'));
test('text', () => assert.fixture('text'));
test('subfolders', () => assert.fixture('subfolders'));
});
suite('layouts & front-matter', function () {
test('works', () => assert.fixture('layouts'));
test('_path is set automatically', () => assert.fixture('_path'));
test('_ext is set automatically', () => assert.fixture('_ext'));
});
suite('_defaults file', function () {
test('works', () => assert.fixture('_defaults'));
test('can set default _layout', () => assert.fixture('default-layout'));
test('works in subfolders', () => assert.fixture('_defaults-subfolders'));
});
suite('onessg.config.js', function () {
test('sets template engine options', function (done) {
suppose(resolve('cli.js'), [
'ejs',
'-s', 'test/fixtures/config-options/src',
'-d', 'test/fixtures/config-options/dist',
'-l', 'test/fixtures/config-options/layouts',
'-c', 'test/fixtures/config-options/',
])
.on('error', err => {
console.error(err);
done(err);
})
.end(function (code) {
assert.dirsEqual('test/fixtures/config-options/dist', 'test/fixtures/config-options/expected');
assert.equal(code, 0, 'CLI exited with non-zero exit code');
done();
});
});
});
suite('file types/extentions', function () {
test('json', () => assert.fixture('json'));
test('yml', () => assert.fixture('yml'));
test('markdown', () => assert.fixture('markdown'));
});
suite('errors', function () {
var dirs = {};
// Run before each test:
setup(() => {
dirs.src = 'test/fixtures/cli/src';
dirs.dist = 'test/fixtures/cli/dist';
dirs.layouts = 'test/fixtures/cli/layouts';
});
test('invalid src/', function (done) {
dirs.src = 'noop';
onessg(dirs)
.catch(e => {
done(assert(e));
});
});
test('invalid layouts/', function (done) {
dirs.layouts = 'noop';
onessg(dirs)
.catch(e => {
done(assert(e));
});
});
test('non-existent layout', function (done) {
onessg({
src: 'test/fixtures/non-existent-layout/src',
dist: 'test/fixtures/non-existent-layout/dist',
layouts: 'test/fixtures/non-existent-layout/layouts',
})
.catch(e => {
done(assert(e));
});
});
test('jstransformer not installed', function (done) {
onessg({
src: 'test/fixtures/jstransformer-not-installed/src',
dist: 'test/fixtures/jstransformer-not-installed/dist',
layouts: 'test/fixtures/jstransformer-not-installed/layouts',
})
.catch(e => {
done(assert(e));
});
});
});