-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test pack-unpack with long file names
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
|
||
module Codec.Archive.Tar.Pack.Tests | ||
( prop_roundtrip | ||
) where | ||
|
||
import qualified Codec.Archive.Tar.Pack as Pack | ||
import Codec.Archive.Tar.Types (Entries(..)) | ||
import qualified Codec.Archive.Tar.Unpack as Unpack | ||
import Control.Exception | ||
import System.Directory | ||
import System.FilePath | ||
import System.IO.Temp | ||
import Test.Tasty.QuickCheck | ||
|
||
-- | Write a single file, deeply buried within nested folders; | ||
-- pack and unpack; read back and compare results. | ||
prop_roundtrip :: [ASCIIString] -> String -> Property | ||
prop_roundtrip xss cnt | ||
| file : dirs <- filter (not . null) $ map mkFilePath xss | ||
-- Filenames longer than 1024 characters throw | ||
-- "withFile: invalid argument (File name too long)", | ||
-- at least on Mac OS | ||
, length (joinPath dirs </> file) < 900 | ||
= ioProperty $ withSystemTempDirectory "tar-test" $ \baseDir -> do | ||
let relDir = joinPath dirs | ||
absDir = baseDir </> relDir | ||
relFile = relDir </> file | ||
absFile = absDir </> file | ||
createDirectoryIfMissing True absDir | ||
writeFile absFile cnt | ||
-- Forcing the result, otherwise lazy IO misbehaves. | ||
!entries <- Pack.pack baseDir [relFile] | ||
|
||
-- Try hard to clean up | ||
removeFile absFile | ||
writeFile absFile "<should be overwritten>" | ||
case dirs of | ||
[] -> pure () | ||
d : _ -> removeDirectoryRecursive (baseDir </> d) | ||
|
||
-- Unpack back | ||
Unpack.unpack baseDir (foldr Next Done entries :: Entries IOException) | ||
cnt' <- readFile absFile | ||
pure $ cnt === cnt' | ||
|
||
| otherwise = discard | ||
|
||
mkFilePath :: ASCIIString -> FilePath | ||
mkFilePath (ASCIIString xs) = makeValid $ | ||
filter (\c -> not $ isPathSeparator c || c == '.') xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters