1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00

Enable multiple directives.

This commit is contained in:
Patrick Thomson 2019-08-29 08:52:38 -04:00
parent 1472fc87e7
commit 31fbc386e9
2 changed files with 12 additions and 9 deletions

View File

@ -1,11 +1,12 @@
module Directive ( Directive (..)
, parseDirective
, parseDirectives
, toProcess
) where
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as ByteString
import Data.List.NonEmpty (NonEmpty)
import Data.Coerce
import System.Process
import qualified Text.Trifecta as Trifecta
@ -49,9 +50,12 @@ jq = do
directive :: Trifecta.Parser Directive
directive = fails <|> jq
parseDirective :: ByteString -> Either String Directive
parseDirective = Trifecta.foldResult (Left . show) Right
. Trifecta.parseByteString (directive <* Trifecta.eof) mempty
toplevel :: Trifecta.Parser (NonEmpty Directive)
toplevel = directive `Trifecta.sepEndByNonEmpty` Trifecta.char '\n'
parseDirectives :: ByteString -> Either String (NonEmpty Directive)
parseDirectives = Trifecta.foldResult (Left . show) Right
. Trifecta.parseByteString toplevel mempty
toProcess :: Directive -> CreateProcess
toProcess (JQ d) = proc "jq" ["-e", ByteString.unpack d]

View File

@ -77,14 +77,13 @@ assertJQExpressionSucceeds directive core = do
assertTranslationSucceeds :: HasCallStack => FilePath -> HUnit.Assertion
assertTranslationSucceeds fp = withFrozenCallStack $ do
fileContents <- ByteString.readFile ("semantic-python/test/fixtures" </> fp)
let first = ByteString.takeWhile (/= '\n') fileContents
directive <- case Directive.parseDirective first of
directives <- case Directive.parseDirectives fileContents of
Right dir -> pure dir
Left err -> HUnit.assertFailure ("Directive parsing error: " <> err)
result <- TS.parseByteString TSP.tree_sitter_python fileContents
let coreResult = fmap (Control.Effect.run . runFail . Py.compile @TSP.Module @_ @(Term (Ann :+: Core))) result
case coreResult of
for_ directives $ \directive -> case coreResult of
Right (Left _) | directive == Directive.Fails -> pure ()
Right (Right item) -> assertJQExpressionSucceeds directive item
Right (Left err) -> HUnit.assertFailure ("Compilation failed: " <> err)
@ -94,8 +93,8 @@ assertTranslationSucceeds fp = withFrozenCallStack $ do
milestoneFixtures :: Tasty.TestTree
milestoneFixtures = HUnit.testCaseSteps "Bootstrapping" $ \step -> do
files <- liftIO (listDirectory "semantic-python/test/fixtures")
let firstGroup = sort $ filter ((== '1') . head) files
for_ firstGroup $ \file -> do
let pyFiles = filter (isExtensionOf ".py") files
for_ pyFiles $ \file -> do
step file
assertTranslationSucceeds file