Skip to content

Commit

Permalink
Merge pull request #7 from carrot/analytics
Browse files Browse the repository at this point in the history
Test Helper Changes
  • Loading branch information
Jeff Escalante committed Jan 12, 2016
2 parents 6fbf08f + 784b235 commit 1a56629
Show file tree
Hide file tree
Showing 4 changed files with 392 additions and 149 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
node_modules
test/fixtures/helpers/public
154 changes: 112 additions & 42 deletions lib/test_helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,132 @@ class Helpers
_path = (f) ->
if opts.base? then path.join(opts.base, f) else path.normalize(f)

_exists = (file, opts = { async: false }) ->
if opts.async
nodefn.call(fs.stat, file).then(-> true).catch(-> false)
else
try fs.statSync(file)?
catch then false

_asyncReadFile = (file) ->
nodefn.call(fs.readFile, file, 'utf8')

@file =
exists: (f) ->
fs.existsSync(_path(f))
doesnt_exist: (f) ->
!fs.existsSync(_path(f))
has_content: (f) ->
fs.readFileSync(_path(f), 'utf8').length > 0
is_empty: (f) ->
fs.readFileSync(_path(f), 'utf8').length < 1
contains: (f, content) ->
fs.readFileSync(_path(f), 'utf8').indexOf(content) > -1
contains_match: (f, regex) ->
!!fs.readFileSync(_path(f), 'utf8').match(regex)
matches_file: (f, expected) ->
exists: (f, opts) ->
_exists(_path(f), opts)

doesnt_exist: (f, opts = { async: false }) ->
if opts.async
_exists(_path(f), opts).then (exists) -> !exists
else
!_exists(_path(f))

has_content: (f, opts = { async: false }) ->
if opts.async
_asyncReadFile(_path(f)).then (c) -> c.length > 0
else
fs.readFileSync(_path(f), 'utf8').length > 0

is_empty: (f, opts = { async: false }) ->
if opts.async
_asyncReadFile(_path(f)).then (c) -> c.length < 1
else
fs.readFileSync(_path(f), 'utf8').length < 1

contains: (f, content, opts = { async: false }) ->
if opts.async
_asyncReadFile(_path(f)).then (c) -> c.indexOf(content) > -1
else
fs.readFileSync(_path(f), 'utf8').indexOf(content) > -1

contains_match: (f, regex, opts = { async: false }) ->
if opts.async
_asyncReadFile(_path(f)).then (c) -> !!c.match(regex)
else
!!fs.readFileSync(_path(f), 'utf8').match(regex)

matches_file: (f, expected, opts = { async: false }) ->
f = _path(f)
expected = _path(expected)
String(fs.readFileSync(f)) == String(fs.readFileSync(expected))
if opts.async
W.all([_asyncReadFile(f), _asyncReadFile(expected)])
.spread (f, expected) -> f == expected
else
String(fs.readFileSync(f)) == String(fs.readFileSync(expected))

@directory =
is_directory: (dir) ->
fs.statSync(_path(dir)).isDirectory()
exists: (dir) ->
is_directory: (dir, opts = { async: false }) ->
if opts.async
nodefn.call(fs.stat, _path(dir)).then (s) -> s.isDirectory()
else
fs.statSync(_path(dir)).isDirectory()

exists: (dir, opts = { async: false }) ->
dir = _path(dir)
try stat = fs.statSync(dir)
catch err then return false
stat.isDirectory() and fs.existsSync(dir)
doesnt_exist: (dir) ->
!fs.existsSync(_path(dir))
has_contents: (dir) ->
fs.readdirSync(_path(dir)).length > 0
is_empty: (dir) ->
fs.readdirSync(_path(dir)).length < 1
contains_file: (dir, file) ->
_.contains(fs.readdirSync(_path(dir)), file)
matches_dir: (dir, expected) ->
if opts.async
nodefn.call(fs.stat, dir)
.then (s) -> s.isDirectory()
.catch -> false
else
try stat = fs.statSync(dir)
catch then return false
stat.isDirectory()

doesnt_exist: (dir, opts = { async: false }) ->
if opts.async
_exists(_path(dir), opts).then (exists) -> !exists
else
!_exists(_path(dir))

has_contents: (dir, opts = { async: false }) ->
if opts.async
nodefn.call(fs.readdir, _path(dir)).then (d) -> d.length > 0
else
fs.readdirSync(_path(dir)).length > 0

is_empty: (dir, opts = { async: false }) ->
if opts.async
nodefn.call(fs.readdir, _path(dir)).then (d) -> d.length < 1
else
fs.readdirSync(_path(dir)).length < 1

contains_file: (dir, file, opts = { async: false }) ->
if opts.async
nodefn.call(fs.readdir, _path(dir)).then (d) -> _.contains(d, file)
else
_.contains(fs.readdirSync(_path(dir)), file)

matches_dir: (dir, expected, opts = { async: false }) ->
dir = _path(dir)
expected = _path(expected)
String(fs.readdirSync(dir)) == String(fs.readdirSync(expected))
if opts.async
W.all([nodefn.call(fs.readdir, dir), nodefn.call(fs.readdir, expected)])
.spread (dir, expected) -> String(dir) == String(expected)
else
String(fs.readdirSync(dir)) == String(fs.readdirSync(expected))

@project =
compile: (Roots, p) ->
Roots.analytics(disable: true)
proj = new Roots(_path(p))
proj.on('error', ->)
proj.compile()
remove_folders: (matcher) ->
rimraf.sync(dir) for dir in glob.sync(_path(matcher))
install_dependencies: (base, cb) ->
tasks = []
proj.compile().then -> Roots.analytics(enable: true)

for d in glob.sync("#{_path(base)}/package.json")
p = path.dirname(d)
if fs.existsSync(path.join(p, 'node_modules')) then continue
tasks.push nodefn.call(run, "npm i", { cwd: p })
remove_folders: (matcher, opts = { async: false }) ->
if opts.async
W.map nodefn.call(glob, _path(matcher)), (dir) ->
nodefn.call(rimraf, dir)
else
rimraf.sync(dir) for dir in glob.sync(_path(matcher))

if tasks.length then console.log 'installing test dependencies...'.grey

W.all(tasks).then(-> console.log(''); cb())
install_dependencies: (base, cb) ->
dirs = nodefn.call(glob, "#{_path(base)}/package.json")
W.map dirs, (d) -> path.dirname(d)
.then (dirs) ->
W.filter dirs, (d) -> !_exists(path.join(d, 'node_modules'))
.tap (tasks) ->
if tasks.length then console.log 'installing test dependencies...'.grey
W.map tasks, (d) -> nodefn.call(run, "npm i", { cwd: d })
.then cb

module.exports = Helpers
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
"main": "lib",
"dependencies": {
"colors": "1.x",
"glob": "5.x",
"glob": "6.x",
"lodash": "3.x",
"minimatch": "2.x",
"minimatch": "3.x",
"mkdirp": "0.5.x",
"rimraf": "2.x",
"vinyl": "0.5.x",
"vinyl": "1.1.x",
"when": "3.x"
},
"devDependencies": {
"coffee-script": "1.9.x",
"coffee-script": "1.10.x",
"coveralls": "2.x",
"istanbul": "0.3.x",
"istanbul": "0.4.x",
"mocha": "2.x",
"mocha-lcov-reporter": "0.0.2",
"mocha-lcov-reporter": "1.0.0",
"roots": "3.x",
"should": "7.x"
"should": "8.x"
},
"scripts": {
"test": "mocha",
Expand Down
Loading

0 comments on commit 1a56629

Please sign in to comment.