Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suppress ensureLink & ensureSymlink dest exists error #169

Merged
merged 1 commit into from
Aug 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
suppress ensurelink dest exists error
suppress ensuresync dest exists error
  • Loading branch information
reggi committed Aug 6, 2015
commit 8d92df4366682a3553da407595505a6121b25b6d
35 changes: 34 additions & 1 deletion lib/ensure/__tests__/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('fse-ensure-link', function () {
[['../foo.txt', './link.txt'], 'file-error', 'file-error'],
[['../dir-foo/foo.txt', './link.txt'], 'file-error', 'file-error'],
// error is thrown if destination path exists
[['./foo.txt', './dir-foo/foo.txt'], 'file-error', 'file-error'],
[['./foo.txt', './dir-foo/foo.txt'], 'file-error', 'file-dest-exists'],
[[path.resolve(path.join(TEST_DIR, './foo.txt')), './link.txt'], 'file-success', 'file-success'],
[[path.resolve(path.join(TEST_DIR, './dir-foo/foo.txt')), './link.txt'], 'file-success', 'file-success'],
[[path.resolve(path.join(TEST_DIR, './missing.txt')), './link.txt'], 'file-error', 'file-error'],
Expand Down Expand Up @@ -107,6 +107,23 @@ describe('fse-ensure-link', function () {
})
}

function fileDestExists (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
it(should, function (done) {
var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
var callback = function (err) {
if (err) return done(err)
var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
return done()
}
args.push(callback)
return fn.apply(null, args)
})
}

function fileSuccessSync (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
Expand Down Expand Up @@ -144,6 +161,18 @@ describe('fse-ensure-link', function () {
})
}

function fileDestExistsSync (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
it(should, function () {
var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
fn.apply(null, args)
var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
})
}

describe('fs.link()', function () {
var fn = fs.link
tests.forEach(function (test) {
Expand All @@ -152,6 +181,7 @@ describe('fse-ensure-link', function () {
// var newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccess(args, fn)
if (nativeBehavior === 'file-error') fileError(args, fn)
if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn)
})
})

Expand All @@ -163,6 +193,7 @@ describe('fse-ensure-link', function () {
var newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccess(args, fn)
if (newBehavior === 'file-error') fileError(args, fn)
if (newBehavior === 'file-dest-exists') fileDestExists(args, fn)
})
})

Expand All @@ -174,6 +205,7 @@ describe('fse-ensure-link', function () {
// var newBehavior = test[2]
if (nativeBehavior === 'file-success') fileSuccessSync(args, fn)
if (nativeBehavior === 'file-error') fileErrorSync(args, fn)
if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn)
})
})

Expand All @@ -185,6 +217,7 @@ describe('fse-ensure-link', function () {
var newBehavior = test[2]
if (newBehavior === 'file-success') fileSuccessSync(args, fn)
if (newBehavior === 'file-error') fileErrorSync(args, fn)
if (newBehavior === 'file-dest-exists') fileDestExistsSync(args, fn)
})
})

