1
1
mirror of https://github.com/github/semantic.git synced 2024-11-27 03:09:48 +03:00

No need to walk directories here.

This commit is contained in:
Patrick Thomson 2020-07-01 13:31:59 -04:00
parent 6d390195e3
commit feb0e46de8
10 changed files with 35 additions and 37 deletions

View File

@ -52,7 +52,7 @@ readCorpusFiles parent = do
files <- globDir1 (compile "**/*.txt") (Path.toString dir)
pure (Path.relPath <$> files)
readCorpusFiles' :: Path.AbsDir -> IO [Path.AbsRelFile]
readCorpusFiles' :: Path.AbsRelDir -> IO [Path.AbsRelFile]
readCorpusFiles' dir = do
files <- globDir1 (compile "**/*.txt") (Path.toString dir)
pure (Path.file <$> files)

View File

@ -2,8 +2,7 @@
{-# LANGUAGE ImplicitParams #-}
module System.Path.Fixture
( absFile,
absRelFile,
( absRelFile,
relDir,
HasFixture,
absRelDir,
@ -30,13 +29,12 @@ delay s = do
hFlush stdout
threadDelay 100000000
absFile :: (HasFixture) => String -> Path.AbsFile
absFile x = root </> Path.relDir "semantic" </> ?project </> Path.relFile x
absRelFile :: (HasFixture) => String -> Path.AbsRelFile
absRelFile x = Path.toAbsRel (root </> Path.relDir "semantic" </> ?project </> Path.relFile x)
where
root = Path.absDir (Bazel.rlocation ?runfiles ".")
absRelFile :: (HasFixture) => String -> Path.AbsRelFile
absRelFile = Path.toAbsRel . absFile
relDir :: HasFixture => String -> Path.AbsDir
relDir x = root </> Path.relDir "semantic" </> ?project </> Path.relDir x

View File

@ -14,10 +14,10 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Ruby.getTestCorpusDir
let ?project = Path.relDir "semantic-codeql"
let ?project = Path.relDir "external/tree-sitter-ql"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-ql/test/corpus"
let dirs = Fixture.absRelDir "test/corpus"
parse = parseByteString @CodeQL.Ql @() tree_sitter_ql
readCorpusFiles' dirs

View File

@ -15,9 +15,9 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Go.getTestCorpusDir
let ?project = Path.relDir "semantic-go"
let ?project = Path.relDir "external/tree-sitter-go"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-go/corpus"
let dirs = Fixture.absRelDir "corpus"
readCorpusFiles' dirs
>>= traverse (testCorpus parse)

View File

@ -20,9 +20,9 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Java.getTestCorpusDir
let ?project = Path.relDir "semantic-java"
let ?project = Path.relDir "external/tree-sitter-java"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-java/corpus"
let dirs = Fixture.absRelDir "corpus"
readCorpusFiles' dirs
>>= traverse (testCorpus parse)
>>= defaultMain . tests

View File

@ -14,10 +14,10 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Ruby.getTestCorpusDir
let ?project = Path.relDir "semantic-python"
let ?project = Path.relDir "external/tree-sitter-python"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-python/test/corpus"
let dirs = Fixture.absRelDir "test/corpus"
parse = parseByteString @Py.Module @() tree_sitter_python
readCorpusFiles' dirs

View File

@ -16,9 +16,9 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Ruby.getTestCorpusDir
let ?project = Path.relDir "semantic-ruby"
let ?project = Path.relDir "external/tree-sitter-ruby"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-ruby/test/corpus"
let dirs = Fixture.absRelDir "test/corpus"
readCorpusFiles' dirs
>>= traverse (testCorpus parse)

View File

@ -15,9 +15,9 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Typescript.getTestCorpusDir
let ?project = Path.relDir "semantic-tsx"
let ?project = Path.relDir "external/semantic-typescript"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-typescript/tsx/corpus"
let dirs = Fixture.absRelDir "tsx/corpus"
readCorpusFiles' dirs
>>= traverse (testCorpus parse)

View File

@ -14,9 +14,9 @@ main :: IO ()
main = do
rf <- Runfiles.create
-- dirs <- Path.absDir <$> Typescript.getTestCorpusDir
let ?project = Path.relDir "semantic-typescript"
let ?project = Path.relDir "external/tree-sitter-typescript"
?runfiles = rf
let dirs = Fixture.relDir "../external/tree-sitter-typescript/typescript/corpus"
let dirs = Fixture.absRelDir "typescript/corpus"
readCorpusFiles' dirs
>>= traverse (testCorpus parse)

View File

@ -14,75 +14,75 @@ spec :: Fixture.HasFixture => Spec
spec = do
describe "go" $ do
it "produces tags for functions with docs (TODO)" $
parseTestFile [P.FUNCTION] (Fixture.absFile "test/fixtures/go/tags/simple_functions.go") `shouldReturn`
parseTestFile [P.FUNCTION] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/go/tags/method.go") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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] (Fixture.absFile "test/fixtures/go/tags/simple_functions.go") `shouldReturn`
parseTestFile [P.CALL] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/javascript/tags/simple_function_with_docs.js") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/typescript/tags/class.ts") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/typescript/tags/module.ts") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/python/tags/simple_functions.py") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/python/tags/simple_function_with_docs.py") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/python/tags/class.py") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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] (Fixture.absFile "test/fixtures/python/tags/multiline.py") `shouldReturn`
parseTestFile [P.FUNCTION] (Fixture.absRelFile "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] (Fixture.absFile "test/fixtures/ruby/tags/simple_method.rb") `shouldReturn`
parseTestFile [P.METHOD] (Fixture.absRelFile "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] (Fixture.absFile "test/fixtures/ruby/tags/simple_method.rb") `shouldReturn`
parseTestFile [P.CALL] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/ruby/tags/simple_method_with_docs.rb") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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 [] (Fixture.absFile "test/fixtures/ruby/tags/unicode_identifiers.rb") `shouldReturn`
parseTestFile [] (Fixture.absRelFile "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] (Fixture.absFile "test/fixtures/ruby/tags/class_module.rb") `shouldReturn`
parseTestFile [P.MODULE, P.CLASS, P.METHOD] (Fixture.absRelFile "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
@ -91,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.AbsFile -> IO [Tag]
parseTestFile :: Foldable t => t P.SyntaxType -> Path.AbsRelFile -> IO [Tag]
parseTestFile include path = runTaskOrDie $ readBlob (File.fromPath path) >>= fmap (filter only) . tagsForBlob
where only t = null include || (`elem` include) (tagSyntaxType t)