@@ -16,6 +16,7 @@ import System.Exit
16
16
import System.FilePath
17
17
import System.IO
18
18
import System.Process
19
+ import qualified Data.ByteString.Char8 as BS
19
20
20
21
import Test.Haddock.Config
21
22
import Test.Haddock.Process
@@ -95,8 +96,8 @@ checkFile cfg file = do
95
96
hasRef <- doesFileExist $ refFile dcfg file
96
97
if hasRef
97
98
then do
98
- mout <- ccfgRead ccfg file <$> readFile (outFile dcfg file)
99
- mref <- ccfgRead ccfg file <$> readFile (refFile dcfg file)
99
+ mout <- readOut cfg file
100
+ mref <- readRef cfg file
100
101
return $ case (mout, mref) of
101
102
(Just out, Just ref)
102
103
| ccfgEqual ccfg out ref -> Pass
@@ -107,11 +108,34 @@ checkFile cfg file = do
107
108
ccfg = cfgCheckConfig cfg
108
109
dcfg = cfgDirConfig cfg
109
110
111
+ -- We use ByteString here to ensure that no lazy I/O is performed.
112
+ -- This way to ensure that the reference file isn't held open in
113
+ -- case after `diffFile` (which is problematic if we need to rewrite
114
+ -- the reference file in `maybeAcceptFile`)
115
+
116
+ -- | Read the reference artifact for a test
117
+ readRef :: Config c -> FilePath -> IO (Maybe c )
118
+ readRef cfg file =
119
+ ccfgRead ccfg . BS. unpack
120
+ <$> BS. readFile (refFile dcfg file)
121
+ where
122
+ ccfg = cfgCheckConfig cfg
123
+ dcfg = cfgDirConfig cfg
124
+
125
+ -- | Read (and clean) the test output artifact for a test
126
+ readOut :: Config c -> FilePath -> IO (Maybe c )
127
+ readOut cfg file =
128
+ fmap (ccfgClean ccfg file) . ccfgRead ccfg . BS. unpack
129
+ <$> BS. readFile (outFile dcfg file)
130
+ where
131
+ ccfg = cfgCheckConfig cfg
132
+ dcfg = cfgDirConfig cfg
133
+
110
134
111
135
diffFile :: Config c -> FilePath -> FilePath -> IO ()
112
136
diffFile cfg diff file = do
113
- Just out <- ccfgRead ccfg file <$> readFile (outFile dcfg file)
114
- Just ref <- ccfgRead ccfg file <$> readFile (refFile dcfg file)
137
+ Just out <- readOut cfg file
138
+ Just ref <- readRef cfg file
115
139
writeFile outFile' $ ccfgDump ccfg out
116
140
writeFile refFile' $ ccfgDump ccfg ref
117
141
@@ -130,10 +154,14 @@ diffFile cfg diff file = do
130
154
131
155
132
156
maybeAcceptFile :: Config c -> FilePath -> CheckResult -> IO CheckResult
133
- maybeAcceptFile cfg@ ( Config { cfgDirConfig = dcfg }) file result
157
+ maybeAcceptFile cfg file result
134
158
| cfgAccept cfg && result `elem` [NoRef , Fail ] = do
135
- copyFile' (outFile dcfg file) (refFile dcfg file)
159
+ Just out <- readOut cfg file
160
+ writeFile (refFile dcfg file) $ ccfgDump ccfg out
136
161
pure Accepted
162
+ where
163
+ dcfg = cfgDirConfig cfg
164
+ ccfg = cfgCheckConfig cfg
137
165
maybeAcceptFile _ _ result = pure result
138
166
139
167
0 commit comments