mirror of
https://github.com/jprichardson/node-fs-extra.git
synced 2026-01-25 16:42:57 +00:00
Refactor test move
This commit is contained in:
parent
7ae3a87a2a
commit
ebd22661a3
@ -1,18 +1,20 @@
|
||||
var assert = require('assert')
|
||||
var os = require('os')
|
||||
var path = require('path')
|
||||
var rimraf = require('rimraf')
|
||||
var fs = require('graceful-fs')
|
||||
var fse = require(process.cwd())
|
||||
'use strict'
|
||||
|
||||
const fs = require('graceful-fs')
|
||||
const os = require('os')
|
||||
const fse = require(process.cwd())
|
||||
const path = require('path')
|
||||
const assert = require('assert')
|
||||
const rimraf = require('rimraf')
|
||||
|
||||
/* global afterEach, beforeEach, describe, it */
|
||||
|
||||
function createAsyncErrFn (errCode) {
|
||||
var fn = function () {
|
||||
const fn = function () {
|
||||
fn.callCount++
|
||||
var callback = arguments[arguments.length - 1]
|
||||
setTimeout(function () {
|
||||
var err = new Error()
|
||||
const callback = arguments[arguments.length - 1]
|
||||
setTimeout(() => {
|
||||
const err = new Error()
|
||||
err.code = errCode
|
||||
callback(err)
|
||||
}, 10)
|
||||
@ -21,8 +23,8 @@ function createAsyncErrFn (errCode) {
|
||||
return fn
|
||||
}
|
||||
|
||||
var originalRename = fs.rename
|
||||
var originalLink = fs.link
|
||||
const originalRename = fs.rename
|
||||
const originalLink = fs.link
|
||||
|
||||
function setUpMockFs (errCode) {
|
||||
fs.rename = createAsyncErrFn(errCode)
|
||||
@ -34,10 +36,10 @@ function tearDownMockFs () {
|
||||
fs.link = originalLink
|
||||
}
|
||||
|
||||
describe('move', function () {
|
||||
var TEST_DIR
|
||||
describe('move', () => {
|
||||
let TEST_DIR
|
||||
|
||||
beforeEach(function () {
|
||||
beforeEach(() => {
|
||||
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move')
|
||||
|
||||
fse.emptyDirSync(TEST_DIR)
|
||||
@ -50,18 +52,16 @@ describe('move', function () {
|
||||
fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n')
|
||||
})
|
||||
|
||||
afterEach(function (done) {
|
||||
rimraf(TEST_DIR, done)
|
||||
})
|
||||
afterEach(done => rimraf(TEST_DIR, done))
|
||||
|
||||
it('should rename a file on the same device', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/a-file-dest'
|
||||
it('should rename a file on the same device', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/a-file-dest`
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
fs.readFile(dest, 'utf8', function (err, contents) {
|
||||
var expected = /^sonic the hedgehog\r?\n$/
|
||||
fs.readFile(dest, 'utf8', (err, contents) => {
|
||||
const expected = /^sonic the hedgehog\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
done()
|
||||
@ -69,43 +69,43 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should not overwrite the destination by default', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/a-folder/another-file'
|
||||
it('should not overwrite the destination by default', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/a-folder/another-file`
|
||||
|
||||
// verify file exists already
|
||||
assert(fs.existsSync(dest))
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not overwrite if overwrite = false', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/a-folder/another-file'
|
||||
it('should not overwrite if overwrite = false', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/a-folder/another-file`
|
||||
|
||||
// verify file exists already
|
||||
assert(fs.existsSync(dest))
|
||||
|
||||
fse.move(src, dest, {overwrite: false}, function (err) {
|
||||
fse.move(src, dest, {overwrite: false}, err => {
|
||||
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should overwrite file if overwrite = true', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/a-folder/another-file'
|
||||
it('should overwrite file if overwrite = true', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/a-folder/another-file`
|
||||
|
||||
// verify file exists already
|
||||
assert(fs.existsSync(dest))
|
||||
|
||||
fse.move(src, dest, {overwrite: true}, function (err) {
|
||||
fse.move(src, dest, {overwrite: true}, err => {
|
||||
assert.ifError(err)
|
||||
fs.readFile(dest, 'utf8', function (err, contents) {
|
||||
var expected = /^sonic the hedgehog\r?\n$/
|
||||
fs.readFile(dest, 'utf8', (err, contents) => {
|
||||
const expected = /^sonic the hedgehog\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
done()
|
||||
@ -121,23 +121,23 @@ describe('move', function () {
|
||||
this.timeout(90000)
|
||||
|
||||
// Create src
|
||||
var src = path.join(TEST_DIR, 'src')
|
||||
const 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')
|
||||
const dest = path.join(TEST_DIR, 'a-folder')
|
||||
|
||||
// verify dest has stuff in it
|
||||
var paths = fs.readdirSync(dest)
|
||||
const paths = fs.readdirSync(dest)
|
||||
assert(paths.indexOf('another-file') >= 0)
|
||||
assert(paths.indexOf('another-folder') >= 0)
|
||||
|
||||
fse.move(src, dest, {overwrite: true}, function (err) {
|
||||
fse.move(src, dest, {overwrite: true}, err => {
|
||||
assert.ifError(err)
|
||||
|
||||
// verify dest does not have old stuff
|
||||
var paths = fs.readdirSync(dest)
|
||||
const paths = fs.readdirSync(dest)
|
||||
assert.strictEqual(paths.indexOf('another-file'), -1)
|
||||
assert.strictEqual(paths.indexOf('another-folder'), -1)
|
||||
|
||||
@ -149,30 +149,30 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should not create directory structure if mkdirp is false', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/does/not/exist/a-file-dest'
|
||||
it('should not create directory structure if mkdirp is false', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
|
||||
|
||||
// verify dest directory does not exist
|
||||
assert(!fs.existsSync(path.dirname(dest)))
|
||||
|
||||
fse.move(src, dest, {mkdirp: false}, function (err) {
|
||||
fse.move(src, dest, {mkdirp: false}, err => {
|
||||
assert.strictEqual(err.code, 'ENOENT')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should create directory structure by default', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/does/not/exist/a-file-dest'
|
||||
it('should create directory structure by default', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
|
||||
|
||||
// verify dest directory does not exist
|
||||
assert(!fs.existsSync(path.dirname(dest)))
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
fs.readFile(dest, 'utf8', function (err, contents) {
|
||||
var expected = /^sonic the hedgehog\r?\n$/
|
||||
fs.readFile(dest, 'utf8', (err, contents) => {
|
||||
const expected = /^sonic the hedgehog\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
done()
|
||||
@ -180,18 +180,18 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should work across devices', function (done) {
|
||||
var src = TEST_DIR + '/a-file'
|
||||
var dest = TEST_DIR + '/a-file-dest'
|
||||
it('should work across devices', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/a-file-dest`
|
||||
|
||||
setUpMockFs('EXDEV')
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(fs.link.callCount, 1)
|
||||
|
||||
fs.readFile(dest, 'utf8', function (err, contents) {
|
||||
var expected = /^sonic the hedgehog\r?\n$/
|
||||
fs.readFile(dest, 'utf8', (err, contents) => {
|
||||
const expected = /^sonic the hedgehog\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
|
||||
@ -201,17 +201,17 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should move folders', function (done) {
|
||||
var src = TEST_DIR + '/a-folder'
|
||||
var dest = TEST_DIR + '/a-folder-dest'
|
||||
it('should move folders', done => {
|
||||
const src = `${TEST_DIR}/a-folder`
|
||||
const dest = `${TEST_DIR}/a-folder-dest`
|
||||
|
||||
// verify it doesn't exist
|
||||
assert(!fs.existsSync(dest))
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
fs.readFile(dest + '/another-file', 'utf8', function (err, contents) {
|
||||
var expected = /^tails\r?\n$/
|
||||
fs.readFile(dest + '/another-file', 'utf8', (err, contents) => {
|
||||
const expected = /^tails\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
done()
|
||||
@ -219,18 +219,18 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should move folders across devices with EISDIR error', function (done) {
|
||||
var src = TEST_DIR + '/a-folder'
|
||||
var dest = TEST_DIR + '/a-folder-dest'
|
||||
it('should move folders across devices with EISDIR error', done => {
|
||||
const src = `${TEST_DIR}/a-folder`
|
||||
const dest = `${TEST_DIR}/a-folder-dest`
|
||||
|
||||
setUpMockFs('EISDIR')
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(fs.link.callCount, 1)
|
||||
|
||||
fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
|
||||
var expected = /^knuckles\r?\n$/
|
||||
fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
|
||||
const expected = /^knuckles\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
|
||||
@ -241,20 +241,20 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should overwrite folders across devices', function (done) {
|
||||
var src = TEST_DIR + '/a-folder'
|
||||
var dest = TEST_DIR + '/a-folder-dest'
|
||||
it('should overwrite folders across devices', done => {
|
||||
const src = `${TEST_DIR}/a-folder`
|
||||
const dest = `${TEST_DIR}/a-folder-dest`
|
||||
|
||||
fs.mkdirSync(dest)
|
||||
|
||||
setUpMockFs('EXDEV')
|
||||
|
||||
fse.move(src, dest, {overwrite: true}, function (err) {
|
||||
fse.move(src, dest, {overwrite: true}, err => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(fs.rename.callCount, 1)
|
||||
|
||||
fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
|
||||
var expected = /^knuckles\r?\n$/
|
||||
fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
|
||||
const expected = /^knuckles\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
|
||||
@ -265,18 +265,18 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
it('should move folders across devices with EXDEV error', function (done) {
|
||||
var src = TEST_DIR + '/a-folder'
|
||||
var dest = TEST_DIR + '/a-folder-dest'
|
||||
it('should move folders across devices with EXDEV error', done => {
|
||||
const src = `${TEST_DIR}/a-folder`
|
||||
const dest = `${TEST_DIR}/a-folder-dest`
|
||||
|
||||
setUpMockFs('EXDEV')
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
assert.strictEqual(fs.link.callCount, 1)
|
||||
|
||||
fs.readFile(dest + '/another-folder/file3', 'utf8', function (err, contents) {
|
||||
var expected = /^knuckles\r?\n$/
|
||||
fs.readFile(dest + '/another-folder/file3', 'utf8', (err, contents) => {
|
||||
const expected = /^knuckles\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
|
||||
@ -287,18 +287,18 @@ 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'
|
||||
describe('clobber', () => {
|
||||
it('should be an alias for overwrite', done => {
|
||||
const src = `${TEST_DIR}/a-file`
|
||||
const dest = `${TEST_DIR}/a-folder/another-file`
|
||||
|
||||
// verify file exists already
|
||||
assert(fs.existsSync(dest))
|
||||
|
||||
fse.move(src, dest, {overwrite: true}, function (err) {
|
||||
fse.move(src, dest, {overwrite: true}, err => {
|
||||
assert.ifError(err)
|
||||
fs.readFile(dest, 'utf8', function (err, contents) {
|
||||
var expected = /^sonic the hedgehog\r?\n$/
|
||||
fs.readFile(dest, 'utf8', (err, contents) => {
|
||||
const expected = /^sonic the hedgehog\r?\n$/
|
||||
assert.ifError(err)
|
||||
assert.ok(contents.match(expected), `${contents} match ${expected}`)
|
||||
done()
|
||||
@ -307,16 +307,16 @@ describe('move', function () {
|
||||
})
|
||||
})
|
||||
|
||||
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')
|
||||
var DEST_DIR = path.join(TEST_DIR, 'test', 'test')
|
||||
describe.skip('> when trying to a move a folder into itself', () => {
|
||||
it('should produce an error', done => {
|
||||
const SRC_DIR = path.join(TEST_DIR, 'test')
|
||||
const DEST_DIR = path.join(TEST_DIR, 'test', 'test')
|
||||
|
||||
assert(!fs.existsSync(SRC_DIR))
|
||||
fs.mkdirSync(SRC_DIR)
|
||||
assert(fs.existsSync(SRC_DIR))
|
||||
|
||||
fse.move(SRC_DIR, DEST_DIR, function (err) {
|
||||
fse.move(SRC_DIR, DEST_DIR, err => {
|
||||
assert(fs.existsSync(SRC_DIR))
|
||||
assert(err)
|
||||
done()
|
||||
@ -327,9 +327,9 @@ describe('move', function () {
|
||||
// tested on Linux ubuntu 3.13.0-32-generic #57-Ubuntu SMP i686 i686 GNU/Linux
|
||||
// this won't trigger a bug on Mac OS X Yosimite with a USB drive (/Volumes)
|
||||
// see issue #108
|
||||
describe('> when actually trying to a move a folder across devices', function () {
|
||||
var differentDevice = '/mnt'
|
||||
var __skipTests = false
|
||||
describe('> when actually trying to a move a folder across devices', () => {
|
||||
const differentDevice = '/mnt'
|
||||
let __skipTests = false
|
||||
|
||||
// must set this up, if not, exit silently
|
||||
if (!fs.existsSync(differentDevice)) {
|
||||
@ -345,12 +345,12 @@ describe('move', function () {
|
||||
__skipTests = true
|
||||
}
|
||||
|
||||
var _it = __skipTests ? it.skip : it
|
||||
const _it = __skipTests ? it.skip : it
|
||||
|
||||
describe('> just the folder', function () {
|
||||
_it('should move the folder', function (done) {
|
||||
var src = '/mnt/some/weird/dir-really-weird'
|
||||
var dest = path.join(TEST_DIR, 'device-weird')
|
||||
describe('> just the folder', () => {
|
||||
_it('should move the folder', done => {
|
||||
const src = '/mnt/some/weird/dir-really-weird'
|
||||
const dest = path.join(TEST_DIR, 'device-weird')
|
||||
|
||||
if (!fs.existsSync(src)) {
|
||||
fse.mkdirpSync(src)
|
||||
@ -360,7 +360,7 @@ describe('move', function () {
|
||||
|
||||
assert(fs.lstatSync(src).isDirectory())
|
||||
|
||||
fse.move(src, dest, function (err) {
|
||||
fse.move(src, dest, err => {
|
||||
assert.ifError(err)
|
||||
assert(fs.existsSync(dest))
|
||||
// console.log(path.normalize(dest))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user