~ubuntu-branches/ubuntu/precise/nodejs/precise

« back to all changes in this revision

Viewing changes to test/simple/test-fs-realpath.js

  • Committer: Bazaar Package Importer
  • Author(s): Jérémy Lal
  • Date: 2010-08-20 11:49:04 UTC
  • mfrom: (7.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100820114904-lz22w6fkth7yh179
Tags: 0.2.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require("../common");
 
1
common = require("../common");
 
2
assert = common.assert
2
3
var fs = require('fs');
3
4
var path = require('path');
4
5
var async_completed = 0, async_expected = 0, unlink = [];
9
10
    var ignoreError = false;
10
11
    if (assertBlock) {
11
12
      try {
12
 
        ignoreError = assertBlock.apply(assertBlock, 
 
13
        ignoreError = assertBlock.apply(assertBlock,
13
14
          Array.prototype.slice.call(arguments));
14
15
      }
15
16
      catch (e) {
22
23
}
23
24
 
24
25
function bashRealpath(path, callback) {
25
 
  exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function (err, o) {
 
26
  common.exec("cd '"+path.replace("'","\\'")+"' && pwd -P",function 
 
27
  (err, o) {
26
28
    callback(err, o.trim());
27
29
  });
28
30
}
30
32
// sub-tests:
31
33
 
32
34
function test_simple_relative_symlink(callback) {
33
 
  var entry = fixturesDir+'/cycles/symlink',
34
 
      expected = fixturesDir+'/cycles/root.js';
 
35
  var entry = common.fixturesDir+'/cycles/symlink',
 
36
      expected = common.fixturesDir+'/cycles/root.js';
35
37
  [
36
38
    [entry, 'root.js'],
37
39
  ].forEach(function(t) {
41
43
  });
42
44
  var result = fs.realpathSync(entry);
43
45
  assert.equal(result, expected,
44
 
      'got '+inspect(result)+' expected '+inspect(expected));
 
46
      'got '+common.inspect(result)+' expected '+common.inspect(expected));
45
47
  asynctest(fs.realpath, [entry], callback, function(err, result){
46
48
    assert.equal(result, expected,
47
 
      'got '+inspect(result)+' expected '+inspect(expected));
 
49
      'got '+common.inspect(result)+' expected '+common.inspect(expected));
48
50
  });
49
51
}
50
52
 
51
53
function test_simple_absolute_symlink(callback) {
52
 
  bashRealpath(fixturesDir, function(err, fixturesAbsDir) {
 
54
  bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
53
55
    if (err) return callback(err);
54
56
    var entry = fixturesAbsDir+'/cycles/symlink',
55
57
        expected = fixturesAbsDir+'/nested-index/one/index.js';
62
64
    });
63
65
    var result = fs.realpathSync(entry);
64
66
    assert.equal(result, expected,
65
 
        'got '+inspect(result)+' expected '+inspect(expected));
 
67
        'got '+common.inspect(result)+' expected '+common.inspect(expected));
66
68
    asynctest(fs.realpath, [entry], callback, function(err, result){
67
69
      assert.equal(result, expected,
68
 
        'got '+inspect(result)+' expected '+inspect(expected));
 
70
        'got '+common.inspect(result)+' expected '+common.inspect(expected));
69
71
    });
70
72
  });
71
73
}
72
74
 
73
75
function test_deep_relative_file_symlink(callback) {
74
 
  var expected = path.join(fixturesDir, 'cycles', 'root.js');
 
76
  var expected = path.join(common.fixturesDir, 'cycles', 'root.js');
75
77
  var linkData1 = "../../cycles/root.js";
76
 
  var linkPath1 = path.join(fixturesDir, "nested-index", 'one', 'symlink1.js');
 
78
  var linkPath1 = path.join(common.fixturesDir, "nested-index", 'one', 'symlink1.js');
77
79
  try {fs.unlinkSync(linkPath1);}catch(e){}
78
80
  fs.symlinkSync(linkData1, linkPath1);
79
81
 
80
82
  var linkData2 = "../one/symlink1.js";
81
 
  var entry = path.join(fixturesDir, "nested-index", 'two', 'symlink1-b.js');
 
83
  var entry = path.join(common.fixturesDir, "nested-index", 'two', 'symlink1-b.js');
82
84
  try {fs.unlinkSync(entry);}catch(e){}
83
85
  fs.symlinkSync(linkData2, entry);
84
86
  unlink.push(linkPath1);
87
89
  assert.equal(fs.realpathSync(entry), expected);
88
90
  asynctest(fs.realpath, [entry], callback, function(err, result){
89
91
    assert.equal(result, expected,
90
 
      'got '+inspect(result)+' expected '+inspect(expected));
 
92
      'got '+common.inspect(result)+' expected '+common.inspect(expected));
91
93
  });
