Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
reflex-frp
GitHub Repository: reflex-frp/reflex-platform
Path: blob/develop/scripts/test.hs
1 views
1
{-# LANGUAGE OverloadedStrings #-}
2
-- | This script provides basic tests for the try-reflex functionality; it should be run before committing code to important branches, such as 'develop'
3
module Main where
4
5
import Test.Hspec
6
import Shelly
7
import Control.Monad
8
import Data.Monoid
9
import Data.String
10
import qualified Data.Text as T
11
12
repos = pwd
13
root = pwd
14
scripts = fmap (</> ("scripts" :: String)) pwd
15
16
main :: IO ()
17
main = hspec $ parallel $ do
18
let silently = id -- Temporarily disable 'silently'
19
describe "try-reflex" $ do
20
-- Test that the try-reflex shell is able to build a simple "Hello, world!" application with both ghc and ghcjs
21
forM_ ["ghc", "ghcjs"] $ \platform -> do
22
it ("can build hello world with " <> platform) $ do
23
shelly $ silently $ do
24
r <- root
25
os <- T.stripEnd <$> run "uname" ["-s"]
26
withTmpDir $ \tmp -> do
27
cd tmp
28
let helloFilename = "hello.hs"
29
flags = if os == "Darwin" then " -dynamic" else ""
30
writefile (fromText helloFilename) "{-# LANGUAGE OverloadedStrings #-}\nimport Reflex.Dom\nmain = mainWidget $ text \"Hello, world!\""
31
run (r </> ("try-reflex" :: String)) ["--pure", "--command", fromString platform <> flags <> " " <> helloFilename <> " ; exit $?"] -- The "exit $?" will no longer be needed when we can assume users will have this patch: https://github.com/NixOS/nix/commit/7ba0e9cb481f00baca02f31393ad49681fc48a5d
32
return () :: IO ()
33
describe "readme" $ do
34
forM_ ["ghc", "ghcjs"] $ \platform -> do
35
forM_ [0..8] $ \i -> do
36
it ("snippet_" <> show i <> " can be built by " <> platform) $ do
37
shelly $ silently $ do
38
os <- T.stripEnd <$> run "uname" ["-s"]
39
d <- pwd
40
let flags = if os == "Darwin" then " -dynamic" else ""
41
filename = "README.lhs" :: String
42
withTmpDir $ \tmp -> do
43
cp (d </> filename) tmp
44
cd tmp
45
run (d </> ("try-reflex" :: String)) ["--pure", "--command", fromString platform <> flags <> " README.lhs -cpp -DSNIPPET_" <> fromString (show i)]
46
return () :: IO ()
47
describe "work-on" $ do
48
-- Test that the work-on shell can build the core reflex libraries in a variety of configurations
49
forM_ ["ghc", "ghcjs"] $ \platform -> do
50
forM_ ["reflex", "reflex-todomvc"] $ \package -> do
51
forM_ [False, True] $ \workOnPath -> do
52
it ("can build " <> package <> " with " <> platform <> " by importing the " <> (if workOnPath then "package" else "path")) $ do
53
shelly $ silently $ do
54
r <- repos
55
s <- scripts
56
withTmpDir $ \tmp -> do
57
cp_r (r </> package) (tmp </> package)
58
cd tmp
59
run "chmod" ["-R", "u+w", "."]
60
run "git" ["init"]
61
run "git" ["add", "-A"]
62
run "git" ["commit", "-m", "Initial commit"]
63
run (s </> ("hack-on" :: String)) [T.pack package]
64
cd $ fromString package
65
let packageSpec = if workOnPath then "./." else fromString package
66
run (s </> ("work-on" :: String)) [fromString platform, packageSpec, "--pure", "--command", "cabal configure" <> (if platform == "ghcjs" then " --ghcjs" else "") <> " ; exit $?"] -- The "exit $?" will no longer be needed when we can assume users will have this patch: https://github.com/NixOS/nix/commit/7ba0e9cb481f00baca02f31393ad49681fc48a5d
67
return () :: IO ()
68
let checkThatRepoIsNotAlreadyBeingHackedOn repo = shelly $ silently $ do
69
d <- pwd
70
gitNixExists <- test_e $ d </> repo </> ("git.json" :: String)
71
githubNixExists <- test_e $ d </> repo </> ("github.json" :: String)
72
let instructions = "; to test hack-on, please ensure that " <> show repo <> " is in a clean, not-being-hacked-on state"
73
when (not $ or [gitNixExists, githubNixExists]) $ fail $ show (repo </> ("{git,github}.json" :: String)) <> " does not exist" <> instructions
74
dotGitExists <- test_d $ d </> repo </> (".git" :: String)
75
when dotGitExists $ fail $ show (repo </> (".git" :: String)) <> " exists" <> instructions
76
return ()
77
describe "hack-on" $ do
78
forM_ ["nixpkgs", "reflex", "reflex-dom", "reflex-todomvc"] $ \repo -> do
79
before_ (checkThatRepoIsNotAlreadyBeingHackedOn repo) $ do
80
let withSetup a = do
81
shelly $ silently $ do
82
s <- scripts
83
r <- repos
84
withTmpDir $ \tmp -> do
85
cd tmp
86
cp_r (r </> repo) tmp
87
run "git" ["init"]
88
run "git" ["add", "-A"]
89
run "git" ["commit", "-m", "Initial commit"]
90
a s tmp
91
return () :: IO ()
92
writefileTest filename = withSetup $ \s tmp -> do
93
let fileToChange = repo </> (filename :: String)
94
contents = "test"
95
writefile fileToChange contents
96
True <- (liftM (const False) $ run (s </> ("hack-on" :: String)) [fromString repo]) `catchany_sh` (\_ -> return True)
97
newContents <- readfile fileToChange
98
when (newContents /= contents) $ fail $ "hack-on changed the contents of " <> show fileToChange
99
return ()
100
it ("won't trample changes in " <> repo) $ writefileTest "default.nix" -- default.nix is an already-existing file
101
it ("won't trample new files in " <> repo) $ writefileTest "test" -- test is a non-existing file
102
it ("can checkout " <> repo) $ withSetup $ \d tmp -> do
103
run (d </> ("hack-on" :: String)) [fromString repo]
104
False <- or <$> sequence
105
[ test_e $ tmp </> repo </> ("git.json" :: String)
106
, test_e $ tmp </> repo </> ("github.json" :: String)
107
]
108
True <- test_d $ tmp </> repo </> (".git" :: String)
109
return ()
110
describe "hack-off" $ do
111
let existingFilename = "test"
112
newFilename = "new"
113
ignoredFilename = "ignored"
114
writefileTest filename = do
115
shelly $ silently $ flip catchany_sh (\_ -> return True) $ liftM (const False) $ do
116
s <- scripts
117
withTmpDir $ \tmp1 -> withTmpDir $ \tmp2 -> do
118
cd tmp1
119
run "git" ["init"]
120
writefile (fromText existingFilename) "originalcontents"
121
writefile ".gitignore" ignoredFilename
122
run "git" ["add", existingFilename, ".gitignore"]
123
run "git" ["commit", "-m", "Initial commit"]
124
run "git" ["clone", toTextArg tmp1, toTextArg tmp2]
125
writefile (tmp2 </> fromText filename) "testcontents"
126
run (s </> ("hack-off" :: String)) [toTextArg tmp2]
127
return () :: IO ()
128
it "won't trample changes" $ writefileTest existingFilename
129
it "won't trample new files" $ writefileTest newFilename
130
it "won't trample new files even if they are in the .gitignore" $ writefileTest ignoredFilename
131
describe "shell.nix" $ do
132
it "can be entered using a bare nix-shell" $ do
133
shelly $ silently $ do
134
root >>= cd
135
run "nix-shell" []
136
return () :: IO ()
137
describe "benchmark" $ do
138
it "can build and run reflex-dom benchmarks" $ do
139
shelly $ silently $ do
140
s <- scripts
141
run (s </> ("benchmark" :: String)) []
142
return () :: IO ()
143
144