-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
111 lines (84 loc) · 2.98 KB
/
Cakefile
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
fs = require 'fs'
path = require 'path'
{exec, spawn} = require 'child_process'
option "-r", "--reporter [REPORTER]", "reporter type for tests"
#############################
compiled = no
task "all", "Compile source files and then run test on compiled library", ->
# Compile source
process.stdout.write "\n\x1b[36m Compiling sources...\x1b[0m\n"
invoke "compile"
# Run tests
test = ->
if compiled
clearInterval wait
process.stdout.write "\x1b[36m Running test suite...\x1b[0m"
invoke "test"
# Wait for compilation
wait = setInterval test, 100
task "compile", "Compile files from '/src' directory into '/lib' directory", ->
__start = Date.now()
exec "rm -rf ./lib && mkdir lib", (error, stdout, stderr) ->
if error
process.stderr.write "\n\x1b[31m Compilation failed\n Error:\x1b[0m #{stderr}\n"
process.exit 1
exec "./node_modules/.bin/coffee -b -c -o lib/ src/", (error, stdout, stderr) ->
if error
process.stderr.write "\n\x1b[31m Compilation failed\n Error:\x1b[0m #{stderr}\n"
process.exit 1
compiled = yes
process.stdout.write "\n\x1b[32m Compilation successful\x1b[90m (#{Date.now() - __start} ms)\x1b[0m\n\n"
task "test", "Run `mocha` test suite", (options) ->
do process.stdin.resume
files = []
results = []
# Helpers
forEachSeries = (arr, iterator, callback) ->
callback = callback or ->
return do callback unless arr.length
completed = 0
iterate = ->
iterator arr[completed], (err) ->
if err
callback err
callback = ->
else
completed += 1
if completed >= arr.length
callback null
else
do iterate
do iterate
resolvePath = (path) ->
return if '/' is path[0] then path else __dirname + '/test/' + path
traverse = (file, next) ->
file = resolvePath path.normalize file
fs.stat file, (error, stat) ->
throw new Error error if error
if do stat.isDirectory
fs.readdir file, (err, files) ->
forEachSeries files.map( (f) -> file + '/' + f ), traverse, next
else
files.push file
do next
test = (file, next) ->
if ".coffee" isnt path.extname file
results.push 0
do next
else
process.stdout.write "\n\x1b[90m File: \x1b[0m #{path.relative __dirname, file}\n"
parameters = [file]
if options.reporter
parameters = parameters.concat "--reporter", options.reporter
mocha = spawn "./node_modules/.bin/mocha", parameters
mocha.stdout.on 'data', (data) -> process.stdout.write data.toString()
mocha.stderr.on 'data', (data) -> process.stderr.write data.toString()
mocha.on "exit", (code) ->
results.push code
do next
#############################
# Read test
fs.readdir "test", (e, f) ->
forEachSeries f, traverse, ->
forEachSeries files, test, ->
process.exit if results.some((result) -> result isnt 0) then 1 else 0