Skip to content

Commit 9f832d6

Browse files
committed
Improve test for pack roundtrip
1 parent 99f124e commit 9f832d6

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

test/Codec/Archive/Tar/Pack/Tests.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
{-# LANGUAGE TemplateHaskell #-}
44
{-# LANGUAGE ScopedTypeVariables #-}
55

6+
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
7+
{-# HLINT ignore "Avoid restricted function" #-}
8+
69
module Codec.Archive.Tar.Pack.Tests
710
( prop_roundtrip
811
, unit_roundtrip_unicode
@@ -23,6 +26,7 @@ import Codec.Archive.Tar.Types (GenEntries(..), Entries, simpleEntry, toTarPath,
2326
import qualified Codec.Archive.Tar.Unpack as Unpack
2427
import qualified Codec.Archive.Tar.Write as Write
2528
import Control.Exception
29+
import qualified Data.List as L
2630
import Data.List.NonEmpty (NonEmpty(..))
2731
import GHC.IO.Encoding
2832
import System.Directory
@@ -46,8 +50,8 @@ supportsUnicode = unsafePerformIO $ do
4650

4751
-- | Write a single file, deeply buried within nested folders;
4852
-- pack and unpack; read back and compare results.
49-
prop_roundtrip :: [String] -> String -> Property
50-
prop_roundtrip xss cnt
53+
prop_roundtrip :: Int -> [String] -> String -> Property
54+
prop_roundtrip n' xss cnt
5155
| x : xs <- filter (not . null) $ map mkFilePath xss
5256
= ioProperty $ withSystemTempDirectory "tar-test" $ \baseDir -> do
5357
file : dirs <- pure $ trimUpToMaxPathLength baseDir (x : xs)
@@ -56,10 +60,17 @@ prop_roundtrip xss cnt
5660
absDir = baseDir </> relDir
5761
relFile = relDir </> file
5862
absFile = absDir </> file
59-
errMsg = "relDir = " ++ relDir ++
60-
"\nabsDir = " ++ absDir ++
61-
"\nrelFile = " ++ relFile ++
62-
"\nabsFile = " ++ absFile
63+
n = n' `mod` (length dirs + 1)
64+
(target, expectedFileNames) = case n of
65+
0 -> (relFile, [relFile])
66+
_ -> (joinPath $ take (n - 1) dirs,
67+
map (addTrailingPathSeparator . joinPath)
68+
(drop (max 1 (n - 1)) $ L.inits dirs) ++ [relFile])
69+
errMsg = "relDir = '" ++ relDir ++ "'" ++
70+
"\nabsDir = '" ++ absDir ++ "'" ++
71+
"\nrelFile = '" ++ relFile ++ "'" ++
72+
"\nabsFile = '" ++ absFile ++ "'" ++
73+
"\ntarget = '" ++ target ++ "'"
6374

6475
-- Not all filesystems allow paths to contain arbitrary Unicode.
6576
-- E. g., at the moment of writing Apple FS does not support characters
@@ -72,17 +83,16 @@ prop_roundtrip xss cnt
7283
case canWriteFile of
7384
Left (e :: IOException) -> discard
7485
Right () -> counterexample errMsg <$> do
75-
7686
-- Forcing the result, otherwise lazy IO misbehaves.
77-
!entries <- Pack.pack baseDir [relFile] >>= evaluate . force
87+
!entries <- Pack.pack baseDir [target] >>= evaluate . force
7888

7989
let fileNames
8090
= map (map (\c -> if c == Posix.pathSeparator then pathSeparator else c))
8191
$ Tar.foldEntries ((:) . entryTarPath) [] undefined
8292
-- decodeLongNames produces FilePath with POSIX path separators
8393
$ Tar.decodeLongNames $ foldr Next Done entries
8494

85-
if [relFile] /= fileNames then pure ([relFile] === fileNames) else do
95+
if expectedFileNames /= fileNames then pure (expectedFileNames === fileNames) else do
8696

8797
-- Try hard to clean up
8898
removeFile absFile

0 commit comments

Comments
 (0)