From 8437814fcf59128bed966d0852c4d0ecaa1d5bf1 Mon Sep 17 00:00:00 2001 From: Patrick Thomson Date: Tue, 30 Jun 2020 13:12:11 -0400 Subject: [PATCH] WIP commit to make parse_examples work --- WORKSPACE | 4 +++ build/example_repos.bzl | 38 ++++++++++++++++++++++++++ semantic/BUILD.bazel | 22 +++++++++++++-- semantic/test/Examples.hs | 43 +++++++++++++++++++----------- semantic/test/System/Path/Bazel.hs | 4 ++- 5 files changed, 93 insertions(+), 18 deletions(-) create mode 100644 build/example_repos.bzl diff --git a/WORKSPACE b/WORKSPACE index 2e54778fa..718e65f41 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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() diff --git a/build/example_repos.bzl b/build/example_repos.bzl new file mode 100644 index 000000000..c38c5c6de --- /dev/null +++ b/build/example_repos.bzl @@ -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"]) diff --git a/semantic/BUILD.bazel b/semantic/BUILD.bazel index 0204d1f56..ec02329af 100644 --- a/semantic/BUILD.bazel +++ b/semantic/BUILD.bazel @@ -118,6 +118,16 @@ 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( @@ -125,6 +135,7 @@ haskell_test( exclude = [ "test/fixtures/**/*.hs", "test/Examples.hs", + "test/System/Path/Bazel.hs", ], ), compiler_flags = STANDARD_GHC_WARNINGS + STANDARD_EXECUTABLE_FLAGS + [ @@ -140,6 +151,7 @@ haskell_test( "test/fixtures/cli/*.json", ]), deps = semantic_common_dependencies + [ + ":fixtureshim", "//:base", "//semantic", "//semantic-proto", @@ -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", ], @@ -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", diff --git a/semantic/test/Examples.hs b/semantic/test/Examples.hs index e52f0be57..453de35fd 100644 --- a/semantic/test/Examples.hs +++ b/semantic/test/Examples.hs @@ -1,5 +1,6 @@ {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE ImplicitParams #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE TypeApplications #-} @@ -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 @@ -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) + 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 () + -- 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) @@ -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 diff --git a/semantic/test/System/Path/Bazel.hs b/semantic/test/System/Path/Bazel.hs index 464dbf911..3e202d5a6 100644 --- a/semantic/test/System/Path/Bazel.hs +++ b/semantic/test/System/Path/Bazel.hs @@ -4,6 +4,7 @@ module System.Path.Bazel ( bazelFile, bazelFile', + absRelFile, bazelDir, HasBazel, ) @@ -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))