2019-02-01 01:13:26 +03:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2019-09-24 10:17:25 +03:00
|
|
|
{-# OPTIONS_GHC -O1 #-}
|
2018-09-18 01:49:18 +03:00
|
|
|
module Main (main) where
|
|
|
|
|
2019-02-01 01:13:26 +03:00
|
|
|
import Control.Effect
|
2018-09-18 01:49:18 +03:00
|
|
|
import Control.Exception (displayException)
|
2019-09-24 10:17:25 +03:00
|
|
|
import qualified Control.Foldl as Foldl
|
|
|
|
import Data.Function ((&))
|
|
|
|
import Control.Concurrent.Async (forConcurrently)
|
2018-09-18 23:00:31 +03:00
|
|
|
import Control.Monad
|
2019-02-01 01:13:26 +03:00
|
|
|
import Control.Monad.IO.Class
|
2019-09-24 10:17:25 +03:00
|
|
|
import Control.Monad.Trans.Resource (ResIO, runResourceT)
|
|
|
|
import Data.Blob
|
2018-09-18 01:49:18 +03:00
|
|
|
import qualified Data.ByteString as B
|
|
|
|
import Data.ByteString.Builder
|
|
|
|
import qualified Data.ByteString.Char8 as BC
|
2019-09-24 10:17:25 +03:00
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BLC
|
|
|
|
import qualified Data.ByteString.Streaming.Char8 as ByteStream
|
2018-09-18 01:49:18 +03:00
|
|
|
import Data.Either
|
2019-09-24 10:17:25 +03:00
|
|
|
import Data.Set (Set)
|
|
|
|
import Data.Traversable
|
|
|
|
import Data.Typeable
|
|
|
|
import qualified Streaming.Prelude as Stream
|
2018-09-18 01:49:18 +03:00
|
|
|
import System.FilePath.Glob
|
2019-09-20 18:34:09 +03:00
|
|
|
import System.Path ((</>))
|
2019-09-24 10:17:25 +03:00
|
|
|
import qualified System.Path as Path
|
|
|
|
import qualified System.Process as Process
|
2018-09-18 01:49:18 +03:00
|
|
|
|
2019-09-24 10:17:25 +03:00
|
|
|
import Data.Flag
|
|
|
|
import Semantic.Api (TermOutputFormat (..), parseTermBuilder)
|
|
|
|
import Semantic.Config as Config
|
|
|
|
import Semantic.Task
|
|
|
|
import Semantic.Task.Files
|
2018-09-18 01:49:18 +03:00
|
|
|
|
2019-09-24 10:17:25 +03:00
|
|
|
import qualified Test.Tasty as Tasty
|
|
|
|
import qualified Test.Tasty.HUnit as HUnit
|
2018-09-18 01:49:18 +03:00
|
|
|
|
2018-09-18 02:46:45 +03:00
|
|
|
data LanguageExample
|
|
|
|
= LanguageExample
|
2019-09-24 10:17:25 +03:00
|
|
|
{ languageName :: String
|
|
|
|
, languageExtension :: String
|
|
|
|
, languageExampleDir :: Path.RelDir
|
2019-09-20 18:34:09 +03:00
|
|
|
, languageKnownFailuresTxt :: Maybe Path.RelFile
|
2018-09-18 02:46:45 +03:00
|
|
|
} deriving (Eq, Show)
|
|
|
|
|
2019-09-20 18:34:09 +03:00
|
|
|
le :: String -> String -> Path.RelDir -> Maybe Path.RelFile -> LanguageExample
|
2018-09-18 02:46:45 +03:00
|
|
|
le = LanguageExample
|
2018-09-18 01:49:18 +03:00
|
|
|
|
2019-09-24 10:17:25 +03:00
|
|
|
examples :: [LanguageExample]
|
|
|
|
examples =
|
2019-09-20 18:34:09 +03:00
|
|
|
[ le "python" ".py" examples (Just $ Path.relFile "script/known_failures.txt")
|
|
|
|
, le "ruby" ".rb" examples (Just $ Path.relFile "script/known_failures.txt")
|
|
|
|
, le "typescript" ".ts" examples (Just $ Path.relFile "typescript/script/known_failures.txt")
|
|
|
|
, le "typescript" ".tsx" examples (Just $ Path.relFile "typescript/script/known_failures.txt")
|
|
|
|
, le "typescript" ".js" examples Nothing -- parse JavaScript with TypeScript parser.
|
|
|
|
, le "go" ".go" examples (Just $ Path.relFile "script/known-failures.txt")
|
2018-09-18 23:00:31 +03:00
|
|
|
|
|
|
|
-- TODO: Java assignment errors need to be investigated
|
2018-09-18 02:46:45 +03:00
|
|
|
-- , le "java" ".java" "examples/guava" (Just "script/known_failures_guava.txt")
|
|
|
|
-- , le "java" ".java" "examples/elasticsearch" (Just "script/known_failures_elasticsearch.txt")
|
|
|
|
-- , le "java" ".java" "examples/RxJava" (Just "script/known_failures_RxJava.txt")
|
2018-09-18 23:00:31 +03:00
|
|
|
|
|
|
|
-- TODO: Haskell assignment errors need to be investigated
|
2018-09-18 02:46:45 +03:00
|
|
|
-- , le "haskell" ".hs" "examples/effects" (Just "script/known-failures-effects.txt")
|
|
|
|
-- , le "haskell" ".hs" "examples/postgrest" (Just "script/known-failures-postgrest.txt")
|
|
|
|
-- , le "haskell" ".hs" "examples/ivory" (Just "script/known-failures-ivory.txt")
|
2018-09-18 01:49:18 +03:00
|
|
|
|
|
|
|
-- , ("php", ".php") -- TODO: No parse-examples in tree-sitter yet
|
2019-09-20 18:34:09 +03:00
|
|
|
] where examples = Path.relDir "examples"
|
2018-09-18 01:49:18 +03:00
|
|
|
|
2019-09-24 10:17:25 +03:00
|
|
|
buildExamples :: TaskSession -> LanguageExample -> Path.RelDir -> IO Tasty.TestTree
|
|
|
|
buildExamples session lang tsDir = do
|
|
|
|
knownFailures <- knownFailuresForPath tsDir (languageKnownFailuresTxt lang)
|
|
|
|
files <- globDir1 (compile ("**/*" <> languageExtension lang)) (Path.toString (tsDir </> languageExampleDir lang))
|
|
|
|
let paths = Path.relFile <$> files
|
2019-09-24 23:07:23 +03:00
|
|
|
trees <- forConcurrently paths $ \file -> pure $ HUnit.testCase (Path.toString file) $ do
|
2019-09-24 10:17:25 +03:00
|
|
|
res <- runTask session (parseFilePath file)
|
|
|
|
case res of
|
|
|
|
Left (SomeException e) -> case cast e of
|
|
|
|
-- We have a number of known assignment timeouts, consider these pending specs instead of failing the build.
|
|
|
|
Just AssignmentTimedOut -> pure ()
|
|
|
|
Just ParserTimedOut -> pure ()
|
|
|
|
-- Other exceptions are true failures
|
|
|
|
_ -> HUnit.assertFailure (show (displayException e))
|
|
|
|
_ -> if file `elem` knownFailures
|
|
|
|
then pure ()
|
|
|
|
else (isRight res) HUnit.@? ("Error: " <> either show show res)
|
|
|
|
pure (Tasty.testGroup (languageName lang) trees)
|
|
|
|
|
|
|
|
testOptions :: Config.Options
|
|
|
|
testOptions = defaultOptions
|
|
|
|
{ optionsFailOnWarning = flag FailOnWarning True
|
|
|
|
, optionsLogLevel = Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = withOptions testOptions $ \ config logger statter -> do
|
|
|
|
void $ Process.system "script/clone-example-repos"
|
|
|
|
|
|
|
|
let session = TaskSession config "-" False logger statter
|
|
|
|
|
|
|
|
allTests <- forConcurrently examples $ \lang@LanguageExample{..} -> do
|
|
|
|
let tsLang = Path.relDir ("tree-sitter-" <> languageName)
|
|
|
|
let tsDir = Path.relDir "tmp/haskell-tree-sitter" </> tsLang </> Path.relDir "vendor" </> tsLang
|
|
|
|
buildExamples session lang tsDir
|
|
|
|
|
|
|
|
Tasty.defaultMain $ Tasty.testGroup "parse-examples" allTests
|
|
|
|
|
|
|
|
knownFailuresForPath :: Path.RelDir -> Maybe Path.RelFile -> IO (Set Path.RelFile)
|
|
|
|
knownFailuresForPath _ Nothing = pure mempty
|
|
|
|
knownFailuresForPath tsDir (Just path)
|
|
|
|
= runResourceT
|
|
|
|
( ByteStream.readFile @ResIO (Path.toString (tsDir </> path))
|
|
|
|
& ByteStream.lines
|
|
|
|
& ByteStream.denull
|
|
|
|
& Stream.mapped ByteStream.toLazy
|
|
|
|
& Stream.filter ((/= '#') . BLC.head)
|
|
|
|
& Stream.map (Path.relFile . BLC.unpack)
|
|
|
|
& Foldl.purely Stream.fold_ Foldl.set
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-09-20 19:48:36 +03:00
|
|
|
parseFilePath :: (Member (Error SomeException) sig, Member Distribute sig, Member Task sig, Member Files sig, Carrier sig m, MonadIO m) => Path.RelFile -> m Bool
|
|
|
|
parseFilePath path = readBlob (fileForRelPath path) >>= parseTermBuilder @[] TermShow . pure >>= const (pure True)
|