From 8ff8f4a543ce0d648ff111f021bb186ec28b6d68 Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Thu, 12 Jan 2023 16:55:43 +0530 Subject: [PATCH 1/2] Use temp directory --- app/Main.hs | 26 +++++++++++++++----------- markdown-doctest.cabal | 2 ++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 5bb2eb7..d535d52 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -7,6 +7,8 @@ import Data.List (find, isPrefixOf) import Streamly.Data.Fold (Fold) import Streamly.Data.Parser (Parser) import System.Environment (getArgs) +import System.IO.Temp (withSystemTempDirectory) +import System.FilePath (()) import qualified Language.Haskell.Ghcid as G import qualified Streamly.Data.Fold as Fold @@ -225,17 +227,19 @@ loopCmdREPL sess (ln:lns) = do loopCmdFile :: G.Ghci -> [(Int, String)] -> IO Bool loopCmdFile _ [] = putStrLn "All good" >> return True -loopCmdFile sess (ln:lns) = do - let padding = replicate (fst ln - 1) '\n' - writeFile ("interpreted.hs") (padding ++ snd ln) - putStrLn (snd ln) - res <- G.exec sess ":load interpreted.hs" - if or (map hasErrorFile res) - then do - mapM_ putStrLn res - putStrLn ("Error at: " ++ show (fst ln)) - return False - else loopCmdFile sess lns +loopCmdFile sess (ln:lns) = + withSystemTempDirectory "docTest" $ \fp -> do + let padding = replicate (fst ln - 1) '\n' + tgt = fp "interpreted.hs" + writeFile tgt (padding ++ snd ln) + putStrLn (snd ln) + res <- G.exec sess (":load " ++ show tgt) + if or (map hasErrorFile res) + then do + mapM_ putStrLn res + putStrLn ("Error at: " ++ show (fst ln)) + return False + else loopCmdFile sess lns ghciSetup :: [String] ghciSetup = [":set -fobject-code"] diff --git a/markdown-doctest.cabal b/markdown-doctest.cabal index 71b07e8..639783a 100644 --- a/markdown-doctest.cabal +++ b/markdown-doctest.cabal @@ -47,5 +47,7 @@ executable markdown-doctest build-depends: base , streamly-core , ghcid + , temporary >= 1.3 && < 1.4 + , filepath >= 1.4 && < 1.5 hs-source-dirs: app default-language: Haskell2010 From 776de22cfa246f05f7286c356202a561803ec225 Mon Sep 17 00:00:00 2001 From: Ranjeet Kumar Ranjan Date: Thu, 19 Jan 2023 09:15:24 +0530 Subject: [PATCH 2/2] Fix code review --- app/Main.hs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index d535d52..54f0c48 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -7,8 +7,7 @@ import Data.List (find, isPrefixOf) import Streamly.Data.Fold (Fold) import Streamly.Data.Parser (Parser) import System.Environment (getArgs) -import System.IO.Temp (withSystemTempDirectory) -import System.FilePath (()) +import System.IO.Temp (withSystemTempFile) import qualified Language.Haskell.Ghcid as G import qualified Streamly.Data.Fold as Fold @@ -228,12 +227,11 @@ loopCmdREPL sess (ln:lns) = do loopCmdFile :: G.Ghci -> [(Int, String)] -> IO Bool loopCmdFile _ [] = putStrLn "All good" >> return True loopCmdFile sess (ln:lns) = - withSystemTempDirectory "docTest" $ \fp -> do + withSystemTempFile "docTest" $ \fp _fh -> do let padding = replicate (fst ln - 1) '\n' - tgt = fp "interpreted.hs" - writeFile tgt (padding ++ snd ln) + writeFile fp (padding ++ snd ln) putStrLn (snd ln) - res <- G.exec sess (":load " ++ show tgt) + res <- G.exec sess (":load " ++ fp) if or (map hasErrorFile res) then do mapM_ putStrLn res