3
3
{-# LANGUAGE TemplateHaskell #-}
4
4
{-# LANGUAGE ScopedTypeVariables #-}
5
5
6
+ {-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
7
+ {-# HLINT ignore "Avoid restricted function" #-}
8
+
6
9
module Codec.Archive.Tar.Pack.Tests
7
10
( prop_roundtrip
8
11
, unit_roundtrip_unicode
@@ -23,6 +26,7 @@ import Codec.Archive.Tar.Types (GenEntries(..), Entries, simpleEntry, toTarPath,
23
26
import qualified Codec.Archive.Tar.Unpack as Unpack
24
27
import qualified Codec.Archive.Tar.Write as Write
25
28
import Control.Exception
29
+ import qualified Data.List as L
26
30
import Data.List.NonEmpty (NonEmpty (.. ))
27
31
import GHC.IO.Encoding
28
32
import System.Directory
@@ -46,8 +50,8 @@ supportsUnicode = unsafePerformIO $ do
46
50
47
51
-- | Write a single file, deeply buried within nested folders;
48
52
-- 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
51
55
| x : xs <- filter (not . null ) $ map mkFilePath xss
52
56
= ioProperty $ withSystemTempDirectory " tar-test" $ \ baseDir -> do
53
57
file : dirs <- pure $ trimUpToMaxPathLength baseDir (x : xs)
@@ -56,10 +60,17 @@ prop_roundtrip xss cnt
56
60
absDir = baseDir </> relDir
57
61
relFile = relDir </> file
58
62
absFile = absDir </> file
59
- errMsg = " relDir = " ++ relDir ++
60
- " \n absDir = " ++ absDir ++
61
- " \n relFile = " ++ relFile ++
62
- " \n absFile = " ++ 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
+ " \n absDir = '" ++ absDir ++ " '" ++
71
+ " \n relFile = '" ++ relFile ++ " '" ++
72
+ " \n absFile = '" ++ absFile ++ " '" ++
73
+ " \n target = '" ++ target ++ " '"
63
74
64
75
-- Not all filesystems allow paths to contain arbitrary Unicode.
65
76
-- E. g., at the moment of writing Apple FS does not support characters
@@ -72,17 +83,16 @@ prop_roundtrip xss cnt
72
83
case canWriteFile of
73
84
Left (e :: IOException ) -> discard
74
85
Right () -> counterexample errMsg <$> do
75
-
76
86
-- Forcing the result, otherwise lazy IO misbehaves.
77
- ! entries <- Pack. pack baseDir [relFile ] >>= evaluate . force
87
+ ! entries <- Pack. pack baseDir [target ] >>= evaluate . force
78
88
79
89
let fileNames
80
90
= map (map (\ c -> if c == Posix. pathSeparator then pathSeparator else c))
81
91
$ Tar. foldEntries ((:) . entryTarPath) [] undefined
82
92
-- decodeLongNames produces FilePath with POSIX path separators
83
93
$ Tar. decodeLongNames $ foldr Next Done entries
84
94
85
- if [relFile] /= fileNames then pure ([relFile] === fileNames) else do
95
+ if expectedFileNames /= fileNames then pure (expectedFileNames === fileNames) else do
86
96
87
97
-- Try hard to clean up
88
98
removeFile absFile
0 commit comments