Skip to content

Commit

Permalink
Merge branch 'master' into jade
Browse files Browse the repository at this point in the history
  • Loading branch information
m1kc committed Feb 24, 2014
2 parents 3337148 + 4b22540 commit ff10e37
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Used by tests
test/
lib-cov/
report.lcov

# Third-party tools
node_modules/
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ before_script:
- # Create test database
- mysql -e 'create database uonline_test;'
script:
- grunt
- grunt default jscoverage_write_lcov
after_script:
- ./node_modules/nodeunit/bin/nodeunit tests_node/ --reporter lcov | travis_retry ./node_modules/coveralls/bin/coveralls.js
- cat report.lcov | ./node_modules/coveralls/bin/coveralls.js
51 changes: 30 additions & 21 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ module.exports = (grunt) ->
src: './browserified/validation.js'
dest: './browserified/validation.min.js'

checklicense:
all:
mustcontain:
license:
src: [
'*.js'
'lib/*.js'
Expand All @@ -75,9 +75,11 @@ module.exports = (grunt) ->
'tests_node/*.coffee'
'grunt-custom-tasks/*.coffee'
]

checkstrict:
all:
regex: /WARRANTY/
success: '{n} file{s} contain{!s} a license.'
fail: '{filename} does not contain a license.'
fatal: false
strict:
src: [
'*.js'
'lib/*.js'
Expand All @@ -88,6 +90,10 @@ module.exports = (grunt) ->
'tests_node/*.coffee'
'grunt-custom-tasks/*.coffee'
]
regex: /['"]use strict['"]\s*[;\n]/
success: '{n} file{s} {is/are} strict.'
fail: '{filename} is not strict.'
fatal: false

coffee:
all:
Expand All @@ -101,8 +107,8 @@ module.exports = (grunt) ->
bare: true

clean:
coffee: [
'lib/validation.js'
shit: [
'lib-cov/*.coffee'
]

coffeelint:
Expand All @@ -115,34 +121,37 @@ module.exports = (grunt) ->
options: JSON.parse fs.readFileSync('.coffeelintrc', 'utf-8')

coffeeCoverage:
options:
path: 'relative'
all:
src: 'lib/'
expand: true
cwd: 'lib/'
src: ['*.coffee']
dest: 'lib-cov/'
ext: '.js'

jscoverage:
options:
inputDirectory: 'lib'
outputDirectory: 'lib-cov'
exclude: 'locparse.coffee'


# These plugins provide necessary tasks.
require('load-grunt-tasks')(grunt)
grunt.loadTasks './grunt-custom-tasks/'

# Basic tasks.
grunt.registerTask 'check', ['checkstrict', 'checklicense', 'coffeelint', 'jshint:all']
# Custom tasks.
grunt.registerTask 'check', ['mustcontain', 'coffeelint', 'jshint:all']
grunt.registerTask 'build', ['browserify', 'uglify']
grunt.registerTask 'test', [
'jscoverage', 'coffeeCoverage', # order is important
'nodeunit:js', 'nodeunit:coffee', # order is important
'jscoverage_report'
]
if grunt.option('target')?
grunt.config.set 'nodeunit.one', [ 'tests_node/'+grunt.option('target') ]

# Custom one.
grunt.registerTask 'ff', ['check', 'build']

testTask = ['jscoverage', 'clean:shit', 'coffeeCoverage'] # order is important
if grunt.option('single')? # allow to test a single file, see Readme
grunt.config.set 'nodeunit.one', [ 'tests_node/'+grunt.option('single') ]
testTask.push 'nodeunit:one'
else
testTask = testTask.concat ['nodeunit:js', 'nodeunit:coffee'] # order is important
testTask.push 'jscoverage_report'
grunt.registerTask 'test', testTask

# Default task.
grunt.registerTask 'default', ['check', 'build', 'test']
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ If you have Heroku Toolbelt, run `foreman start` to get the server running. If n
Programmers' guidelines
-----------------------

**Hint:** Run `grunt` to check and test your code. Run something like `grunt nodeunit:one --target health-check.coffee` to run a single testsuite.
**Hint:** Run `grunt` to check and test your code. Run something like `grunt test --single health-check.coffee` to run a single testsuite.

* Use tabs, not spaces. Don't mix them and don't use smarttabs.
* Prefer single quotes. Use double quotes when you need to escape `'` itself.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@

'use strict'

require '../lib/strings'


reportFile = (filename, data, log) ->
log "SF:#{filename}"
data.source.forEach (line, num) ->
# increase the line number, as JS arrays are zero-based
num++
if data[num] isnt undefined
log "DA:#{num},#{data[num]}"
log 'end_of_record'


module.exports = (grunt) ->
# Please see the Grunt documentation for more information regarding task
# creation: http://gruntjs.com/creating-tasks
grunt.registerMultiTask 'checkstrict', 'Check if every file is in strict mode.', ->
done = @async()
fs = require 'fs'
async = require 'async'
async.map @filesSrc, ((item, callback) ->
fs.readFile item, (error, data) ->
if error?
callback error, null
else
callback null, [item, ( /['"]use strict['"]\s*[;\n]/ ).test(data.toString())]
), (error, results) ->
if error?
grunt.log.error error
done(false)
count = results.length
results = results.filter (item) -> item[1] is false
if results.length is 0
grunt.log.ok "#{count} files are strict."
else
for i in results
grunt.log.warn "#{i[0]} is not strict."
done()
grunt.registerTask 'jscoverage_write_lcov', 'Write jscoverage report in lcov format to file.', ->
cov = (global or window)._$jscoverage or {}
output = ''
log = (x) ->
output = "#{output}#{x}\n"
Object.keys(cov).forEach (filename) ->
data = cov[filename]
reportFile(filename, data, log)
try
require('fs').writeFileSync('./report.lcov', output)
grunt.log.ok 'report.lcov'
catch error
grunt.fail.warn(error)
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,41 @@
module.exports = (grunt) ->
# Please see the Grunt documentation for more information regarding task
# creation: http://gruntjs.com/creating-tasks
grunt.registerMultiTask 'checklicense', 'Check if every file contains a license.', ->
grunt.registerMultiTask 'mustcontain', 'Check if every file contains specified things.', ->
done = @async()
fs = require 'fs'
async = require 'async'
regex = @data.regex
success = @data.success
fail = @data.fail
fatal = @data.fatal

async.map @filesSrc, ((item, callback) ->
fs.readFile item, (error, data) ->
if error?
callback error, null
else
callback null, [item, ( /WARRANTY/ ).test(data.toString())]
callback null, [item, regex.test(data.toString())]
), (error, results) ->
if error?
grunt.log.error error
done(false)
count = results.length
results = results.filter (item) -> item[1] is false
if results.length is 0
grunt.log.ok "#{count} files contain a license."
msg = success.replace /\{n\}/g, "#{count}"
if count == 1 or (count > 20 and count % 10 == 1)
msg = msg.replace /\{s\}/g, ''
msg = msg.replace /\{\!s\}/g, 's'
msg = msg.replace /\{is\/are\}/g, 'is'
else
msg = msg.replace /\{s\}/g, 's'
msg = msg.replace /\{\!s\}/g, ''
msg = msg.replace /\{is\/are\}/g, 'are'
grunt.log.ok msg
done()
else
for i in results
grunt.log.warn "#{i[0]} does not contain a license."
done()
msg = fail.replace /\{filename\}/g, "#{i[0]}"
grunt.log.warn msg
done(not fatal)
23 changes: 23 additions & 0 deletions lib.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


'use strict'

list = require('fs').readdirSync('./lib')
for i in list
for targetExt in ['.js', '.coffee']
ext = i.substring(i.length-(targetExt.length))
if ext is targetExt
name = i.substring(0, i.length-(targetExt.length))
exports[name] = require './lib/'+i
4 changes: 2 additions & 2 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ var list = require('fs').readdirSync('./lib');
for (var i in list)
{
i = list[i];
var name = i.substring(0, i.length-3); // ".js".length === 3
var ext = i.substring(i.length-3);
var ext = i.substring(i.length-3); // ".js".length === 3
if (ext === '.js')
{
var name = i.substring(0, i.length-3);
exports[name] = require('./lib/'+i);
}
}
25 changes: 25 additions & 0 deletions lib/strings.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


'use strict'

String::startsWith = (x) ->
if x.length > this.length
return false
return this.substring(0,x.length) == x

String::endsWith = (x) ->
if x.length > this.length
return false
return this.substring(this.length-x.length, this.length) == x
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"jshint": "~2.4.0",
"jscoverage": "~0.3.8",
"nodemon": "~1.0.5",
"coveralls": "~2.7.0",
"coveralls": "^2.8.0",
"david": "~2.4.0",
"rmrf": "~1.0.2",
"ncp": "~0.5.0"
Expand Down
35 changes: 35 additions & 0 deletions tests_node/strings.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


'use strict'

strings = require '../lib-cov/strings'

exports.startsWith = (test) ->
test.strictEqual true, 'abcd'.startsWith('a')
test.strictEqual true, 'abcd'.startsWith('abc')
test.strictEqual true, 'abcd'.startsWith('abcd')
test.strictEqual false, 'abcd'.startsWith('abcdd')
test.strictEqual false, 'abcd'.startsWith('bc')
test.strictEqual true, 'abcd'.startsWith('')
test.done()

exports.endsWith = (test) ->
test.strictEqual true, 'abcd'.endsWith('d')
test.strictEqual true, 'abcd'.endsWith('bcd')
test.strictEqual true, 'abcd'.endsWith('abcd')
test.strictEqual false, 'abcd'.endsWith('abcdd')
test.strictEqual false, 'abcd'.endsWith('bc')
test.strictEqual true, 'abcd'.endsWith('')
test.done()

0 comments on commit ff10e37

Please sign in to comment.