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
|
var os = require('os');
var path = require('path');
var Q = require('q');
var _ = require('lodash');
var fsUtil = require('../lib/utils/fs');
var Book = require('../').Book;
var LOG_LEVELS = require('../').LOG_LEVELS;
require('./assertions');
var BOOKS = {};
var TMPDIR = os.tmpdir();
// Generate and return a book
function generateBook(bookId, test, opts) {
opts = _.defaults(opts || {}, {
prepare: function() {}
});
return parseBook(bookId, test, opts)
.then(function(book) {
return Q(opts.prepare(book))
.then(function() {
return book.generate(test);
})
.thenResolve(book);
});
}
// Generate and return a book
function parseBook(bookId, test, opts) {
opts = _.defaults(opts || {}, {
testId: ''
});
test = test || 'website';
var testId = [test, opts.testId].join('-');
BOOKS[bookId] = BOOKS[bookId] || {};
if (BOOKS[bookId][testId]) return Q(BOOKS[bookId][testId]);
BOOKS[bookId][testId] = new Book(path.resolve(__dirname, 'books', bookId), {
logLevel: LOG_LEVELS.DISABLED,
config: {
output: path.resolve(TMPDIR, bookId+'-'+testId)
}
});
return BOOKS[bookId][testId].parse()
.then(function() {
return BOOKS[bookId][testId];
});
}
global.books = {
parse: parseBook,
generate: generateBook
};
// Cleanup all tests
after(function() {
return _.chain(BOOKS)
.map(function(types) {
return _.values(types);
})
.flatten()
.reduce(function(prev, book) {
return prev.then(function() {
return fsUtil.remove(book.options.output);
});
}, Q())
.value();
});
|