Debian Codegraph @

Test/Tasty/Golden/Internal.hs

raw
1 {-# LANGUAGE RankNTypes, ExistentialQuantification, DeriveDataTypeable,
2 MultiParamTypeClasses, GeneralizedNewtypeDeriving, CPP #-}
3 module Test.Tasty.Golden.Internal where
4
5 import Control.DeepSeq
6 import Control.Exception
7 import Control.Monad (when)
8 import Data.Typeable (Typeable)
9 import Data.Proxy
10 import Data.Int
11 import Data.Char (toLower)
12 import System.IO.Error (isDoesNotExistError)
13 import Options.Applicative (metavar)
14 import Test.Tasty.Providers
15 import Test.Tasty.Options
16 #if !MIN_VERSION_base(4,11,0)
17 import Data.Monoid ((<>))
18 #endif
19
20 -- | See 'goldenTest' for explanation of the fields
21 data Golden =
22 forall a .
23 Golden
24 (IO a)
25 (IO a)
26 (a -> a -> IO (Maybe String))
27 (a -> IO ())
28 (IO ())
29 deriving Typeable
30
31 -- | This option, when set to 'True', specifies that we should run in the
32 -- «accept tests» mode
33 newtype AcceptTests = AcceptTests Bool
34 deriving (Eq, Ord, Typeable)
35 instance IsOption AcceptTests where
36 defaultValue = AcceptTests False
37 parseValue = fmap AcceptTests . safeReadBool
38 optionName = return "accept"
39 optionHelp = return "Accept current results of golden tests"
40 optionCLParser = flagCLParser Nothing (AcceptTests True)
41
42 -- | This option, when set to 'True', specifies to error when a file does
43 -- not exist, instead of creating a new file.
44 newtype NoCreateFile = NoCreateFile Bool
45 deriving (Eq, Ord, Typeable)
46 instance IsOption NoCreateFile where
47 defaultValue = NoCreateFile False
48 parseValue = fmap NoCreateFile . safeReadBool
49 optionName = return "no-create"
50 optionHelp = return "Error when golden file does not exist"
51 optionCLParser = flagCLParser Nothing (NoCreateFile True)
52
53 -- | The size, in bytes, such that the (incorrect) test output is not
54 -- displayed when it exceeds this size. Numeric underscores are accepted
55 -- for readability.
56 --
57 -- The default value is 1000 (i.e. 1Kb).
58 --
59 -- @since 2.3.3
60 newtype SizeCutoff = SizeCutoff { getSizeCutoff :: Int64 }
61 deriving (Eq, Ord, Typeable, Num, Real, Enum, Integral)
62 instance IsOption SizeCutoff where
63 defaultValue = 1000
64 showDefaultValue = Just . show . getSizeCutoff
65 parseValue = fmap SizeCutoff . safeRead . filter (/= '_')
66 optionName = return "size-cutoff"
67 optionHelp = return "hide golden test output if it's larger than n bytes"
68 optionCLParser = mkOptionCLParser $ metavar "n"
69
70 -- | When / whether to delete the test output file,
71 -- when there is a golden file
72 --
73 -- @since 2.3.4
74 data DeleteOutputFile
75 = Never -- ^ Never delete the output file (default)
76 | OnPass -- ^ Delete the output file if the test passes
77 | Always -- ^ Always delete the output file. (May not be commonly used,
78 -- but provided for completeness.)
79 deriving (Eq, Ord, Typeable, Show)
80
81 -- | This option controls when / whether the test output file is deleted
82 -- For example, it may be convenient to delete the output file when a test
83 -- passes, since it will be the same as the golden file.
84 --
85 -- It does nothing if
86 --
87 -- * running the test or accessing an existing golden value threw an exception.
88 --
89 -- * there is no golden file for the test
90 instance IsOption DeleteOutputFile where
91 defaultValue = Never
92 parseValue = parseDeleteOutputFile
93 optionName = return "delete-output"
94 optionHelp = return "If there is a golden file, when to delete output files"
95 showDefaultValue = Just . displayDeleteOutputFile
96 optionCLParser = mkOptionCLParser $ metavar "never|onpass|always"
97
98 parseDeleteOutputFile :: String -> Maybe DeleteOutputFile
99 parseDeleteOutputFile s =
100 case map toLower s of
101 "never" -> Just Never
102 "onpass" -> Just OnPass
103 "always" -> Just Always
104 _ -> Nothing
105
106 displayDeleteOutputFile :: DeleteOutputFile -> String
107 displayDeleteOutputFile dof = map toLower (show dof)
108
109 instance IsTest Golden where
110 run opts golden _ = runGolden golden opts
111 testOptions =
112 return
113 [ Option (Proxy :: Proxy AcceptTests)
114 , Option (Proxy :: Proxy NoCreateFile)
115 , Option (Proxy :: Proxy SizeCutoff)
116 , Option (Proxy :: Proxy DeleteOutputFile)
117 ]
118
119 runGolden :: Golden -> OptionSet -> IO Result
120 runGolden (Golden getGolden getTested cmp update delete) opts = do
121
122 mbNew <- try getTested
123
124 case mbNew of
125 Left e -> do
126 return $ testFailed $ show (e :: SomeException)
127 Right new -> do
128
129 mbRef <- try getGolden
130
131 case mbRef of
132 Left e
133 | Just e' <- fromException e, isDoesNotExistError e' ->
134 if noCreate
135 then
136 -- Don't ever delete the output file in this case, as there is
137 -- no duplicate golden file
138 return $ testFailed "Golden file does not exist; --no-create flag specified"
139 else do
140 update new
141 when (delOut `elem` [Always, OnPass]) delete
142 return $ testPassed "Golden file did not exist; created"
143
144 | Just (_ :: AsyncException) <- fromException e -> throwIO e
145 | Just (_ :: IOError) <- fromException e -> throwIO e
146
147
148 | otherwise -> do
149 -- Other types of exceptions may be due to failing to decode the
150 -- golden file. In that case, it makes sense to replace a broken
151 -- golden file with the current version.
152 update new
153 when (delOut `elem` [Always, OnPass]) delete
154 return $ testPassed $ "Accepted the new version. Was failing with exception:\n" ++ show e
155
156 Right ref -> do
157
158 result <- cmp ref new
159
160 case result of
161 Just _reason | accept -> do
162 -- test failed; accept the new version
163 update new
164 when (delOut `elem` [Always, OnPass]) delete
165 return $ testPassed "Accepted the new version"
166
167 Just reason -> do
168 -- Make sure that the result is fully evaluated and doesn't depend
169 -- on yet un-read lazy input
170 evaluate . rnf $ reason
171 when (delOut == Always) delete
172 return $ testFailed reason
173
174 Nothing -> do
175 when (delOut `elem` [Always, OnPass]) delete
176 return $ testPassed ""
177 where
178 AcceptTests accept = lookupOption opts
179 NoCreateFile noCreate = lookupOption opts
180 delOut = lookupOption opts