Skip to content

Commit

Permalink
copy & copySync: handle case-insensitive paths, check paths stricter …
Browse files Browse the repository at this point in the history
…before copying
  • Loading branch information
manidlou committed Apr 18, 2018
1 parent eb4901b commit 8f05322
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 295 deletions.
131 changes: 131 additions & 0 deletions lib/copy-sync/__tests__/copy-sync-case-insensitive-paths.test.js
@@ -0,0 +1,131 @@
'use strict'

const assert = require('assert')
const os = require('os')
const path = require('path')
const fs = require(process.cwd())

/* global beforeEach, afterEach, describe, it */

describe('+ copySync() - case insensitive paths', () => {
let TEST_DIR = ''
let src = ''
let dest = ''

beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-case-insensitive-paths')
fs.emptyDir(TEST_DIR, done)
})

afterEach(done => fs.remove(TEST_DIR, done))

describe('> when src is a directory', () => {
it('should behave correctly based on the OS', () => {
src = path.join(TEST_DIR, 'srcdir')
fs.outputFileSync(path.join(src, 'subdir', 'file.txt'), 'some data')
dest = path.join(TEST_DIR, 'srcDir')
let errThrown = false

try {
fs.copySync(src, dest)
} catch (err) {
if (os === 'darwin' || os === 'win32') {
assert.strictEqual(err.message, 'Source and destination must not be the same.')
assert(fs.existsSync(src))
assert(!fs.existsSync(dest))
errThrown = true
}
}
if (os === 'darwin' || os === 'win32') assert(errThrown)
if (os === 'linux') {
assert(fs.existsSync(dest))
assert.strictEqual(fs.readFileSync(path.join(dest, 'subdir', 'file.txt'), 'utf8'), 'some data')
assert(!errThrown)
}
})
})

describe('> when src is a file', () => {
it('should behave correctly based on the OS', () => {
src = path.join(TEST_DIR, 'srcfile')
fs.outputFileSync(src, 'some data')
dest = path.join(TEST_DIR, 'srcFile')
let errThrown = false

try {
fs.copySync(src, dest)
} catch (err) {
if (os === 'darwin' || os === 'win32') {
assert.strictEqual(err.message, 'Source and destination must not be the same.')
assert(fs.existsSync(src))
assert(!fs.existsSync(dest))
errThrown = true
}
}
if (os === 'darwin' || os === 'win32') assert(errThrown)
if (os === 'linux') {
assert(fs.existsSync(dest))
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'some data')
assert(!errThrown)
}
})
})

describe('> when src is a symlink', () => {
it('should behave correctly based on the OS, symlink dir', () => {
src = path.join(TEST_DIR, 'srcdir')
fs.outputFileSync(path.join(src, 'subdir', 'file.txt'), 'some data')
const srcLink = path.join(TEST_DIR, 'src-symlink')
fs.symlinkSync(src, srcLink, 'dir')
dest = path.join(TEST_DIR, 'srcDir')
let errThrown = false

try {
fs.copySync(src, dest)
} catch (err) {
if (os === 'darwin' || os === 'win32') {
assert.strictEqual(err.message, 'Source and destination must not be the same.')
assert(fs.existsSync(src))
assert(!fs.existsSync(dest))
errThrown = true
}
}
if (os === 'darwin' || os === 'win32') assert(errThrown)
if (os === 'linux') {
assert(fs.existsSync(dest))
assert.strictEqual(fs.readFileSync(path.join(dest, 'subdir', 'file.txt'), 'utf8'), 'some data')
const link = fs.readlinkSync(srcLink)
assert.strictEqual(link, dest)
assert(!errThrown)
}
})

it('should behave correctly based on the OS, symlink file', () => {
src = path.join(TEST_DIR, 'srcfile')
fs.outputFileSync(src, 'some data')
const srcLink = path.join(TEST_DIR, 'src-symlink')
fs.symlinkSync(src, srcLink, 'file')
dest = path.join(TEST_DIR, 'srcFile')
let errThrown = false

try {
fs.copySync(src, dest)
} catch (err) {
if (os === 'darwin' || os === 'win32') {
assert.strictEqual(err.message, 'Source and destination must not be the same.')
assert(fs.existsSync(src))
assert(!fs.existsSync(dest))
errThrown = true
}
}
if (os === 'darwin' || os === 'win32') assert(errThrown)
if (os === 'linux') {
assert(fs.existsSync(dest))
assert.strictEqual(fs.readFileSync(dest, 'utf8'), 'some data')
const link = fs.readlinkSync(srcLink)
assert.strictEqual(link, dest)
assert(!errThrown)
}
})
})
})
2 changes: 1 addition & 1 deletion lib/copy-sync/__tests__/copy-sync-dir.test.js
Expand Up @@ -20,7 +20,7 @@ describe('+ copySync()', () => {
fs.emptyDir(TEST_DIR, done)
})

describe('> when the source is a directory', () => {
describe('> when src is a directory', () => {
describe('> when dest exists and is a file', () => {
it('should throw error', () => {
const src = path.join(TEST_DIR, 'src')
Expand Down
4 changes: 2 additions & 2 deletions lib/copy-sync/__tests__/copy-sync-file.test.js
Expand Up @@ -20,7 +20,7 @@ describe('+ copySync()', () => {

afterEach(done => fs.remove(TEST_DIR, done))

describe('> when the source is a file', () => {
describe('> when src is a file', () => {
it('should copy the file synchronously', () => {
const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('+ copySync()', () => {
})
})

describe('> when the source file does not have write permissions', () => {
describe('> when src file does not have write permissions', () => {
it('should be able to copy contents of file', () => {
const fileSrc = path.join(TEST_DIR, 'file.txt')
const fileDest = path.join(TEST_DIR, 'file-copy.txt')
Expand Down
Expand Up @@ -35,9 +35,9 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
// src is symlink, dest is regular
// src is symlink, dest is symlink

describe('> when the source is a directory', () => {
describe('> when src is a directory', () => {
describe(`>> when src is regular and dest is a symlink that points to src`, () => {
it('should not copy and return', () => {
it('should error', () => {
src = path.join(TEST_DIR, 'src')
fs.mkdirsSync(src)
const subdir = path.join(TEST_DIR, 'src', 'subdir')
Expand All @@ -49,7 +49,11 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {

const oldlen = klawSync(src).length

fs.copySync(src, destLink)
try {
fs.copySync(src, destLink)
} catch (err) {
assert.strictEqual(err.message, 'Source and destination must not be the same.')
}

const newlen = klawSync(src).length
assert.strictEqual(newlen, oldlen)
Expand Down Expand Up @@ -117,17 +121,21 @@ describe('+ copySync() - prevent copying identical files and dirs', () => {
// src is symlink, dest is regular
// src is symlink, dest is symlink

describe('> when the source is a file', () => {
describe('> when src is a file', () => {
describe(`>> when src is regular and dest is a symlink that points to src`, () => {
it('should not copy and return', () => {
it('should error', () => {
src = path.join(TEST_DIR, 'src', 'somefile.txt')
fs.ensureFileSync(src)
fs.writeFileSync(src, 'some data')

const destLink = path.join(TEST_DIR, 'dest-symlink')
fs.symlinkSync(src, destLink, 'file')

fs.copySync(src, destLink)
try {
fs.copySync(src, destLink)
} catch (err) {
assert.strictEqual(err.message, 'Source and destination must not be the same.')
}

const link = fs.readlinkSync(destLink)
assert.strictEqual(link, src)
Expand Down

0 comments on commit 8f05322

Please sign in to comment.