forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPscPublish.hs
More file actions
120 lines (106 loc) · 4.38 KB
/
TestPscPublish.hs
File metadata and controls
120 lines (106 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module TestPscPublish where
import Prelude
import Control.Exception (tryJust)
import Control.Monad (void, guard)
import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Lazy (ByteString)
import Data.Time.Clock (getCurrentTime)
import Data.Aeson qualified as A
import Data.Version (Version(..))
import Data.Foldable (forM_)
import Text.PrettyPrint.Boxes qualified as Boxes
import System.Directory (listDirectory, removeDirectoryRecursive)
import System.FilePath ((</>))
import System.IO.Error (isDoesNotExistError)
import Language.PureScript.Docs (UploadedPackage, VerifiedPackage)
import Language.PureScript.Publish (PublishOptions(..), defaultPublishOptions)
import Language.PureScript.Publish qualified as Publish
import Language.PureScript.Publish.ErrorsWarnings qualified as Publish
import Test.Hspec (Expectation, Spec, context, expectationFailure, it, runIO)
import TestUtils (pushd)
spec :: Spec
spec = do
context "preparePackage with json roundtrips" $ do
it "purescript-prelude" $ do
testPackage
"tests/support/bower_components/purescript-prelude"
"bower.json"
"../../prelude-resolutions.json"
it "basic example (bower.json)" $ do
testPackage
"tests/purs/publish/basic-example"
"bower.json"
"resolutions.json"
it "basic example (purs.json)" $ do
testPackage
"tests/purs/publish/basic-example"
"purs.json"
"resolutions.json"
context "json compatibility" $ do
let compatDir = "tests" </> "json-compat"
versions <- runIO $ listDirectory compatDir
forM_ versions $ \version -> do
context ("json produced by " ++ version) $ do
files <- runIO $ listDirectory (compatDir </> version)
forM_ files $ \file -> do
it file $ do
result <- A.eitherDecodeFileStrict' (compatDir </> version </> file)
case result of
Right (_ :: VerifiedPackage) ->
pure ()
Left err ->
expectationFailure ("JSON parsing failed: " ++ err)
data TestResult
= ParseFailed String
| Mismatch ByteString ByteString -- ^ encoding before, encoding after
| Pass ByteString
deriving (Show)
roundTrip :: UploadedPackage -> TestResult
roundTrip pkg =
let before' = A.encode pkg
in case A.eitherDecode before' of
Left err -> ParseFailed err
Right parsed -> do
let after' = A.encode (parsed :: UploadedPackage)
if before' == after'
then Pass before'
else Mismatch before' after'
testRunOptions :: FilePath -> FilePath -> PublishOptions
testRunOptions manifestFile resolutionsFile = defaultPublishOptions
{ publishResolutionsFile = resolutionsFile
, publishManifestFile = manifestFile
, publishGetVersion = return testVersion
, publishGetTagTime = const (liftIO getCurrentTime)
, publishWorkingTreeDirty = return ()
}
where testVersion = ("v999.0.0", Version [999,0,0] [])
-- | Given a directory which contains a package, produce JSON from it, and then
-- | attempt to parse it again, and ensure that it doesn't change.
testPackage :: FilePath -> FilePath -> FilePath -> Expectation
testPackage packageDir manifestFile resolutionsFile = do
res <- preparePackage packageDir manifestFile resolutionsFile
case res of
Left err ->
expectationFailure $
"Failed to produce JSON from " ++ packageDir ++ ":\n" ++
Boxes.render (Publish.renderError err)
Right package ->
case roundTrip package of
Pass _ ->
pure ()
ParseFailed msg ->
expectationFailure ("Failed to re-parse: " ++ msg)
Mismatch _ _ ->
expectationFailure "JSON did not match"
-- A version of Publish.preparePackage suitable for use in tests. We remove the
-- output directory each time to ensure that we are actually testing the docs
-- code in the working tree as it is now (as opposed to how it was at some
-- point in the past when the tests were previously successfully run).
preparePackage :: FilePath -> FilePath -> FilePath -> IO (Either Publish.PackageError UploadedPackage)
preparePackage packageDir manifestFile resolutionsFile =
pushd packageDir $ do
removeDirectoryRecursiveIfPresent "output"
Publish.preparePackage (testRunOptions manifestFile resolutionsFile)
removeDirectoryRecursiveIfPresent :: FilePath -> IO ()
removeDirectoryRecursiveIfPresent =
void . tryJust (guard . isDoesNotExistError) . removeDirectoryRecursive