-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
testers.coffee
executable file
·289 lines (234 loc) · 6.96 KB
/
testers.coffee
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# Requires
pathUtil = require('path')
safefs = require('safefs')
balUtil = require('bal-util')
extendr = require('extendr')
joe = require('joe')
{expect} = require('chai')
CSON = require('cson')
_ = require('lodash')
DocPad = require('./docpad')
# Prepare
pluginPort = 2000+String((new Date()).getTime()).substr(-6,4)
testers = {
CSON,
DocPad
}
# Plugin Tester
testers.PluginTester =
class PluginTester
# Plugin Config
config:
testerName: null
pluginName: null
pluginPath: null
testPath: null
outExpectedPath: null
removeWhitespace: false
contentRemoveRegex: null
autoExit: 'safe'
# DocPad Config
docpadConfig:
global: true
port: null
logLevel: (if ('-d' in process.argv) then 7 else 5)
rootPath: null
outPath: null
srcPath: null
pluginPaths: null
enableUnlistedPlugins: true
enabledPlugins: null
skipUnsupportedPlugins: false
catchExceptions: false
environment: null
# DocPad Instance
docpad: null
# Constructor
constructor: (config={},docpadConfig={},next) ->
# Apply Configuration
tester = @
@config = extendr.deepExtendPlainObjects({}, PluginTester::config, @config, config)
@docpadConfig = extendr.deepExtendPlainObjects({}, PluginTester::docpadConfig, @docpadConfig, docpadConfig)
@docpadConfig.port ?= ++pluginPort
@config.testerName ?= @config.pluginName
# Extend Configuration
@config.testPath or= pathUtil.join(@config.pluginPath, 'test')
@config.outExpectedPath or= pathUtil.join(@config.testPath, 'out-expected')
# Extend DocPad Configuration
@docpadConfig.rootPath or= @config.testPath
@docpadConfig.outPath or= pathUtil.join(@docpadConfig.rootPath, 'out')
@docpadConfig.srcPath or= pathUtil.join(@docpadConfig.rootPath, 'src')
@docpadConfig.pluginPaths ?= [@config.pluginPath]
defaultEnabledPlugins = {}
defaultEnabledPlugins[@config.pluginName] = true
@docpadConfig.enabledPlugins or= defaultEnabledPlugins
# Test API
joe.describe @config.testerName, (suite,task) ->
tester.describe = tester.suite = suite
tester.it = tester.test = task
tester.done = tester.exit = (next) ->
tester.docpad?.destroy(next)
next?(null, tester)
# Chain
@
# Get Tester Configuration
getConfig: ->
return @config
# Get Plugin Instance
getPlugin: ->
return @docpad.getPlugin(@getConfig().pluginName)
# Create DocPad Instance
testCreate: =>
# Prepare
tester = @
docpadConfig = @docpadConfig
# Create Instance
@test "create", (done) ->
DocPad.createInstance docpadConfig, (err,docpad) ->
return done(err) if err
tester.docpad = docpad
# init docpad in case the plugin is starting from scratch
tester.docpad.action 'init', (err) ->
# ignore error as it is probably just related to there already being something
# clean up the docpad out directory
tester.docpad.action 'clean', (err) ->
return done(err) if err
# install anything that needs to be installed
tester.docpad.action('install', done)
# Chain
@
# Test Loaded
testLoad: =>
# Prepare
tester = @
# Test
@test "load plugin #{tester.config.pluginName}", (done) ->
tester.docpad.loadedPlugin tester.config.pluginName, (err,loaded) ->
return done(err) if err
expect(loaded).to.be.ok
return done()
# Chain
@
# Perform Server
testServer: (next) =>
# Prepare
tester = @
# Handle
@test "server", (done) ->
tester.docpad.action 'server', (err) ->
return done(err)
# Chain
@
# Test Generate
testGenerate: =>
# Prepare
tester = @
# Test
@test "generate", (done) ->
tester.docpad.action 'generate', (err) ->
return done(err)
# Chain
@
# Test everything
testEverything: =>
# Prepare
tester = @
# Tests
@testCreate()
@testLoad()
@testGenerate()
@testServer()
@testCustom?()
# Finish
@finish()
# Chain
@
# Finish
finish: ->
# Prepare
tester = @
# Finish
if tester.config.autoExit
@test 'finish up', (next) ->
tester.exit(next)
# Chain
@
# Server Tester
testers.ServerTester =
class ServerTester extends PluginTester
# Renderer Tester
testers.RendererTester =
class RendererTester extends PluginTester
# Test Generation
testGenerate: ->
# Prepare
tester = @
# Test
@suite "generate", (suite,test) ->
test 'action', (done) ->
tester.docpad.action 'generate', (err) ->
return done(err)
suite 'results', (suite,test,done) ->
# Get actual results
balUtil.scanlist tester.docpadConfig.outPath, (err,outResults) ->
return done(err) if err
# Get expected results
balUtil.scanlist tester.config.outExpectedPath, (err,outExpectedResults) ->
return done(err) if err
# Remove empty lines
if tester.config.removeWhitespace is true
replaceLinesRegex = /(\\r|\\n|\\t|\s)+/g
outResults = JSON.parse JSON.stringify(outResults).replace(replaceLinesRegex, '')
outExpectedResults = JSON.parse JSON.stringify(outExpectedResults).replace(replaceLinesRegex, '')
# Content regex
if tester.config.contentRemoveRegex
outResults = JSON.parse JSON.stringify(outResults).replace(tester.config.contentRemoveRegex, '')
outExpectedResults = JSON.parse JSON.stringify(outExpectedResults).replace(tester.config.contentRemoveRegex, '')
# Prepare
outResultsKeys = Object.keys(outResults)
outExpectedResultsKeys = Object.keys(outExpectedResults)
# Check we have the same files
test 'same files', ->
outDifferenceKeys = _.difference(outResultsKeys, outExpectedResultsKeys)
expect(outDifferenceKeys).to.be.empty
# Check the contents of those files match
outResultsKeys.forEach (key) ->
test "same file content for: #{key}", ->
actual = outResults[key]
expected = outExpectedResults[key]
expect(actual).to.eql(expected)
# Forward
done()
# Chain
@
# Test a plugin
# test({pluginPath: String})
testers.test =
test = (testerConfig, docpadConfig) ->
# Configure
testerConfig.testerClass ?= PluginTester
testerConfig.pluginPath = pathUtil.resolve(testerConfig.pluginPath)
testerConfig.pluginName ?= pathUtil.basename(testerConfig.pluginPath).replace('docpad-plugin-','')
testerConfig.testerPath ?= pathUtil.join('out', "#{testerConfig.pluginName}.tester.js")
testerConfig.testerPath = pathUtil.resolve(testerConfig.pluginPath, testerConfig.testerPath) if testerConfig.testerPath
# Create tester
complete = ->
# Accept string inputs for testerClass
testerConfig.testerClass = testers[testerConfig.testerClass] if typeof testerConfig.testerClass is 'string'
# Create our tester
new testerConfig.testerClass testerConfig, docpadConfig, (err,testerInstance) ->
throw err if err
# Run the tests
testerInstance.testEverything()
# Load the tester file
if testerConfig.testerPath
safefs.exists testerConfig.testerPath, (exists) ->
testerConfig.testerClass = require(testerConfig.testerPath)(testers) if exists
complete()
# User the default tester
else
complete()
# Chain
return testers
# Export Testers
module.exports = testers