foliage/tests/Foliage/Tests/Tar.hs
Andrea Bedini cbd0c5da8f
Minimal test suite (#81)
* Minimal test suite

- Add support for urls with file: schema; both absolute (file:/path) and
  relative (file:path) paths are supported.

- Log curl invocation in case of failure

- Rename fetchRemoteAsset to fetchURL

- Add verbosity flag

- Bump GHC to 9.4.7

- Bump flake inputs

* Apply suggestions from code review

Co-authored-by: Michael Peyton Jones <me@michaelpj.com>

* Add short option '-v' for '--verbosity'

* Whitespace

* Add comment explaining why the dot

* Rename withFixture to inTemporaryDirectoryWithFixture

* Small refactor of PrepareSource

* Rename TarballSource to URISource

- Move sourceUrl to Foliage.Meta.packageVersionSourceToUri

* Simplify inTemporaryDirectoryWithFixture

* Document tar and cp flags

* Reformat

---------

Co-authored-by: Michael Peyton Jones <me@michaelpj.com>
2023-09-15 12:18:30 +08:00

33 lines
1.1 KiB
Haskell

module Foliage.Tests.Tar where
import Codec.Archive.Tar qualified as Tar
import Codec.Archive.Tar.Entry (Entry (..))
import Codec.Archive.Tar.Index (TarIndexEntry (..))
import Codec.Archive.Tar.Index qualified as Tar
import Control.Monad (when)
import Data.ByteString.Lazy qualified as B
import System.IO
import Test.Tasty.HUnit
newtype TarballAccessFn = TarballAccessFn
{ lookupEntry :: FilePath -> IO (Maybe Entry)
}
withTarball :: (HasCallStack) => FilePath -> (TarballAccessFn -> IO r) -> IO r
withTarball path action = do
eIdx <- Tar.build . Tar.read <$> B.readFile path
case eIdx of
Left err ->
assertFailure (show err)
Right idx ->
withFile path ReadMode $ \handle -> do
hIsClosed handle >>= flip when (putStrLn "[after reading] it's closed!")
let lookupEntry :: FilePath -> IO (Maybe Entry)
lookupEntry filepath =
case Tar.lookup idx filepath of
Just (TarFileEntry offset) -> do
Just <$> Tar.hReadEntry handle offset
_otherwise ->
return Nothing
action $ TarballAccessFn{lookupEntry}