Skip to content

Commit

Permalink
[#33] Split the 'ToolCheckResult' constructor (#65)
Browse files Browse the repository at this point in the history
* Change some naming and remember about testing

Resolves #33
  • Loading branch information
charrsky authored Aug 21, 2022
1 parent e352b65 commit 0b9c50d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/Iris/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import System.IO (stderr, stdout)
import Iris.Cli.Version (VersionSettings, mkVersionParser)
import Iris.Cli.Interactive (InteractiveMode, interactiveModeP)
import Iris.Colour.Mode (ColourMode, handleColourMode)
import Iris.Tool (Tool, ToolCheckResult (..), checkTool)
import Iris.Tool (Tool, ToolCheckResult (..), checkTool, ToolCheckError (..))

import qualified Options.Applicative as Opt

Expand Down Expand Up @@ -120,7 +120,7 @@ data CliEnv (cmd :: Type) (appEnv :: Type) = CliEnv
-}
newtype CliEnvError
-- | @since 0.0.0.0
= CliEnvToolError ToolCheckResult
= CliEnvToolError ToolCheckError
deriving stock
( Show -- ^ @since 0.0.0.0
)
Expand Down Expand Up @@ -175,7 +175,7 @@ mkCliEnv CliEnvSettings{..} = do
for_ cliEnvSettingsRequiredTools $ \tool ->
checkTool cmdCmd tool >>= \case
ToolOk -> pure ()
toolErr -> throwIO $ CliEnvException $ CliEnvToolError toolErr
(ToolCheckError toolErr) -> throwIO $ CliEnvException $ CliEnvToolError toolErr

pure CliEnv
{ cliEnvCmd = cmdCmd
Expand Down
33 changes: 24 additions & 9 deletions src/Iris/Tool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Iris.Tool

-- * Tool requirements check
, ToolCheckResult (..)
, ToolCheckError (..)
, checkTool
) where

Expand Down Expand Up @@ -82,24 +83,38 @@ defaultToolSelector = ToolSelector
data ToolCheckResult
{- |
@since x.x.x.x
-}
= ToolCheckError ToolCheckError
{- |
@since 0.0.0.0
-}
= ToolNotFound Text
| ToolOk
deriving stock
( Show -- ^ @since 0.0.0.0
, Eq -- ^ @since 0.0.0.0
)

{- |
@since x.x.x.x
-}
data ToolCheckError
{- |
@since 0.0.0.0
@since x.x.x.x
-}
| ToolWrongVersion Text
= ToolNotFound Text

{- |
@since 0.0.0.0
@since x.x.x.x
-}
| ToolOk
| ToolWrongVersion Text
deriving stock
( Show -- ^ @since 0.0.0.0
, Eq -- ^ @since 0.0.0.0
( Show -- ^ @since x.x.x.x
, Eq -- ^ @since x.x.x.x
)

{- |
Expand All @@ -108,7 +123,7 @@ data ToolCheckResult
-}
checkTool :: cmd -> Tool cmd -> IO ToolCheckResult
checkTool cmd Tool{..} = findExecutable (Text.unpack toolName) >>= \case
Nothing -> pure $ ToolNotFound toolName
Nothing -> pure $ ToolCheckError $ ToolNotFound toolName
Just exe -> case toolSelector of
Nothing -> pure ToolOk
Just ToolSelector{..} -> case toolSelectorVersionArg of
Expand All @@ -119,4 +134,4 @@ checkTool cmd Tool{..} = findExecutable (Text.unpack toolName) >>= \case

if toolSelectorFunction cmd version
then pure ToolOk
else pure $ ToolWrongVersion version
else pure $ ToolCheckError $ ToolWrongVersion version
6 changes: 3 additions & 3 deletions test/Test/Iris/Tool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.Version (Version, makeVersion, parseVersion)
import Test.Hspec (Spec, describe, it, shouldReturn, shouldSatisfy)
import Text.ParserCombinators.ReadP (readP_to_S)

import Iris.Tool (Tool (..), ToolCheckResult (..), ToolSelector (..), checkTool,
import Iris.Tool (Tool (..), ToolCheckResult (..), ToolCheckError (..), ToolSelector (..), checkTool,
defaultToolSelector)

import qualified Data.Text as Text
Expand All @@ -18,7 +18,7 @@ toolSpec = describe "Tool" $ do

it "shouldn't find 'xxx_unknown_executable'" $ do
checkTool () "xxx_unknown_executable"
`shouldReturn` ToolNotFound "xxx_unknown_executable"
`shouldReturn` ToolCheckError (ToolNotFound "xxx_unknown_executable")

it "shouldn't find 'ghc' version 100" $ do
let tool :: Tool ()
Expand All @@ -32,7 +32,7 @@ toolSpec = describe "Tool" $ do
}

let isToolWrongVersion :: ToolCheckResult -> Bool
isToolWrongVersion (ToolWrongVersion _) = True
isToolWrongVersion (ToolCheckError (ToolWrongVersion _)) = True
isToolWrongVersion _other = False

checkTool () tool >>= (`shouldSatisfy` isToolWrongVersion)
Expand Down

0 comments on commit 0b9c50d

Please sign in to comment.