forked from paultranvan/cozy-data-system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCakefile
159 lines (140 loc) · 5.65 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
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
{exec} = require 'child_process'
fs = require 'fs'
logger = require('printit')
date: false
prefix: 'cake'
option '-f', '--file [FILE*]' , 'List of test files to run'
option '-d', '--dir [DIR*]' , 'Directory of test files to run'
option '-e' , '--env [ENV]', 'Run tests with NODE_ENV=ENV. Default is test'
option '' , '--use-js', 'If enabled, tests will run with the built files'
options = # defaults, will be overwritten by command line options
file : no
dir : no
# Grab test files of a directory recursively
walk = (dir, excludeElements = []) ->
fileList = []
list = fs.readdirSync dir
if list
for file in list
if file and file not in excludeElements
filename = "#{dir}/#{file}"
stat = fs.statSync filename
if stat and stat.isDirectory()
fileList2 = walk filename, excludeElements
fileList = fileList.concat fileList2
else if filename.substr(-6) is "coffee"
fileList.push filename
return fileList
taskDetails = '(default: ./tests, use -f or -d to specify files and directory)'
task 'tests', "Run tests #{taskDetails}", (opts) ->
logger.options.prefix = 'cake:tests'
files = []
options = opts
if options.dir
dirList = options.dir
files = walk(dir, files) for dir in dirList
if options.file
files = files.concat options.file
unless options.dir or options.file
files = walk "tests"
env = if options['env'] then "NODE_ENV=#{options.env}" else "NODE_ENV=test"
env += " DB_NAME=cozy_test AXON_PORT=9223 "
env += "USE_JS=true" if options['use-js']? and options['use-js']
logger.info "Running tests with #{env}..."
command = "#{env} mocha " + files.join(" ") + " --reporter spec --colors "
command += "--globals clearImmediate,setImmediate "
command += "--compilers coffee:coffee-script/register"
exec command, (err, stdout, stderr) ->
console.log stdout
if err
console.log stderr
logger.error "Running mocha caught exception:\n" + err
process.exit 1
else
logger.info "Tests succeeded!"
process.exit 0
task "coverage", "Generate code coverage of tests", ->
logger.options.prefix = 'cake:coverage'
files = walk "tests"
logger.info "Generating instrumented files..."
bin = "./node_modules/.bin/coffeeCoverage --path abbr"
command = "mkdir instrumented && " + \
"#{bin} server.coffee instrumented/server.js && " + \
"#{bin} server instrumented/server"
exec command, (err, stdout, stderr) ->
if err
logger.error err
cleanCoverage -> process.exit 1
else
logger.info "Instrumented files generated."
env = "COVERAGE=true NODE_ENV=test " + \
"DB_NAME=cozy_test AXON_PORT=9223"
command = "#{env} mocha tests/ " + \
"--compilers coffee:coffee-script/register " + \
"--reporter html-cov > coverage/coverage.html"
logger.info "Generating code coverage..."
exec command, (err, stdout, stderr) ->
if err
logger.error err
cleanCoverage -> process.exit 1
else
cleanCoverage ->
logger.info "Code coverage generation succeeded!"
process.exit 0
# use exec-sync npm module and use "invoke" in other tasks
cleanCoverage = (callback) ->
logger.info "Cleaning..."
command = "rm -rf instrumented"
exec command, (err, stdout, stderr) ->
if err
logger.error err
callback err
else
logger.info "Cleaned!"
callback()
task "clean-coverage", "Clean the files generated for coverage report", ->
cleanCoverage (err) ->
if err
process.exit 1
else
process.exit 0
task "lint", "Run Coffeelint", ->
process.env.TZ = "Europe/Paris"
command = "coffeelint "
command += " -f coffeelint.json -r server/"
logger.options.prefix = 'cake:lint'
logger.info 'Start linting...'
exec command, (err, stdout, stderr) ->
if err
logger.error err
else
console.log stdout
task 'build', 'Build CoffeeScript to Javascript', ->
logger.options.prefix = 'cake:build'
logger.info "Start compilation..."
command = "coffee -cb --output build/server server && " + \
"coffee -cb --output build/ server.coffee"
exec command, (err, stdout, stderr) ->
if err
logger.error "An error has occurred while compiling:\n" + err
process.exit 1
else
logger.info "Compilation succeeded."
process.exit 0
task 'check-build', 'Check if the compiled files are up to date', ->
logger.options.prefix = 'cake:check-build'
jsDate = fs.statSync('build/server.js').mtime
coffeeFiles = walk './server'
coffeeFiles.push './server.coffee'
coffeeDate = null
for file in coffeeFiles
fileDate = fs.statSync(file).mtime
coffeeDate = fileDate if fileDate > coffeeDate or coffeeDate is null
if coffeeDate > jsDate
msg = "Javascript build doesn't seem to be up to date. Are you " + \
"sure you've built the sources? If, not please run 'cake build'."
logger.warn msg
process.exit 1
else
logger.info "Javascript build is up to date."
process.exit 0