2019-09-03 20:33:45 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass, DerivingStrategies, GeneralizedNewtypeDeriving, OverloadedStrings, TypeApplications, TypeOperators #-}
|
2019-08-13 21:01:50 +03:00
|
|
|
|
|
|
|
module Main (main) where
|
|
|
|
|
2019-08-16 20:20:08 +03:00
|
|
|
import qualified Analysis.Eval as Eval
|
2019-08-13 21:01:50 +03:00
|
|
|
import Control.Effect
|
2019-08-16 19:34:56 +03:00
|
|
|
import Control.Effect.Fail
|
2019-09-18 21:41:58 +03:00
|
|
|
import Control.Effect.Reader
|
2019-08-16 19:34:56 +03:00
|
|
|
import Control.Monad hiding (fail)
|
2019-08-16 20:20:08 +03:00
|
|
|
import Control.Monad.Catch
|
2019-08-13 21:01:50 +03:00
|
|
|
import Control.Monad.IO.Class
|
2019-08-16 20:20:08 +03:00
|
|
|
import qualified Data.Aeson as Aeson
|
|
|
|
import qualified Data.Aeson.Encode.Pretty as Aeson
|
2019-08-16 19:34:56 +03:00
|
|
|
import qualified Data.ByteString.Char8 as ByteString
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy
|
2019-08-16 20:20:08 +03:00
|
|
|
import qualified Data.ByteString.Streaming.Char8 as ByteStream
|
2019-08-13 21:01:50 +03:00
|
|
|
import Data.Core
|
2019-08-16 20:20:08 +03:00
|
|
|
import Data.Core.Pretty
|
2019-08-16 19:34:56 +03:00
|
|
|
import Data.File
|
2019-08-13 21:01:50 +03:00
|
|
|
import Data.Foldable
|
2019-08-16 20:20:08 +03:00
|
|
|
import Data.Function
|
2019-08-29 16:32:39 +03:00
|
|
|
import Data.List (sort)
|
2019-08-16 19:34:56 +03:00
|
|
|
import Data.Loc
|
2019-08-16 20:20:08 +03:00
|
|
|
import Data.Maybe
|
2019-08-13 21:01:50 +03:00
|
|
|
import Data.Name
|
|
|
|
import Data.Term
|
|
|
|
import GHC.Stack
|
|
|
|
import qualified Language.Python.Core as Py
|
2019-08-16 20:20:08 +03:00
|
|
|
import Prelude hiding (fail)
|
|
|
|
import Streaming
|
|
|
|
import qualified Streaming.Process
|
2019-08-13 21:01:50 +03:00
|
|
|
import System.Directory
|
2019-08-16 20:20:08 +03:00
|
|
|
import System.Exit
|
2019-09-17 17:46:20 +03:00
|
|
|
import qualified TreeSitter.Span as TS (Span)
|
2019-08-13 21:01:50 +03:00
|
|
|
import qualified TreeSitter.Python as TSP
|
|
|
|
import qualified TreeSitter.Python.AST as TSP
|
|
|
|
import qualified TreeSitter.Unmarshal as TS
|
2019-09-11 18:45:21 +03:00
|
|
|
import Text.Show.Pretty (ppShow)
|
2019-09-18 21:41:58 +03:00
|
|
|
import qualified System.Path as Path
|
|
|
|
import qualified System.Path.Directory as Path
|
|
|
|
import System.Path ((</>))
|
2019-08-13 21:01:50 +03:00
|
|
|
|
|
|
|
import qualified Test.Tasty as Tasty
|
|
|
|
import qualified Test.Tasty.HUnit as HUnit
|
|
|
|
|
2019-08-16 22:54:17 +03:00
|
|
|
import Analysis.ScopeGraph
|
2019-09-03 20:34:00 +03:00
|
|
|
import qualified Directive
|
|
|
|
import Instances ()
|
2019-08-16 19:34:56 +03:00
|
|
|
|
2019-09-19 01:07:43 +03:00
|
|
|
|
2019-09-11 18:45:21 +03:00
|
|
|
assertJQExpressionSucceeds :: Show a => Directive.Directive -> a -> Term (Ann :+: Core) Name -> HUnit.Assertion
|
|
|
|
assertJQExpressionSucceeds directive tree core = do
|
2019-08-16 22:54:17 +03:00
|
|
|
bod <- case scopeGraph Eval.eval [File interactive core] of
|
2019-09-04 19:47:43 +03:00
|
|
|
(heap, [File _ (Right result)]) -> pure $ Aeson.object
|
|
|
|
[ "scope" Aeson..= heap
|
|
|
|
, "heap" Aeson..= result
|
|
|
|
, "tree" Aeson..= Aeson.toJSON1 core
|
|
|
|
]
|
2019-09-03 20:34:00 +03:00
|
|
|
_other -> HUnit.assertFailure "Couldn't run scope dumping mechanism; this shouldn't happen"
|
2019-08-16 20:20:08 +03:00
|
|
|
|
|
|
|
let ignore = ByteStream.effects . hoist ByteStream.effects
|
2019-08-16 22:54:17 +03:00
|
|
|
sgJSON = ByteStream.fromLazy $ Aeson.encode bod
|
|
|
|
jqPipeline = Streaming.Process.withStreamingProcess (Directive.toProcess directive) sgJSON ignore
|
2019-08-16 20:20:08 +03:00
|
|
|
errorMsg = "jq(1) returned non-zero exit code"
|
|
|
|
dirMsg = "jq expression: " <> show directive
|
2019-08-16 22:54:17 +03:00
|
|
|
jsonMsg = "JSON value: " <> ByteString.Lazy.unpack (Aeson.encodePretty bod)
|
2019-09-11 18:45:21 +03:00
|
|
|
astMsg = "AST (pretty): " <> ppShow tree
|
|
|
|
treeMsg = "Core expr (pretty): " <> showCore (stripAnnotations core)
|
|
|
|
treeMsg' = "Core expr (Show): " <> ppShow (stripAnnotations core)
|
|
|
|
|
2019-08-16 20:20:08 +03:00
|
|
|
|
|
|
|
catch @_ @Streaming.Process.ProcessExitedUnsuccessfully jqPipeline $ \err -> do
|
2019-09-11 18:45:21 +03:00
|
|
|
HUnit.assertFailure (unlines [errorMsg, dirMsg, jsonMsg, astMsg, treeMsg, treeMsg', show err])
|
2019-08-16 20:20:08 +03:00
|
|
|
|
2019-09-18 21:41:58 +03:00
|
|
|
fixtureTestTreeForFile :: HasCallStack => Path.RelFile -> Tasty.TestTree
|
|
|
|
fixtureTestTreeForFile fp = HUnit.testCaseSteps (Path.toString fp) $ \step -> withFrozenCallStack $ do
|
|
|
|
let fullPath = Path.relDir "semantic-python/test/fixtures" </> fp
|
|
|
|
|
|
|
|
fileContents <- ByteString.readFile (Path.toString fullPath)
|
2019-08-29 15:52:38 +03:00
|
|
|
directives <- case Directive.parseDirectives fileContents of
|
2019-08-27 18:29:12 +03:00
|
|
|
Right dir -> pure dir
|
|
|
|
Left err -> HUnit.assertFailure ("Directive parsing error: " <> err)
|
|
|
|
|
|
|
|
result <- TS.parseByteString TSP.tree_sitter_python fileContents
|
2019-09-18 21:41:58 +03:00
|
|
|
let coreResult = fmap (Control.Effect.run
|
|
|
|
. runFail
|
|
|
|
. runReader fp
|
|
|
|
. Py.compile @(TSP.Module TS.Span) @_ @(Term (Ann :+: Core))) result
|
2019-08-29 16:20:13 +03:00
|
|
|
for_ directives $ \directive -> do
|
|
|
|
step (Directive.describe directive)
|
2019-09-19 01:07:43 +03:00
|
|
|
case (coreResult, directive) of
|
|
|
|
(Right (Left _), Directive.Fails) -> pure ()
|
|
|
|
(Left err, _) -> HUnit.assertFailure ("Parsing failed: " <> err)
|
|
|
|
(Right (Left err), _) -> HUnit.assertFailure ("Compilation failed: " <> err)
|
|
|
|
(Right (Right _), Directive.Fails) -> HUnit.assertFailure ("Expected translation to fail")
|
|
|
|
(Right (Right item), Directive.JQ _) -> assertJQExpressionSucceeds directive result item
|
|
|
|
(Right (Right item), Directive.Tree t) -> let msg = "lhs = " <> showCore t <> "\n rhs " <> showCore item'
|
|
|
|
item' = stripAnnotations item
|
|
|
|
in HUnit.assertEqual msg t item' where
|
2019-08-13 21:01:50 +03:00
|
|
|
|
2019-08-29 16:20:13 +03:00
|
|
|
milestoneFixtures :: IO Tasty.TestTree
|
|
|
|
milestoneFixtures = do
|
2019-09-18 21:41:58 +03:00
|
|
|
files <- liftIO (Path.filesInDir (Path.relDir "semantic-python/test/fixtures"))
|
|
|
|
let pythons = sort (filter (Path.hasExtension ".py") files)
|
2019-08-29 16:30:58 +03:00
|
|
|
pure $ Tasty.testGroup "Translation" (fmap fixtureTestTreeForFile pythons)
|
2019-08-13 21:01:50 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
2019-08-16 19:34:56 +03:00
|
|
|
main = do
|
|
|
|
jq <- findExecutable "jq"
|
|
|
|
when (isNothing jq) (die "Error: jq(1) not found in $PATH.")
|
2019-08-29 16:20:13 +03:00
|
|
|
milestoneFixtures >>= Tasty.defaultMain
|