-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathRunTests.hs
150 lines (118 loc) · 5.29 KB
/
RunTests.hs
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
{-# OPTIONS -Wall #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE QuasiQuotes #-}
import Data.Foldable (for_)
import Data.List (isInfixOf)
import System.Exit (ExitCode(..))
import qualified System.Process as Process
import Test.Hspec.Core.Spec (SpecM)
import Test.Hspec (hspec, it, describe, runIO, shouldSatisfy, expectationFailure)
main :: IO ()
main = hspec $ do
it "bazel lint" $ do
assertSuccess (bazel ["run", "//:buildifier"])
it "bazel test" $ do
assertSuccess (bazel ["test", "//...", "--build_tests_only"])
it "bazel test prof" $ do
assertSuccess (bazel ["test", "-c", "dbg", "//...", "--build_tests_only"])
describe "repl" $ do
it "for libraries" $ do
-- Test whether building of repl forces all runtime dependencies by itself:
-- TODO(Profpatsch) remove clean once repl uses runfiles
-- because otherwise the test runner succeeds if the first test fails
assertSuccess (bazel ["clean"])
assertSuccess (bazel ["run", "//tests/repl-targets:hs-lib@repl", "--", "-ignore-dot-ghci", "-e", "show (foo 10) ++ bar ++ baz ++ gen"])
assertSuccess (bazel ["run", "//tests/repl-targets:hs-lib-bad@repl", "--", "-ignore-dot-ghci", "-e", "1 + 2"])
it "for binaries" $ do
-- Test whether building of repl forces all runtime dependencies by itself:
-- TODO(Profpatsch) remove clean once repl uses runfiles
assertSuccess (bazel ["clean"])
assertSuccess (bazel ["run", "//tests/repl-targets:hs-bin@repl", "--", "-ignore-dot-ghci", "-e", ":main"])
assertSuccess (bazel ["run", "//tests/binary-indirect-cbits:binary-indirect-cbits@repl", "--", "-ignore-dot-ghci", "-e", ":main"])
-- Test `compiler_flags` from toolchain and rule for REPL
it "compiler flags" $ do
assertSuccess (bazel ["run", "//tests/repl-flags:compiler_flags@repl", "--", "-ignore-dot-ghci", "-e", ":main"])
-- Test `repl_ghci_args` from toolchain and rule for REPL
it "repl flags" $ do
assertSuccess (bazel ["run", "//tests/repl-flags:repl_flags@repl", "--", "-ignore-dot-ghci", "-e", "foo"])
it "startup script" $ do
assertSuccess (safeShell [
"pwd=$(pwd)"
, "cd $(mktemp -d)"
, "$pwd/start"
-- Copy the bazel configuration, this is only useful for CI
, "mkdir tools"
, "cp $pwd/.bazelrc .bazelrc"
-- Set Nixpkgs in environment variable to avoid hardcoding it in
-- start script itself
, "NIX_PATH=nixpkgs=$pwd/nixpkgs/default.nix \
\bazel fetch \
\--config=ci \
\--override_repository=io_tweag_rules_haskell=$pwd \
\//..."
])
describe "failures" $ do
all_failure_tests <- bazelQuery "kind(rule, //tests/failures/...) intersect attr('tags', 'manual', //tests/failures/...)"
for_ all_failure_tests $ \test -> do
it test $ do
assertFailure (bazel ["build", "test"])
-- Test that the repl still works if we shadow some Prelude functions
it "repl name shadowing" $ do
outputSatisfy p (bazel ["run", "//tests/repl-name-conflicts:lib@repl", "--", "-ignore-dot-ghci", "-e", "stdin"])
where
p (stdout, stderr) = not $ any ("error" `isInfixOf`) [stdout, stderr]
-- * Bazel commands
-- | Returns a bazel command line suitable for CI
-- This should be called with the action as first item of the list. e.g 'bazel ["build", "//..."]'.
bazel :: [String] -> Process.CreateProcess
-- Note: --config=ci is intercalated between the action and the list
-- of arguments. It should appears after the action, but before any
-- @--@ following argument.
bazel (command:args) = Process.proc "bazel" (command:"--config=ci":args)
bazel [] = Process.proc "bazel" []
-- | Runs a bazel query and return the list of matching targets
bazelQuery :: String -> SpecM a [String]
bazelQuery q = lines <$> runIO (Process.readProcess "bazel" ["query", q] "")
-- * Action helpers
-- | Ensure that @(stdout, stderr)@ of the command satisfies a predicate
outputSatisfy
:: ((String, String) -> Bool)
-> Process.CreateProcess
-> IO ()
outputSatisfy predicate cmd = do
(exitCode, stdout, stderr) <- Process.readCreateProcessWithExitCode cmd ""
case exitCode of
ExitSuccess -> (stdout, stderr) `shouldSatisfy` predicate
ExitFailure _ -> expectationFailure (formatOutput exitCode stdout stderr)
-- | The command must success
assertSuccess :: Process.CreateProcess -> IO ()
assertSuccess = outputSatisfy (const True)
-- | The command must fail
assertFailure :: Process.CreateProcess -> IO ()
assertFailure cmd = do
(exitCode, stdout, stderr) <- Process.readCreateProcessWithExitCode cmd ""
case exitCode of
ExitFailure _ -> pure ()
ExitSuccess -> expectationFailure ("Unexpected success of a failure test with output:\n" ++ formatOutput exitCode stdout stderr)
-- | Execute in a sub shell the list of command
-- This will fail if any of the command in the list fail
safeShell :: [String] -> Process.CreateProcess
safeShell l = Process.shell (unlines ("set -e":l))
-- * Formatting helpers
formatOutput :: ExitCode -> String -> String -> String
formatOutput exitcode stdout stderr =
let
header = replicate 20 '-'
headerLarge = replicate 20 '='
in unlines [
headerLarge
, "Exit Code: " <> show exitcode
, headerLarge
, "Standard Output"
, header
, stdout
, headerLarge
, "Error Output"
, header
, stderr
, header]