From e10ed097cb3b506009ab28af4dec93402aa48693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Ma=C5=82ecki?= Date: Sat, 21 Jan 2012 02:37:57 +0100 Subject: [PATCH] path fs: move `path.exists*` to `fs.exists*` `path.exists*` functions show a deprecation warning and call functions from `fs`. They should be removed later. test: fix references to `path.exists*` in tests test fs: add test for `fs.exists` and `fs.existsSync` doc: reflect moving `path.exists*` to `fs` --- doc/api/fs.markdown | 14 ++++++++++++ doc/api/path.markdown | 16 ------------- lib/fs.js | 15 +++++++++++++ lib/path.js | 14 +++++------- test/simple/test-fs-exists.js | 42 +++++++++++++++++++++++++++++++++++ test/simple/test-fs-mkdir.js | 7 +++--- test/simple/test-path.js | 3 --- 7 files changed, 79 insertions(+), 32 deletions(-) create mode 100644 test/simple/test-fs-exists.js diff --git a/doc/api/fs.markdown b/doc/api/fs.markdown index b520d7f983d..62c40b3d91d 100644 --- a/doc/api/fs.markdown +++ b/doc/api/fs.markdown @@ -464,6 +464,20 @@ callback, and have some fallback logic if it is null. } }); +### fs.exists(p, [callback]) + +Test whether or not the given path exists by checking with the file system. +Then call the `callback` argument with either true or false. Example: + + fs.exists('/etc/passwd', function (exists) { + util.debug(exists ? "it's there" : "no passwd!"); + }); + + +### fs.existsSync(p) + +Synchronous version of `fs.exists`. + ## fs.Stats Objects returned from `fs.stat()`, `fs.lstat()` and `fs.fstat()` and their diff --git a/doc/api/path.markdown b/doc/api/path.markdown index ba48bd29c8e..5c33adb4f07 100644 --- a/doc/api/path.markdown +++ b/doc/api/path.markdown @@ -4,9 +4,6 @@ This module contains utilities for handling and transforming file paths. Almost all these methods perform only string transformations. The file system is not consulted to check whether paths are valid. -`path.exists` and `path.existsSync` are the exceptions, and should -logically be found in the fs module as they do access the file system. - Use `require('path')` to use this module. The following methods are provided: ### path.normalize(p) @@ -140,16 +137,3 @@ an empty string. Examples: // returns '' -### path.exists(p, [callback]) - -Test whether or not the given path exists by checking with the file system. -Then call the `callback` argument with either true or false. Example: - - path.exists('/etc/passwd', function (exists) { - util.debug(exists ? "it's there" : "no passwd!"); - }); - - -### path.existsSync(p) - -Synchronous version of `path.exists`. diff --git a/lib/fs.js b/lib/fs.js index 51637c13a64..2dc11a750d0 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -71,6 +71,21 @@ fs.Stats.prototype.isSocket = function() { return this._checkModeProperty(constants.S_IFSOCK); }; +fs.exists = function(path, callback) { + binding.stat(path, function(err, stats) { + if (callback) callback(err ? false : true); + }); +}; + +fs.existsSync = function(path) { + try { + binding.stat(path); + return true; + } catch (e) { + return false; + } +}; + fs.readFile = function(path, encoding_) { var encoding = typeof(encoding_) === 'string' ? encoding_ : null; var callback = arguments[arguments.length - 1]; diff --git a/lib/path.js b/lib/path.js index b70225b1d62..6a744dfeca7 100644 --- a/lib/path.js +++ b/lib/path.js @@ -21,6 +21,7 @@ var isWindows = process.platform === 'win32'; +var _deprecationWarning = require('util')._deprecationWarning; // resolves . and .. elements in a path array with directory names there @@ -402,19 +403,14 @@ exports.extname = function(path) { exports.exists = function(path, callback) { - process.binding('fs').stat(path, function(err, stats) { - if (callback) callback(err ? false : true); - }); + _deprecationWarning('path', '`path.exists` is now called `fs.exists`'); + require('fs').exists(path, callback); }; exports.existsSync = function(path) { - try { - process.binding('fs').stat(path); - return true; - } catch (e) { - return false; - } + _deprecationWarning('path', '`path.exists` is now called `fs.exists`'); + return require('fs').existsSync(path); }; diff --git a/test/simple/test-fs-exists.js b/test/simple/test-fs-exists.js new file mode 100644 index 00000000000..cf785ccdb12 --- /dev/null +++ b/test/simple/test-fs-exists.js @@ -0,0 +1,42 @@ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var assert = require('assert'); +var fs = require('fs'); +var f = __filename; +var exists; +var doesNotExist; + +fs.exists(f, function(y) { + exists = y; +}); + +fs.exists(f + '-NO', function (y) { + doesNotExist = y; +}); + +assert(fs.existsSync(f)); +assert(!fs.existsSync(f + '-NO')); + +process.on('exit', function () { + assert.strictEqual(exists, true); + assert.strictEqual(doesNotExist, false); +}); diff --git a/test/simple/test-fs-mkdir.js b/test/simple/test-fs-mkdir.js index 324c08a5f91..9d00b1e75a0 100644 --- a/test/simple/test-fs-mkdir.js +++ b/test/simple/test-fs-mkdir.js @@ -21,7 +21,6 @@ var common = require('../common'); var assert = require('assert'); -var path = require('path'); var fs = require('fs'); function unlink(pathname) { @@ -39,7 +38,7 @@ function unlink(pathname) { fs.mkdir(pathname, function(err) { assert.equal(err, null); - assert.equal(path.existsSync(pathname), true); + assert.equal(fs.existsSync(pathname), true); ncalls++; }); @@ -57,7 +56,7 @@ function unlink(pathname) { fs.mkdir(pathname, 511 /*=0777*/, function(err) { assert.equal(err, null); - assert.equal(path.existsSync(pathname), true); + assert.equal(fs.existsSync(pathname), true); ncalls++; }); @@ -73,7 +72,7 @@ function unlink(pathname) { unlink(pathname); fs.mkdirSync(pathname); - var exists = path.existsSync(pathname); + var exists = fs.existsSync(pathname); unlink(pathname); assert.equal(exists, true); diff --git a/test/simple/test-path.js b/test/simple/test-path.js index e16b459d924..c60230ab814 100644 --- a/test/simple/test-path.js +++ b/test/simple/test-path.js @@ -76,9 +76,6 @@ if (isWindows) { '\\\\unc\\share\\foo\\bar'); } -path.exists(f, function(y) { assert.equal(y, true) }); - -assert.equal(path.existsSync(f), true); assert.equal(path.extname(''), ''); assert.equal(path.extname('/path/to/file'), '');