Skip to content

Commit

Permalink
Merge pull request #197 from gelisam/issue-174
Browse files Browse the repository at this point in the history
Issue 174
  • Loading branch information
gelisam authored Nov 27, 2018
2 parents 91396b6 + bd6cbf9 commit a5e2b58
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 140 deletions.
20 changes: 11 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ before_install:
- export PATH="$(stack --resolver="$RESOLVER" path --compiler-bin):$PATH"

install:
# Build dependencies (stack)
- if [ "$BUILD" == "stack" ]; then stack --resolver="$RESOLVER" --no-terminal --install-ghc test --only-dependencies ;fi
# Build dependencies (cabal)
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal update ;fi
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal new-build --only-dependencies ;fi
# Build the package, and its tests and its docs (stack)
- if [ "$BUILD" == "stack" ]; then stack --resolver="$RESOLVER" --no-terminal --install-ghc test --no-run-tests --haddock --no-haddock-deps; fi
# Build the package and its tests (cabal)
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal update; fi
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal sandbox init; fi
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal install happy; fi
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal install --enable-tests; fi

script:
# Build the package, its tests, and its docs and run the tests (stack)
- if [ "$BUILD" == "stack" ]; then stack --resolver="$RESOLVER" --no-terminal test --haddock --no-haddock-deps ;fi
# Build the package (cabal)
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal new-build ;fi
# Run the tests (stack)
- if [ "$BUILD" == "stack" ]; then stack --resolver="$RESOLVER" --no-terminal test; fi
# Run the tests (cabal)
- if [ "$BUILD" == "cabal" ]; then ~/.local/bin/cabal test; fi
59 changes: 30 additions & 29 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,33 @@ executables:
- transformers >= 0.5.2.0
source-dirs: src

#tests:
# reference:
# source-dirs:
# - src
# - tests
# main: RunTests.hs
# dependencies:
# - aeson
# - directory
# - doctest >=0.3.0
# - easy-file
# - exceptions >=0.1
# - extra
# - filelock
# - filepath
# - haskell-awk
# - haskell-src-exts >=1.14.0 && < 1.18
# - hint >=0.3.3.5
# - hspec >=0.2.0
# - HUnit >=1.1
# - mtl >=2.1.2
# - network >=2.3.1.0
# - process
# - template-haskell
# - temporary >=1.0
# - test-framework >=0.1
# - test-framework-hunit >=0.2.0
# - time
# - transformers >=0.3.0.0
# TODO: set the lower bounds to the lts-7.0 versions
tests:
reference:
source-dirs:
- src
- tests
main: RunTests.hs
dependencies:
- aeson >= 0.11.2.1
- directory >= 1.2.6.2
- doctest >= 0.11.0
- easy-file >= 0.2.1
- exceptions >= 0.8.3
- extra >= 1.4.10
- filelock >= 0.1.0.1
- filepath >= 1.4.1.0
- haskell-awk >= 1.1.1
- haskell-src-exts >= 1.20.3
- hint >= 0.6.0
- hspec >= 2.2.3
- HUnit >= 1.3.1.2
- mtl >= 2.2.1
- network >= 2.6.3.1
- process >= 1.4.2.0
- template-haskell >= 2.11.0.0
- temporary >= 1.2.0.4
- test-framework >= 0.8.1.1
- test-framework-hunit >= 0.3.0.2
- time >= 1.6.0.1
- transformers >= 0.5.2.0
112 changes: 61 additions & 51 deletions src/Control/Monad/Trans/OptionParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,21 @@ optionsHelp = optionsHelpWith shortName longName helpMsg optionType

