|
| 1 | +module BuckArgsTest where |
| 2 | + |
| 3 | +import Data.List.NonEmpty (NonEmpty (..)) |
| 4 | +import Data.Map.Strict qualified as Map |
| 5 | +import Hedgehog (TestT, evalEither, (===)) |
| 6 | +import Test.Run (assertJust, unitTest) |
| 7 | +import Test.Tasty (TestTree, testGroup) |
| 8 | +import Types.Args (TargetId (..)) |
| 9 | +import Types.BuckArgs (BuckArgs (..), Mode (..), parseBuckArgsCli) |
| 10 | +import Types.Grpc (CommandEnv (..), RequestArgs (..)) |
| 11 | + |
| 12 | +-- | Typical metadata command sent by Buck. |
| 13 | +metadataCommand :: [String] |
| 14 | +metadataCommand = |
| 15 | + [ |
| 16 | + "-M", |
| 17 | + "--ghc-dir", "buck/ghc_dir", |
| 18 | + "--worker-target-id", "singleton", |
| 19 | + "--build-plan", "buck/lib1.depends.json", |
| 20 | + "--fields", "exposed_modules,module_graph,package_deps,th_modules,cache", |
| 21 | + "--dep-units", "buck/lib1.json", |
| 22 | + "--unit", "lib1", |
| 23 | + "--ghc-args", "buck/lib1.args" |
| 24 | + ] |
| 25 | + |
| 26 | +-- | Compile-mode command with GHC passthrough flags. |
| 27 | +compileCommand :: [String] |
| 28 | +compileCommand = |
| 29 | + [ |
| 30 | + "-c", |
| 31 | + "--unit", "lib2", |
| 32 | + "--module", "Lib.Module", |
| 33 | + "-O2", |
| 34 | + "-package-db", "buck/db" |
| 35 | + ] |
| 36 | + |
| 37 | +runParser :: [String] -> Either String BuckArgs |
| 38 | +runParser cmd = |
| 39 | + parseBuckArgsCli (CommandEnv Map.empty) (RequestArgs cmd) |
| 40 | + |
| 41 | +test_parseMetadata :: TestT IO () |
| 42 | +test_parseMetadata = do |
| 43 | + BuckArgs {..} |
| 44 | + <- evalEither (runParser metadataCommand) |
| 45 | + assertJust ModeMetadata mode |
| 46 | + assertJust "lib1" unit |
| 47 | + assertJust (TargetId "singleton") workerTargetId |
| 48 | + assertJust "buck/ghc_dir" ghcDirFile |
| 49 | + assertJust "buck/lib1.depends.json" buildPlan |
| 50 | + assertJust ("exposed_modules" :| ["module_graph", "package_deps", "th_modules", "cache"]) fields |
| 51 | + assertJust "buck/lib1.json" depUnits |
| 52 | + assertJust "buck/lib1.args" ghcArgsFile |
| 53 | + [] === ghcOptions |
| 54 | + |
| 55 | +test_parseCompile :: TestT IO () |
| 56 | +test_parseCompile = do |
| 57 | + BuckArgs {mode, unit, moduleName, ghcOptions} <- evalEither (runParser compileCommand) |
| 58 | + assertJust ModeCompile mode |
| 59 | + assertJust "lib2" unit |
| 60 | + assertJust "Lib.Module" moduleName |
| 61 | + ["-O2", "-package-db", "buck/db"] === ghcOptions |
| 62 | + |
| 63 | +test_parseBuckArgs :: TestTree |
| 64 | +test_parseBuckArgs = |
| 65 | + testGroup "parseBuckArgsCli" [ |
| 66 | + unitTest "metadata command" test_parseMetadata, |
| 67 | + unitTest "compile command" test_parseCompile |
| 68 | + ] |
0 commit comments