def test_trie_traversal(self): trie = SourceRootTrie(SourceRootFactory({})) trie.add_pattern('foo1/bar1/baz1') trie.add_pattern('foo1/bar1/baz2/qux') trie.add_pattern('foo1/bar2/baz1') trie.add_pattern('foo1/bar2/baz2') trie.add_pattern('foo2/bar1') trie.add_fixed('fixed1/bar1', ['lang1'], SOURCE) trie.add_fixed('fixed2/bar2', ['lang2'], TEST) # Test raw traversal. self.assertEqual( {('baz1', (), UNKNOWN), ('baz2/qux', (), UNKNOWN)}, set(trie._root.children['foo1'].children['bar1'].subpatterns())) self.assertEqual( {('bar1/baz1', (), UNKNOWN), ('bar1/baz2/qux', (), UNKNOWN), ('bar2/baz1', (), UNKNOWN), ('bar2/baz2', (), UNKNOWN)}, set(trie._root.children['foo1'].subpatterns())) self.assertEqual( {('foo1/bar1/baz1', (), UNKNOWN), ('foo1/bar1/baz2/qux', (), UNKNOWN), ('foo1/bar2/baz1', (), UNKNOWN), ('foo1/bar2/baz2', (), UNKNOWN), ('foo2/bar1', (), UNKNOWN), ('^/fixed1/bar1', ('lang1', ), SOURCE), ('^/fixed2/bar2', ('lang2', ), TEST)}, set(trie._root.subpatterns())) # Test the fixed() method. self.assertEqual( {('fixed1/bar1', ('lang1', ), SOURCE), ('fixed2/bar2', ('lang2', ), TEST)}, set(trie.fixed()))
def test_invalid_patterns(self): trie = SourceRootTrie(SourceRootFactory({})) # Bad normalization. self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_fixed("foo/bar/", ("bar",))) self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_pattern("foo//*", ("java",))) self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_pattern("foo/*/", ("java",))) # Asterisk in fixed pattern. self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_fixed("src/*", ("java",)))
def test_invalid_patterns(self): trie = SourceRootTrie(SourceRootFactory({})) # Bad normalization. self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_fixed('foo/bar/', ('bar',))) self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_pattern('foo//*', ('java',))) self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_pattern('foo/*/', ('java',))) # Asterisk in fixed pattern. self.assertRaises(SourceRootTrie.InvalidPath, lambda: trie.add_fixed('src/*', ('java',)))
def test_trie_traversal(self): trie = SourceRootTrie(SourceRootFactory({})) trie.add_pattern("foo1/bar1/baz1") trie.add_pattern("foo1/bar1/baz2/qux") trie.add_pattern("foo1/bar2/baz1") trie.add_pattern("foo1/bar2/baz2") trie.add_pattern("foo2/bar1") trie.add_fixed("fixed1/bar1", ["lang1"], SOURCE) trie.add_fixed("fixed2/bar2", ["lang2"], TEST) # Test raw traversal. self.assertEqual( {("baz1", (), UNKNOWN), ("baz2/qux", (), UNKNOWN)}, set(trie._root.children["foo1"].children["bar1"].subpatterns()), ) self.assertEqual( { ("bar1/baz1", (), UNKNOWN), ("bar1/baz2/qux", (), UNKNOWN), ("bar2/baz1", (), UNKNOWN), ("bar2/baz2", (), UNKNOWN), }, set(trie._root.children["foo1"].subpatterns()), ) self.assertEqual( { ("foo1/bar1/baz1", (), UNKNOWN), ("foo1/bar1/baz2/qux", (), UNKNOWN), ("foo1/bar2/baz1", (), UNKNOWN), ("foo1/bar2/baz2", (), UNKNOWN), ("foo2/bar1", (), UNKNOWN), ("^/fixed1/bar1", ("lang1", ), SOURCE), ("^/fixed2/bar2", ("lang2", ), TEST), }, set(trie._root.subpatterns()), ) # Test the fixed() method. self.assertEqual( {("fixed1/bar1", ("lang1", ), SOURCE), ("fixed2/bar2", ("lang2", ), TEST)}, set(trie.fixed()), )
def test_source_root_trie(self): trie = SourceRootTrie( SourceRootFactory({ 'jvm': ('java', 'scala'), 'py': ('python', ) })) self.assertIsNone(trie.find('src/java/org/pantsbuild/foo/Foo.java')) def root(path, langs): return SourceRoot(path, langs, UNKNOWN) # Wildcard at the end. trie.add_pattern('src/*') self.assertEqual(root('src/java', ('java', )), trie.find('src/java/org/pantsbuild/foo/Foo.java')) self.assertEqual( root('my/project/src/java', ('java', )), trie.find('my/project/src/java/org/pantsbuild/foo/Foo.java')) self.assertEqual(root('src/python', ('python', )), trie.find('src/python/pantsbuild/foo/foo.py')) self.assertEqual( root('my/project/src/python', ('python', )), trie.find('my/project/src/python/org/pantsbuild/foo/foo.py')) # Overlapping pattern. trie.add_pattern('src/main/*') self.assertEqual( root('src/main/java', ('java', )), trie.find('src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEqual( root('my/project/src/main/java', ('java', )), trie.find('my/project/src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEqual(root('src/main/python', ('python', )), trie.find('src/main/python/pantsbuild/foo/foo.py')) self.assertEqual( root('my/project/src/main/python', ('python', )), trie.find('my/project/src/main/python/org/pantsbuild/foo/foo.py')) # Wildcard in the middle. trie.add_pattern('src/*/code') self.assertEqual( root('src/java/code', ('java', )), trie.find('src/java/code/org/pantsbuild/foo/Foo.java')) self.assertEqual( root('my/project/src/java/code', ('java', )), trie.find('my/project/src/java/code/org/pantsbuild/foo/Foo.java')) self.assertEqual(root('src/python/code', ('python', )), trie.find('src/python/code/pantsbuild/foo/foo.py')) self.assertEqual( root('my/project/src/python/code', ('python', )), trie.find('my/project/src/python/code/org/pantsbuild/foo/foo.py')) # Verify that the now even-more-overlapping pattern still works. self.assertEqual( root('src/main/java', ('java', )), trie.find('src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEqual( root('my/project/src/main/java', ('java', )), trie.find('my/project/src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEqual(root('src/main/python', ('python', )), trie.find('src/main/python/pantsbuild/foo/foo.py')) self.assertEqual( root('my/project/src/main/python', ('python', )), trie.find('my/project/src/main/python/org/pantsbuild/foo/foo.py')) # Verify that we take the first matching prefix. self.assertEqual(root('src/java', ('java', )), trie.find('src/java/src/python/Foo.java')) # Test canonicalization. self.assertEqual(root('src/jvm', ('java', 'scala')), trie.find('src/jvm/org/pantsbuild/foo/Foo.java')) self.assertEqual(root('src/jvm', ('java', 'scala')), trie.find('src/jvm/org/pantsbuild/foo/Foo.scala')) self.assertEqual(root('src/py', ('python', )), trie.find('src/py/pantsbuild/foo/foo.py')) # Non-canonicalized language names should also be detected. self.assertEqual(root('src/kotlin', ('kotlin', )), trie.find('src/kotlin/org/pantsbuild/foo/Foo.kotlin')) # Test fixed roots. trie.add_fixed('mysrc/scalastuff', ('scala', )) self.assertEqual( SourceRoot('mysrc/scalastuff', ('scala', ), UNKNOWN), trie.find('mysrc/scalastuff/org/pantsbuild/foo/Foo.scala')) self.assertIsNone( trie.find( 'my/project/mysrc/scalastuff/org/pantsbuild/foo/Foo.scala')) # Verify that a fixed root wins over a pattern that is a prefix of it # (e.g., that src/go/src wins over src/go). trie.add_fixed('src/go/src', ('go', )) self.assertEqual(root('src/go/src', ('go', )), trie.find('src/go/src/foo/bar/baz.go'))
def test_source_root_pattern_at_buildroot(self): trie = SourceRootTrie(SourceRootFactory({})) trie.add_pattern('*') self.assertEqual(SourceRoot('java', ('java', ), UNKNOWN), trie.find('java/bar/baz.proto'))
def test_fixed_source_root_at_buildroot(self): trie = SourceRootTrie(SourceRootFactory({})) trie.add_fixed('', ('proto', )) self.assertEqual(SourceRoot('', ('proto', ), UNKNOWN), trie.find('foo/proto/bar/baz.proto'))
def make_trie() -> SourceRootTrie: return SourceRootTrie( SourceRootFactory({ 'jvm': ('java', 'scala'), 'py': ('python', ) }))
def test_source_root_trie(self): trie = SourceRootTrie(SourceRootFactory({"jvm": ("java", "scala"), "py": ("python",)})) self.assertIsNone(trie.find("src/java/org/pantsbuild/foo/Foo.java")) def root(path, langs): return SourceRoot(path, langs, UNKNOWN) # Wildcard at the end. trie.add_pattern("src/*") self.assertEqual( root("src/java", ("java",)), trie.find("src/java/org/pantsbuild/foo/Foo.java") ) self.assertEqual( root("my/project/src/java", ("java",)), trie.find("my/project/src/java/org/pantsbuild/foo/Foo.java"), ) self.assertEqual( root("src/python", ("python",)), trie.find("src/python/pantsbuild/foo/foo.py") ) self.assertEqual( root("my/project/src/python", ("python",)), trie.find("my/project/src/python/org/pantsbuild/foo/foo.py"), ) # Overlapping pattern. trie.add_pattern("src/main/*") self.assertEqual( root("src/main/java", ("java",)), trie.find("src/main/java/org/pantsbuild/foo/Foo.java") ) self.assertEqual( root("my/project/src/main/java", ("java",)), trie.find("my/project/src/main/java/org/pantsbuild/foo/Foo.java"), ) self.assertEqual( root("src/main/python", ("python",)), trie.find("src/main/python/pantsbuild/foo/foo.py") ) self.assertEqual( root("my/project/src/main/python", ("python",)), trie.find("my/project/src/main/python/org/pantsbuild/foo/foo.py"), ) # Wildcard in the middle. trie.add_pattern("src/*/code") self.assertEqual( root("src/java/code", ("java",)), trie.find("src/java/code/org/pantsbuild/foo/Foo.java") ) self.assertEqual( root("my/project/src/java/code", ("java",)), trie.find("my/project/src/java/code/org/pantsbuild/foo/Foo.java"), ) self.assertEqual( root("src/python/code", ("python",)), trie.find("src/python/code/pantsbuild/foo/foo.py") ) self.assertEqual( root("my/project/src/python/code", ("python",)), trie.find("my/project/src/python/code/org/pantsbuild/foo/foo.py"), ) # Verify that the now even-more-overlapping pattern still works. self.assertEqual( root("src/main/java", ("java",)), trie.find("src/main/java/org/pantsbuild/foo/Foo.java") ) self.assertEqual( root("my/project/src/main/java", ("java",)), trie.find("my/project/src/main/java/org/pantsbuild/foo/Foo.java"), ) self.assertEqual( root("src/main/python", ("python",)), trie.find("src/main/python/pantsbuild/foo/foo.py") ) self.assertEqual( root("my/project/src/main/python", ("python",)), trie.find("my/project/src/main/python/org/pantsbuild/foo/foo.py"), ) # Verify that we take the first matching prefix. self.assertEqual(root("src/java", ("java",)), trie.find("src/java/src/python/Foo.java")) # Test canonicalization. self.assertEqual( root("src/jvm", ("java", "scala")), trie.find("src/jvm/org/pantsbuild/foo/Foo.java") ) self.assertEqual( root("src/jvm", ("java", "scala")), trie.find("src/jvm/org/pantsbuild/foo/Foo.scala") ) self.assertEqual(root("src/py", ("python",)), trie.find("src/py/pantsbuild/foo/foo.py")) # Non-canonicalized language names should also be detected. self.assertEqual( root("src/kotlin", ("kotlin",)), trie.find("src/kotlin/org/pantsbuild/foo/Foo.kotlin") ) # Test fixed roots. trie.add_fixed("mysrc/scalastuff", ("scala",)) self.assertEqual( SourceRoot("mysrc/scalastuff", ("scala",), UNKNOWN), trie.find("mysrc/scalastuff/org/pantsbuild/foo/Foo.scala"), ) self.assertIsNone(trie.find("my/project/mysrc/scalastuff/org/pantsbuild/foo/Foo.scala")) # Verify that a fixed root wins over a pattern that is a prefix of it # (e.g., that src/go/src wins over src/go). trie.add_fixed("src/go/src", ("go",)) self.assertEqual(root("src/go/src", ("go",)), trie.find("src/go/src/foo/bar/baz.go")) # Verify that the repo root can be a fixed source root. trie.add_fixed("", ("python",)) self.assertEqual(root("", ("python",)), trie.find("foo/bar/baz.py"))
def test_source_root_pattern_at_buildroot(self): trie = SourceRootTrie(SourceRootFactory({})) trie.add_pattern("*") self.assertEqual(SourceRoot("java", ("java",), UNKNOWN), trie.find("java/bar/baz.proto"))
def make_trie() -> SourceRootTrie: return SourceRootTrie(SourceRootFactory({"jvm": ("java", "scala"), "py": ("python",)}))
def test_source_root_trie(self): trie = SourceRootTrie( SourceRootFactory({ 'jvm': ('java', 'scala'), 'py': ('python', ) })) self.assertIsNone(trie.find('src/java/org/pantsbuild/foo/Foo.java')) # Wildcard at the end. trie.add_pattern('src/*') self.assertEquals(SourceRoot('src/java', ('java', )), trie.find('src/java/org/pantsbuild/foo/Foo.java')) self.assertEquals( SourceRoot('my/project/src/java', ('java', )), trie.find('my/project/src/java/org/pantsbuild/foo/Foo.java')) self.assertEquals(SourceRoot('src/python', ('python', )), trie.find('src/python/pantsbuild/foo/foo.py')) self.assertEquals( SourceRoot('my/project/src/python', ('python', )), trie.find('my/project/src/python/org/pantsbuild/foo/foo.py')) # Overlapping pattern. trie.add_pattern('src/main/*') self.assertEquals( SourceRoot('src/main/java', ('java', )), trie.find('src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEquals( SourceRoot('my/project/src/main/java', ('java', )), trie.find('my/project/src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEquals(SourceRoot('src/main/python', ('python', )), trie.find('src/main/python/pantsbuild/foo/foo.py')) self.assertEquals( SourceRoot('my/project/src/main/python', ('python', )), trie.find('my/project/src/main/python/org/pantsbuild/foo/foo.py')) # Wildcard in the middle. trie.add_pattern('src/*/code') self.assertEquals( SourceRoot('src/java/code', ('java', )), trie.find('src/java/code/org/pantsbuild/foo/Foo.java')) self.assertEquals( SourceRoot('my/project/src/java/code', ('java', )), trie.find('my/project/src/java/code/org/pantsbuild/foo/Foo.java')) self.assertEquals(SourceRoot('src/python/code', ('python', )), trie.find('src/python/code/pantsbuild/foo/foo.py')) self.assertEquals( SourceRoot('my/project/src/python/code', ('python', )), trie.find('my/project/src/python/code/org/pantsbuild/foo/foo.py')) # Verify that the now even-more-overlapping pattern still works. self.assertEquals( SourceRoot('src/main/java', ('java', )), trie.find('src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEquals( SourceRoot('my/project/src/main/java', ('java', )), trie.find('my/project/src/main/java/org/pantsbuild/foo/Foo.java')) self.assertEquals(SourceRoot('src/main/python', ('python', )), trie.find('src/main/python/pantsbuild/foo/foo.py')) self.assertEquals( SourceRoot('my/project/src/main/python', ('python', )), trie.find('my/project/src/main/python/org/pantsbuild/foo/foo.py')) # Verify that we take the first matching prefix. self.assertEquals(SourceRoot('src/java', ('java', )), trie.find('src/java/src/python/Foo.java')) # Test canonicalization. self.assertEquals(SourceRoot('src/jvm', ('java', 'scala')), trie.find('src/jvm/org/pantsbuild/foo/Foo.java')) self.assertEquals(SourceRoot('src/jvm', ('java', 'scala')), trie.find('src/jvm/org/pantsbuild/foo/Foo.scala')) self.assertEquals(SourceRoot('src/py', ('python', )), trie.find('src/py/pantsbuild/foo/foo.py')) # Test fixed patterns. trie.add_fixed('mysrc/scalastuff', ('scala', )) self.assertEquals( ('mysrc/scalastuff', ('scala', )), trie.find('mysrc/scalastuff/org/pantsbuild/foo/Foo.scala')) self.assertIsNone( trie.find( 'my/project/mysrc/scalastuff/org/pantsbuild/foo/Foo.scala'))