Skip to content

Commit

Permalink
Added tests for move.
Browse files Browse the repository at this point in the history
  • Loading branch information
alinex committed May 10, 2014
1 parent e85c24c commit ccb7442
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 125 deletions.
10 changes: 5 additions & 5 deletions src/move.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ remove = require './remove'
# overwrite it
# * `clean`
# if set to `true` it will clean old files from target.
module.exports.async = (source, target, options, cb = ->) ->
module.exports.async = (source, target, options = {}, cb = ->) ->
if typeof options is 'function' or not options
cb = options ? ->
options = null
options = {}
debug "Move filepath #{source} to #{target}."
# collect methods to run
async.series [
Expand All @@ -51,7 +51,7 @@ module.exports.async = (source, target, options, cb = ->) ->
remove.async target, cb
# create parent directories
(cb) ->
mkdirs.async path.basedir(target), cb
mkdirs.async path.dirname(target), cb
# try to rename file
(cb) ->
return cb() if options
Expand Down Expand Up @@ -97,13 +97,13 @@ copyRemove = (source, target, options, cb) ->
#
# * `Error`
# If anything out of order happened.
module.exports.sync = (source, target, options) ->
module.exports.sync = (source, target, options = {}) ->
debug "Move filepath #{source} to #{target}."
# remove old target first
if options.clean
remove.sync target
# create parent directories
mkdirs.sync path.basedir target
mkdirs.sync path.dirname target
# try to rename file
unless options
try
Expand Down
62 changes: 37 additions & 25 deletions test/mocha/copy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ describe "Recursive copy", ->
it "should copy single file", (cb) ->
fs.copy 'test/temp/file1', 'test/temp/file10', (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/file10', 'real file').to.exist
expect(fs.existsSync('test/temp/file10'), 'real file').to.be.true
cb()

it "should copy single link", (cb) ->
fs.copy 'test/temp/dir3', 'test/temp/dir4', (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir4', 'new softlink').to.exist
expect(fs.existsSync('test/temp/dir4'), 'new softlink').to.be.true
stats = fs.lstatSync 'test/temp/dir4'
expect(stats.isSymbolicLink(), 'new softlink').to.be.true
cb()

it "should copy empty dir", (cb) ->
fs.copy 'test/temp/dir2', 'test/temp/dir4', (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.has.length 0
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.has.length 0
cb()

it "should copy deep dir", (cb) ->
fs.copy 'test/temp/dir1', 'test/temp/dir4', (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']
cb()

it "should fail on copy dir into file", (cb) ->
Expand All @@ -74,8 +74,8 @@ describe "Recursive copy", ->
include: '*1'
, (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']
cb()

it "should copy deep dir", (cb) ->
Expand All @@ -84,24 +84,24 @@ describe "Recursive copy", ->
maxdepth: 1
, (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']
fs.copy 'test/temp/dir1', 'test/temp/dir5',
mindepth: 0
maxdepth: 0
, (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir5', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir5', 'new dir').to.deep.equal []
expect(fs.existsSync('test/temp/dir5'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir5'), 'new dir').to.deep.equal []
cb()

it "should copy with dereferencing", (cb) ->
fs.copy 'test/temp/dir3', 'test/temp/dir4',
dereference: true
, (err) ->
expect(err, 'error').to.not.exist
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']
cb()

it "should fail on copy to existing file", (cb) ->
Expand All @@ -120,7 +120,7 @@ describe "Recursive copy", ->
fs.copy 'test/temp/file1', 'test/temp/file2',
ignore: true
, (err) ->
expect(err, 'error').to.not.exist
expect(err, 'error').to.not.exist
cb()


Expand All @@ -133,23 +133,23 @@ describe "Recursive copy", ->

it "should copy single file", ->
fs.copySync 'test/temp/file1', 'test/temp/file10'
expect(fs.existsSync 'test/temp/file10', 'real file').to.exist
expect(fs.existsSync('test/temp/file10'), 'real file').to.be.true

it "should copy single link", ->
fs.copySync 'test/temp/dir3', 'test/temp/dir4'
expect(fs.existsSync 'test/temp/dir4', 'new softlink').to.exist
expect(fs.existsSync('test/temp/dir4'), 'new softlink').to.be.true
stats = fs.lstatSync 'test/temp/dir4'
expect(stats.isSymbolicLink(), 'new softlink').to.be.true

it "should copy empty dir", ->
fs.copySync 'test/temp/dir2', 'test/temp/dir4'
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.has.length 0
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.has.length 0

it "should copy deep dir", ->
fs.copySync 'test/temp/dir1', 'test/temp/dir4'
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']

it "should fail on copy dir into file", ->
expect ->
Expand All @@ -169,14 +169,26 @@ describe "Recursive copy", ->
it "should copy dir with filter", ->
fs.copySync 'test/temp/dir1', 'test/temp/dir4',
include: '*1'
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']

it "should copy deep dir", ->
fs.copySync 'test/temp/dir1', 'test/temp/dir4',
mindepth: 1
maxdepth: 1
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']
fs.copySync 'test/temp/dir1', 'test/temp/dir5',
mindepth: 0
maxdepth: 0
expect(fs.existsSync('test/temp/dir5'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir5'), 'new dir').to.deep.equal []

it "should copy with dereferencing", ->
fs.copySync 'test/temp/dir3', 'test/temp/dir4',
dereference: true
expect(fs.existsSync 'test/temp/dir4', 'new dir').to.exist
expect(fs.readdirSync 'test/temp/dir4', 'new dir').to.deep.equal ['file11']
expect(fs.existsSync('test/temp/dir4'), 'new dir').to.be.true
expect(fs.readdirSync('test/temp/dir4'), 'new dir').to.deep.equal ['file11']

it "should fail on copy to existing file", ->
expect ->
Expand Down
Loading

0 comments on commit ccb7442

Please sign in to comment.