Skip to content
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

Be able to run TUI applications with "stack test" #5916

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Behavior changes:
command are inconsistent and now takes no action. Previously the flag was
silently ignored.

* `stack test` will check if the process is running in a terminal. If so, it
will start the test suite in a way that it can use the terminal. This is
intended to support interactive terminal UI based testing.

Other enhancements:

* Help documentation for `stack upgrade` warns that if GHCup is used to install
Expand Down
107 changes: 62 additions & 45 deletions src/Stack/Build/Execute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import RIO.PrettyPrint
import RIO.Process
import Pantry.Internal.Companion
import System.Random (randomIO)
import System.Terminal (hIsTerminalDeviceOrMinTTY)

-- | Has an executable been built or not?
data ExecutableBuildStatus
Expand Down Expand Up @@ -1020,8 +1021,9 @@ withLockedDistDir announce root inner = do
-- | How we deal with output from GHC, either dumping to a log file or the
-- console (with some prefix).
data OutputType
= OTLogFile !(Path Abs File) !Handle
| OTConsole !(Maybe Utf8Builder)
= OTLogFile !(Path Abs File) !Handle
| OTConsolePrefix !(Maybe Utf8Builder)
| OTConsoleTTY

-- | This sets up a context for executing build steps which need to run
-- Cabal (via a compiled Setup.hs). In particular it does the following:
Expand Down Expand Up @@ -1112,12 +1114,16 @@ withSingleContext ActionContext {..} ee@ExecuteEnv {..} task@Task {..} allDeps m
withOutputType pkgDir package inner
-- Not in interleaved mode. When building a single wanted package, dump
-- to the console with no prefix.
| console = inner $ OTConsole Nothing
| console = do
isTerminal <- hIsTerminalDeviceOrMinTTY stdout
if isTerminal
then inner OTConsoleTTY
else inner $ OTConsolePrefix Nothing

-- If the user requested interleaved output, dump to the console with a
-- prefix.
| boptsInterleavedOutput eeBuildOpts =
inner $ OTConsole $ Just $ packageNamePrefix ee $ packageName package
inner $ OTConsolePrefix $ Just $ packageNamePrefix ee $ packageName package

-- Neither condition applies, dump to a file.
| otherwise = do
Expand Down Expand Up @@ -1261,7 +1267,6 @@ withSingleContext ActionContext {..} ee@ExecuteEnv {..} task@Task {..} allDeps m
runAndOutput compilerVer `catch` \ece -> do
(mlogFile, bss) <-
case outputType of
OTConsole _ -> pure (Nothing, [])
OTLogFile logFile h ->
if keepOutputOpen == KeepOpen
then pure (Nothing, []) -- expected failure build continues further
Expand All @@ -1273,6 +1278,7 @@ withSingleContext ActionContext {..} ee@ExecuteEnv {..} task@Task {..} allDeps m
.| CT.decodeUtf8Lenient
.| mungeBuildOutput stripTHLoading makeAbsolute pkgDir compilerVer
.| CL.consume
_ -> pure (Nothing, [])
throwM $ CabalExitedUnsuccessfully
(eceExitCode ece)
taskProvides
Expand All @@ -1291,11 +1297,15 @@ withSingleContext ActionContext {..} ee@ExecuteEnv {..} task@Task {..} allDeps m
void $ sinkProcessStderrStdout (toFilePath exeName) fullArgs
(sinkWithTimestamps prefixWithTimestamps h)
(sinkWithTimestamps prefixWithTimestamps h)
OTConsole mprefix ->
let prefix = fold mprefix in
void $ sinkProcessStderrStdout (toFilePath exeName) fullArgs
(outputSink KeepTHLoading LevelWarn compilerVer prefix)
(outputSink stripTHLoading LevelInfo compilerVer prefix)
OTConsoleTTY -> runWithPrefix compilerVer ""
OTConsolePrefix mprefix -> runWithPrefix compilerVer (fold mprefix)

runWithPrefix :: ActualCompiler -> Utf8Builder -> RIO env ()
runWithPrefix compilerVer prefix =
void $ sinkProcessStderrStdout (toFilePath exeName) fullArgs
(outputSink KeepTHLoading LevelWarn compilerVer prefix)
(outputSink stripTHLoading LevelInfo compilerVer prefix)

