1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE TemplateHaskell #-}
module Main (main) where
import Control.Monad
import Control.Monad.Catch
import Control.Monad.IO.Class (MonadIO (..))
import Data.Bifunctor
import Data.List (sort)
import Path
import Path.IO
import System.Environment
import System.PosixCompat.Files
import Test.Hspec
main :: IO ()
main = hspec . around withSandbox $ do
{- ORMOLU_DISABLE -}
#ifndef mingw32_HOST_OS
beforeWith populatedDir $ do
-- NOTE These tests fail on Windows as unix-compat does not implement
-- createSymbolicLink for windows.
describe "listDir" listDirSpec
describe "listDirRel" listDirRelSpec
describe "listDirRecur" listDirRecurSpec
describe "listDirRecurRel" listDirRecurRelSpec
describe "listDirRecurWith" listDirRecurWithSpec
describe "walkDir Finish" walkDirFinishSpec
describe "copyDirRecur" copyDirRecurSpec
describe "copyDirRecur'" copyDirRecur'Spec
describe "findFile" findFileSpec
describe "removeDirLink" removeDirLinkSpec
beforeWith populatedCyclicDir $
describe "listDirRecur Cyclic" listDirRecurCyclicSpec
#endif
describe "getCurrentDir" getCurrentDirSpec
describe "setCurrentDir" setCurrentDirSpec
describe "withCurrentDir" withCurrentDirSpec
describe "walkDirRel" walkDirRelSpec
#ifndef mingw32_HOST_OS
-- NOTE We can't quite test this on Windows as well, because the
-- environmental variables HOME and TMPDIR do not exist there.
describe "getHomeDir" getHomeDirSpec
describe "getTempDir" getTempDirSpec
describe "getXdgDir Data" getXdgDataDirSpec
describe "getXdgDir Config" getXdgConfigDirSpec
describe "getXdgDir Cache" getXdgCacheDirSpec
#endif
{- ORMOLU_ENABLE -}
listDirSpec :: SpecWith (Path Abs Dir)
listDirSpec = it "lists directory" $ \dir ->
getDirStructure listDir dir `shouldReturn` populatedDirTop
listDirRelSpec :: SpecWith (Path Abs Dir)
listDirRelSpec = it "lists directory" $ \dir ->
getDirStructureRel listDirRel dir `shouldReturn` populatedDirTop
listDirRecurSpec :: SpecWith (Path Abs Dir)
listDirRecurSpec = it "lists directory recursively" $ \dir ->
getDirStructure listDirRecur dir `shouldReturn` populatedDirStructure
listDirRecurRelSpec :: SpecWith (Path Abs Dir)
listDirRecurRelSpec = it "lists directory recursively" $ \dir ->
getDirStructureRel listDirRecurRel dir `shouldReturn` populatedDirStructure
listDirRecurWithSpec :: SpecWith (Path Abs Dir)
listDirRecurWithSpec =
it "lists directory recursively using predicates" $ \dir ->
getDirStructure
( listDirRecurWith
(return . ($(mkRelDir "c") /=) . dirname)
(return . ($(mkRelFile "two.txt") /=) . filename)
)
dir
`shouldReturn` populatedDirRecurWith
listDirRecurWith ::
-- | Dir match predicate
(Path Abs Dir -> IO Bool) ->
-- | File match predicate
(Path Abs File -> IO Bool) ->
-- | Top dir to traverse
Path Abs Dir ->
-- | Matched subdirs and files
IO ([Path Abs Dir], [Path Abs File])
listDirRecurWith dirPred filePred =
walkDirAccum Nothing $ \_ d f -> do
d' <- filterM dirPred d
f' <- filterM filePred f
return (d', f')
-- | 'walkDir' with a 'WalkFinish' handler may have unpredictable output
-- depending on the order of traversal. The only guarantee is that we will
-- finish only after we find the directory "c". Though if we test only for
-- the presence of "c" we are not really testing if we indeed cut the
-- traversal short.
walkDirFinishSpec :: SpecWith (Path Abs Dir)
walkDirFinishSpec =
it "Finishes only after finding what it is looking for" $ \dir -> do
(d, _) <- getDirStructure (walkDirAccum (Just dHandler) writer) dir
map dirname d `shouldContain` [$(mkRelDir "c")]
where
dHandler p _ _
| dirname p == $(mkRelDir "c") = return WalkFinish
| otherwise = return (WalkExclude [])
writer _ d f = return (d, f)
copyDirRecurSpec :: SpecWith (Path Abs Dir)
copyDirRecurSpec = do
context "when source directory is editable" $
it "copies directory" $ \src -> do
let dest = parent src </> $(mkRelDir "copied-dir")
copyDirRecur src dest
old <- getDirStructure listDirRecur src
new <- getDirStructure listDirRecur dest
old `shouldBe` new
context "when source directory is read-only" $
it "copies directory just as well (preserving permissions)" $ \src -> do
let dest = parent src </> $(mkRelDir "copied-dir")
srcPermissions <- setOwnerWritable False <$> getPermissions src
setPermissions src srcPermissions
copyDirRecur src dest
old <- getDirStructure listDirRecur src
new <- getDirStructure listDirRecur dest
old `shouldBe` new
getPermissions dest `shouldReturn` srcPermissions
copyDirRecur'Spec :: SpecWith (Path Abs Dir)
copyDirRecur'Spec =
context "when source directory is read-only" $
it "copies directory but now it's editable" $ \src -> do
let dest = parent src </> $(mkRelDir "copied-dir")
srcPermissions <- setOwnerWritable False <$> getPermissions src
setPermissions src srcPermissions
copyDirRecur' src dest
old <- getDirStructure listDirRecur src
new <- getDirStructure listDirRecur dest
old `shouldBe` new
getPermissions dest `shouldReturn` srcPermissions {writable = True}
findFileSpec :: SpecWith (Path Abs Dir)
findFileSpec = it "finds a file lazily" $ \dir -> do
let relFile = head (snd populatedDirTop)
found <- findFile (dir : undefined) relFile
found `shouldBe` Just (dir </> relFile)
removeDirLinkSpec :: SpecWith (Path Abs Dir)
removeDirLinkSpec = it "remove dir link" $ \dir -> do
let target = dir </> $(mkRelDir "a")
link = dir </> $(mkRelDir "link-a")
createDirLink target link
removeDirLink link
exists <- doesDirExist link
exists `shouldBe` False
listDirRecurCyclicSpec :: SpecWith (Path Abs Dir)
listDirRecurCyclicSpec =
it "lists directory trees having traversal cycles" $ \dir ->
getDirStructure listDirRecurCyclic dir
`shouldReturn` populatedCyclicDirStructure
-- | Follows symbolic links.
listDirRecurCyclic ::
(MonadIO m) =>
-- | Directory to list
Path b Dir ->
-- | Sub-directories and files
m ([Path Abs Dir], [Path Abs File])
listDirRecurCyclic = walkDirAccum Nothing (\_ d f -> return (d, f))
getCurrentDirSpec :: SpecWith (Path Abs Dir)
getCurrentDirSpec = it "returns current dir" $ \dir ->
getCurrentDir `shouldNotReturn` dir
setCurrentDirSpec :: SpecWith (Path Abs Dir)
setCurrentDirSpec = it "sets current dir" $ \dir -> do
wdir <- getCurrentDir
setCurrentDir dir
new <- getCurrentDir
setCurrentDir wdir
new `shouldBe` dir
withCurrentDirSpec :: SpecWith (Path Abs Dir)
withCurrentDirSpec = it "temporarily modifies current dir" $ \dir -> do
withCurrentDir dir $
getCurrentDir `shouldReturn` dir
getCurrentDir `shouldNotReturn` dir
walkDirRelSpec :: SpecWith (Path Abs Dir)
walkDirRelSpec = it "does not throw exceptions" $ \dir -> do
let handler curdir subdirs files = do
curdir `shouldBe` $(mkRelDir ".")
subdirs `shouldBe` []
files `shouldBe` []
return WalkFinish
walkDirRel handler dir
getHomeDirSpec :: SpecWith (Path Abs Dir)
getHomeDirSpec =
it "home dir is influenced by environment variable HOME" $ \dir ->
bracket (getEnv evar) (setEnv evar) $ \_ -> do
setEnv evar (toFilePath dir)
getHomeDir `shouldReturn` dir
where
evar = "HOME"
getTempDirSpec :: SpecWith (Path Abs Dir)
getTempDirSpec =
it "temp dir is influenced by environment variable TMPDIR" $ \dir ->
flip finally (unsetEnv evar) $ do
setEnv evar (toFilePath dir)
getTempDir `shouldReturn` dir
unsetEnv evar
where
evar = "TMPDIR"
getXdgDataDirSpec :: SpecWith (Path Abs Dir)
getXdgDataDirSpec =
it "XDG data dir is influenced by environment variable XDG_DATA_HOME" $ \dir ->
flip finally (unsetEnv evar) $ do
setEnv evar (toFilePath dir)
getXdgDir XdgData (Just name) `shouldReturn` (dir </> name)
getXdgDir XdgData Nothing `shouldReturn` dir
unsetEnv evar
where
evar = "XDG_DATA_HOME"
name = $(mkRelDir "test")
getXdgConfigDirSpec :: SpecWith (Path Abs Dir)
getXdgConfigDirSpec =
it "XDG config dir is influenced by environment variable XDG_CONFIG_HOME" $ \dir ->
flip finally (unsetEnv evar) $ do
setEnv evar (toFilePath dir)
getXdgDir XdgConfig (Just name) `shouldReturn` (dir </> name)
getXdgDir XdgConfig Nothing `shouldReturn` dir
unsetEnv evar
where
evar = "XDG_CONFIG_HOME"
name = $(mkRelDir "test")
getXdgCacheDirSpec :: SpecWith (Path Abs Dir)
getXdgCacheDirSpec =
it "XDG cache dir is influenced by environment variable XDG_CACHE_HOME" $ \dir ->
flip finally (unsetEnv evar) $ do
setEnv evar (toFilePath dir)
getXdgDir XdgCache (Just name) `shouldReturn` (dir </> name)
getXdgDir XdgCache Nothing `shouldReturn` dir
unsetEnv evar
where
evar = "XDG_CACHE_HOME"
name = $(mkRelDir "test")
----------------------------------------------------------------------------
-- Helpers
-- | Create a sandbox directory to model some situation in it and run some
-- tests. Note that we're using a new unique sandbox directory for each test
-- case and it's unconditionally deleted after test case finishes.
withSandbox :: ActionWith (Path Abs Dir) -> IO ()
withSandbox = withSystemTempDir "path-io-sandbox"
-- | Create directory and some sub-directories and files in it. Return path
-- to that directory.
--
-- Created objects are described in 'populatedDirStructure'.
populatedDir :: Path Abs Dir -> IO (Path Abs Dir)
populatedDir root = do
let (_, files) = populatedDirStructure
pdir = root </> $(mkRelDir "pdir")
withinSandbox = (pdir </>)
ensureDir pdir
let b = withinSandbox $(mkRelDir "b")
ensureDir b
ensureDir $ withinSandbox $(mkRelDir "b/c")
-- Create a read-only directory with a file inside to test that the code
-- that copies directory permissions can handle that gracefully.
--
-- See: https://github.com/mrkkrp/path-io/pull/82
let readonlyDir = withinSandbox $(mkRelDir "readonly-dir")
ensureDir readonlyDir
-- We should not list b's tree under 'a' in order to verify that we do not
-- follow symbolic links.
createSymbolicLink "b" (toFilePath $ withinSandbox $(mkRelFile "a"))
forM_ files $ (`writeFile` "") . toFilePath . withinSandbox
getPermissions readonlyDir
>>= setPermissions readonlyDir . setOwnerWritable False
return pdir
-- | Get the inner structure of a directory. Items are sorted, so it's
-- easier to compare results.
getDirStructure ::
-- | Which function to use for scanning
(Path Abs Dir -> IO ([Path Abs Dir], [Path Abs File])) ->
-- | Path to directory to scan
Path Abs Dir ->
IO ([Path Rel Dir], [Path Rel File])
getDirStructure f path = do
(dirs, files) <- f path
rdirs <- sort <$> mapM (makeRelative path) dirs
rfiles <- sort <$> mapM (makeRelative path) files
return (rdirs, rfiles)
-- | A version of 'getDirStructure' that accepts scanning function that
-- returns relative paths.
getDirStructureRel ::
-- | Which function to use for scanning
(Path Abs Dir -> IO ([Path Rel Dir], [Path Rel File])) ->
-- | Path to directory to scan
Path Abs Dir ->
IO ([Path Rel Dir], [Path Rel File])
getDirStructureRel f path = bimap sort sort <$> f path
-- | Structure of directory created by the 'populatedDir' function. Please
-- keep it sorted.
populatedDirStructure :: ([Path Rel Dir], [Path Rel File])
populatedDirStructure =
( [ $(mkRelDir "a"),
$(mkRelDir "b"),
$(mkRelDir "b/c"),
$(mkRelDir "readonly-dir")
],
[ $(mkRelFile "b/c/three.txt"),
$(mkRelFile "b/two.txt"),
$(mkRelFile "one.txt"),
$(mkRelFile "readonly-dir/two.txt")
]
)
-- | Create a directory structure which has cycles in it due to directory
-- symbolic links.
--
-- 1) Mutual cycles between two directory trees. If we traverse @a@ or @c@ we
-- will get into the same cycle:
-- a\/(b -> c), c\/(d -> a)
-- c\/(d -> a), a\/(b -> c)
-- 2) Cycle with own ancestor
-- e\/f\/(g -> e)
populatedCyclicDirStructure :: ([Path Rel Dir], [Path Rel File])
populatedCyclicDirStructure =
( [ $(mkRelDir "a"),
$(mkRelDir "a/b"), -- b points to c
$(mkRelDir "a/b/d"), -- because b is same as c
$(mkRelDir "c"),
$(mkRelDir "c/d"), -- d points to a
$(mkRelDir "c/d/b"), -- because d is same as a
$(mkRelDir "e"),
$(mkRelDir "e/f"),
$(mkRelDir "e/f/g") -- g points to e
],
[]
)
-- | Created the objects described in 'populatedCyclicDirStructure'.
-- Return path to that directory.
populatedCyclicDir :: Path Abs Dir -> IO (Path Abs Dir)
populatedCyclicDir root = do
let pdir = root </> $(mkRelDir "pdir")
withinSandbox = (pdir </>)
ensureDir pdir
ensureDir $ withinSandbox $(mkRelDir "a")
ensureDir $ withinSandbox $(mkRelDir "c")
ensureDir $ withinSandbox $(mkRelDir "e/f")
createSymbolicLink "../c" (toFilePath $ withinSandbox $(mkRelFile "a/b"))
createSymbolicLink "../a" (toFilePath $ withinSandbox $(mkRelFile "c/d"))
createSymbolicLink "../../e" (toFilePath $ withinSandbox $(mkRelFile "e/f/g"))
return pdir
-- | Top-level structure of the populated directory as it should be scanned
-- by the 'listDir' function.
populatedDirTop :: ([Path Rel Dir], [Path Rel File])
populatedDirTop =
( [ $(mkRelDir "a"),
$(mkRelDir "b"),
$(mkRelDir "readonly-dir")
],
[ $(mkRelFile "one.txt")
]
)
-- | Structure of the populated directory as it should be scanned by
-- 'listDirRecurWith' function using predicates to filter out dir 'c' and
-- the file @two.txt@.
populatedDirRecurWith :: ([Path Rel Dir], [Path Rel File])
populatedDirRecurWith =
( [ $(mkRelDir "a"),
$(mkRelDir "b"),
$(mkRelDir "readonly-dir")
],
[ $(mkRelFile "a/c/three.txt"), -- via a symbolic link
$(mkRelFile "b/c/three.txt"),
$(mkRelFile "one.txt")
]
)
|