-- | A version of `optionsHelp` which doesn't use the Option typeclass.
--
-- >>> :{
-- let { tp "cowbell" = flag
-- ; tp "guitar" = string
-- ; tp "saxophone" = optional int
-- }
-- :}
--
-- >>> testH tp
-- Usage: more [option]... <song.mp3>
-- Options:
-- -c --cowbell adds more cowbell.
-- -g str --guitar=str adds more guitar.
-- -s[int] --saxophone[=int] adds more saxophone.
-- TODO: why is this test failing?
-- -->>> :{
-- --let { tp "cowbell" = flag
-- -- ; tp "guitar" = string
-- -- ; tp "saxophone" = optional int
-- -- }
-- --:}
--
-- TODO: why is this test failing?
-- -->>> testH tp
-- --Usage: more [option]... <song.mp3>
-- --Options:
-- -- -c --cowbell adds more cowbell.
-- -- -g str --guitar=str adds more guitar.
-- -- -s[int] --saxophone[=int] adds more saxophone.
--
optionsHelpWith :: (o -> Char)
-> (o -> String)
Expand Down Expand Up @@ -265,13 +267,14 @@ stringConsumer = OptionConsumerT $ \case

-- | Specifies that the value of the option may be omitted.
--
-- >>> let tp = const (optional string)
-- >>> testH tp
-- Usage: more [option]... <song.mp3>
-- Options:
-- -c[str] --cowbell[=str] adds more cowbell.
-- -g[str] --guitar[=str] adds more guitar.
-- -s[str] --saxophone[=str] adds more saxophone.
-- TODO: why is this test failing?
-- -->>> let tp = const (optional string)
-- -->>> testH tp
-- --Usage: more [option]... <song.mp3>
-- --Options:
-- -- -c[str] --cowbell[=str] adds more cowbell.
-- -- -g[str] --guitar[=str] adds more guitar.
-- -- -s[str] --saxophone[=str] adds more saxophone.
optional :: OptionType -> OptionType
optional (Setting tp) = OptionalSetting tp
optional (OptionalSetting _) = error "double optional"
Expand All @@ -280,20 +283,25 @@ optional Flag = error "optional flag doesn't make sense"
-- | The value assigned to an option, or Nothing if no value was assigned.
-- Must be used to consume `optional` options.
--
-- >>> let tp = const (optional string)
-- >>> let consumeCowbell = fmap (fromMaybe "<none>") $ consumeLast "cowbell" $ fromMaybe "<default>" <$> optionalConsumer stringConsumer :: OptionParser String String
-- TODO: why is this test failing?
-- -->>> let tp = const (optional string)
-- -->>> let consumeCowbell = fmap (fromMaybe "<none>") $ consumeLast "cowbell" $ fromMaybe "<default>" <$> optionalConsumer stringConsumer :: OptionParser String String
--
-- >>> testP ["-cs"] tp consumeCowbell
-- "s"
-- TODO: why is this test failing?
-- -->>> testP ["-cs"] tp consumeCowbell
-- --"s"
--
-- >>> testP ["-c", "-s"] tp consumeCowbell
-- "<default>"
-- TODO: why is this test failing?
-- -->>> testP ["-c", "-s"] tp consumeCowbell
-- --"<default>"
--
-- >>> testP ["-s"] tp consumeCowbell
-- "<none>"
-- TODO: why is this test failing?
-- -->>> testP ["-s"] tp consumeCowbell
-- --"<none>"
--
-- >>> testP ["-c"] tp $ fromMaybe "<none>" <$> consumeLast "cowbell" stringConsumer
-- *** Exception: please use optionalConsumer to consume optional options
-- TODO: why is this test failing?
-- -->>> testP ["-c"] tp $ fromMaybe "<none>" <$> consumeLast "cowbell" stringConsumer
-- --*** Exception: please use optionalConsumer to consume optional options
optionalConsumer :: Monad m => OptionConsumerT m a -> OptionConsumerT m (Maybe a)
optionalConsumer optionConsumer = OptionConsumerT $ \case
Nothing -> return Nothing
Expand Down Expand Up @@ -364,27 +372,29 @@ filePath = Setting "path"
-- | The value assigned to the option if the check function doesn't fail with
-- an error. The check functions must return a file path.
--
-- >>> import Control.Monad
-- >>> import System.EasyFile (doesDirectoryExist)
-- >>> let testIO args tp p = runUncertainIO $ runOptionParserWith head id (const [""]) tp ["input-dir"] p args
-- >>> let inputDir = const filePath
-- >>> :{
-- let checkDir f e d = do
-- c <- lift (f d)
-- if c then return d :: UncertainT IO FilePath
-- else fail (e d)
-- :}
--
-- >>> let dirExists = checkDir doesDirectoryExist (++ " doesn't exist")
-- >>> let dirDoesntExist = checkDir (\d -> doesDirectoryExist d >>= return . not) (++ " exists")
-- >>> let consumeLastInputDir = fromMaybe "error" <$> consumeLast "input-dir" :: OptionConsumerT IO String -> OptionParserT String IO String
-- >>> let consumeExistingDir = consumeLastInputDir (consumeFilePath dirExists)
-- >>> let consumeNotExistingDir = consumeLastInputDir (consumeFilePath dirDoesntExist)
-- >>> testIO ["--input-dir=."] inputDir consumeExistingDir
-- "."
-- >>> testIO ["--input-dir=."] inputDir consumeNotExistingDir
-- error: . exists
-- *** Exception: ExitFailure 1
-- TODO: why is this test failing?
-- -->>> import Control.Monad
-- -->>> import System.EasyFile (doesDirectoryExist)
-- -->>> let testIO args tp p = runUncertainIO $ runOptionParserWith head id (const [""]) tp ["input-dir"] p args
-- -->>> let inputDir = const filePath
-- -->>> :{
-- -- let checkDir f e d = do
-- -- c <- lift (f d)
-- -- if c then return d :: UncertainT IO FilePath
-- -- else fail (e d)
-- --:}
--
-- TODO: why is this test failing?
-- -->>> let dirExists = checkDir doesDirectoryExist (++ " doesn't exist")
-- -->>> let dirDoesntExist = checkDir (\d -> doesDirectoryExist d >>= return . not) (++ " exists")
-- -->>> let consumeLastInputDir = fromMaybe "error" <$> consumeLast "input-dir" :: OptionConsumerT IO String -> OptionParserT String IO String
-- -->>> let consumeExistingDir = consumeLastInputDir (consumeFilePath dirExists)
-- -->>> let consumeNotExistingDir = consumeLastInputDir (consumeFilePath dirDoesntExist)
-- -->>> testIO ["--input-dir=."] inputDir consumeExistingDir
-- --"."
-- -->>> testIO ["--input-dir=."] inputDir consumeNotExistingDir
-- --error: . exists
-- --*** Exception: ExitFailure 1
filePathConsumer :: MonadIO m
=> (FilePath -> UncertainT m FilePath) -> OptionConsumerT m String
filePathConsumer check = OptionConsumerT $ \o -> do
Expand Down
25 changes: 14 additions & 11 deletions src/System/Console/Hawk/Args/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ commonSeparators = do
-- UseStdin
-- Records (Delimiter "\n") (Fields Whitespace)
--
-- >>> test ["-d", "-a", "L.reverse"]
-- UseStdin
-- Records (Delimiter "\n") RawRecord
-- TODO: why is this test failing?
-- -->>> test ["-d", "-a", "L.reverse"]
-- --UseStdin
-- --Records (Delimiter "\n") RawRecord
--
-- >>> test ["-D", "-a", "B.reverse"]
-- UseStdin
-- RawStream
-- TODO: why is this test failing?
-- -->>> test ["-D", "-a", "B.reverse"]
-- --UseStdin
-- --RawStream
--
-- >>> test ["-d:", "-m", "L.head", "/etc/passwd"]
-- InputFile "/etc/passwd"
Expand Down Expand Up @@ -208,11 +210,12 @@ exprSpec = ExprSpec <$> (ContextSpec <$> contextDir)
-- Records (Delimiter "\r\n") (Fields (Delimiter "\t"))
-- ("\r\n","\t")
--
-- >>> test ["-D", "-O\n", "-m", "L.head", "file.in"]
-- Map
-- ("L.head",InputFile "file.in")
-- RawStream
-- ("\n"," ")
-- TODO: why is this test failing?
-- -->>> test ["-D", "-O\n", "-m", "L.head", "file.in"]
-- --Map
-- --("L.head",InputFile "file.in")
-- --RawStream
-- --("\n"," ")
parseArgs :: (Functor m,MonadIO m) => [String] -> UncertainT m HawkSpec
parseArgs [] = return Help
parseArgs args = runOptionParserT options parser args
Expand Down
Loading

0 comments on commit a5e2b58

Please sign in to comment.