Skip to content

Instantly share code, notes, and snippets.

@schell
Created September 1, 2018 18:18
Show Gist options
  • Save schell/71393090671f8ccf6386dea5aae226e4 to your computer and use it in GitHub Desktop.
Save schell/71393090671f8ccf6386dea5aae226e4 to your computer and use it in GitHub Desktop.
read from ag handle - ran with `stack test :target`
-- | The `ag` search process.
agSearchProcess :: Maybe FilePath -> CreateProcess
agSearchProcess mdir = ag { std_out = CreatePipe
, std_err = CreatePipe
, cwd = mdir
}
where ag = proc "ag" ["--noheading", "TODO"]
main :: IO ()
main = do
hspec $ describe "prerequisites" $
it "should be able to locate and use ag - silver searcher" $ do
(code, _, _) <- waitForProcessExit checkForAg
unless (code == ExitSuccess) $ fail "ag could not be found."
(_, mayStdOut, _, _) <- createProcess $ agSearchProcess Nothing
void $ flip (maybe (fail "could not get ag stdout")) mayStdOut $ \stdOut -> do
hSetBuffering stdOut NoBuffering
race_
-- delay three seconds
(threadDelay 3000000 >> fail "timed out waiting for input from ag stdout handle")
$ fix $ \loop -> do
isReady <- hReady stdOut
unless isReady loop
bytes <- B8.hGetContents stdOut
bytes `shouldSatisfy` (not . B8.null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment