Refactor move() tests

- Refactor legacy setup
- Merge move-clobber.test.js into move.test.js
- Add more complete testing for overwrite option
- Remove fixtures directory, create fixtures with code
This commit is contained in:
RyanZim 2016-12-31 13:21:05 -05:00
parent f6e2109a95
commit fde5b026d5
5 changed files with 119 additions and 146 deletions

View File

@ -1 +0,0 @@
sonic the hedgehog

View File

@ -1 +0,0 @@
tails

View File

@ -1,112 +0,0 @@
var assert = require('assert')
var fs = require('fs')
var path = require('path')
var os = require('os')
var fse = require(process.cwd())
/* global afterEach, beforeEach, describe, it */
describe('move / overwrite', function () {
var TEST_DIR, FIXTURES_DIR
beforeEach(function (done) {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move')
fse.emptyDir(TEST_DIR, function (err) {
assert.ifError(err)
FIXTURES_DIR = path.join(TEST_DIR, 'fixtures')
fse.remove(FIXTURES_DIR, function (err) {
assert.ifError(err)
fse.copy(path.join(__dirname, './fixtures'), FIXTURES_DIR, done)
})
})
})
afterEach(function (done) {
fse.remove(TEST_DIR, done)
})
describe('> when overwrite = true', function () {
describe('> when dest is a directory', function () {
it('should overwrite the destination', function (done) {
// Tests fail on appveyor/Windows due to
// https://github.com/isaacs/node-graceful-fs/issues/98.
// Workaround by increasing the timeout by a minute (because
// graceful times out after a minute).
this.timeout(90000)
// use fixtures dir as dest since it has stuff
var dest = FIXTURES_DIR
var paths = fs.readdirSync(dest)
// verify dest has stuff
assert(paths.indexOf('a-file') >= 0)
assert(paths.indexOf('a-folder') >= 0)
// create new source dir
var src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fse.mkdirsSync(path.join(src, 'some-folder'))
fs.writeFileSync(path.join(src, 'some-file'), 'hi')
// verify source has stuff
paths = fs.readdirSync(src)
assert(paths.indexOf('some-file') >= 0)
assert(paths.indexOf('some-folder') >= 0)
fse.move(src, dest, {overwrite: true}, function (err) {
if (err) return done(err)
// verify dest does not have old stuff
var paths = fs.readdirSync(dest)
assert.strictEqual(paths.indexOf('a-file'), -1)
assert.strictEqual(paths.indexOf('a-folder'), -1)
// verify dest has new stuff
assert(paths.indexOf('some-file') >= 0)
assert(paths.indexOf('some-folder') >= 0)
done()
})
})
})
})
describe('> clobber', function () {
it('is an alias for overwrite', function (done) {
// Tests fail on appveyor/Windows due to
// https://github.com/isaacs/node-graceful-fs/issues/98.
// Workaround by increasing the timeout by a minute (because
// graceful times out after a minute).
this.timeout(90000)
// use fixtures dir as dest since it has stuff
var dest = FIXTURES_DIR
var paths = fs.readdirSync(dest)
// verify dest has stuff
assert(paths.indexOf('a-file') >= 0)
// create new source dir
var src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fs.writeFileSync(path.join(src, 'some-file'), 'hi')
// verify source has stuff
paths = fs.readdirSync(src)
assert(paths.indexOf('some-file') >= 0)
fse.move(src, dest, {clobber: true}, function (err) {
if (err) return done(err)
// verify dest does not have old stuff
var paths = fs.readdirSync(dest)
assert.strictEqual(paths.indexOf('a-file'), -1)
// verify dest has new stuff
assert(paths.indexOf('some-file') >= 0)
done()
})
})
})
})

View File