Expand Down
70 changes: 70 additions & 0 deletions lib/ensure/__tests__/symlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ describe('fse-ensure-symlink', function () {
[['./foo.txt', './alpha/beta/gamma/symlink.txt'], 'file-error', 'file-success'],
[['./missing.txt', './symlink.txt'], 'file-broken', 'file-error'],
[['./missing.txt', './missing-dir/symlink.txt'], 'file-error', 'file-error'],
// error is thrown if destination path exists
[['./foo.txt', './dir-foo/foo.txt'], 'file-error', 'file-dest-exists'],
[['./dir-foo', './symlink-dir-foo'], 'dir-success', 'dir-success'],
[['../dir-bar', './dir-foo/symlink-dir-bar'], 'dir-success', 'dir-success'],
[['./dir-bar', './dir-foo/symlink-dir-bar'], 'dir-broken', 'dir-success'],
Expand All @@ -45,6 +47,8 @@ describe('fse-ensure-symlink', function () {
[['./dir-foo', './alpha/beta/dir-foo'], 'dir-error', 'dir-success'],
[['./dir-foo', './alpha/beta/gamma/dir-foo'], 'dir-error', 'dir-success'],
[['./missing', './dir-foo/symlink-dir-missing'], 'dir-broken', 'dir-error'],
// error is thrown if destination path exists
[['./dir-foo', './real-alpha/real-beta'], 'dir-error', 'dir-dest-exists'],
[[path.resolve(path.join(TEST_DIR, './foo.txt')), './symlink.txt'], 'file-success', 'file-success'],
[[path.resolve(path.join(TEST_DIR, './dir-foo/foo.txt')), './symlink.txt'], 'file-success', 'file-success'],
[[path.resolve(path.join(TEST_DIR, './missing.txt')), './symlink.txt'], 'file-broken', 'file-error'],
Expand Down Expand Up @@ -141,6 +145,23 @@ describe('fse-ensure-symlink', function () {
})
}

function fileDestExists (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
it(should, function (done) {
var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
var callback = function (err) {
if (err) return done(err)
var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
return done()
}
args.push(callback)
return fn.apply(null, args)
})
}

function dirSuccess (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
Expand Down Expand Up @@ -206,6 +227,23 @@ describe('fse-ensure-symlink', function () {
})
}

function dirDestExists (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
it(should, function (done) {
var destinationContentBefore = fs.readdirSync(dstpath)
var callback = function (err) {
if (err) return done(err)
var destinationContentAfter = fs.readdirSync(dstpath)
assert.deepEqual(destinationContentBefore, destinationContentAfter)
return done()
}
args.push(callback)
return fn.apply(null, args)
})
}

function fileSuccessSync (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
Expand Down Expand Up @@ -261,6 +299,18 @@ describe('fse-ensure-symlink', function () {
})
}

function fileDestExistsSync (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
it(should, function () {
var destinationContentBefore = fs.readFileSync(dstpath, 'utf8')
fn.apply(null, args)
var destinationContentAfter = fs.readFileSync(dstpath, 'utf8')
assert.equal(destinationContentBefore, destinationContentAfter)
})
}

function dirSuccessSync (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
Expand Down Expand Up @@ -316,6 +366,18 @@ describe('fse-ensure-symlink', function () {
})
}

function dirDestExistsSync (args, fn) {
var srcpath = args[0]
var dstpath = args[1]
var should = util.format('should do nothing using src `%s` and dst `%s`', srcpath, dstpath)
it(should, function () {
var destinationContentBefore = fs.readdirSync(dstpath)
fn.apply(null, args)
var destinationContentAfter = fs.readdirSync(dstpath)
assert.deepEqual(destinationContentBefore, destinationContentAfter)
})
}

describe('fs.symlink()', function () {
var fn = fs.symlink
tests.forEach(function (test) {
Expand All @@ -325,10 +387,12 @@ describe('fse-ensure-symlink', function () {
if (nativeBehavior === 'file-success') fileSuccess(args, fn)
if (nativeBehavior === 'file-broken') fileBroken(args, fn)
if (nativeBehavior === 'file-error') fileError(args, fn)
if (nativeBehavior === 'file-dest-exists') fileDestExists(args, fn)
args.push('dir')
if (nativeBehavior === 'dir-success') dirSuccess(args, fn)
if (nativeBehavior === 'dir-broken') dirBroken(args, fn)
if (nativeBehavior === 'dir-error') dirError(args, fn)
if (nativeBehavior === 'dir-dest-exists') dirDestExists(args, fn)
})
})

Expand All @@ -341,9 +405,11 @@ describe('fse-ensure-symlink', function () {
if (newBehavior === 'file-success') fileSuccess(args, fn)
if (newBehavior === 'file-broken') fileBroken(args, fn)
if (newBehavior === 'file-error') fileError(args, fn)
if (newBehavior === 'file-dest-exists') fileDestExists(args, fn)
if (newBehavior === 'dir-success') dirSuccess(args, fn)
if (newBehavior === 'dir-broken') dirBroken(args, fn)
if (newBehavior === 'dir-error') dirError(args, fn)
if (newBehavior === 'dir-dest-exists') dirDestExists(args, fn)
})
})

Expand All @@ -356,10 +422,12 @@ describe('fse-ensure-symlink', function () {
if (nativeBehavior === 'file-success') fileSuccessSync(args, fn)
if (nativeBehavior === 'file-broken') fileBrokenSync(args, fn)
if (nativeBehavior === 'file-error') fileErrorSync(args, fn)
if (nativeBehavior === 'file-dest-exists') fileDestExistsSync(args, fn)
args.push('dir')
if (nativeBehavior === 'dir-success') dirSuccessSync(args, fn)
if (nativeBehavior === 'dir-broken') dirBrokenSync(args, fn)
if (nativeBehavior === 'dir-error') dirErrorSync(args, fn)
if (nativeBehavior === 'dir-dest-exists') dirDestExistsSync(args, fn)
})
})

Expand All @@ -372,9 +440,11 @@ describe('fse-ensure-symlink', function () {
if (newBehavior === 'file-success') fileSuccessSync(args, fn)
if (newBehavior === 'file-broken') fileBrokenSync(args, fn)
if (newBehavior === 'file-error') fileErrorSync(args, fn)
if (newBehavior === 'file-dest-exists') fileDestExistsSync(args, fn)
if (newBehavior === 'dir-success') dirSuccessSync(args, fn)
if (newBehavior === 'dir-broken') dirBrokenSync(args, fn)
if (newBehavior === 'dir-error') dirErrorSync(args, fn)
if (newBehavior === 'dir-dest-exists') dirDestExistsSync(args, fn)
})
})

