Skip to content

Commit 4ca91ad

Browse files
committed
testsuite: Rework handling of output sanitization
Previously un-cleaned artifacts were kept as reference output, making it difficult to tell what has changed and causing spurious changes in the version control history. Here we rework this, cleaning the output during acceptance. To accomplish this it was necessary to move to strict I/O to ensure the reference handle was closed before accept attempts to open the reference file.
1 parent 25b8d00 commit 4ca91ad

File tree

7 files changed

+48
-12
lines changed

7 files changed

+48
-12
lines changed

haddock-test/haddock-test.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ library
1616
default-language: Haskell2010
1717
ghc-options: -Wall
1818
hs-source-dirs: src
19-
build-depends: base, directory, process, filepath, Cabal, xml, xhtml, syb
19+
build-depends: base, bytestring, directory, process, filepath, Cabal, xml, xhtml, syb
2020

2121
exposed-modules:
2222
Test.Haddock

haddock-test/src/Test/Haddock.hs

+34-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import System.Exit
1616
import System.FilePath
1717
import System.IO
1818
import System.Process
19+
import qualified Data.ByteString.Char8 as BS
1920

2021
import Test.Haddock.Config
2122
import Test.Haddock.Process
@@ -95,8 +96,8 @@ checkFile cfg file = do
9596
hasRef <- doesFileExist $ refFile dcfg file
9697
if hasRef
9798
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
100101
return $ case (mout, mref) of
101102
(Just out, Just ref)
102103
| ccfgEqual ccfg out ref -> Pass
@@ -107,11 +108,34 @@ checkFile cfg file = do
107108
ccfg = cfgCheckConfig cfg
108109
dcfg = cfgDirConfig cfg
109110

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+
110134

111135
diffFile :: Config c -> FilePath -> FilePath -> IO ()
112136
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
115139
writeFile outFile' $ ccfgDump ccfg out
116140
writeFile refFile' $ ccfgDump ccfg ref
117141

@@ -130,10 +154,14 @@ diffFile cfg diff file = do
130154

131155

132156
maybeAcceptFile :: Config c -> FilePath -> CheckResult -> IO CheckResult
133-
maybeAcceptFile cfg@(Config { cfgDirConfig = dcfg }) file result
157+
maybeAcceptFile cfg file result
134158
| 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
136161
pure Accepted
162+
where
163+
dcfg = cfgDirConfig cfg
164+
ccfg = cfgCheckConfig cfg
137165
maybeAcceptFile _ _ result = pure result
138166

139167

haddock-test/src/Test/Haddock/Config.hs

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ data TestPackage = TestPackage
4242

4343

4444
data CheckConfig c = CheckConfig
45-
{ ccfgRead :: String -> String -> Maybe c
45+
{ ccfgRead :: String -> Maybe c
46+
-- ^ @f contents@ parses file contents @contents@ to
47+
-- produce a thing to be compared.
48+
, ccfgClean :: String -> c -> c
49+
-- ^ @f fname x@ cleans @x@ to such that it can be compared
4650
, ccfgDump :: c -> String
4751
, ccfgEqual :: c -> c -> Bool
4852
}

hoogle-test/Main.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Test.Haddock
99

1010
checkConfig :: CheckConfig String
1111
checkConfig = CheckConfig
12-
{ ccfgRead = \_ input -> Just input
12+
{ ccfgRead = Just
13+
, ccfgClean = \_ -> id
1314
, ccfgDump = id
1415
, ccfgEqual = (==)
1516
}

html-test/Main.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import Test.Haddock.Xhtml
1212

1313
checkConfig :: CheckConfig Xml
1414
checkConfig = CheckConfig
15-
{ ccfgRead = \mdl input -> stripIfRequired mdl <$> parseXml input
15+
{ ccfgRead = parseXml
16+
, ccfgClean = stripIfRequired
1617
, ccfgDump = dumpXml
1718
, ccfgEqual = (==)
1819
}

hypsrc-test/Main.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import Test.Haddock.Xhtml
1313

1414
checkConfig :: CheckConfig Xml
1515
checkConfig = CheckConfig
16-
{ ccfgRead = \_ input -> strip <$> parseXml input
16+
{ ccfgRead = parseXml
17+
, ccfgClean = \_ -> strip
1718
, ccfgDump = dumpXml
1819
, ccfgEqual = (==)
1920
}

latex-test/Main.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import Test.Haddock
99

1010
checkConfig :: CheckConfig String
1111
checkConfig = CheckConfig
12-
{ ccfgRead = \_ input -> Just input
12+
{ ccfgRead = Just
13+
, ccfgClean = \_ -> id
1314
, ccfgDump = id
1415
, ccfgEqual = (==)
1516
}

0 commit comments

Comments
 (0)