@ -7,9 +7,6 @@ var fse = require(process.cwd())
/* global afterEach, beforeEach, describe, it */
var FIXTURES_DIR = ''
var SRC_FIXTURES_DIR = path.join(__dirname, './fixtures')
function createAsyncErrFn (errCode) {
var fn = function () {
fn.callCount++
@ -45,8 +42,12 @@ describe('move', function () {
fse.emptyDirSync(TEST_DIR)
FIXTURES_DIR = path.join(TEST_DIR, 'fixtures')
fse.copySync(SRC_FIXTURES_DIR, FIXTURES_DIR)
// Create fixtures:
fs.writeFileSync(path.join(TEST_DIR, 'a-file'), 'sonic the hedgehog\n')
fs.mkdirSync(path.join(TEST_DIR, 'a-folder'))
fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-file'), 'tails\n')
fs.mkdirSync(path.join(TEST_DIR, 'a-folder/another-folder'))
fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n')
})
afterEach(function (done) {
@ -54,23 +55,36 @@ describe('move', function () {
})
it('should rename a file on the same device', function (done) {
var src = FIXTURES_DIR + '/a-file'
var dest = FIXTURES_DIR + '/a-file-dest'
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/a-file-dest'
fse.move(src, dest, function (err) {
assert.ifError(err)
fs.readFile(dest, 'utf8', function (err, contents) {
var expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
})
})
})
it('should not overwrite the destination by default', function (done) {
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/a-folder/another-file'
// verify file exists already
assert(fs.existsSync(dest))
fse.move(src, dest, function (err) {
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
done()
})
})
it('should not overwrite if overwrite = false', function (done) {
var src = FIXTURES_DIR + '/a-file'
var dest = FIXTURES_DIR + '/a-folder/another-file'
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/a-folder/another-file'
// verify file exists already
assert(fs.existsSync(dest))
@ -81,9 +95,63 @@ describe('move', function () {
})
})
it('should overwrite file if overwrite = true', function (done) {
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/a-folder/another-file'
// verify file exists already
assert(fs.existsSync(dest))
fse.move(src, dest, {overwrite: true}, function (err) {
assert.ifError(err)
fs.readFile(dest, 'utf8', function (err, contents) {
var expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
})
})
})
it('should overwrite the destination directory if overwrite = true', function (done) {
// Tests fail on appveyor/Windows due to
// https://github.com/isaacs/node-graceful-fs/issues/98.
// Workaround by increasing the timeout by a minute (because
// graceful times out after a minute).
this.timeout(90000)
// Create src
var src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fse.mkdirsSync(path.join(src, 'some-folder'))
fs.writeFileSync(path.join(src, 'some-file'), 'hi')
var dest = path.join(TEST_DIR, 'a-folder')
// verify dest has stuff in it
var paths = fs.readdirSync(dest)
assert(paths.indexOf('another-file') >= 0)
assert(paths.indexOf('another-folder') >= 0)
fse.move(src, dest, {overwrite: true}, function (err) {
assert.ifError(err)
// verify dest does not have old stuff
var paths = fs.readdirSync(dest)
assert.strictEqual(paths.indexOf('another-file'), -1)
assert.strictEqual(paths.indexOf('another-folder'), -1)
// verify dest has new stuff
assert(paths.indexOf('some-file') >= 0)
assert(paths.indexOf('some-folder') >= 0)
done()
})
})
it('should not create directory structure if mkdirp is false', function (done) {
var src = FIXTURES_DIR + '/a-file'
var dest = FIXTURES_DIR + '/does/not/exist/a-file-dest'
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/does/not/exist/a-file-dest'
// verify dest directory does not exist
assert(!fs.existsSync(path.dirname(dest)))
@ -95,8 +163,8 @@ describe('move', function () {
})
it('should create directory structure by default', function (done) {
var src = FIXTURES_DIR + '/a-file'
var dest = FIXTURES_DIR + '/does/not/exist/a-file-dest'
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/does/not/exist/a-file-dest'
// verify dest directory does not exist
assert(!fs.existsSync(path.dirname(dest)))
@ -106,15 +174,15 @@ describe('move', function () {
fs.readFile(dest, 'utf8', function (err, contents) {
var expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
})
})
})
it('should work across devices', function (done) {
var src = FIXTURES_DIR + '/a-file'
var dest = FIXTURES_DIR + '/a-file-dest'
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/a-file-dest'
setUpMockFs('EXDEV')
@ -125,7 +193,7 @@ describe('move', function () {
fs.readFile(dest, 'utf8', function (err, contents) {
var expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
done()
@ -134,8 +202,8 @@ describe('move', function () {
})
it('should move folders', function (done) {
var src = FIXTURES_DIR + '/a-folder'
var dest = FIXTURES_DIR + '/a-folder-dest'
var src = TEST_DIR + '/a-folder'
var dest = TEST_DIR + '/a-folder-dest'
// verify it doesn't exist
assert(!fs.existsSync(dest))
@ -145,15 +213,15 @@ describe('move', function () {
fs.readFile(dest + '/another-file', 'utf8', function (err, contents) {
var expected = /^tails\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
})
})
})
it('should move folders across devices with EISDIR erro', function (done) {
var src = FIXTURES_DIR + '/a-folder'
var dest = FIXTURES_DIR + '/a-folder-dest'
it('should move folders across devices with EISDIR error', function (done) {
var src = TEST_DIR + '/a-folder'
var dest = TEST_DIR + '/a-folder-dest'
setUpMockFs('EISDIR')
@ -164,7 +232,7 @@ describe('move', function () {
fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
var expected = /^knuckles\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs('EISDIR')
@ -174,8 +242,8 @@ describe('move', function () {
})
it('should overwrite folders across devices', function (done) {
var src = FIXTURES_DIR + '/a-folder'
var dest = FIXTURES_DIR + '/a-folder-dest'
var src = TEST_DIR + '/a-folder'
var dest = TEST_DIR + '/a-folder-dest'
fs.mkdirSync(dest)
@ -188,7 +256,7 @@ describe('move', function () {
fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
var expected = /^knuckles\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs('EXDEV')
@ -198,8 +266,8 @@ describe('move', function () {
})
it('should move folders across devices with EXDEV error', function (done) {
var src = FIXTURES_DIR + '/a-folder'
var dest = FIXTURES_DIR + '/a-folder-dest'
var src = TEST_DIR + '/a-folder'
var dest = TEST_DIR + '/a-folder-dest'
setUpMockFs('EXDEV')
@ -210,7 +278,7 @@ describe('move', function () {
fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
var expected = /^knuckles\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), contents + ' match ' + expected)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
@ -219,6 +287,26 @@ describe('move', function () {
})
})
describe('clobber', function () {
it('should be an alias for overwrite', function (done) {
var src = TEST_DIR + '/a-file'
var dest = TEST_DIR + '/a-folder/another-file'
// verify file exists already
assert(fs.existsSync(dest))
fse.move(src, dest, {overwrite: true}, function (err) {
assert.ifError(err)
fs.readFile(dest, 'utf8', function (err, contents) {
var expected = /^sonic the hedgehog\r?\n$/
assert.ifError(err)
assert.ok(contents.match(expected), `${contents} match ${expected}`)
done()
})
})
})
})
describe.skip('> when trying to a move a folder into itself', function () {
it('should produce an error', function (done) {
var SRC_DIR = path.join(TEST_DIR, 'test')