Skip to content

Commit

Permalink
WIP commit to make parse_examples work
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickt committed Jun 30, 2020
1 parent 20b7982 commit 8437814
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 19 deletions.
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,7 @@ exports_files(["src/node-types.json"])
commit = "41a408d5b996ef54d8b9e1b9a2469fad00c1b52b",
remote = "https://github.com/tree-sitter/tree-sitter-php.git",
)

load("//:build/example_repos.bzl", "declare_example_repos")

declare_example_repos()
38 changes: 38 additions & 0 deletions build/example_repos.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load(
"@bazel_tools//tools/build_defs/repo:git.bzl",
"git_repository",
"new_git_repository",
)

all_example_repos = {
"golang": {
"data": ["src/**/*.go"],
"commit": "870e12d7bfaea70fb0d743842f5864eb059cb939",
"repo": "golang/go",
"since": "1533485518 -0700",
},
"moby": {
"data": ["**/*.go"],
"commit": "f57f260b49b6142366e6bc1274204ee0a1205945",
"repo": "moby/moby",
"since": "1533485518 -0700",
},
}

def example_repo(name, data, commit, repo):
new_git_repository(
name = name,
build_file_content = """
filegroup(
name = "src",
data = glob({}),
visibility = ["//visibility:public"]
)
""".format(data),
commit = commit,
remote = "https://github.com/{}.git".format(repo),
)

def declare_example_repos():
for k, v in all_example_repos.items():
example_repo(name = k, data = v["data"], commit = v["commit"], repo = v["repo"])
22 changes: 20 additions & 2 deletions semantic/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,24 @@ haskell_binary(
],
)

haskell_library(
name = "fixtureshim",
srcs = ["test/System/Path/Bazel.hs"],
deps = [
"//:base",
"@stackage//:bazel-runfiles",
"@stackage//:pathtype",
],
)

