Skip to content

Commit

Permalink
Adds async tree()
Browse files Browse the repository at this point in the history
  • Loading branch information
mynyml committed Mar 6, 2011
1 parent 935e7d9 commit 85182d9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 63 deletions.
28 changes: 24 additions & 4 deletions src/pathname.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class Pathname
@

# TODO mode defaults to 0666
# FIXME so many ifelses!
open: (flags, mode, cb) ->
[cb, flags, mode] = extractCallback(flags, mode, cb)

Expand Down Expand Up @@ -256,14 +255,35 @@ class Pathname
else
@open('w+', mode); @close()

# TODO async version
tree: (depth, cb) ->
[cb, depth] = extractCallback(depth, cb)

done = no
paths = [@]

# FIXME im teh uglie
if cb?
else
paths = [@]
if not @isSymbolicLink() and @isDirectory() and (!depth? or depth > 0)
core.fs.readdir @path, (err, files) =>
return if done
if err?
done = yes
cb(err, null)
else
count = files.length
files.forEach (fname) =>
@join(fname).tree (depth and (depth - 1)), (err, _paths) ->
return if done #avoid calling cb more than once
if err?
done = yes
cb(err, null)
else
paths.push(_paths)
cb(null, flatten(paths)) if --count is 0
else
cb(null, paths)

else
if not @isSymbolicLink() and @isDirectory() and (!depth? or depth > 0)
core.fs.readdirSync(@path).forEach (fname) => paths.push(@join(fname).tree(depth and (depth - 1)))

Expand Down
108 changes: 49 additions & 59 deletions test/pathname_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -607,52 +607,49 @@ with_tmpdir (path) ->
root.join('bar' ).unlink()

with_tmpdir (path) ->
try
root = new Pathname(path)
root.join('bar' ).touch()
root.join('boo' ).mkdir()
root.join('boo/moo' ).mkdir()
root.join('boo/moo/zoo').touch()

assert.ok root.tree().every (path) -> path.constructor == Pathname
root = new Pathname(path)
root.join('bar' ).touch()
root.join('boo' ).mkdir()
root.join('boo/moo' ).mkdir()
root.join('boo/moo/zoo').touch()

tree = root.tree(0)
assert.equal tree.length, 1
assert.include tree, root

assert.equal root.tree(-1).length, tree.length

tree = root.tree(1)
assert.equal tree.length, 3
assert.include tree, root
assert.include tree, root.join('bar')
assert.include tree, root.join('boo')

tree = root.tree(2)
assert.equal tree.length, 4
assert.include tree, root
assert.include tree, root.join('bar')
assert.include tree, root.join('boo')
assert.include tree, root.join('boo/moo')
root.tree (err, files) ->
assert.ifError(err)
assert.ok files.every (path) -> path.constructor == Pathname

tree = root.tree(3)
assert.equal tree.length, 5
assert.include tree, root
assert.include tree, root.join('bar')
assert.include tree, root.join('boo')
assert.include tree, root.join('boo/moo')
assert.include tree, root.join('boo/moo/zoo')
root.tree 0, (err, files) ->
assert.ifError(err)
assert.equal files.length, 1
assert.include files, root

assert.equal root.tree(undefined).length, tree.length
assert.equal root.tree(null ).length, tree.length
catch e
up(e)
finally
if root?
root.join('boo/moo/zoo').unlink()
root.join('boo/moo' ).rmdir()
root.join('boo' ).rmdir()
root.join('bar' ).unlink()
root.tree -1, (err, files2) ->
assert.ifError(err)
assert.equal files2.length, files.length

root.tree 1, (err, files) ->
assert.equal files.length, 3
assert.include files, root
assert.include files, root.join('bar')
assert.include files, root.join('boo')

root.tree 2, (err, files) ->
assert.equal files.length, 4
assert.include files, root
assert.include files, root.join('bar')
assert.include files, root.join('boo')
assert.include files, root.join('boo/moo')

root.tree 3, (err, files) ->
assert.equal files.length, 5
assert.include files, root
assert.include files, root.join('bar')
assert.include files, root.join('boo')
assert.include files, root.join('boo/moo')
assert.include files, root.join('boo/moo/zoo')

root.tree undefined, (err, files2) -> assert.equal files2.length, files.length
root.tree null, (err, files2) -> assert.equal files2.length, files.length
root.tree (err, files2) -> assert.equal files2.length, files.length


## deletes directory tree
Expand Down Expand Up @@ -700,22 +697,15 @@ with_tmpdir (path) ->
root.rmR() if root?.exists()

with_tmpdir (path) ->
try
root = new Pathname(path)
root.join('bar' ).touch()
root.join('boo' ).mkdir()
root.join('boo/moo').touch()

root.readdir (err, files) ->
assert.equal files.length, 2
assert.include files, 'bar'
assert.include files, 'boo'
catch e
up(e)
finally
root.rmR() if root?.exists()


root = new Pathname(path)
root.join('bar' ).touch()
root.join('boo' ).mkdir()
root.join('boo/moo').touch()

root.readdir (err, files) ->
assert.equal files.length, 2
assert.include files, 'bar'
assert.include files, 'boo'

###

0 comments on commit 85182d9

Please sign in to comment.