From 3884263717396615cc65eb6e723349ecfe1c11b9 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Wed, 21 Feb 2018 13:36:11 +0000 Subject: [PATCH 1/4] Reformat --- index.js | 26 +++++++++++++------------- tests/tests.js | 34 ++++++++++++---------------------- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index 3813e3c..1da133f 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,27 @@ -var tmp = require('tmp'); +var tmp = require("tmp"); -module.exports = function testCanSymlink (options) { +module.exports = function testCanSymlink(options) { options = options || {}; - var fs = options.fs || require('fs'); + var fs = options.fs || require("fs"); - var canLinkSrc = tmp.tmpNameSync(); + var canLinkSrc = tmp.tmpNameSync(); var canLinkDest = tmp.tmpNameSync(); try { - fs.writeFileSync(canLinkSrc, ''); + fs.writeFileSync(canLinkSrc, ""); } catch (e) { - return false + return false; } try { - fs.symlinkSync(canLinkSrc, canLinkDest) + fs.symlinkSync(canLinkSrc, canLinkDest); } catch (e) { - fs.unlinkSync(canLinkSrc) - return false + fs.unlinkSync(canLinkSrc); + return false; } - fs.unlinkSync(canLinkSrc) - fs.unlinkSync(canLinkDest) + fs.unlinkSync(canLinkSrc); + fs.unlinkSync(canLinkDest); - return true -} + return true; +}; diff --git a/tests/tests.js b/tests/tests.js index d3e3b8f..04eabdf 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,40 +1,30 @@ -var assert = require('assert'); -var canSymlink = require('..'); +var assert = require("assert"); +var canSymlink = require(".."); -describe('can-symlink', function() { - it('returns true if no exceptions are thrown', function() { +describe("can-symlink", function() { + it("returns true if no exceptions are thrown", function() { var options = { fs: { - symlinkSync: function() { - // noop - }, - writeFileSync: function() { - - }, - unlinkSync: function() { - - } + symlinkSync: function() {}, + writeFileSync: function() {}, + unlinkSync: function() {} } }; assert.equal(canSymlink(options), true); }); - it('returns false if exceptions are thrown', function() { + it("returns false if exceptions are thrown", function() { var options = { fs: { symlinkSync: function() { - throw Error('Symlink failed'); - }, - writeFileSync: function() { - + throw Error("Symlink failed"); }, - unlinkSync: function() { - - } + writeFileSync: function() {}, + unlinkSync: function() {} } }; assert.equal(canSymlink(options), false); }); -}); \ No newline at end of file +}); From c6bd6067842fe58e95dc056d93573e37d13e5b99 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Wed, 21 Feb 2018 13:49:12 +0000 Subject: [PATCH 2/4] Add CHANGELOG.md --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..284ab8e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# master + +# 1.0.0 + +* Initial release From d07033c9c5e176a5bf04a8e46eb1e7f8055fa020 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Wed, 21 Feb 2018 13:49:18 +0000 Subject: [PATCH 3/4] Short-circuit on non-Windows platforms, and use directory instead of file to test symlinking Some users have reported that they can symlink files but not directories. https://github.com/broccolijs/node-symlink-or-copy/issues/35 It is not entirely clear when this can happen. --- CHANGELOG.md | 3 +++ index.js | 13 +++++++++---- tests/tests.js | 20 ++++++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 284ab8e..2652668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # master +* Short-circuit on non-Windows platforms unless `forceTest` option is passed +* Use directory instead of file to test symlinking + # 1.0.0 * Initial release diff --git a/index.js b/index.js index 1da133f..dba78b4 100644 --- a/index.js +++ b/index.js @@ -2,25 +2,30 @@ var tmp = require("tmp"); module.exports = function testCanSymlink(options) { options = options || {}; + + if (!options.forceTest && process.platform !== "win32") { + return true; + } + var fs = options.fs || require("fs"); var canLinkSrc = tmp.tmpNameSync(); var canLinkDest = tmp.tmpNameSync(); try { - fs.writeFileSync(canLinkSrc, ""); + fs.mkdirSync(canLinkSrc); } catch (e) { return false; } try { - fs.symlinkSync(canLinkSrc, canLinkDest); + fs.symlinkSync(canLinkSrc, canLinkDest, "dir"); } catch (e) { - fs.unlinkSync(canLinkSrc); return false; + } finally { + fs.rmdirSync(canLinkSrc); } - fs.unlinkSync(canLinkSrc); fs.unlinkSync(canLinkDest); return true; diff --git a/tests/tests.js b/tests/tests.js index 04eabdf..e8432cf 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -6,9 +6,11 @@ describe("can-symlink", function() { var options = { fs: { symlinkSync: function() {}, - writeFileSync: function() {}, + mkdirSync: function() {}, + rmdirSync: function() {}, unlinkSync: function() {} - } + }, + forceTest: true }; assert.equal(canSymlink(options), true); @@ -20,11 +22,21 @@ describe("can-symlink", function() { symlinkSync: function() { throw Error("Symlink failed"); }, - writeFileSync: function() {}, + mkdirSync: function() {}, + rmdirSync: function() {}, unlinkSync: function() {} - } + }, + forceTest: true }; assert.equal(canSymlink(options), false); }); + + it("works on the real file-system", function() { + let result = canSymlink({ forceTest: true }); + // On win32 we only care that we do not get an exception + if (process.platform !== "win32") { + assert.equal(result, true); + } + }); }); From d8ff4a613156912ded8be59c1456b347294eaef1 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Wed, 21 Feb 2018 13:54:00 +0000 Subject: [PATCH 4/4] Expand README --- README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bd203e8..b97b39f 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,32 @@ -# Can you create symlinks? +# node-can-symlink -Its easy to find out. Install using `npm install -g can-symlink`. Once installed run `can-symlink` to find out if you have what it takes to create symlinks. \ No newline at end of file +This package tests whether you can create symlinks on this system. + +## Command-line usage + +```sh +npm install -g can-symlink + +can-symlink +``` + +## Programmatic usage + +```sh +npm install --save can-symlink +``` + +```js +let canSymlink = require("can-symlink")(); +``` + +`canSymlink` will be either `true` or `false`. Note that we are calling the +module. + +On non-Windows platforms, `require("can-symlink")()` automatically returns true. +To force testing for symlinkability on non-Windows platforms, pass the +`forceTest` option: + +```js +let canSymlink = require("can-symlink")({ forceTest: true }); +```