[commit: packages/directory] master: Fixes inconsistent results from calling canonicalizePath on an empty string (d1d3528)

git at git.haskell.org git at git.haskell.org
Thu Mar 19 11:37:34 UTC 2015


Repository : ssh://git@git.haskell.org/directory

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/d1d35287c2b154e15f8f1587e35dd147f15c1ee9/directory

>---------------------------------------------------------------

commit d1d35287c2b154e15f8f1587e35dd147f15c1ee9
Author: Elliot Robinson <elliot.robinson at argiopetech.com>
Date:   Sun Feb 22 14:48:50 2015 -0500

    Fixes inconsistent results from calling canonicalizePath on an empty string
    
    Calling canonicalizePath on an empty string now explicitly calls canonicalizePath on the current directory.
    
    This should fix #21 for all platforms.


>---------------------------------------------------------------

d1d35287c2b154e15f8f1587e35dd147f15c1ee9
 System/Directory.hs                                |  7 +++++-
 tests/all.T                                        |  1 +
 tests/canonicalizePath001.hs                       | 28 ++++++++++++++++++++++
 ...yExist001.stdout => canonicalizePath001.stdout} |  0
 4 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/System/Directory.hs b/System/Directory.hs
index 6ed772e..0a24e11 100644
--- a/System/Directory.hs
+++ b/System/Directory.hs
@@ -736,7 +736,11 @@ copyFile fromFPath toFPath =
 -- on paths that do not exist is known to vary from platform
 -- to platform. Some platforms do not alter the input, some
 -- do, and on some an exception will be thrown.
+--
+-- If passed an empty string, behaviour is equivalent to
+-- calling canonicalizePath on the current directory.
 canonicalizePath :: FilePath -> IO FilePath
+canonicalizePath ""    = canonicalizePath "."
 canonicalizePath fpath =
 #if defined(mingw32_HOST_OS)
          do path <- Win32.getFullPathName fpath
@@ -744,7 +748,8 @@ canonicalizePath fpath =
   do enc <- getFileSystemEncoding
      GHC.withCString enc fpath $ \pInPath ->
        allocaBytes long_path_size $ \pOutPath ->
-         do _ <- throwErrnoPathIfNull "canonicalizePath" fpath $ c_realpath pInPath pOutPath
+         do _ <-c_realpath pInPath pOutPath
+
             -- NB: pOutPath will be passed thru as result pointer by c_realpath
             path <- GHC.peekCString enc pOutPath
 #endif
diff --git a/tests/all.T b/tests/all.T
index 4efd688..a29a5d8 100644
--- a/tests/all.T
+++ b/tests/all.T
@@ -1,3 +1,4 @@
+test('canonicalizePath001',     normal, compile_and_run, [''])
 test('currentDirectory001',     normal, compile_and_run, [''])
 test('directory001',            normal, compile_and_run, [''])
 test('doesDirectoryExist001',   normal, compile_and_run, [''])
diff --git a/tests/canonicalizePath001.hs b/tests/canonicalizePath001.hs
new file mode 100644
index 0000000..48108a4
--- /dev/null
+++ b/tests/canonicalizePath001.hs
@@ -0,0 +1,28 @@
+module Main(main) where
+
+import Control.Concurrent
+import Control.Monad
+import Control.Exception
+import System.Directory
+import System.FilePath
+import System.IO.Error
+
+main = do
+  dot <- canonicalizePath "."
+  nul <- (canonicalizePath "")
+           `catch` ((\_ -> return "") :: IOException -> IO String)
+  print $ dot == nul
+
+report :: Show a => IO a -> IO ()
+report io = do
+  r <- try io
+  case r of
+   Left e  -> print (e :: SomeException)
+   Right a -> print a
+
+ignore :: IO a -> IO ()
+ignore io = do
+  r <- try io
+  case r of
+   Left e  -> let _ = e :: SomeException in return ()
+   Right a -> return ()
diff --git a/tests/doesDirectoryExist001.stdout b/tests/canonicalizePath001.stdout
similarity index 100%
copy from tests/doesDirectoryExist001.stdout
copy to tests/canonicalizePath001.stdout



More information about the ghc-commits mailing list