Skip to content

Commit

Permalink
[#56] Implement tests for the '--help' parser (#68)
Browse files Browse the repository at this point in the history
* Moved cmdParserInfo to separate file. Moved envSettings to separate file. Created initial and failing test for testing help

* getting somewhere

* Added help tests

* Reverted main

* Renamed function

* Fixes #56 along with PR 68 comments

* Renamed cmd to internal
  • Loading branch information
CThuleHansen authored Sep 28, 2022
1 parent 0b9c50d commit 1bede5f
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 83 deletions.
7 changes: 7 additions & 0 deletions iris.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ library
Iris.Browse
Iris.Cli
Iris.Cli.Browse
Iris.Cli.Internal
Iris.Cli.Interactive
Iris.Cli.ParserInfo
Iris.Cli.Version
Iris.Colour
Iris.Colour.Formatting
Iris.Colour.Mode
Iris.Env
Iris.Settings
Iris.Tool

build-depends:
Expand Down Expand Up @@ -122,11 +125,15 @@ test-suite iris-test
other-modules:
Test.Iris
Test.Iris.Tool
Test.Iris.Cli
Paths_iris


build-depends:
, iris
, hspec >= 2.9.7 && < 2.11
, text
, optparse-applicative

ghc-options: -threaded
-rtsopts
Expand Down
7 changes: 7 additions & 0 deletions src/Iris.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ module Iris
, module Iris.Colour
-- $env
, module Iris.Env
-- $settings
, module Iris.Settings
-- $tool
, module Iris.Tool
) where
Expand All @@ -50,6 +52,7 @@ import Iris.Browse
import Iris.Cli
import Iris.Colour
import Iris.Env
import Iris.Settings
import Iris.Tool

{- $app
Expand All @@ -72,6 +75,10 @@ Functions to detect terminal support for colouring and print coloured output.
Global environment for a CLI application and CLI app settings.
-}

{- $settings
Settings for the environment.
-}

{- $tool
Capabilities to check required tools on the application start.
-}
3 changes: 2 additions & 1 deletion src/Iris/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Reader (MonadReader, ReaderT (..))
import Control.Monad.IO.Unlift (MonadUnliftIO)

import Iris.Env (CliEnv, CliEnvSettings, mkCliEnv)
import Iris.Env (CliEnv, mkCliEnv)
import Iris.Settings (CliEnvSettings)


