From 4a4d9ecf66bcb6561f7b10c07742c4b6688332b8 Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Thu, 13 Aug 2015 12:21:45 +0200 Subject: Refactor HTML test suite boilerplate to external package. --- haddock-test/src/Test/Haddock/Utils.hs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 haddock-test/src/Test/Haddock/Utils.hs (limited to 'haddock-test/src/Test/Haddock/Utils.hs') diff --git a/haddock-test/src/Test/Haddock/Utils.hs b/haddock-test/src/Test/Haddock/Utils.hs new file mode 100644 index 00000000..1d57107f --- /dev/null +++ b/haddock-test/src/Test/Haddock/Utils.hs @@ -0,0 +1,8 @@ +module Test.Haddock.Utils where + + +import Data.Maybe + + +mlast :: [a] -> Maybe a +mlast = listToMaybe . reverse -- cgit v1.2.3 From 163da5a4b6268de54594e18f69f06799df637305 Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Tue, 18 Aug 2015 18:06:59 +0200 Subject: Create utility function for recursive obtaining directory contents. --- haddock-test/src/Test/Haddock/Utils.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'haddock-test/src/Test/Haddock/Utils.hs') diff --git a/haddock-test/src/Test/Haddock/Utils.hs b/haddock-test/src/Test/Haddock/Utils.hs index 1d57107f..4640fe97 100644 --- a/haddock-test/src/Test/Haddock/Utils.hs +++ b/haddock-test/src/Test/Haddock/Utils.hs @@ -1,8 +1,33 @@ module Test.Haddock.Utils where +import Control.Monad + import Data.Maybe +import System.Directory +import System.FilePath + mlast :: [a] -> Maybe a mlast = listToMaybe . reverse + + +partitionM :: Monad m => (a -> m Bool) -> [a] -> m ([a], [a]) +partitionM _ [] = pure ([], []) +partitionM p (x:xs) = do + (ss, fs) <- partitionM p xs + b <- p x + pure $ if b then (x:ss, fs) else (ss, x:fs) + + +getDirectoryTree :: FilePath -> IO [FilePath] +getDirectoryTree path = do + (dirs, files) <- partitionM isDirectory =<< contents + subfiles <- fmap concat . forM dirs $ \dir -> + map (dir ) <$> getDirectoryTree (path dir) + pure $ files ++ subfiles + where + contents = filter realEntry <$> getDirectoryContents path + isDirectory entry = doesDirectoryExist $ path entry + realEntry entry = not $ entry == "." || entry == ".." -- cgit v1.2.3 From 863d33c4d125e13f87193802f6d4faed38da24db Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Tue, 18 Aug 2015 23:42:25 +0200 Subject: Fix bug with unnecessary checking old test output. --- haddock-test/src/Test/Haddock.hs | 4 +++- haddock-test/src/Test/Haddock/Utils.hs | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'haddock-test/src/Test/Haddock/Utils.hs') diff --git a/haddock-test/src/Test/Haddock.hs b/haddock-test/src/Test/Haddock.hs index ab6ce775..18ae38ca 100644 --- a/haddock-test/src/Test/Haddock.hs +++ b/haddock-test/src/Test/Haddock.hs @@ -69,9 +69,11 @@ maybeDiff cfg@(Config { cfgDiffTool = (Just diff) }) files = do runHaddock :: Config c -> IO () -runHaddock (Config { .. }) = do +runHaddock cfg@(Config { .. }) = do haddockStdOut <- openFile cfgHaddockStdOut WriteMode + createEmptyDirectory $ cfgOutDir cfg + putStrLn "Generating documentation..." forM_ cfgPackages $ \tpkg -> do handle <- runProcess' cfgHaddockPath $ processConfig diff --git a/haddock-test/src/Test/Haddock/Utils.hs b/haddock-test/src/Test/Haddock/Utils.hs index 4640fe97..4f97fa72 100644 --- a/haddock-test/src/Test/Haddock/Utils.hs +++ b/haddock-test/src/Test/Haddock/Utils.hs @@ -21,6 +21,10 @@ partitionM p (x:xs) = do pure $ if b then (x:ss, fs) else (ss, x:fs) +whenM :: Monad m => m Bool -> m () -> m () +whenM mb action = mb >>= \b -> when b action + + getDirectoryTree :: FilePath -> IO [FilePath] getDirectoryTree path = do (dirs, files) <- partitionM isDirectory =<< contents @@ -31,3 +35,9 @@ getDirectoryTree path = do contents = filter realEntry <$> getDirectoryContents path isDirectory entry = doesDirectoryExist $ path entry realEntry entry = not $ entry == "." || entry == ".." + + +createEmptyDirectory :: FilePath -> IO () +createEmptyDirectory path = do + whenM (doesDirectoryExist path) $ removeDirectoryRecursive path + createDirectory path -- cgit v1.2.3 From 136c48c5fe074ac8a2755c4705d555de24e22a3a Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Mon, 24 Aug 2015 23:09:20 +0200 Subject: Fix bug with accepting to non-existing directory. --- haddock-test/src/Test/Haddock.hs | 2 +- haddock-test/src/Test/Haddock/Utils.hs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'haddock-test/src/Test/Haddock/Utils.hs') diff --git a/haddock-test/src/Test/Haddock.hs b/haddock-test/src/Test/Haddock.hs index 87c16739..e8a0ac8e 100644 --- a/haddock-test/src/Test/Haddock.hs +++ b/haddock-test/src/Test/Haddock.hs @@ -132,7 +132,7 @@ diffFile cfg diff file = do maybeAcceptFile :: Config c -> FilePath -> CheckResult -> IO CheckResult maybeAcceptFile cfg@(Config { cfgDirConfig = dcfg }) file result | cfgAccept cfg && result `elem` [NoRef, Fail] = do - copyFile (outFile dcfg file) (refFile dcfg file) + copyFile' (outFile dcfg file) (refFile dcfg file) pure Accepted maybeAcceptFile _ _ result = pure result diff --git a/haddock-test/src/Test/Haddock/Utils.hs b/haddock-test/src/Test/Haddock/Utils.hs index 4f97fa72..a947fea1 100644 --- a/haddock-test/src/Test/Haddock/Utils.hs +++ b/haddock-test/src/Test/Haddock/Utils.hs @@ -41,3 +41,10 @@ createEmptyDirectory :: FilePath -> IO () createEmptyDirectory path = do whenM (doesDirectoryExist path) $ removeDirectoryRecursive path createDirectory path + + +-- | Just like 'copyFile' but output directory path is not required to exist. +copyFile' :: FilePath -> FilePath -> IO () +copyFile' old new = do + createDirectoryIfMissing True $ takeDirectory new + copyFile old new -- cgit v1.2.3