From db649798efa387c44c623b5070702ab0bbcb3389 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Fri, 9 Aug 2013 12:18:57 -0700 Subject: [PATCH] Ignore missing symlinks in fsUtils.traverseTreeSync() These were previously throwing an error from the call to statSync(). Now statSync() is wrapped in a try block and only performed when lstatSync() reports a symbolic link. --- spec/stdlib/fs-utils-spec.coffee | 9 +++++++++ src/stdlib/fs-utils.coffee | 7 +++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/stdlib/fs-utils-spec.coffee b/spec/stdlib/fs-utils-spec.coffee index 33f452436..3fd48d17d 100644 --- a/spec/stdlib/fs-utils-spec.coffee +++ b/spec/stdlib/fs-utils-spec.coffee @@ -1,6 +1,7 @@ fsUtils = require 'fs-utils' fs = require 'fs' path = require 'path' +temp = require 'temp' describe "fsUtils", -> describe ".read(path)", -> @@ -82,6 +83,14 @@ describe "fsUtils", -> expect(symlinkPaths).toEqual(paths) + it "ignores missing symlinks", -> + directory = temp.mkdirSync('symlink-in-here') + paths = [] + onPath = (childPath) -> paths.push(childPath) + fs.symlinkSync(path.join(directory, 'source'), path.join(directory, 'destination')) + fsUtils.traverseTreeSync(directory, onPath) + expect(paths.length).toBe 0 + describe ".md5ForPath(path)", -> it "returns the MD5 hash of the file at the given path", -> expect(fsUtils.md5ForPath(require.resolve('fixtures/sample.js'))).toBe 'dd38087d0d7e3e4802a6d3f9b9745f2b' diff --git a/src/stdlib/fs-utils.coffee b/src/stdlib/fs-utils.coffee index 271380754..77a32b62c 100644 --- a/src/stdlib/fs-utils.coffee +++ b/src/stdlib/fs-utils.coffee @@ -147,13 +147,16 @@ module.exports = makeTree: (directoryPath) -> mkdirp.sync(directoryPath) if directoryPath and not @exists(directoryPath) - traverseTreeSync: (rootPath, onFile, onDirectory) -> + traverseTreeSync: (rootPath, onFile, onDirectory=onFile) -> return unless @isDirectorySync(rootPath) traverse = (directoryPath, onFile, onDirectory) -> for file in fs.readdirSync(directoryPath) childPath = path.join(directoryPath, file) - stats = fs.statSync(childPath) + stats = fs.lstatSync(childPath) + if stats.isSymbolicLink() + try + stats = fs.statSync(childPath) if stats.isDirectory() traverse(childPath, onFile, onDirectory) if onDirectory(childPath) else if stats.isFile()