Skip to content

Commit

Permalink
Refactor move() tests
Browse files Browse the repository at this point in the history
- Merge move-clobber.test.js into move.test.js
- Remove fixtures directory, create fixtures with code
  • Loading branch information
RyanZim committed Dec 31, 2016
1 parent f6e2109 commit a0d9652
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 146 deletions.
1 change: 0 additions & 1 deletion lib/move/__tests__/fixtures/a-file

This file was deleted.

1 change: 0 additions & 1 deletion lib/move/__tests__/fixtures/a-folder/another-file

This file was deleted.

1 change: 0 additions & 1 deletion lib/move/__tests__/fixtures/a-folder/another-folder/file3

This file was deleted.

112 changes: 0 additions & 112 deletions lib/move/__tests__/move-clobber.test.js

This file was deleted.

150 changes: 119 additions & 31 deletions lib/move/__tests__/move.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand Down Expand Up @@ -45,32 +42,49 @@ 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) {
rimraf(TEST_DIR, done)
})

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))
Expand All @@ -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)))
Expand All @@ -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)))
Expand All @@ -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')

Expand All @@ -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()
Expand All @@ -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))
Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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)

Expand All @@ -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')

Expand All @@ -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')

Expand All @@ -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()

Expand All @@ -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')
Expand Down

0 comments on commit a0d9652

Please sign in to comment.