92
94
}
93
95
 
94
96
function test_deep_relative_dir_symlink(callback) {
95
 
  var expected = path.join(fixturesDir, 'cycles', 'folder');
 
97
  var expected = path.join(common.fixturesDir, 'cycles', 'folder');
96
98
  var linkData1b = "../../cycles/folder";
97
 
  var linkPath1b = path.join(fixturesDir, "nested-index", 'one', 'symlink1-dir');
 
99
  var linkPath1b = path.join(common.fixturesDir, "nested-index", 'one', 'symlink1-dir');
98
100
  try {fs.unlinkSync(linkPath1b);}catch(e){}
99
101
  fs.symlinkSync(linkData1b, linkPath1b);
100
102
 
101
103
  var linkData2b = "../one/symlink1-dir";
102
 
  var entry = path.join(fixturesDir, "nested-index", 'two', 'symlink12-dir');
 
104
  var entry = path.join(common.fixturesDir, "nested-index", 'two', 'symlink12-dir');
103
105
  try {fs.unlinkSync(entry);}catch(e){}
104
106
  fs.symlinkSync(linkData2b, entry);
105
107
  unlink.push(linkPath1b);
106
108
  unlink.push(entry);
107
109
 
108
110
  assert.equal(fs.realpathSync(entry), expected);
109
 
  
 
111
 
110
112
  asynctest(fs.realpath, [entry], callback, function(err, result){
111
113
    assert.equal(result, expected,
112
 
      'got '+inspect(result)+' expected '+inspect(expected));
 
114
      'got '+common.inspect(result)+' expected '+common.inspect(expected));
113
115
  });
114
116
}
115
117
 
