1
1
mirror of https://github.com/github/semantic.git synced 2024-11-23 08:27:56 +03:00

fixing some of these tests

This commit is contained in:
Patrick Thomson 2020-06-10 11:36:47 -04:00
parent 1a6447c18e
commit 91879718e9
4 changed files with 50 additions and 24 deletions

View File

@ -138,7 +138,14 @@ haskell_test(
compiler_flags = STANDARD_GHC_WARNINGS + [
"-XStrictData",
],
data = glob(include = ["test/fixtures/**/*.json"]),
data = glob(include = [
"test/fixtures/**/*.json",
"test/fixtures/go/**/*.go",
"test/fixtures/python/**/*.py",
"test/fixtures/ruby/**/*.rb",
"test/fixtures/javascript/**/*.js",
"test/fixtures/typescript/**/*.ts",
]),
src_strip_prefix = "test",
deps = semantic_common_dependencies + [
":base",

View File

@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE TypeApplications #-}
module Graphing.Calls.Spec ( spec ) where
@ -17,8 +18,9 @@ import Data.Graph.ControlFlowVertex
import qualified Data.Language as Language
import Semantic.Graph
import qualified System.Path as Path
import qualified System.Path.Bazel as Path
callGraphPythonProject :: Path.RelFile -> IO (Semantic.Graph.Graph ControlFlowVertex)
callGraphPythonProject :: Path.AbsFile -> IO (Semantic.Graph.Graph ControlFlowVertex)
callGraphPythonProject path = runTaskOrDie $ do
let proxy = Proxy @'Language.Python
lang = Language.Python
@ -28,24 +30,24 @@ callGraphPythonProject path = runTaskOrDie $ do
modules <- topologicalSort <$> runImportGraphToModules proxy package
runCallGraph proxy False modules package
spec :: Spec
spec :: Path.HasBazel => Spec
spec = describe "call graphing" $ do
let needs r v = unGraph r `shouldSatisfy` hasVertex v
it "should work for a simple example" $ do
res <- callGraphPythonProject (Path.relFile "test/fixtures/python/graphing/simple/simple.py")
res <- callGraphPythonProject (Path.bazelFile "test/fixtures/python/graphing/simple/simple.py")
res `needs` Variable "magnus" "simple.py" (Span (Pos 4 1) (Pos 4 7))
it "should evaluate both sides of an if-statement" $ do
res <- callGraphPythonProject (Path.relFile "test/fixtures/python/graphing/conditional/conditional.py")
res <- callGraphPythonProject (Path.bazelFile "test/fixtures/python/graphing/conditional/conditional.py")
res `needs` Variable "merle" "conditional.py" (Span (Pos 5 5) (Pos 5 10))
res `needs` Variable "taako" "conditional.py" (Span (Pos 8 5) (Pos 8 10))
it "should continue even when a type error is encountered" $ do
res <- callGraphPythonProject (Path.relFile "test/fixtures/python/graphing/typeerror/typeerror.py")
res <- callGraphPythonProject (Path.bazelFile "test/fixtures/python/graphing/typeerror/typeerror.py")
res `needs` Variable "lup" "typeerror.py" (Span (Pos 5 1) (Pos 5 4))
it "should continue when an unbound variable is encountered" $ do
res <- callGraphPythonProject (Path.relFile "test/fixtures/python/graphing/unbound/unbound.py")
res <- callGraphPythonProject (Path.bazelFile "test/fixtures/python/graphing/unbound/unbound.py")
res `needs` Variable "lucretia" "unbound.py" (Span (Pos 5 1) (Pos 5 9))

View File

@ -0,0 +1,16 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE ImplicitParams #-}
module System.Path.Bazel
( bazelFile,
HasBazel,
)
where
import qualified Bazel.Runfiles as Bazel
import qualified System.Path as Path
type HasBazel = ?runfiles :: Bazel.Runfiles
bazelFile :: HasBazel => String -> Path.AbsFile
bazelFile x = Path.absFile (Bazel.rlocation ?runfiles ("semantic/semantic/" <> x))

View File

@ -7,81 +7,82 @@ import Semantic.Api.Symbols
import Source.Loc
import SpecHelpers
import qualified System.Path as Path
import qualified System.Path.Bazel as Path
import Tags.Tagging.Precise
spec :: Spec
spec :: Path.HasBazel => Spec
spec = do
describe "go" $ do
it "produces tags for functions with docs (TODO)" $
parseTestFile [P.FUNCTION] (Path.relFile "test/fixtures/go/tags/simple_functions.go") `shouldReturn`
parseTestFile [P.FUNCTION] (Path.bazelFile "test/fixtures/go/tags/simple_functions.go") `shouldReturn`
[ Tag "TestFromBits" P.FUNCTION P.DEFINITION (Loc (Range 56 68) (Span (Pos 6 6) (Pos 6 18))) "func TestFromBits(t *testing.T) {" Nothing
, Tag "Hi" P.FUNCTION P.DEFINITION (Loc (Range 99 101) (Span (Pos 10 6) (Pos 10 8))) "func Hi() {" Nothing ]
it "produces tags for methods" $
parseTestFile [] (Path.relFile "test/fixtures/go/tags/method.go") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/go/tags/method.go") `shouldReturn`
[ Tag "CheckAuth" P.METHOD P.DEFINITION (Loc (Range 39 48) (Span (Pos 3 21) (Pos 3 30))) "func (c *apiClient) CheckAuth(req *http.Request, user, repo string) (*authenticatedActor, error) {}" Nothing]
it "produces tags for calls" $
parseTestFile [P.CALL] (Path.relFile "test/fixtures/go/tags/simple_functions.go") `shouldReturn`
parseTestFile [P.CALL] (Path.bazelFile "test/fixtures/go/tags/simple_functions.go") `shouldReturn`
[ Tag "Hi" P.CALL P.REFERENCE (Loc (Range 86 88) (Span (Pos 7 2) (Pos 7 4))) "Hi()" Nothing]
describe "javascript and typescript" $ do
it "produces tags for functions with docs (TODO)" $
parseTestFile [] (Path.relFile "test/fixtures/javascript/tags/simple_function_with_docs.js") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/javascript/tags/simple_function_with_docs.js") `shouldReturn`
[ Tag "myFunction" P.FUNCTION P.DEFINITION (Loc (Range 31 41) (Span (Pos 2 10) (Pos 2 20))) "function myFunction() {" Nothing ]
it "produces tags for classes" $
parseTestFile [] (Path.relFile "test/fixtures/typescript/tags/class.ts") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/typescript/tags/class.ts") `shouldReturn`
[ Tag "FooBar" P.CLASS P.DEFINITION (Loc (Range 6 12) (Span (Pos 1 7) (Pos 1 13))) "class FooBar {}" Nothing ]
it "produces tags for modules" $
parseTestFile [] (Path.relFile "test/fixtures/typescript/tags/module.ts") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/typescript/tags/module.ts") `shouldReturn`
[ Tag "APromise" P.MODULE P.DEFINITION (Loc (Range 7 15) (Span (Pos 1 8) (Pos 1 16))) "module APromise { }" Nothing ]
describe "python" $ do
it "produces tags for functions" $
parseTestFile [] (Path.relFile "test/fixtures/python/tags/simple_functions.py") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/python/tags/simple_functions.py") `shouldReturn`
[ Tag "Foo" P.FUNCTION P.DEFINITION (Loc (Range 4 7) (Span (Pos 1 5) (Pos 1 8))) "def Foo(x):" Nothing
, Tag "Bar" P.FUNCTION P.DEFINITION (Loc (Range 74 77) (Span (Pos 7 5) (Pos 7 8))) "def Bar():" Nothing
, Tag "local" P.FUNCTION P.DEFINITION (Loc (Range 89 94) (Span (Pos 8 9) (Pos 8 14))) "def local():" Nothing
]
it "produces tags for functions with docs" $
parseTestFile [] (Path.relFile "test/fixtures/python/tags/simple_function_with_docs.py") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/python/tags/simple_function_with_docs.py") `shouldReturn`
[ Tag "Foo" P.FUNCTION P.DEFINITION (Loc (Range 4 7) (Span (Pos 1 5) (Pos 1 8))) "def Foo(x):" (Just "\"\"\"This is the foo function\"\"\"") ]
it "produces tags for classes" $
parseTestFile [] (Path.relFile "test/fixtures/python/tags/class.py") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/python/tags/class.py") `shouldReturn`
[ Tag "Foo" P.CLASS P.DEFINITION (Loc (Range 6 9) (Span (Pos 1 7) (Pos 1 10))) "class Foo:" (Just "\"\"\"The Foo class\"\"\"")
, Tag "f" P.FUNCTION P.DEFINITION (Loc (Range 43 44) (Span (Pos 3 9) (Pos 3 10))) "def f(self):" (Just "\"\"\"The f method\"\"\"")
]
it "produces tags for multi-line functions" $
parseTestFile [P.FUNCTION] (Path.relFile "test/fixtures/python/tags/multiline.py") `shouldReturn`
parseTestFile [P.FUNCTION] (Path.bazelFile "test/fixtures/python/tags/multiline.py") `shouldReturn`
[ Tag "Foo" P.FUNCTION P.DEFINITION (Loc (Range 4 7) (Span (Pos 1 5) (Pos 1 8))) "def Foo(x," Nothing ]
describe "ruby" $ do
it "produces tags for methods" $
parseTestFile [P.METHOD] (Path.relFile "test/fixtures/ruby/tags/simple_method.rb") `shouldReturn`
parseTestFile [P.METHOD] (Path.bazelFile "test/fixtures/ruby/tags/simple_method.rb") `shouldReturn`
[ Tag "foo" P.METHOD P.DEFINITION (Loc (Range 4 7) (Span (Pos 1 5) (Pos 1 8))) "def foo" Nothing ]
it "produces tags for sends" $
parseTestFile [P.CALL] (Path.relFile "test/fixtures/ruby/tags/simple_method.rb") `shouldReturn`
parseTestFile [P.CALL] (Path.bazelFile "test/fixtures/ruby/tags/simple_method.rb") `shouldReturn`
[ Tag "puts" P.CALL P.REFERENCE (Loc (Range 10 14) (Span (Pos 2 3) (Pos 2 7))) "puts \"hi\"" Nothing
, Tag "bar" P.CALL P.REFERENCE (Loc (Range 24 27) (Span (Pos 3 5) (Pos 3 8))) "a.bar" Nothing
, Tag "a" P.CALL P.REFERENCE (Loc (Range 22 23) (Span (Pos 3 3) (Pos 3 4))) "a" Nothing
]
it "produces tags for methods with docs (TODO)" $
parseTestFile [] (Path.relFile "test/fixtures/ruby/tags/simple_method_with_docs.rb") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/ruby/tags/simple_method_with_docs.rb") `shouldReturn`
[ Tag "foo" P.METHOD P.DEFINITION (Loc (Range 18 21) (Span (Pos 2 5) (Pos 2 8))) "def foo" Nothing ]
it "correctly tags files containing multibyte UTF-8 characters (TODO)" $
parseTestFile [] (Path.relFile "test/fixtures/ruby/tags/unicode_identifiers.rb") `shouldReturn`
parseTestFile [] (Path.bazelFile "test/fixtures/ruby/tags/unicode_identifiers.rb") `shouldReturn`
[ Tag "日本語" P.METHOD P.DEFINITION (Loc (Range 20 29) (Span (Pos 2 5) (Pos 2 14))) "def 日本語" Nothing]
it "produces tags for methods and classes with docs (TODO)" $
parseTestFile [P.MODULE, P.CLASS, P.METHOD] (Path.relFile "test/fixtures/ruby/tags/class_module.rb") `shouldReturn`
parseTestFile [P.MODULE, P.CLASS, P.METHOD] (Path.bazelFile "test/fixtures/ruby/tags/class_module.rb") `shouldReturn`
[ Tag "Foo" P.MODULE P.DEFINITION (Loc (Range 21 24) (Span (Pos 2 8) (Pos 2 11))) "module Foo" Nothing
, Tag "Bar" P.CLASS P.DEFINITION (Loc (Range 50 53) (Span (Pos 5 9) (Pos 5 12))) "class Bar" Nothing
, Tag "baz" P.METHOD P.DEFINITION (Loc (Range 81 84) (Span (Pos 8 9) (Pos 8 12))) "def baz(a)" Nothing
@ -90,6 +91,6 @@ spec = do
, Tag "foo" P.METHOD P.DEFINITION (Loc (Range 175 178) (Span (Pos 18 12) (Pos 18 15))) "def self.foo" Nothing
]
parseTestFile :: Foldable t => t P.SyntaxType -> Path.RelFile -> IO [Tag]
parseTestFile :: Foldable t => t P.SyntaxType -> Path.AbsFile -> IO [Tag]
parseTestFile include path = runTaskOrDie $ readBlob (File.fromPath path) >>= fmap (filter only) . tagsForBlob
where only t = null include || (`elem` include) (tagSyntaxType t)