@@ -2,33 +2,24 @@
22{-# OPTIONS_GHC -fno-warn-unused-imports #-}
33{-# LANGUAGE ScopedTypeVariables #-}
44
5−-- | To run these tests:
6−--
7−-- * `cabal repl psc-publish`
8−-- * `:l psc-publish/tests/Test.hs`
9−-- * `test`
10−
11−module Test where
5+module TestPscPublish where
126
137import Control.Monad
148import Control.Applicative
159import Control.Exception
1610import System.Process
1711import System.Directory
12+import System.IO
13+import System.Exit
1814import qualified Data.ByteString.Lazy as BL
1915import Data.ByteString.Lazy (ByteString)
2016import qualified Data.Aeson as A
2117import Data.Aeson.BetterErrors
2218import Data.Version
2319
24−import Main
2520import Language.PureScript.Docs
2621import Language.PureScript.Publish
2722
28−pkgName = "purescript-prelude"
29−packageUrl = "https://github.com/purescript/" ++ pkgName
30−packageDir = "tmp/" ++ pkgName
31−
3223pushd :: forall a. FilePath -> IO a -> IO a
3324pushd dir act = do
3425 original <- getCurrentDirectory
@@ -37,44 +28,12 @@ pushd dir act = do
3728 setCurrentDirectory original
3829 either throwIO return result
3930
40−clonePackage :: IO ()
41−clonePackage = do
42− createDirectoryIfMissing True packageDir
43− pushd packageDir $ do
44− exists <- doesDirectoryExist ".git"
45− unless exists $ do
46− putStrLn ("Cloning " ++ pkgName ++ " into " ++ packageDir ++ "...")
47− readProcess "git" ["clone", packageUrl, "."] "" >>= putStr
48− readProcess "git" ["tag", "v999.0.0"] "" >>= putStr
49−
50−bowerInstall :: IO ()
51−bowerInstall =
52− pushd packageDir $
53− readProcess "bower" ["install"] "" >>= putStr
54−
55−testRunOptions :: PublishOptions
56−testRunOptions = defaultPublishOptions
57− { publishGetVersion = return testVersion
58− }
59− where testVersion = ("v999.0.0", Version [999,0,0] [])
60−
61−getPackage :: IO UploadedPackage
62−getPackage = do
63− clonePackage
64− bowerInstall
65− pushd packageDir $ preparePackage testRunOptions
66−
6731data TestResult
6832 = ParseFailed String
6933 | Mismatch ByteString ByteString -- ^ encoding before, encoding after
7034 | Pass ByteString
7135 deriving (Show, Read)
7236
73−-- | Test JSON encoding/decoding; parse the package, roundtrip to/from JSON,
74−-- and check we get the same string.
75−test :: IO TestResult
76−test = roundTrip <$> getPackage
77−
7837roundTrip :: UploadedPackage -> TestResult
7938roundTrip pkg =
8039 let before = A.encode pkg
@@ -85,3 +44,22 @@ roundTrip pkg =
8544 if before == after
8645 then Pass before
8746 else Mismatch before after
47+
48+testRunOptions :: PublishOptions
49+testRunOptions = defaultPublishOptions
50+ { publishGetVersion = return testVersion
51+ }
52+ where testVersion = ("v999.0.0", Version [999,0,0] [])
53+
54+-- | Given a directory which contains a package, produce JSON from it, and then
55+-- | attempt to parse it again, and ensure that it doesn't change.
56+testPackage :: String -> IO ()
57+testPackage dir = do
58+ pushd dir $ do
59+ r <- roundTrip <$> preparePackage testRunOptions
60+ case r of
61+ Pass _ -> pure ()
62+ other -> do
63+ hPutStrLn stderr ("psc-publish tests failed on " ++ dir ++ ":")
64+ hPutStrLn stderr (show other)
65+ exitFailure