Skip to content

Commit 8498ee0

Browse files
Igor Zinkovskyry
Igor Zinkovsky
authored andcommitted
make simple\test-fs-symlink.js work on windows
1 parent 8f1ba25 commit 8498ee0

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

test/simple/test-fs-symlink.js

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,76 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22-
// libuv-broken
23-
24-
2522
var common = require('../common');
2623
var assert = require('assert');
2724
var path = require('path');
2825
var fs = require('fs');
26+
var exec = require('child_process').exec;
2927
var completed = 0;
28+
var expected_tests = 2;
29+
30+
var is_windows = process.platform === 'win32';
31+
32+
var runtest = function(skip_symlinks) {
33+
if (!skip_symlinks) {
34+
// test creating and reading symbolic link
35+
var linkData = path.join(common.fixturesDir, '/cycles/root.js');
36+
var linkPath = path.join(common.tmpDir, 'symlink1.js');
37+
38+
// Delete previously created link
39+
try {
40+
fs.unlinkSync(linkPath);
41+
} catch(e)
42+
{}
43+
44+
fs.symlink(linkData, linkPath, function(err) {
45+
if (err) throw err;
46+
console.log('symlink done');
47+
// todo: fs.lstat?
48+
fs.readlink(linkPath, function(err, destination) {
49+
if (err) throw err;
50+
assert.equal(destination, linkData);
51+
completed++;
52+
});
53+
});
54+
}
3055

31-
// test creating and reading symbolic link
32-
var linkData = path.join(common.fixturesDir, '/cycles/root.js');
33-
var linkPath = path.join(common.tmpDir, 'symlink1.js');
34-
fs.symlink(linkData, linkPath, function(err) {
35-
if (err) throw err;
36-
console.log('symlink done');
37-
// todo: fs.lstat?
38-
fs.readlink(linkPath, function(err, destination) {
56+
// test creating and reading hard link
57+
var srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
58+
var dstPath = path.join(common.tmpDir, 'link1.js');
59+
60+
// Delete previously created link
61+
try {
62+
fs.unlinkSync(dstPath);
63+
} catch(e)
64+
{}
65+
66+
fs.link(srcPath, dstPath, function(err) {
3967
if (err) throw err;
40-
assert.equal(destination, linkData);
68+
console.log('hard link done');
69+
var srcContent = fs.readFileSync(srcPath, 'utf8');
70+
var dstContent = fs.readFileSync(dstPath, 'utf8');
71+
assert.equal(srcContent, dstContent);
4172
completed++;
4273
});
43-
});
74+
}
4475

45-
// test creating and reading hard link
46-
var srcPath = path.join(common.fixturesDir, 'cycles', 'root.js');
47-
var dstPath = path.join(common.tmpDir, 'link1.js');
48-
fs.link(srcPath, dstPath, function(err) {
49-
if (err) throw err;
50-
console.log('hard link done');
51-
var srcContent = fs.readFileSync(srcPath, 'utf8');
52-
var dstContent = fs.readFileSync(dstPath, 'utf8');
53-
assert.equal(srcContent, dstContent);
54-
completed++;
55-
});
76+
if (is_windows) {
77+
// On Windows, creating symlinks requires admin privileges.
78+
// We'll only try to run symlink test if we have enough privileges.
79+
exec("whoami /priv", function(err, o) {
80+
if (err || o.indexOf('SeCreateSymbolicLinkPrivilege') == -1) {
81+
expected_tests = 1;
82+
runtest(true);
83+
} else {
84+
runtest(false);
85+
}
86+
});
87+
} else {
88+
runtest(false);
89+
}
5690

5791
process.on('exit', function() {
58-
assert.equal(completed, 2);
92+
assert.equal(completed, expected_tests);
5993
});
6094

0 commit comments

Comments
 (0)