Skip to content

Commit

Permalink
readdir() and readlink() return Pathname objs
Browse files Browse the repository at this point in the history
  • Loading branch information
mynyml committed Mar 7, 2011
1 parent a3606f9 commit 313b2db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
14 changes: 9 additions & 5 deletions src/pathname.coffee
Expand Up @@ -191,15 +191,19 @@ class Pathname

readlink: (cb) ->
if cb?
core.fs.readlink(@path, cb)
core.fs.readlink @path, (err, path) => cb(err, new @constructor(path))
else
core.fs.readlinkSync(@path)
new @constructor(core.fs.readlinkSync(@path))

readdir: (cb) ->
if cb?
core.fs.readdir(@path, cb)
core.fs.readdir @path, (err, paths) =>
if err?
cb(err, null)
else
cb(null, paths.map (path) => new @constructor(path))
else
core.fs.readdirSync(@path)
core.fs.readdirSync(@path).map (path) => new @constructor(path)

watchFile: (args...) ->
core.fs.watchFile(@path, args...)
Expand Down Expand Up @@ -322,7 +326,7 @@ class Pathname
create()

traverse: (cb) ->
cb(@components().map((path) -> new Pathname(path)).reduce((curr, next) -> cb(curr); curr.join(next)))
cb(@components().map((path) => new @constructor(path)).reduce((curr, next) -> cb(curr); curr.join(next)))

components: ->
elements = @toString().split('/').filter (e) -> e.length isnt 0
Expand Down
24 changes: 14 additions & 10 deletions test/pathname_test.coffee
Expand Up @@ -15,8 +15,8 @@ assert = require('assert')
temp = require('temp')
Pathname = require('../src/index')

process._events = {}
process.setMaxListeners(50)
process._events ?= {}
process.setMaxListeners(60)

# --------------------------------------------------
# Helpers
Expand Down Expand Up @@ -443,14 +443,16 @@ with_tmpdir (path) ->
path1 = new Pathname(path).join('foo')
path2 = new Pathname(path).join('bar').touch().symlink(path1)

assert.equal path1.readlink(), path2.toString()
assert.equal path1.readlink().constructor, Pathname
assert.equal path1.readlink().toString(), path2.toString()

with_tmpdir (path) ->
path1 = new Pathname(path).join('foo')
path2 = new Pathname(path).join('bar').touch().symlink(path1)

path1.readlink (err, resolvedPath) ->
assert.equal resolvedPath, path2.toString()
assert.equal resolvedPath.constructor, Pathname
assert.equal resolvedPath.toString(), path2.toString()


## test watches and unwatches a file
Expand Down Expand Up @@ -566,7 +568,7 @@ with_tmpdir (path) ->
root.join('boo/moo' ).mkdir()
root.join('boo/moo/zoo').touch()

assert.ok root.tree().every (path) -> path.constructor == Pathname
assert.ok root.tree().every (path) -> path.constructor is Pathname

tree = root.tree(0)
assert.equal tree.length, 1
Expand Down Expand Up @@ -615,7 +617,7 @@ with_tmpdir (path) ->

root.tree (err, files) ->
assert.ifError(err)
assert.ok files.every (path) -> path.constructor == Pathname
assert.ok files.every (path) -> path.constructor is Pathname

root.tree 0, (err, files) ->
assert.ifError(err)
Expand Down Expand Up @@ -704,9 +706,10 @@ with_tmpdir (path) ->
root.join('boo' ).mkdir()
root.join('boo/moo').touch()

assert.ok root.readdir().every (path) -> path.constructor is Pathname
assert.equal root.readdir().length, 2
assert.include root.readdir(), 'bar'
assert.include root.readdir(), 'boo'
assert.include root.readdir(), root.join('bar').basename()
assert.include root.readdir(), root.join('boo').basename()
catch e
up(e)
finally
Expand All @@ -719,9 +722,10 @@ with_tmpdir (path) ->
root.join('boo/moo').touch()

root.readdir (err, files) ->
assert.ok files.every (path) -> path.constructor is Pathname
assert.equal files.length, 2
assert.include files, 'bar'
assert.include files, 'boo'
assert.include files, root.join('bar').basename()
assert.include files, root.join('boo').basename()


## test creates many levels of directories
Expand Down

0 comments on commit 313b2db

Please sign in to comment.