Example #1
0
    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()))
Example #2
0
    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",)))
Example #3
0
  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',)))
Example #4
0
    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()),
        )
Example #5
0
    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'))
Example #6
0
    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'))
Example #7
0
    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'))
Example #8
0
 def make_trie() -> SourceRootTrie:
     return SourceRootTrie(
         SourceRootFactory({
             'jvm': ('java', 'scala'),
             'py': ('python', )
         }))
Example #9
0
    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"))
Example #10
0
    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"))
Example #11
0
 def make_trie() -> SourceRootTrie:
     return SourceRootTrie(SourceRootFactory({"jvm": ("java", "scala"), "py": ("python",)}))
Example #12
0
    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'))