{- | Main monad for your CLI application.
Expand Down
9 changes: 8 additions & 1 deletion src/Iris/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@ CLI options parsing.
module Iris.Cli
( -- $browse
module Iris.Cli.Browse
-- $parserInfo
, module Iris.Cli.ParserInfo
-- $version
, module Iris.Cli.Version
, module Iris.Cli.Version
) where

import Iris.Cli.Browse
import Iris.Cli.Version
import Iris.Cli.ParserInfo

{- $browse
CLI parsers for @--browse@ flags.
-}

{- $parserInfo
Info needed to create a CLI parser.
-}

{- $version
CLI parsers for @--version@ and @--numeric-version@ flags.
-}
30 changes: 30 additions & 0 deletions src/Iris/Cli/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{- |
Module : Iris.Cli.Cmd
Copyright : (c) 2022 Dmitrii Kovanikov
SPDX-License-Identifier : MPL-2.0
Maintainer : Dmitrii Kovanikov <[email protected]>
Stability : Experimental
Portability : Portable
Wrapper around cmd.
@since x.x.x.x
-}

module Iris.Cli.Internal
(
Cmd (..)
) where

import Data.Kind (Type)
import Iris.Cli.Interactive (InteractiveMode)

{- |
Wrapper around @cmd@ with additional predefined fields
-}

data Cmd (cmd :: Type) = Cmd
{ cmdInteractiveMode :: InteractiveMode
, cmdCmd :: cmd
}
48 changes: 48 additions & 0 deletions src/Iris/Cli/ParserInfo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ApplicativeDo #-}

{- |
Module : Iris.Cli.ParserInfo
Copyright : (c) 2022 Dmitrii Kovanikov
SPDX-License-Identifier : MPL-2.0
Maintainer : Dmitrii Kovanikov <[email protected]>
Stability : Experimental
Portability : Portable
Parser information for the default CLI parser.
@since 0.0.0.0
-}

module Iris.Cli.ParserInfo (cmdParserInfo) where

import Iris.Cli.Internal ( Cmd (..) )
import Iris.Cli.Interactive (interactiveModeP)
import Iris.Settings (CliEnvSettings (..))
import Iris.Cli.Version (mkVersionParser)

import qualified Options.Applicative as Opt

{- |
@since 0.0.0.0
-}

cmdParserInfo :: forall cmd appEnv . CliEnvSettings cmd appEnv -> Opt.ParserInfo (Cmd cmd)
cmdParserInfo CliEnvSettings{..} = Opt.info
( Opt.helper
<*> mkVersionParser cliEnvSettingsVersionSettings
<*> cmdP
)
$ mconcat
[ Opt.fullDesc
, Opt.header cliEnvSettingsHeaderDesc
, Opt.progDesc cliEnvSettingsProgDesc
]
where
cmdP :: Opt.Parser (Cmd cmd)
cmdP = do
cmdInteractiveMode <- interactiveModeP
cmdCmd <- cliEnvSettingsCmdParser

pure Cmd{..}
91 changes: 10 additions & 81 deletions src/Iris/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ Environment of a CLI app.


module Iris.Env
( -- * Settings for the CLI app
CliEnvSettings (..)
, defaultCliEnvSettings

-- * CLI application environment
( -- * CLI application environment
-- ** Constructing
, CliEnv (..)
CliEnv (..)
, CliEnvException (..)
, CliEnvError (..)
, mkCliEnv
Expand All @@ -38,54 +34,15 @@ import Data.Foldable (for_)
import Data.Kind (Type)
import System.IO (stderr, stdout)

import Iris.Cli.Version (VersionSettings, mkVersionParser)
import Iris.Cli.Interactive (InteractiveMode, interactiveModeP)
import Iris.Cli.Internal (Cmd (..))
import Iris.Cli.ParserInfo (cmdParserInfo)
import Iris.Cli.Interactive (InteractiveMode)
import Iris.Colour.Mode (ColourMode, handleColourMode)
import Iris.Tool (Tool, ToolCheckResult (..), checkTool, ToolCheckError (..))
import Iris.Settings (CliEnvSettings (..))
import Iris.Tool (ToolCheckResult (..), checkTool, ToolCheckError (..))

import qualified Options.Applicative as Opt


{- |
@since 0.0.0.0
-}
data CliEnvSettings (cmd :: Type) (appEnv :: Type) = CliEnvSettings
{ -- | @since 0.0.0.0
cliEnvSettingsCmdParser :: Opt.Parser cmd

-- | @since 0.0.0.0
, cliEnvSettingsAppEnv :: appEnv

-- | @since 0.0.0.0
, cliEnvSettingsHeaderDesc :: String

-- | @since 0.0.0.0
, cliEnvSettingsProgDesc :: String

-- | @since 0.0.0.0
, cliEnvSettingsVersionSettings :: Maybe VersionSettings

-- | @since 0.0.0.0
, cliEnvSettingsRequiredTools :: [Tool cmd]
}


{- |
@since 0.0.0.0
-}
defaultCliEnvSettings :: CliEnvSettings () ()
defaultCliEnvSettings = CliEnvSettings
{ cliEnvSettingsCmdParser = pure ()
, cliEnvSettingsAppEnv = ()
, cliEnvSettingsHeaderDesc = "Simple CLI program"
, cliEnvSettingsProgDesc = "CLI tool build with iris - a Haskell CLI framework"
, cliEnvSettingsVersionSettings = Nothing
, cliEnvSettingsRequiredTools = []
}


{- | CLI application environment. It contains default settings for
every CLI app and parameter
Expand Down Expand Up @@ -147,16 +104,6 @@ newtype CliEnvException = CliEnvException
( Exception -- ^ @since 0.0.0.0
)

{- |
Wrapper around @cmd@ with additional predefined fields
-}

data Cmd (cmd :: Type) = Cmd
{ cmdInteractiveMode :: InteractiveMode
, cmdCmd :: cmd
}

{- |
__Throws:__ 'CliEnvException'
Expand All @@ -167,8 +114,8 @@ mkCliEnv
:: forall cmd appEnv
. CliEnvSettings cmd appEnv
-> IO (CliEnv cmd appEnv)
mkCliEnv CliEnvSettings{..} = do
Cmd{..} <- Opt.execParser cmdParserInfo
mkCliEnv cliEnvSettings@CliEnvSettings{..} = do
Cmd{..} <- Opt.execParser $ cmdParserInfo cliEnvSettings
stdoutColourMode <- handleColourMode stdout
stderrColourMode <- handleColourMode stderr

Expand All @@ -184,25 +131,7 @@ mkCliEnv CliEnvSettings{..} = do
, cliEnvAppEnv = cliEnvSettingsAppEnv
, cliEnvInteractiveMode = cmdInteractiveMode
}
where
cmdParserInfo :: Opt.ParserInfo (Cmd cmd)
cmdParserInfo = Opt.info
( Opt.helper
<*> mkVersionParser cliEnvSettingsVersionSettings
<*> cmdP
)
$ mconcat
[ Opt.fullDesc
, Opt.header cliEnvSettingsHeaderDesc
, Opt.progDesc cliEnvSettingsProgDesc
]
cmdP :: Opt.Parser (Cmd cmd)
cmdP = do
cmdInteractiveMode <- interactiveModeP
cmdCmd <- cliEnvSettingsCmdParser

pure Cmd{..}


{- | Get a field from the global environment 'CliEnv'.
@since 0.0.0.0
Expand Down
63 changes: 63 additions & 0 deletions src/Iris/Settings.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{- |
Module : Iris.Settings
Copyright : (c) 2022 Dmitrii Kovanikov
SPDX-License-Identifier : MPL-2.0
Maintainer : Dmitrii Kovanikov <[email protected]>
Stability : Experimental
Portability : Portable
Settings of a CLI app environment.
@since x.x.x.x
-}


module Iris.Settings
( -- * Settings for the CLI app
CliEnvSettings (..)
, defaultCliEnvSettings
) where

import Data.Kind (Type)
import qualified Options.Applicative as Opt
import Iris.Cli.Version (VersionSettings)
import Iris.Tool (Tool)

{- |
@since 0.0.0.0
-}
data CliEnvSettings (cmd :: Type) (appEnv :: Type) = CliEnvSettings
{ -- | @since 0.0.0.0
cliEnvSettingsCmdParser :: Opt.Parser cmd

-- | @since 0.0.0.0
, cliEnvSettingsAppEnv :: appEnv

-- | @since 0.0.0.0
, cliEnvSettingsHeaderDesc :: String

-- | @since 0.0.0.0
, cliEnvSettingsProgDesc :: String

-- | @since 0.0.0.0
, cliEnvSettingsVersionSettings :: Maybe VersionSettings

-- | @since 0.0.0.0
, cliEnvSettingsRequiredTools :: [Tool cmd]
}


{- |
@since 0.0.0.0
-}
defaultCliEnvSettings :: CliEnvSettings () ()
defaultCliEnvSettings = CliEnvSettings
{ cliEnvSettingsCmdParser = pure ()
, cliEnvSettingsAppEnv = ()
, cliEnvSettingsHeaderDesc = "Simple CLI program"
, cliEnvSettingsProgDesc = "CLI tool build with iris - a Haskell CLI framework"
, cliEnvSettingsVersionSettings = Nothing
, cliEnvSettingsRequiredTools = []
}
2 changes: 2 additions & 0 deletions test/Test/Iris.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Test.Iris (irisSpec) where
import Test.Hspec (Spec, describe)

import Test.Iris.Tool (toolSpec)
import Test.Iris.Cli (cliSpec)

irisSpec :: Spec
irisSpec = describe "Iris" $ do
toolSpec
cliSpec
Loading

0 comments on commit 1bede5f

Please sign in to comment.