116
118
function test_cyclic_link_protection(callback) {
117
 
  var entry = fixturesDir+'/cycles/realpath-3a';
 
119
  var entry = common.fixturesDir+'/cycles/realpath-3a';
118
120
  [
119
121
    [entry, '../cycles/realpath-3b'],
120
 
    [fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'],
121
 
    [fixturesDir+'/cycles/realpath-3c', '../cycles/realpath-3a'],
 
122
    [common.fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'],
 
123
    [common.fixturesDir+'/cycles/realpath-3c', '../cycles/realpath-3a'],
122
124
  ].forEach(function(t) {
123
125
    try {fs.unlinkSync(t[0]);}catch(e){}
124
126
    fs.symlinkSync(t[1], t[0]);
132
134
}
133
135
 
134
136
function test_relative_input_cwd(callback) {
135
 
  var p = fixturesDir.lastIndexOf('/');
136
 
  var entrydir = fixturesDir.substr(0, p);
137
 
  var entry = fixturesDir.substr(p+1)+'/cycles/realpath-3a';
138
 
  var expected = fixturesDir+'/cycles/root.js';
 
137
  var p = common.fixturesDir.lastIndexOf('/');
 
138
  var entrydir = common.fixturesDir.substr(0, p);
 
139
  var entry = common.fixturesDir.substr(p+1)+'/cycles/realpath-3a';
 
140
  var expected = common.fixturesDir+'/cycles/root.js';
139
141
  [
140
142
    [entry, '../cycles/realpath-3b'],
141
 
    [fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'],
142
 
    [fixturesDir+'/cycles/realpath-3c', 'root.js'],
 
143
    [common.fixturesDir+'/cycles/realpath-3b', '../cycles/realpath-3c'],
 
144
    [common.fixturesDir+'/cycles/realpath-3c', 'root.js'],
143
145
  ].forEach(function(t) {
144
146
    var fn = t[0];
145
147
    if (fn.charAt(0) !== '/') fn = entrydir + '/' + fn;
153
155
  asynctest(fs.realpath, [entry], callback, function(err, result){
154
156
    process.chdir(origcwd);
155
157
    assert.equal(result, expected,
156
 
      'got '+inspect(result)+' expected '+inspect(expected));
 
158
      'got '+common.inspect(result)+' expected '+common.inspect(expected));
157
159
    return true;
158
160
  });
159
161
}
160
162
 
161
163
function test_deep_symlink_mix(callback) {
162
 
  // todo: check to see that fixturesDir is not rooted in the
 
164
  // todo: check to see that common.fixturesDir is not rooted in the
163
165
  //       same directory as our test symlink.
164
166
  // obtain our current realpath using bash (so we can test ourselves)
165
 
  bashRealpath(fixturesDir, function(err, fixturesAbsDir) {
 
167
  bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
166
168
    if (err) return callback(err);
167
169
    /*
168
170
    /tmp/node-test-realpath-f1 -> ../tmp/node-test-realpath-d1/foo
169
171
    /tmp/node-test-realpath-d1 -> ../node-test-realpath-d2
170
172
    /tmp/node-test-realpath-d2/foo -> ../node-test-realpath-f2
171
 
    /tmp/node-test-realpath-f2 
 
173
    /tmp/node-test-realpath-f2
172
174
      -> /node/test/fixtures/nested-index/one/realpath-c
173
 
    /node/test/fixtures/nested-index/one/realpath-c 
 
175
    /node/test/fixtures/nested-index/one/realpath-c
174
176
      -> /node/test/fixtures/nested-index/two/realpath-c
175
177
    /node/test/fixtures/nested-index/two/realpath-c -> ../../cycles/root.js
176
178
    /node/test/fixtures/cycles/root.js (hard)
188
190
        [fixturesAbsDir+'/nested-index/one/realpath-c', fixturesAbsDir+'/nested-index/two/realpath-c'],
189
191
        [fixturesAbsDir+'/nested-index/two/realpath-c', '../../cycles/root.js'],
190
192
      ].forEach(function(t) {
191
 
        //debug('setting up '+t[0]+' -> '+t[1]);
 
193
        //common.debug('setting up '+t[0]+' -> '+t[1]);
192
194
        try {fs.unlinkSync(t[0]);}catch(e){}
193
195
        fs.symlinkSync(t[1], t[0]);
194
196
        unlink.push(t[0]);
200
202
    assert.equal(fs.realpathSync(entry), expected);
201
203
    asynctest(fs.realpath, [entry], callback, function(err, result){
202
204
      assert.equal(result, expected,
203
 
        'got '+inspect(result)+' expected '+inspect(expected));
 
205
        'got '+common.inspect(result)+' expected '+common.inspect(expected));
204
206
      return true;
205
207
    });
206
208
  });
207
209
}
208
210
 
209
211
function test_non_symlinks(callback) {
210
 
  bashRealpath(fixturesDir, function(err, fixturesAbsDir) {
 
212
  bashRealpath(common.fixturesDir, function(err, fixturesAbsDir) {
211
213
    if (err) return callback(err);
212
214
    var p = fixturesAbsDir.lastIndexOf('/');
213
215
    var entrydir = fixturesAbsDir.substr(0, p);
219
221
    asynctest(fs.realpath, [entry], callback, function(err, result){
220
222
      process.chdir(origcwd);
221
223
      assert.equal(result, expected,
222
 
        'got '+inspect(result)+' expected '+inspect(expected));
 
224
        'got '+common.inspect(result)+' expected '+common.inspect(expected));
223
225
      return true;
224
226
    });
225
227
  });
241
243
function runNextTest(err) {
242
244
  if (err) throw err;
243
245
  var test = tests.shift()
244
 
  if (!test) puts(numtests+' subtests completed OK for fs.realpath');
 
246
  if (!test) console.log(numtests+' subtests completed OK for fs.realpath');
245
247
  else test(runNextTest);
246
248
}
247
249
runNextTest();