-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (125 loc) · 4.24 KB
/
index.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
/*globals describe, before, it*/
'use strict'
var parseFile = require('./parseFile'),
fs = require('fs'),
path = require('path'),
MongoClient = require('mongodb').MongoClient,
baseContext = require('./baseContext'),
parseUrl = require('url').parse,
crypto = require('crypto')
/**
* Execute the tests described by *.md files in a given folder
* @param {string} file The folder path
* @param {Object} options an object with optional keys:
* @param {string} options.mongoUri
* @param {string} options.baseUrl
* @param {string} [options.name='api']
* @param {boolean} [options.recursive=false]
* @param {boolean} [options.strict=true]
* @param {Object} [options.context]
* @param {string[]} [options.ignoredFindKeys=['_id', '__v']]
* @param {Buffer} [options.ca]
* @param {function(string):boolean} [options.filterFile]
* @param {function(Test)} [options.onTest]
* @param {function(Test,Insertion|Clear|Declaration)} [options.onSetup]
* @param {function(Test,Case)} [options.onCase]
* @param {function(Case,*)} [options.onPost]
* @param {function(Case,*)} [options.onOut]
* @param {function(Case,Find)} [options.onFind]
* @param {Function} [options.describe]
* @param {Function} [options.before]
* @param {Function} [options.it]
* @param {Object<String, Object>} [options.defaultDocuments]
*/
module.exports = function (folder, options) {
options.mongoUri = validateMongoUri(options.mongoUri)
// Save defaultDocuments to baseContext to make it available for the script
baseContext.defaultDocuments = options.defaultDocuments || Object.create(null)
// Prepare options
options.name = options.name || 'api'
options.describe = options.describe || describe
options.before = options.before || before
options.it = options.it || it
options.context = options.context || {}
options.context.__proto__ = baseContext
options.recursive = options.recursive || false
options.strict = options.strict === undefined ? true : options.strict
options.filterFile = options.filterFile || function () {
return true
}
options.ignoredFindKeys = options.ignoredFindKeys || ['_id', '__v']
options.describe(options.name, function () {
options.before(function (done) {
// Connect to mongo
MongoClient.connect(options.mongoUri, function (err, db) {
if (err) {
return done(err)
}
options.db = db
done()
})
})
// Load files
walk(options.recursive, folder, function (file) {
if (file.substr(-3) === '.md' && options.filterFile(file)) {
var test = parseFile(file)
if (options.onTest) {
options.onTest(test)
}
test.execute(options)
}
})
})
}
/**
* Make sure the mongo uri has 'localhost' as hostname and 'test' in the DB name
* @param {string} mongoUri
*/
function validateMongoUri(mongoUri) {
var tag, rightTag, sha
if (mongoUri.match(/^![a-z0-9]{10}!/)) {
// Extract tag
tag = mongoUri.substr(1, 10)
mongoUri = mongoUri.substr(12)
}
if (mongoUri.indexOf('test') === -1 || parseUrl(mongoUri).hostname !== 'localhost') {
// Propably an error, we force the mongoUri to be localhost and the DB/username to be test
sha = crypto.createHash('sha1')
sha.end(new Buffer(mongoUri))
rightTag = sha.read().toString('hex').substr(0, 10)
}
// Compare tags
if (tag !== rightTag) {
console.log('++++++++++')
if (rightTag) {
console.log('The mongoUri "' + mongoUri.substr(0, 17) + '..." seems not to be test-safe')
console.log('I recommend you to connect to a localhost instance and a database with "test" in the name')
console.log('Remember that the database is DROPPED before every test!')
console.log('If you are really sure, please prepend "!' + rightTag + '!" to your mongoUri')
console.log('Like this: "!' + rightTag + '!' + mongoUri.substr(0, 17) + '..."')
} else {
console.log('Please remove the !tag! from the mongoUri to run the test')
}
console.log('++++++++++')
throw new Error('Invalid protection tag')
}
return mongoUri
}
/**
* Call a function for each file in a given directory
* @param {boolean} recursive
* @param {string} dir
* @param {Function} fn
*/
function walk(recursive, dir, fn) {
fs.readdirSync(dir).forEach(function (item) {
item = path.join(dir, item)
if (fs.statSync(item).isDirectory()) {
if (recursive) {
walk(recursive, item, fn)
}
} else {
fn(item)
}
})
}