haskell_test(
name = "spec",
srcs = glob(
include = ["test/**/*.hs"],
exclude = [
"test/fixtures/**/*.hs",
"test/Examples.hs",
"test/System/Path/Bazel.hs",
],
),
compiler_flags = STANDARD_GHC_WARNINGS + STANDARD_EXECUTABLE_FLAGS + [
Expand All @@ -140,6 +151,7 @@ haskell_test(
"test/fixtures/cli/*.json",
]),
deps = semantic_common_dependencies + [
":fixtureshim",
"//:base",
"//semantic",
"//semantic-proto",
Expand All @@ -166,7 +178,9 @@ haskell_test(

haskell_test(
name = "parse-examples",
srcs = ["test/Examples.hs"],
srcs = [
"test/Examples.hs",
],
compiler_flags = STANDARD_GHC_WARNINGS + STANDARD_EXECUTABLE_FLAGS + [
"-XStrictData",
],
Expand All @@ -178,8 +192,12 @@ haskell_test(
"test/fixtures/javascript/**/*.js",
"test/fixtures/typescript/**/*.ts",
"test/fixtures/cli/*.json",
]),
]) + [
"@golang//:src",
"@moby//:src",
],
deps = semantic_common_dependencies + [
":fixtureshim",
"//:base",
"//semantic",
"//semantic-proto",
Expand Down
45 changes: 29 additions & 16 deletions semantic/test/Examples.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -25,7 +26,10 @@ import Data.Traversable
import System.FilePath.Glob
import System.Path ((</>))
import qualified System.Path as Path
import qualified System.Path.Directory as Path
import qualified System.Process as Process
import qualified System.Path.Bazel as Fixture
import qualified Bazel.Runfiles as Runfiles
import qualified Test.Tasty as Tasty
import qualified Test.Tasty.HUnit as HUnit

Expand Down Expand Up @@ -127,23 +131,29 @@ typescriptSkips = Path.relFile <$>
, "npm/node_modules/request/node_modules/har-validator/node_modules/ajv/dist/regenerator.min.js"
]

buildExamples :: TaskSession -> LanguageExample -> Path.RelDir -> IO Tasty.TestTree
buildExamples :: Fixture.HasBazel => TaskSession -> LanguageExample -> Path.RelDir -> IO Tasty.TestTree
buildExamples session lang tsDir = do
let fileSkips = fmap (tsDir </>) (languageSkips lang)
dirSkips = fmap (tsDir </>) (languageDirSkips lang)
files <- globDir1 (compile (languageExtension lang)) (Path.toString tsDir)
let paths = filter (\x -> Path.takeDirectory x `notElem` dirSkips) . filter (`notElem` fileSkips) $ Path.relFile <$> files
trees <- for paths $ \file -> do
pure . HUnit.testCase (Path.toString file) $ do
precise <- runTask session (runParse (parseSymbolsFilePath preciseLanguageModes file))
assertOK "precise" precise
pure (Tasty.testGroup (languageName lang) trees)

where
assertOK msg = either (\e -> HUnit.assertFailure (msg <> " failed to parse" <> show e)) (refuteErrors msg)
refuteErrors msg a = case toList (a^.files) of
[x] | (e:_) <- toList (x^.errors) -> HUnit.assertFailure (msg <> " parse errors " <> show e)
_ -> pure ()
pure $ Tasty.testGroup "examples"
[ HUnit.testCase "golang" $ do
for_ goFileSkips $ \fp -> do
does <- Path.doesFileExist fp
HUnit.assertBool ("Can't find " <> Path.toString fp) does
]
-- let fileSkips = fmap (tsDir </>) (languageSkips lang)
-- dirSkips = fmap (tsDir </>) (languageDirSkips lang)
-- files <- globDir1 (compile (languageExtension lang)) (Path.toString tsDir)
-- let paths = filter (\x -> Path.takeDirectory x `notElem` dirSkips) . filter (`notElem` fileSkips) $ Path.relFile <$> files
-- trees <- for paths $ \file -> do
-- pure . HUnit.testCase (Path.toString file) $ do
-- precise <- runTask session (runParse (parseSymbolsFilePath preciseLanguageModes file))
-- assertOK "precise" precise
-- pure (Tasty.testGroup (languageName lang) trees)

-- where
-- assertOK msg = either (\e -> HUnit.assertFailure (msg <> " failed to parse" <> show e)) (refuteErrors msg)
-- refuteErrors msg a = case toList (a^.files) of
-- [x] | (e:_) <- toList (x^.errors) -> HUnit.assertFailure (msg <> " parse errors " <> show e)
-- _ -> pure ()

data SortableSymbol = SortableSymbol Text.Text Int32 Int32 Int32 Int32
deriving (Eq, Show, Ord)
Expand All @@ -160,6 +170,9 @@ main :: IO ()
main = withOptions testOptions $ \ config logger statter -> do
void $ Process.system "script/clone-example-repos"

rf <- Runfiles.create
let ?runfiles = rf

let session = TaskSession config "-" False logger statter

allTests <- forConcurrently examples $ \lang@LanguageExample{..} -> do
Expand Down
4 changes: 3 additions & 1 deletion semantic/test/System/Path/Bazel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module System.Path.Bazel
( bazelFile,
bazelFile',
absRelFile,
bazelDir,
HasBazel,
)
Expand All @@ -18,8 +19,9 @@ type HasBazel = (?runfiles :: Bazel.Runfiles, HasCallStack)
bazelFile :: (HasBazel) => String -> Path.AbsFile
bazelFile x = Path.absFile (Bazel.rlocation ?runfiles ("semantic/semantic/" <> x))

bazelFile' :: (HasBazel) => String -> Path.AbsRelFile
bazelFile', absRelFile :: (HasBazel) => String -> Path.AbsRelFile
bazelFile' = Path.toAbsRel . bazelFile
absRelFile = bazelFile'

bazelDir :: HasBazel => String -> Path.AbsDir
bazelDir x = Path.absDir (Bazel.rlocation ?runfiles ("semantic/semantic/" <> x))

0 comments on commit 8437814

Please sign in to comment.