Expand Down
30 changes: 18 additions & 12 deletions lib/ensure/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,30 @@ function createLink (srcpath, dstpath, callback) {
})
}

fs.lstat(srcpath, function (err, stat) {
if (err) {
err.message = err.message.replace('lstat', 'ensureLink')
return callback(err)
}

var dir = path.dirname(dstpath)
fs.exists(dir, function (dirExists) {
if (dirExists) return makeLink(srcpath, dstpath)
mkdir.mkdirs(dir, function (err) {
if (err) return callback(err)
makeLink(srcpath, dstpath)
fs.exists(dstpath, function (destinationExists) {
if (destinationExists) return callback(null)
fs.lstat(srcpath, function (err, stat) {
if (err) {
err.message = err.message.replace('lstat', 'ensureLink')
return callback(err)
}
var dir = path.dirname(dstpath)
fs.exists(dir, function (dirExists) {
if (dirExists) return makeLink(srcpath, dstpath)
mkdir.mkdirs(dir, function (err) {
if (err) return callback(err)
makeLink(srcpath, dstpath)
})
})
})
})
}

function createLinkSync (srcpath, dstpath, callback) {

var destinationExists = fs.existsSync(dstpath)
if (destinationExists) return undefined

try {
fs.lstatSync(srcpath)
} catch (err) {
Expand All @@ -40,6 +45,7 @@ function createLinkSync (srcpath, dstpath, callback) {
var dirExists = fs.existsSync(dir)
if (dirExists) return fs.linkSync(srcpath, dstpath)
mkdir.mkdirsSync(dir)

return fs.linkSync(srcpath, dstpath)
}

Expand Down
28 changes: 18 additions & 10 deletions lib/ensure/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ var symlinkTypeSync = _symlinkType.symlinkTypeSync
function createSymlink (srcpath, dstpath, type, callback) {
callback = (typeof type === 'function') ? type : callback
type = (typeof type === 'function') ? false : type
symlinkPaths(srcpath, dstpath, function (err, relative) {
if (err) return callback(err)
srcpath = relative.toDst
symlinkType(relative.toCwd, type, function (err, type) {

fs.exists(dstpath, function (destinationExists) {
if (destinationExists) return callback(null)
symlinkPaths(srcpath, dstpath, function (err, relative) {
if (err) return callback(err)
var dir = path.dirname(dstpath)
fs.exists(dir, function (dirExists) {
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
mkdirs(dir, function (err) {
if (err) return callback(err)
fs.symlink(srcpath, dstpath, type, callback)
srcpath = relative.toDst
symlinkType(relative.toCwd, type, function (err, type) {
if (err) return callback(err)
var dir = path.dirname(dstpath)
fs.exists(dir, function (dirExists) {
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
mkdirs(dir, function (err) {
if (err) return callback(err)
fs.symlink(srcpath, dstpath, type, callback)
})
})
})
})
Expand All @@ -35,6 +39,10 @@ function createSymlink (srcpath, dstpath, type, callback) {
function createSymlinkSync (srcpath, dstpath, type, callback) {
callback = (typeof type === 'function') ? type : callback
type = (typeof type === 'function') ? false : type

var destinationExists = fs.existsSync(dstpath)
if (destinationExists) return undefined

var relative = symlinkPathsSync(srcpath, dstpath)
srcpath = relative.toDst
type = symlinkTypeSync(relative.toCwd, type)
Expand Down