Skip to content

Commit

Permalink
Update Hspec options
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jul 13, 2023
1 parent e2ea217 commit 2e77f49
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
3 changes: 3 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cradle:
cabal:
component: sensei:test:spec
73 changes: 53 additions & 20 deletions src/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,63 @@ splitArgs args = case break (== "--") $ reverse args of
x : _ -> (dropEnd (length x) args, x)
[] -> (args, [])
where
isHspecArgs :: [String] -> Bool
isHspecArgs xs = case getOpt Permute options xs of
(_, [], []) -> True
(result, [], []) -> all (== Valid) result
_ -> False

dropEnd :: Int -> [a] -> [a]
dropEnd n = reverse . drop n . reverse

options :: [OptDescr ()]
options = map ($ "") [
Option [] ["help"] (NoArg ())
, Option "m" ["match"] (ReqArg (const ()) "PATTERN")
, Option [] ["skip"] (ReqArg (const ()) "PATTERN")
, Option [] ["color"] (NoArg ())
, Option [] ["no-color"] (NoArg ())
, Option "f" ["format"] (ReqArg (const ()) "FORMATTER")
, Option "o" ["out"] (ReqArg (const ()) "FILE")
, Option [] ["depth"] (ReqArg (const ()) "N")
, Option "a" ["qc-max-success"] (ReqArg (const ()) "N")
, Option [] ["qc-max-size"] (ReqArg (const ()) "N")
, Option [] ["qc-max-discard"] (ReqArg (const ()) "N")
, Option [] ["seed"] (ReqArg (const ()) "N")
, Option [] ["print-cpu-time"] (NoArg ())
, Option [] ["focused-only"] (NoArg ())
, Option [] ["dry-run"] (NoArg ())
, Option [] ["fail-fast"] (NoArg ())
, Option "r" ["rerun"] (NoArg ())
data Valid = Valid | Invalid
deriving (Eq, Show)

options :: [OptDescr Valid]
options = concat [
flag "color"
, flag "diff"
, flag "dry-run"
, flag "expert"
, flag "fail-fast"
, flag "fail-on=ITEMS"
, flag "focused-only"
, flag "pretty"
, flag "randomize"
, flag "strict"
, flag "times"
, flag "unicode"
, noArg "" "help"
, noArg "" "ignore-dot-hspec"
, noArg "" "print-cpu-time"
, noArg "" "rerun-all-on-success"
, noArg "r" "rerun"
, reqArg "" "depth" "N"
, reqArg "" "diff-command" "CMD"
, reqArg "" "diff-context" "N"
, reqArg "" "failure-report" "FILE"
, reqArg "" "jobs" "N"
, reqArg "" "qc-max-discard" "N"
, reqArg "" "qc-max-shrinks" "N"
, reqArg "" "qc-max-size" "N"
, reqArg "" "seed" "N"
, reqArg "" "skip" "PATTERN"
, reqArg "a" "qc-max-success" "N"
, reqArg "f" "format" "NAME"
, reqArg "m" "match" "PATTERN"
, [Option "p" ["print-slow-items"] (OptArg (maybe Valid intArg) "N") ""]
]
where
flag :: String -> [OptDescr Valid]
flag name = concat [
noArg "" name
, noArg "" ("no-" <> name)
]

noArg :: [Char] -> String -> [OptDescr Valid]
noArg short name = [Option short [name] (NoArg Valid) ""]

reqArg :: [Char] -> String -> String -> [OptDescr Valid]
reqArg short name arg = [Option short [name] (ReqArg (const Valid) arg) ""]

intArg :: String -> Valid
intArg = maybe Invalid (const Valid) . readMaybe @Int
13 changes: 11 additions & 2 deletions test/OptionsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ main = hspec spec
spec :: Spec
spec = do
describe "splitArgs" $ do
it "returns longest matching list of Hspec arguments from end of given list" $ do
it "returns longest matching list of Hspec options from end of given list" $ do
splitArgs ["foo", "--bar", "-m", "FooSpec", "-a", "1000"] `shouldBe` (["foo", "--bar"], ["-m", "FooSpec", "-a", "1000"])

it "assumes everything after the last '--' to be Hspec arguments" $ do
it "assumes everything after the last '--' to be Hspec options" $ do
splitArgs ["foo", "bar", "--", "foo", "baz"] `shouldBe` (["foo", "bar"], ["foo", "baz"])

it "recognizes -p as an Hspec option" $ do
splitArgs ["-p"] `shouldBe` ([], ["-p"])

it "recognizes -pN as an Hspec option" $ do
splitArgs ["-p20"] `shouldBe` ([], ["-p20"])

it "recognizes -packageNAME as a GHC option" $ do
splitArgs ["-packagebase"] `shouldBe` (["-packagebase"], [])

0 comments on commit 2e77f49

Please sign in to comment.