Skip to content

Commit

Permalink
Add more tests for moveSync when moving files into src subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
manidlou committed Mar 8, 2017
1 parent 08d7d3a commit 535fe8d
Showing 1 changed file with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,76 +37,89 @@ describe('+ moveSync() - prevent moving into itself', () => {
afterEach(() => fs.removeSync(TEST_DIR))

describe('> when source is a file', () => {
it(`should move the file successfully even when dest is a subdir of src`, () => {
const srcFile = path.join(TEST_DIR, 'src', 'srcfile.txt')
it(`should move the file successfully even when dest parent is 'src/dest'`, () => {
const destFile = path.join(TEST_DIR, 'src', 'dest', 'destfile.txt')
fs.writeFileSync(srcFile, dat0)
return testSuccessFile(src, destFile)
})

it(`should move the file successfully when dest parent is 'src/src_dest'`, () => {
const destFile = path.join(TEST_DIR, 'src', 'src_dest', 'destfile.txt')
return testSuccessFile(src, destFile)
})

fs.moveSync(srcFile, destFile)
it(`should move the file successfully when dest parent is 'src/dest_src'`, () => {
const destFile = path.join(TEST_DIR, 'src', 'dest_src', 'destfile.txt')
return testSuccessFile(src, destFile)
})

it(`should move the file successfully when dest parent is 'src/dest/src'`, () => {
const destFile = path.join(TEST_DIR, 'src', 'dest', 'src', 'destfile.txt')
return testSuccessFile(src, destFile)
})

const out = fs.readFileSync(destFile, 'utf8')
assert.strictEqual(out, dat0, 'file contents matched')
assert(!fs.existsSync(srcFile))
it(`should move the file successfully when dest parent is 'srcsrc/dest'`, () => {
const destFile = path.join(TEST_DIR, 'srcsrc', 'dest', 'destfile.txt')
return testSuccessFile(src, destFile)
})
})

describe('> when source is a directory', () => {
it(`should move the directory successfully when dest is 'src_dest'`, () => {
dest = path.join(TEST_DIR, 'src_dest')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'src-dest'`, () => {
dest = path.join(TEST_DIR, 'src-dest')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'dest_src'`, () => {
dest = path.join(TEST_DIR, 'dest_src')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'src_dest/src'`, () => {
dest = path.join(TEST_DIR, 'src_dest', 'src')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'src-dest/src'`, () => {
dest = path.join(TEST_DIR, 'src-dest', 'src')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'dest_src/src'`, () => {
dest = path.join(TEST_DIR, 'dest_src', 'src')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'src_src/dest'`, () => {
dest = path.join(TEST_DIR, 'src_src', 'dest')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'src-src/dest'`, () => {
dest = path.join(TEST_DIR, 'src-src', 'dest')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'srcsrc/dest'`, () => {
dest = path.join(TEST_DIR, 'srcsrc', 'dest')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should move the directory successfully when dest is 'dest/src'`, () => {
dest = path.join(TEST_DIR, 'dest', 'src')
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it('should move the directory successfully when dest is very nested that all its parents need to be created', () => {
dest = path.join(TEST_DIR, 'dest', 'src', 'foo', 'bar', 'baz', 'qux', 'quux', 'waldo',
'grault', 'garply', 'fred', 'plugh', 'thud', 'some', 'highly', 'deeply',
'badly', 'nasty', 'crazy', 'mad', 'nested', 'dest')
assert(!fs.existsSync(dest))
return testSuccess(src, dest)
return testSuccessDir(src, dest)
})

it(`should throw error when dest is 'src/dest'`, () => {
Expand All @@ -131,7 +144,17 @@ describe('+ moveSync() - prevent moving into itself', () => {
})
})

function testSuccess (src, dest) {
function testSuccessFile (src, destFile) {
const srcFile = path.join(src, FILES[0])

fs.moveSync(srcFile, destFile)

const o0 = fs.readFileSync(destFile, 'utf8')
assert.strictEqual(o0, dat0, 'file contents matched')
assert(!fs.existsSync(srcFile))
}

function testSuccessDir (src, dest) {
const srclen = klawSync(src).length
// assert src has contents
assert(srclen > 2)
Expand Down

0 comments on commit 535fe8d

Please sign in to comment.