test: fix test-fs-symlink-dir-junction-relative
authorBert Belder <bertbelder@gmail.com>
Tue, 9 Dec 2014 20:15:32 +0000 (21:15 +0100)
committerBert Belder <bertbelder@gmail.com>
Tue, 9 Dec 2014 20:16:29 +0000 (21:16 +0100)
  * The test no longer relies on being invoked from a particular
    working directory to function properly.
  * fs.symlink() and fs.symlinkSync() are both tested.
  * The test now cleans up after itself.

This commit fixes https://github.com/iojs/io.js/issues/126

PR-URL: https://github.com/iojs/io.js/pull/129
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
test/simple/test-fs-symlink-dir-junction-relative.js

index d54938a..c598d2e 100644 (file)
 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 
+// Test creating and resolving relative junction or symbolic link
+
 var common = require('../common');
 var assert = require('assert');
 var path = require('path');
 var fs = require('fs');
 var completed = 0;
-var expected_tests = 4;
-
-// test creating and reading symbolic link
-var linkData = path.join(common.fixturesDir, 'cycles');
-var linkPath = path.join(common.tmpDir, 'cycles_link');
-var relative = '../fixtures/cycles';
+var expected_tests = 2;
 
-// Delete previously created link
-try {
-  fs.unlinkSync(linkPath);
-} catch (e) {}
+var linkPath1 = path.join(common.tmpDir, 'junction1');
+var linkPath2 = path.join(common.tmpDir, 'junction2');
+var linkTarget = path.join(common.fixturesDir);
+var linkData = '../fixtures';
 
-console.log('linkData: ' + linkData);
-console.log('linkPath: ' + linkPath);
-console.log('relative target: ' + relative)
+// Prepare.
+try { fs.mkdirSync(common.tmpDir); } catch (e) {}
+try { fs.unlinkSync(linkPath1); } catch (e) {}
+try { fs.unlinkSync(linkPath2); } catch (e) {}
 
-fs.symlink(relative, linkPath, 'junction', function(err) {
+// Test fs.symlink()
+fs.symlink(linkData, linkPath1, 'junction', function(err) {
   if (err) throw err;
-  completed++;
+  verifyLink(linkPath1);
+});
 
-  fs.lstat(linkPath, function(err, stats) {
-    if (err) throw err;
-    assert.ok(stats.isSymbolicLink());
-    completed++;
+// Test fs.symlinkSync()
+fs.symlinkSync(linkData, linkPath2, 'junction');
+verifyLink(linkPath2);
 
-    fs.readlink(linkPath, function(err, destination) {
-      if (err) throw err;
-      assert.equal(path.resolve(destination), linkData);
-      completed++;
+function verifyLink(linkPath) {
+  var stats = fs.lstatSync(linkPath);
+  assert.ok(stats.isSymbolicLink());
 
-      fs.unlink(linkPath, function(err) {
-        if (err) throw err;
-        assert(!fs.existsSync(linkPath));
-        assert(fs.existsSync(linkData));
-        completed++;
-      });
-    });
-  });
-});
+  var data1 = fs.readFileSync(linkPath + '/x.txt', 'ascii');
+  var data2 = fs.readFileSync(linkTarget + '/x.txt', 'ascii');
+  assert.strictEqual(data1, data2);
+
+  // Clean up.
+  fs.unlinkSync(linkPath);
+
+  completed++;
+}
 
 process.on('exit', function() {
   assert.equal(completed, expected_tests);