Skip to content

Commit 4675d01

Browse files
committed
PackageTestMain: factor out successTest
1 parent ac439ee commit 4675d01

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

tests/PackageTestMain.hs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Distribution.Server.Packages.Unpack
1616
import Distribution.Server.Packages.UnpackTest
1717

1818
import Test.Tasty (defaultMain, TestTree, testGroup)
19-
import Test.Tasty.HUnit (testCase, Assertion)
19+
import Test.Tasty.HUnit (testCase, Assertion, HasCallStack)
2020

2121
main :: IO ()
2222
main = defaultMain allTests
@@ -105,15 +105,43 @@ badSpecVer =
105105

106106
missingDirsInTarFileTest :: Assertion
107107
missingDirsInTarFileTest =
108-
do tar <- fmap keepOnlyFiles (tarGzFile "correct-package-0.1.0.0")
109-
now <- getCurrentTime
110-
case unpackPackage now "correct-package-0.1.0.0.tar.gz" tar of
111-
Right _ -> return ()
112-
Left err ->
113-
HUnit.assertFailure ("Expected success but got: " ++ show err)
108+
successTestTGZ pkg =<< do keepOnlyFiles <$> tarGzFile pkg
109+
where
110+
pkg = "correct-package-0.1.0.0"
111+
112+
---------------------------------------------------------------------------
113+
-- * Auxiliary functions to construct tests
114+
115+
-- | A generic successful test, given a directory with the package contents.
116+
--
117+
-- Note: the 'HasCallStack' constraint ensures that the assertion failure
118+
-- is thrown at the invocation site of this function.
119+
--
120+
successTest
121+
:: HasCallStack
122+
=> String -- ^ The directory which is also the package name.
123+
-> Assertion
124+
successTest pkg = successTestTGZ pkg =<< tarGzFile pkg
125+
126+
-- | A successful test, given the package name and its @.tgz@ stream.
127+
--
128+
-- Note: the 'HasCallStack' constraint ensures that the assertion failure
129+
-- is thrown at the invocation site of this function.
130+
--
131+
successTestTGZ
132+
:: HasCallStack
133+
=> String -- ^ The package name which is also the stem of the @.tgz@ file.
134+
-> ByteString -- ^ The content of the @.tgz@ archive.
135+
-> Assertion
136+
successTestTGZ pkg tar = do
137+
now <- getCurrentTime
138+
case unpackPackage now (pkg ++ ".tar.gz") tar of
139+
Right _ -> return ()
140+
Left err ->
141+
HUnit.assertFailure $ "Expected success, but got: " ++ show err
114142

115143
---------------------------------------------------------------------------
116-
-- * Auxiliary functions
144+
-- * Tar utilities
117145

118146
tarGzFile :: String -> IO ByteString
119147
tarGzFile name = do

0 commit comments

Comments
 (0)