outputSink
:: HasCallStack
=> ExcludeTHLoading
Expand Down Expand Up @@ -1973,53 +1983,60 @@ singleTest topts testsToRun ac ee task installedMap = do
-- Clear "Progress: ..." message before
-- redirecting output.
case outputType of
OTConsole _ -> do
OTConsoleTTY -> do
logStickyDone ""
liftIO $ hFlush stdout
liftIO $ hFlush stderr
OTConsolePrefix _ -> do
logStickyDone ""
liftIO $ hFlush stdout
liftIO $ hFlush stderr
OTLogFile _ _ -> pure ()
_ -> pure ()

let optionalTimeout action
| Just maxSecs <- toMaximumTimeSeconds topts, maxSecs > 0 = do
timeout (maxSecs * 1000000) action
| otherwise = Just <$> action

let output =
case outputType of
OTConsole Nothing -> Nothing <$ inherit
OTConsole (Just prefix) -> fmap
mec <- withWorkingDir (toFilePath pkgDir) $
case outputType of
OTConsoleTTY ->
(Just <$>) $ proc (toFilePath exePath) args runProcess
_ -> optionalTimeout $ proc (toFilePath exePath) args $ \pc0 -> do
stdinBS <-
if isTestTypeLib
then do
logPath <- buildLogPath package (Just stestName)
ensureDir (parent logPath)
pure $ BL.fromStrict
$ encodeUtf8 $ fromString $
show (logPath, mkUnqualComponentName (T.unpack testName))
else pure mempty
let output = case outputType of
OTConsolePrefix (Just prefix) -> fmap
(\src -> Just $ runConduit $ src .|
CT.decodeUtf8Lenient .|
CT.lines .|
CL.map stripCR .|
CL.mapM_ (\t -> logInfo $ prefix <> RIO.display t))
createSource
OTLogFile _ h -> Nothing <$ useHandleOpen h
optionalTimeout action
| Just maxSecs <- toMaximumTimeSeconds topts, maxSecs > 0 = do
timeout (maxSecs * 1000000) action
| otherwise = Just <$> action

mec <- withWorkingDir (toFilePath pkgDir) $
optionalTimeout $ proc (toFilePath exePath) args $ \pc0 -> do
stdinBS <-
if isTestTypeLib
then do
logPath <- buildLogPath package (Just stestName)
ensureDir (parent logPath)
pure $ BL.fromStrict
$ encodeUtf8 $ fromString $
show (logPath, mkUnqualComponentName (T.unpack testName))
else pure mempty
let pc = setStdin (byteStringInput stdinBS)
$ setStdout output
$ setStderr output
pc0
withProcessWait pc $ \p -> do
case (getStdout p, getStderr p) of
(Nothing, Nothing) -> pure ()
(Just x, Just y) -> concurrently_ x y
(x, y) -> assert False $ concurrently_ (fromMaybe (pure ()) x) (fromMaybe (pure ()) y)
waitExitCode p
_ -> Nothing <$ inherit
let pc = setStdin (byteStringInput stdinBS)
$ setStdout output
$ setStderr output
pc0
withProcessWait pc $ \p -> do
case (getStdout p, getStderr p) of
(Nothing, Nothing) -> pure ()
(Just x, Just y) -> concurrently_ x y
(x, y) -> assert False $ concurrently_ (fromMaybe (pure ()) x) (fromMaybe (pure ()) y)
waitExitCode p
-- Add a trailing newline, incase the test
-- output didn't finish with a newline.
case outputType of
OTConsole Nothing -> logInfo ""
OTConsolePrefix Nothing -> logInfo ""
OTConsoleTTY -> logInfo ""
_ -> pure ()
-- Move the .tix file out of the package
-- directory into the hpc work dir, for
Expand Down Expand Up @@ -2059,18 +2076,18 @@ singleTest topts testsToRun ac ee task installedMap = do

bs <- liftIO $
case outputType of
OTConsole _ -> pure ""
OTLogFile logFile h -> do
hClose h
S.readFile $ toFilePath logFile
_ -> pure ""

let succeeded = Map.null errs
unless (succeeded || expectFailure) $ throwM $ TestSuiteFailure
(taskProvides task)
errs
(case outputType of
OTLogFile fp _ -> Just fp
OTConsole _ -> Nothing)
_ -> Nothing)
bs

setTestStatus pkgDir $ if succeeded then TSSuccess else TSFailure
Expand Down