-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,040 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
var util = require('util') | ||
var path = require('path') | ||
var chai = require('chai') | ||
var os = require('os') | ||
var fs = require('graceful-fs') | ||
var expect = chai.expect | ||
var CWD = process.cwd() | ||
var fse = require(CWD) | ||
var ensureLink = fse.ensureLink | ||
var ensureLinkSync = fse.ensureLinkSync | ||
|
||
/* global afterEach, beforeEach, describe, it, after, before */ | ||
|
||
var TEST_DIR | ||
|
||
describe('fse-ensure-link', function () { | ||
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') | ||
|
||
var tests = [ | ||
// [[srcpath, dstpath], fs.link expect, ensureLink expect] | ||
[['./foo.txt', './link.txt'], 'file-success', 'file-success'], | ||
[['./foo.txt', './dir-foo/link.txt'], 'file-success', 'file-success'], | ||
[['./foo.txt', './empty-dir/link.txt'], 'file-success', 'file-success'], | ||
[['./foo.txt', './real-alpha/link.txt'], 'file-success', 'file-success'], | ||
[['./foo.txt', './real-alpha/real-beta/link.txt'], 'file-success', 'file-success'], | ||
[['./foo.txt', './real-alpha/real-beta/real-gamma/link.txt'], 'file-success', 'file-success'], | ||
[['./foo.txt', './alpha/link.txt'], 'file-error', 'file-success'], | ||
[['./foo.txt', './alpha/beta/link.txt'], 'file-error', 'file-success'], | ||
[['./foo.txt', './alpha/beta/gamma/link.txt'], 'file-error', 'file-success'], | ||
[['./missing.txt', './link.txt'], 'file-error', 'file-error'], | ||
[[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'], | ||
[[path.resolve(path.join(TEST_DIR, '../foo.txt')), './link.txt'], 'file-error', 'file-error'], | ||
[[path.resolve(path.join(TEST_DIR, '../dir-foo/foo.txt')), './link.txt'], 'file-error', 'file-error'] | ||
] | ||
|
||
before(function () { | ||
fse.emptyDirSync(TEST_DIR) | ||
process.chdir(TEST_DIR) | ||
}) | ||
|
||
beforeEach(function () { | ||
fs.writeFileSync('./foo.txt', 'foo\n') | ||
fse.mkdirsSync('empty-dir') | ||
fse.mkdirsSync('dir-foo') | ||
fs.writeFileSync('dir-foo/foo.txt', 'dir-foo\n') | ||
fse.mkdirsSync('dir-bar') | ||
fs.writeFileSync('dir-bar/bar.txt', 'dir-bar\n') | ||
fse.mkdirsSync('real-alpha/real-beta/real-gamma') | ||
}) | ||
|
||
afterEach(function (done) { | ||
fse.emptyDir(TEST_DIR, done) | ||
}) | ||
|
||
after(function () { | ||
process.chdir(CWD) | ||
fse.removeSync(TEST_DIR) | ||
}) | ||
|
||
function fileSuccess (args, fn) { | ||
var srcpath = args[0] | ||
var dstpath = args[1] | ||
var should = util.format('should create link file using src `%s` and dst `%s`', srcpath, dstpath) | ||
it(should, function (done) { | ||
var callback = function (err) { | ||
if (err) return done(err) | ||
var fileContent = fs.readFileSync(srcpath, 'utf8') | ||
var dstDir = path.dirname(dstpath) | ||
var dstBasename = path.basename(dstpath) | ||
expect(fs.readdirSync(dstDir)).to.contain(dstBasename) | ||
expect(fs.lstatSync(dstpath).isFile()).to.equal(true) | ||
expect(fs.readFileSync(dstpath, 'utf8')).to.equal(fileContent) | ||
return done() | ||
} | ||
args.push(callback) | ||
return fn.apply(null, args) | ||
}) | ||
} | ||
|
||
function fileError (args, fn) { | ||
var srcpath = args[0] | ||
var dstpath = args[1] | ||
var should = util.format('should return error when creating link file using src `%s` and dst `%s`', srcpath, dstpath) | ||
it(should, function (done) { | ||
var callback = function (err) { | ||
expect(err).to.be.instanceof(Error) | ||
return done() | ||
} | ||
args.push(callback) | ||
return fn.apply(null, args) | ||
}) | ||
} | ||
|
||
function fileSuccessSync (args, fn) { | ||
var srcpath = args[0] | ||
var dstpath = args[1] | ||
var should = util.format('should create link file using src `%s` and dst `%s`', srcpath, dstpath) | ||
it(should, function () { | ||
fn.apply(null, args) | ||
var fileContent = fs.readFileSync(srcpath, 'utf8') | ||
var dstDir = path.dirname(dstpath) | ||
var dstBasename = path.basename(dstpath) | ||
expect(fs.readdirSync(dstDir)).to.contain(dstBasename) | ||
expect(fs.lstatSync(dstpath).isFile()).to.equal(true) | ||
expect(fs.readFileSync(dstpath, 'utf8')).to.equal(fileContent) | ||
}) | ||
} | ||
|
||
function fileErrorSync (args, fn) { | ||
var srcpath = args[0] | ||
var dstpath = args[1] | ||
var should = util.format('should throw error using` src `%s` and dst `%s`', srcpath, dstpath) | ||
it(should, function () { | ||
expect(function () { | ||
return fn.apply(null, args) | ||
}).to.throw | ||
}) | ||
} | ||
|
||
describe('fs.link()', function () { | ||
var fn = fs.link | ||
tests.forEach(function (test) { | ||
var args = test[0].slice(0) | ||
var nativeBehavior = test[1] | ||
// var newBehavior = test[2] | ||
if (nativeBehavior === 'file-success') fileSuccess(args, fn) | ||
if (nativeBehavior === 'file-error') fileError(args, fn) | ||
}) | ||
}) | ||
|
||
describe('ensureLink()', function () { | ||
var fn = ensureLink | ||
tests.forEach(function (test) { | ||
var args = test[0].slice(0) | ||
// var nativeBehavior = test[1] | ||
var newBehavior = test[2] | ||
if (newBehavior === 'file-success') fileSuccess(args, fn) | ||
if (newBehavior === 'file-error') fileError(args, fn) | ||
}) | ||
}) | ||
|
||
describe('fs.linkSync()', function () { | ||
var fn = fs.linkSync | ||
tests.forEach(function (test) { | ||
var args = test[0].slice(0) | ||
var nativeBehavior = test[1] | ||
// var newBehavior = test[2] | ||
if (nativeBehavior === 'file-success') fileSuccessSync(args, fn) | ||
if (nativeBehavior === 'file-error') fileErrorSync(args, fn) | ||
}) | ||
}) | ||
|
||
describe('ensureLinkSync()', function () { | ||
var fn = ensureLinkSync | ||
tests.forEach(function (test) { | ||
var args = test[0].slice(0) | ||
// var nativeBehavior = test[1] | ||
var newBehavior = test[2] | ||
if (newBehavior === 'file-success') fileSuccessSync(args, fn) | ||
if (newBehavior === 'file-error') fileErrorSync(args, fn) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
var chai = require('chai') | ||
var util = require('util') | ||
var path = require('path') | ||
var os = require('os') | ||
var fs = require('graceful-fs') | ||
var expect = chai.expect | ||
var CWD = process.cwd() | ||
var fse = require(CWD) | ||
var _symlinkPaths = require('../symlink-paths') | ||
var symlinkPaths = _symlinkPaths.symlinkPaths | ||
var symlinkPathsSync = _symlinkPaths.symlinkPathsSync | ||
var TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure-symlink') | ||
|
||
/* global afterEach, beforeEach, describe, it, after, before */ | ||
|
||
describe('symlink-type', function () { | ||
|
||
before(function () { | ||
fse.emptyDirSync(TEST_DIR) | ||
process.chdir(TEST_DIR) | ||
}) | ||
|
||
beforeEach(function () { | ||
fs.writeFileSync('./foo.txt', 'foo\n') | ||
fse.mkdirsSync('./empty-dir') | ||
fse.mkdirsSync('./dir-foo') | ||
fs.writeFileSync('./dir-foo/foo.txt', 'dir-foo\n') | ||
fse.mkdirsSync('./dir-bar') | ||
fs.writeFileSync('./dir-bar/bar.txt', 'dir-bar\n') | ||
fse.mkdirsSync('./real-alpha/real-beta/real-gamma') | ||
}) | ||
|
||
afterEach(function (done) { | ||
fse.emptyDir(TEST_DIR, done) | ||
}) | ||
|
||
after(function () { | ||
process.chdir(CWD) | ||
fse.removeSync(TEST_DIR) | ||
}) | ||
|
||
var tests = [ | ||
[['foo.txt', 'symlink.txt'], {toCwd: 'foo.txt', toDst: 'foo.txt'}], // smart && nodestyle | ||
[['foo.txt', 'empty-dir/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // smart | ||
[['../foo.txt', 'empty-dir/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // nodestyle | ||
[['foo.txt', 'dir-bar/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // smart | ||
[['../foo.txt', 'dir-bar/symlink.txt'], {toCwd: 'foo.txt', toDst: '../foo.txt'}], // nodestyle | ||
// this is to preserve node's symlink capability these arguments say create | ||
// a link to 'dir-foo/foo.txt' this works because it exists this is unlike | ||
// the previous example with 'empty-dir' because 'empty-dir/foo.txt' does not exist. | ||
[['foo.txt', 'dir-foo/symlink.txt' ], {toCwd: 'dir-foo/foo.txt', toDst: 'foo.txt'}], // nodestyle | ||
[['foo.txt', 'real-alpha/real-beta/real-gamma/symlink.txt' ], {toCwd: 'foo.txt', toDst: '../../../foo.txt'}] | ||
] | ||
|
||
tests.forEach(function (test) { | ||
test[1] = { | ||
toCwd: path.join(test[1].toCwd), | ||
toDst: path.join(test[1].toDst) | ||
} | ||
}) | ||
|
||
describe('symlinkPaths()', function () { | ||
tests.forEach(function (test) { | ||
var args = test[0].slice(0) | ||
var expectedType = test[1] | ||
var should = util.format('should return \'%s\' when src \'%s\' and dst is \'%s\'', JSON.stringify(expectedType), args[0], args[1]) | ||
it(should, function (done) { | ||
var callback = function (err, type) { | ||
if (err) done(err) | ||
expect(type).to.deep.equal(expectedType) | ||
done() | ||
} | ||
args.push(callback) | ||
return symlinkPaths.apply(null, args) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('symlinkPathsSync()', function () { | ||
tests.forEach(function (test) { | ||
var args = test[0].slice(0) | ||
var expectedType = test[1] | ||
var should = util.format('should return \'%s\' when src \'%s\' and dst is \'%s\'', JSON.stringify(expectedType), args[0], args[1]) | ||
it(should, function () { | ||
var type = symlinkPathsSync.apply(null, args) | ||
expect(type).to.deep.equal(type, expectedType) | ||
}) | ||
}) | ||
}) | ||
|
||
}) |
Oops, something went wrong.