-
Notifications
You must be signed in to change notification settings - Fork 847
Cleanup integration tests for stack repl
#6740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
445d092
tests: chmod +x on test scripts
ulidtko f68fc0f
tests: fix missed -ignore-dot-ghci
ulidtko 8e99af9
tests(3926): fix test [on linux]
ulidtko 007da91
refactor: split off StackTest.Repl
ulidtko a123a28
refactor: move -ignore-dot-ghci to repl fixture
ulidtko 609205c
fix: resolve a TODO about unexpected exit code 1
ulidtko 368a0e7
tests: redirect stderr, instead of shuffling bytes one-by-one
ulidtko 38d03c2
feat: show repl stderr on exceptions in test
ulidtko 5b2927c
refactor: rename StackTest.Repl.{repl → stackRepl}
ulidtko 27e3587
fix: avoid quadratic String traversals, resolving a fixme
ulidtko 547f274
refactor(tests): reexport StackTest from StackTest.Repl
ulidtko a0a5fd9
cleanup: undo tests workarounds for msys2-20230526
ulidtko 3a939d5
cleanup: code review
ulidtko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
{- | | ||
Integration-test helpers & fixtures for testing `stack repl` | ||
-} | ||
module StackTest.Repl | ||
( Repl | ||
, ReplConnection (..) | ||
, nextPrompt | ||
, replCommand | ||
, replGetChar | ||
, replGetLine | ||
, stackRepl | ||
-- * Reexport | ||
, module StackTest | ||
) where | ||
|
||
import Control.Exception (SomeException, catch, displayException, finally) | ||
import Control.Monad ((>=>), unless, when) | ||
import Control.Monad.IO.Class (liftIO) | ||
import Control.Monad.Trans (lift) | ||
import Control.Monad.Trans.Reader | ||
import Control.Monad.Trans.State qualified as State | ||
import Data.Maybe (fromMaybe) | ||
import Data.Foldable (toList) | ||
import Data.Sequence as Seq (Seq(Empty), (|>), fromList) | ||
import GHC.Stack (HasCallStack) | ||
import System.Directory (removeFile) | ||
import System.Environment (lookupEnv) | ||
import System.Exit (ExitCode (..), exitFailure) | ||
import System.IO | ||
( BufferMode (NoBuffering, LineBuffering), Handle, IOMode (ReadMode) | ||
, hClose, hGetChar, hGetContents', hGetLine, hPutStrLn, hSetBuffering | ||
, openTempFile | ||
, withFile | ||
) | ||
import System.Process | ||
( CreateProcess (std_err, std_in, std_out) | ||
, StdStream (CreatePipe, UseHandle) | ||
, createProcess, proc, waitForProcess | ||
) | ||
|
||
import StackTest | ||
|
||
type Repl = ReaderT ReplConnection IO | ||
|
||
data ReplConnection = ReplConnection | ||
{ replStdin :: Handle | ||
, replStdout :: Handle | ||
} | ||
|
||
replCommand :: String -> Repl () | ||
replCommand cmd = do | ||
(ReplConnection replStdinHandle _) <- ask | ||
-- echo what we send to the test's stdout | ||
liftIO . putStrLn $ "____> " <> cmd | ||
liftIO $ hPutStrLn replStdinHandle cmd | ||
|
||
replGetChar :: Repl Char | ||
replGetChar = asks replStdout >>= liftIO . hGetChar | ||
|
||
replGetLine :: Repl String | ||
replGetLine = ask >>= liftIO . hGetLine . replStdout | ||
|
||
nextPrompt :: Repl () | ||
nextPrompt = State.evalStateT poll Seq.Empty where | ||
poll = do | ||
c <- lift (asks replStdout) >>= liftIO . hGetChar | ||
State.modify (|> c) | ||
when (c == '\n') $ do | ||
State.get >>= liftIO . putStr . ("ghci> " ++) . toList | ||
State.put Seq.Empty | ||
buf <- State.get | ||
unless (buf == Seq.fromList "ghci> ") | ||
poll | ||
|
||
runRepl | ||
:: HasCallStack | ||
=> FilePath | ||
-> [String] | ||
-> Repl () | ||
-> IO ExitCode | ||
runRepl cmd args actions = do | ||
(stderrBufPath, stderrBufHandle) <- openTempStderrBufferFile | ||
hSetBuffering stderrBufHandle NoBuffering | ||
|
||
logInfo $ "Running: " ++ cmd ++ " " ++ unwords (map showProcessArgDebug args) ++ "\n\ | ||
\ with stderr in " ++ stderrBufPath | ||
|
||
-- launch the GHCi subprocess, grab its FD handles and process handle | ||
(Just rStdin, Just rStdout, Nothing, ph) <- | ||
createProcess (proc cmd args) | ||
{ std_in = CreatePipe | ||
, std_out = CreatePipe | ||
, std_err = UseHandle stderrBufHandle | ||
} | ||
hSetBuffering rStdin LineBuffering | ||
hSetBuffering rStdout NoBuffering | ||
|
||
-- run the test script which is to talk to the GHCi subprocess. | ||
runReaderT actions (ReplConnection rStdin rStdout) | ||
-- the nested actions script may fail in arbitrary ways; handle that here, | ||
-- attaching the subprocess stderr as relevant context | ||
`catch` \(e :: SomeException) -> do | ||
putStrLn "==============================" | ||
putStrLn "EXCEPTION in test: " | ||
putStrLn . quote $ displayException e | ||
putStrLn "------[ stderr of repl ]------" | ||
withFile stderrBufPath ReadMode $ hGetContents' >=> putStr . quote | ||
putStrLn "==============================" | ||
`finally` do | ||
hClose stderrBufHandle | ||
removeFile stderrBufPath | ||
|
||
-- once done with the test, signal EOF on stdin for clean termination of ghci | ||
hClose rStdin | ||
-- read out the exit-code | ||
waitForProcess ph | ||
|
||
-- | Roll a bicycle, rather than just `import Path.IO (getTempDir, openTempFile)`, | ||
-- because it's a hassle to use anything beyond base & boot libs here. | ||
openTempStderrBufferFile :: IO (FilePath, Handle) | ||
openTempStderrBufferFile = getTempDir >>= (`openTempFile` "err.log") where | ||
getTempDir | isWindows = fromMaybe "" <$> lookupEnv "TEMP" | ||
| otherwise = pure "/tmp" | ||
|
||
-- | Testing helper to exercise `stack repl`. | ||
stackRepl :: HasCallStack => [String] -> Repl () -> IO () | ||
stackRepl args action = do | ||
stackExe' <- stackExe | ||
ec <- runRepl stackExe' ("repl" : "--ghci-options=-ignore-dot-ghci" : args) action | ||
unless (ec == ExitSuccess) $ do | ||
putStrLn $ "repl exited with " <> show ec | ||
exitFailure | ||
|
||
quote :: String -> String | ||
quote = unlines . map ("> " <>) . lines |
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import StackTest | ||
import StackTest.Repl | ||
|
||
main :: IO () | ||
main = do | ||
stack ["build", "--ghc-options=-ddump-simpl -ddump-asm -DBAR -DBAZ"] | ||
repl ["--ghc-options=-ddump-simpl -ddump-asm"] (pure ()) | ||
stackRepl ["--ghc-options=-ddump-simpl -ddump-asm"] (pure ()) |
63 changes: 22 additions & 41 deletions
63
tests/integration/tests/3926-ghci-with-sublibraries/Main.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 3 additions & 7 deletions
10
tests/integration/tests/module-added-multiple-times/Main.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.