summaryrefslogtreecommitdiff
path: root/tests/TestUtils.hs
blob: 3e04f69168d3e0772d87ad535769a0523fb72112 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{-# LANGUAGE ScopedTypeVariables #-}

module TestUtils where

import Prelude ()
import Prelude.Compat

import Control.Monad
import Control.Monad.Trans.Maybe
import Control.Exception

import System.Process
import System.Directory
import System.Info
import System.Exit (exitFailure)
import System.IO (stderr, hPutStrLn)

findNodeProcess :: IO (Maybe String)
findNodeProcess = runMaybeT . msum $ map (MaybeT . findExecutable) names
  where
  names = ["nodejs", "node"]

-- |
-- Fetches code necessary to run the tests with. The resulting support code
-- should then be checked in, so that npm/bower etc is not required to run the
-- tests.
--
-- Simply rerun this (via ghci is probably easiest) when the support code needs
-- updating.
--
updateSupportCode :: IO ()
updateSupportCode = do
  setCurrentDirectory "tests/support"
  if System.Info.os == "mingw32"
    then callProcess "setup-win.cmd" []
    else do
      callProcess "npm" ["install"]
      -- bower uses shebang "/usr/bin/env node", but we might have nodejs
      node <- maybe cannotFindNode pure =<< findNodeProcess
      -- Sometimes we run as a root (e.g. in simple docker containers)
      -- And we are non-interactive: https://github.com/bower/bower/issues/1162
      callProcess node ["node_modules/.bin/bower", "--allow-root", "install", "--config.interactive=false"]
  setCurrentDirectory "../.."
  where
  cannotFindNode :: IO a
  cannotFindNode = do
    hPutStrLn stderr "Cannot find node (or nodejs) executable"
    exitFailure

-- |
-- The support modules that should be cached between test cases, to avoid
-- excessive rebuilding.
--
supportModules :: [String]
supportModules =
  [ "Control.Alt"
  , "Control.Alternative"
  , "Control.Applicative"
  , "Control.Apply"
  , "Control.Biapplicative"
  , "Control.Biapply"
  , "Control.Bind"
  , "Control.Category"
  , "Control.Comonad"
  , "Control.Extend"
  , "Control.Lazy"
  , "Control.Monad"
  , "Control.Monad.Eff"
  , "Control.Monad.Eff.Class"
  , "Control.Monad.Eff.Console"
  , "Control.Monad.Eff.Unsafe"
  , "Control.Monad.Rec.Class"
  , "Control.Monad.ST"
  , "Control.MonadPlus"
  , "Control.MonadZero"
  , "Control.Plus"
  , "Control.Semigroupoid"
  , "Data.Array"
  , "Data.Array.Partial"
  , "Data.Array.ST"
  , "Data.Array.ST.Iterator"
  , "Data.Bifoldable"
  , "Data.Bifunctor"
  , "Data.Bifunctor.Clown"
  , "Data.Bifunctor.Flip"
  , "Data.Bifunctor.Join"
  , "Data.Bifunctor.Joker"
  , "Data.Bifunctor.Product"
  , "Data.Bifunctor.Wrap"
  , "Data.Bitraversable"
  , "Data.Boolean"
  , "Data.BooleanAlgebra"
  , "Data.Bounded"
  , "Data.Char"
  , "Data.CommutativeRing"
  , "Data.Either"
  , "Data.Either.Nested"
  , "Data.Eq"
  , "Data.EuclideanRing"
  , "Data.Field"
  , "Data.Foldable"
  , "Data.Function"
  , "Data.Function.Uncurried"
  , "Data.Functor"
  , "Data.Functor.Invariant"
  , "Data.Generic"
  , "Data.Generic.Rep"
  , "Data.Generic.Rep.Bounded"
  , "Data.Generic.Rep.Eq"
  , "Data.Generic.Rep.Monoid"
  , "Data.Generic.Rep.Ord"
  , "Data.Generic.Rep.Semigroup"
  , "Data.Generic.Rep.Show"
  , "Data.HeytingAlgebra"
  , "Data.Identity"
  , "Data.Maybe"
  , "Data.Maybe.First"
  , "Data.Maybe.Last"
  , "Data.Monoid"
  , "Data.Monoid.Additive"
  , "Data.Monoid.Alternate"
  , "Data.Monoid.Conj"
  , "Data.Monoid.Disj"
  , "Data.Monoid.Dual"
  , "Data.Monoid.Endo"
  , "Data.Monoid.Multiplicative"
  , "Data.NaturalTransformation"
  , "Data.Newtype"
  , "Data.NonEmpty"
  , "Data.Ord"
  , "Data.Ord.Unsafe"
  , "Data.Ordering"
  , "Data.Ring"
  , "Data.Semigroup"
  , "Data.Semiring"
  , "Data.Show"
  , "Data.String"
  , "Data.String.CaseInsensitive"
  , "Data.String.Regex"
  , "Data.String.Regex.Flags"
  , "Data.String.Regex.Unsafe"
  , "Data.String.Unsafe"
  , "Data.Symbol"
  , "Data.Traversable"
  , "Data.Tuple"
  , "Data.Tuple.Nested"
  , "Data.Unfoldable"
  , "Data.Unit"
  , "Data.Void"
  , "PSCI.Support"
  , "Partial"
  , "Partial.Unsafe"
  , "Prelude"
  , "Test.Assert"
  , "Type.Data.Ordering"
  , "Type.Data.Symbol"
  , "Type.Equality"
  , "Type.Prelude"
  , "Type.Proxy"
  , "Unsafe.Coerce"
  ]

pushd :: forall a. FilePath -> IO a -> IO a
pushd dir act = do
  original <- getCurrentDirectory
  setCurrentDirectory dir
  result <- try act :: IO (Either IOException a)
  setCurrentDirectory original
  either throwIO return result