packages feed

haskoin-store-2.1.0: test/Haskoin/Store/CommandLineSpec.hs

-- |
-- Module      : Haskoin.Store.CommandLineSpec
-- Copyright   : (c) Haskoin Developers
-- License     : MIT
-- Maintainer  : jprupp@protonmail.ch
-- Stability   : experimental
-- Portability : POSIX
--
-- The command line of the program, run as it was built. Its parser is in
-- @app\/Main.hs@, which the tests cannot import, so they run the program
-- with arguments and an environment, and read what it says and how it
-- ends: an option that its variable does not set, or a value that is not
-- refused, shows there.
--
-- The program is beside the test suite, in the build folder of Stack and
-- of Cabal (@build\/haskoin-store\/haskoin-store@ next to
-- @build\/spec\/spec@). A suite run without it says that the tests are
-- pending.
module Haskoin.Store.CommandLineSpec (spec) where

import Data.List (isInfixOf)
import System.Directory (doesFileExist)
import System.Environment (getEnvironment, getExecutablePath)
import System.Exit (ExitCode (..))
import System.FilePath (takeDirectory, (</>))
import System.Process (CreateProcess (..), proc, readCreateProcessWithExitCode)
import Test.Hspec

spec :: Spec
spec =
  describe "The wait for a peer's mempool, --mempool-wait or MEMPOOL_WAIT" $ do
    it "is in the help, with its variable and its default of twenty seconds" $
      withProgram $ \run -> do
        (code, out, _) <- run [] ["--help"]
        code `shouldBe` ExitSuccess
        let text = unwords (words out)
        text `shouldSatisfy` ("--mempool-wait SECONDS" `isInfixOf`)
        text `shouldSatisfy` ("[env: MEMPOOL_WAIT] (default: 20)" `isInfixOf`)
    it "takes its default from the variable" $
      withProgram $ \run -> do
        (_, out, _) <- run [("MEMPOOL_WAIT", "3")] ["--help"]
        unwords (words out) `shouldSatisfy` ("[env: MEMPOOL_WAIT] (default: 3)" `isInfixOf`)
    it "takes a number of seconds on the command line" $
      withProgram $ \run -> do
        (code, _, _) <- run [] ["--mempool-wait", "3", "--version"]
        code `shouldBe` ExitSuccess
        (code', _, err) <- run [] ["--mempool-wait", "soon", "--version"]
        code' `shouldBe` ExitFailure 1
        err `shouldSatisfy` ("--mempool-wait" `isInfixOf`)
    it "refuses no time at all, from the command line, which comes first, and from the variable" $
      withProgram $ \run -> do
        let refused (code, _, err) = do
              code `shouldBe` ExitFailure 1
              err `shouldSatisfy` ("--mempool-wait (or MEMPOOL_WAIT) must be at least 1 second" `isInfixOf`)
        refused =<< run [] ["--mempool-wait", "0"]
        refused =<< run [] ["--mempool-wait", "-5"]
        refused =<< run [("MEMPOOL_WAIT", "3")] ["--mempool-wait", "0"]
        refused =<< run [("MEMPOOL_WAIT", "0")] []

-- | Run an action with a way to run the program: with variables added to
-- the environment of the tests, less any of the program's that the
-- action does not set, and arguments. Gives how it ended, what it wrote
-- and what it wrote as errors.
withProgram ::
  (([(String, String)] -> [String] -> IO (ExitCode, String, String)) -> Expectation) ->
  Expectation
withProgram action = do
  here <- getExecutablePath
  let program = takeDirectory (takeDirectory here) </> "haskoin-store" </> "haskoin-store"
  built <- doesFileExist program
  if not built
    then pendingWith ("The program is not built at " <> program)
    else do
      outer <- filter ((/= "MEMPOOL_WAIT") . fst) <$> getEnvironment
      action $ \vars args ->
        readCreateProcessWithExitCode (proc program args) {env = Just (vars <> outer)} ""