def test_source_roots_request() -> None: rule_runner = RuleRunner(rules=[ *source_root_rules(), QueryRule(SourceRootsResult, (SourceRootsRequest, OptionsBootstrapper)), ]) req = SourceRootsRequest( files=(PurePath("src/python/foo/bar.py"), PurePath("tests/python/foo/bar_test.py")), dirs=(PurePath("src/python/foo"), PurePath("src/python/baz/qux")), ) res = rule_runner.request_product( SourceRootsResult, [ req, create_options_bootstrapper( args=["--source-root-patterns=['src/python','tests/python']"]), ], ) assert { PurePath("src/python/foo/bar.py"): SourceRoot("src/python"), PurePath("tests/python/foo/bar_test.py"): SourceRoot("tests/python"), PurePath("src/python/foo"): SourceRoot("src/python"), PurePath("src/python/baz/qux"): SourceRoot("src/python"), } == dict(res.path_to_root)
def test_source_root_suffixes() -> None: srs = SourceRoots(["src/python", "/"]) assert SourceRoot("src/python") == srs.strict_find_by_path( "src/python/foo/bar.py") assert SourceRoot("src/python/foo/src/python") == srs.strict_find_by_path( "src/python/foo/src/python/bar.py") assert SourceRoot(".") == srs.strict_find_by_path("foo/bar.py")
def test_all_roots_with_root_at_buildroot(self): options = { "pants_ignore": [], "root_patterns": ["/"], } options.update(self.options[""]) # We need inherited values for pants_workdir etc. self.context( for_subsystems=[SourceRootConfig], options={SourceRootConfig.options_scope: options} ) source_root_config = SourceRootConfig.global_instance() # This function mocks out reading real directories off the file system def provider_rule(path_globs: PathGlobs) -> Snapshot: dirs = ("foo",) # A python package at the buildroot. return Snapshot(Digest("abcdef", 10), (), dirs) output = run_rule( list_roots.all_roots, rule_args=[source_root_config], mock_gets=[ MockGet(product_type=Snapshot, subject_type=PathGlobs, mock=provider_rule), MockGet( product_type=OptionalSourceRoot, subject_type=SourceRootRequest, mock=lambda req: OptionalSourceRoot(SourceRoot(".")), ), ], ) self.assertEqual({SourceRoot(".")}, set(output))
def test_all_roots_with_root_at_buildroot() -> None: source_root_config = create_subsystem( SourceRootConfig, root_patterns=["/"], marker_filenames=[], ) # This function mocks out reading real directories off the file system def provider_rule(_: PathGlobs) -> Snapshot: dirs = ("foo", ) # A python package at the buildroot. return Snapshot(Digest("abcdef", 10), (), dirs) output = run_rule_with_mocks( all_roots, rule_args=[source_root_config], mock_gets=[ MockGet(product_type=Snapshot, subject_type=PathGlobs, mock=provider_rule), MockGet( product_type=OptionalSourceRoot, subject_type=SourceRootRequest, mock=lambda req: OptionalSourceRoot(SourceRoot(".")), ), ], ) assert {SourceRoot(".")} == set(output)
def test_source_root_at_buildroot() -> None: srs = SourceRoots(["/"]) assert SourceRoot(".") == srs.strict_find_by_path("foo/bar.py") assert SourceRoot(".") == srs.strict_find_by_path("foo/") assert SourceRoot(".") == srs.strict_find_by_path("foo") with pytest.raises(NoSourceRootError): srs.strict_find_by_path("../foo/bar.py")
def test_source_root_patterns() -> None: srs = SourceRoots(["src/*", "/project/*"]) assert SourceRoot("src/python") == srs.strict_find_by_path( "src/python/foo/bar.py") assert SourceRoot("src/python/foo/src/shell") == srs.strict_find_by_path( "src/python/foo/src/shell/bar.sh") assert SourceRoot("project/python") == srs.strict_find_by_path( "project/python/foo/bar.py") with pytest.raises(NoSourceRootError): srs.strict_find_by_path("prefix/project/python/foo/bar.py")
def test_fixed_source_roots() -> None: srs = SourceRoots(["/root1", "/foo/root2", "/root1/root3"]) assert SourceRoot("root1") == srs.strict_find_by_path("root1/bar.py") assert SourceRoot("foo/root2") == srs.strict_find_by_path( "foo/root2/bar/baz.py") assert SourceRoot("root1/root3") == srs.strict_find_by_path( "root1/root3/qux.py") assert SourceRoot("root1/root3") == srs.strict_find_by_path( "root1/root3/qux/quux.py") assert SourceRoot("root1/root3") == srs.strict_find_by_path("root1/root3") with pytest.raises(NoSourceRootError): srs.strict_find_by_path("blah/blah.py")
def test_all_roots(self): self.create_dir('contrib/go/examples/3rdparty/go') self.create_dir('contrib/go/examples/src/go') self.create_dir('src/java') self.create_dir('src/python') self.create_dir('src/example/java') self.create_dir('src/example/python') self.create_dir('my/project/src/java') self.create_dir('not/a/srcroot/java') options = { 'source_root_patterns': ['src/*', 'src/example/*'], # Test that our 'go_remote' hack works. # TODO: This will be redundant once we have proper "3rdparty"/"remote" support. 'source_roots': { 'contrib/go/examples/3rdparty/go': ['go_remote'] } } options.update(self.options['']) # We need inherited values for pants_workdir etc. source_roots = create_subsystem(SourceRootConfig, **options).get_source_roots() self.assertEquals({SourceRoot('contrib/go/examples/3rdparty/go', ('go_remote',)), SourceRoot('contrib/go/examples/src/go', ('go',)), SourceRoot('src/java', ('java',)), SourceRoot('src/python', ('python',)), SourceRoot('src/example/java', ('java',)), SourceRoot('src/example/python', ('python',)), SourceRoot('my/project/src/java', ('java',))}, set(source_roots.all_roots()))
def test_all_roots(self): self.create_dir("contrib/go/examples/3rdparty/go") self.create_dir("contrib/go/examples/src/go/src") self.create_dir("src/java") self.create_dir("src/python") self.create_dir("src/kotlin") self.create_dir("src/example/java") self.create_dir("src/example/python") self.create_dir("my/project/src/java") self.create_dir("fixed/root/jvm") self.create_dir("not/a/srcroot/java") options = { "pants_ignore": [], "source_root_patterns": ["src/*", "src/example/*"], "source_roots": { # Fixed roots should trump patterns which would detect contrib/go/examples/src/go here. "contrib/go/examples/src/go/src": ["go"], # Dir does not exist, should not be listed as a root. "java": ["java"], }, } options.update(self.options[""] ) # We need inherited values for pants_workdir etc. self.context(for_subsystems=[SourceRootConfig], options={SourceRootConfig.options_scope: options}) source_roots = SourceRootConfig.global_instance().get_source_roots() # Ensure that we see any manually added roots. source_roots.add_source_root("fixed/root/jvm", ("java", "scala"), TEST) source_roots.all_roots() self.assertEqual( { SourceRoot("contrib/go/examples/3rdparty/go", ("go", ), THIRDPARTY), SourceRoot("contrib/go/examples/src/go/src", ("go", ), SOURCE), SourceRoot("src/java", ("java", ), SOURCE), SourceRoot("src/python", ("python", ), SOURCE), SourceRoot("src/kotlin", ("kotlin", ), SOURCE), SourceRoot("src/example/java", ("java", ), SOURCE), SourceRoot("src/example/python", ("python", ), SOURCE), SourceRoot("my/project/src/java", ("java", ), SOURCE), SourceRoot("fixed/root/jvm", ("java", "scala"), TEST), }, set(source_roots.all_roots()), )
def test_all_roots(self): self.create_dir('contrib/go/examples/3rdparty/go') self.create_dir('contrib/go/examples/src/go/src') self.create_dir('src/java') self.create_dir('src/python') self.create_dir('src/kotlin') self.create_dir('src/example/java') self.create_dir('src/example/python') self.create_dir('my/project/src/java') self.create_dir('fixed/root/jvm') self.create_dir('not/a/srcroot/java') options = { 'pants_ignore': [], 'source_root_patterns': ['src/*', 'src/example/*'], 'source_roots': { # Fixed roots should trump patterns which would detect contrib/go/examples/src/go here. 'contrib/go/examples/src/go/src': ['go'], # Dir does not exist, should not be listed as a root. 'java': ['java'] } } options.update(self.options[''] ) # We need inherited values for pants_workdir etc. self.context(for_subsystems=[SourceRootConfig], options={SourceRootConfig.options_scope: options}) source_roots = SourceRootConfig.global_instance().get_source_roots() # Ensure that we see any manually added roots. source_roots.add_source_root('fixed/root/jvm', ('java', 'scala'), TEST) source_roots.all_roots() self.assertEqual( { SourceRoot('contrib/go/examples/3rdparty/go', ('go', ), THIRDPARTY), SourceRoot('contrib/go/examples/src/go/src', ('go', ), SOURCE), SourceRoot('src/java', ('java', ), SOURCE), SourceRoot('src/python', ('python', ), SOURCE), SourceRoot('src/kotlin', ('kotlin', ), SOURCE), SourceRoot('src/example/java', ('java', ), SOURCE), SourceRoot('src/example/python', ('python', ), SOURCE), SourceRoot('my/project/src/java', ('java', ), SOURCE), SourceRoot('fixed/root/jvm', ('java', 'scala'), TEST) }, set(source_roots.all_roots()))
def test_source_roots_request(self) -> None: req = SourceRootsRequest( files=(PurePath("src/python/foo/bar.py"), PurePath("tests/python/foo/bar_test.py")), dirs=(PurePath("src/python/foo"), PurePath("src/python/baz/qux")), ) res = self.request_single_product( SourceRootsResult, Params( req, create_options_bootstrapper(args=[ "--source-root-patterns=['src/python','tests/python']" ]), ), ) assert { PurePath("src/python/foo/bar.py"): SourceRoot("src/python"), PurePath("tests/python/foo/bar_test.py"): SourceRoot("tests/python"), PurePath("src/python/foo"): SourceRoot("src/python"), PurePath("src/python/baz/qux"): SourceRoot("src/python"), } == dict(res.path_to_root)
def root(path): return SourceRoot(path)
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 test_all_roots(self): dirs = ( "contrib/go/examples/src/go/src", "src/java", "src/python", "src/python/subdir/src/python", # We allow source roots under source roots. "src/kotlin", "my/project/src/java", "src/example/java", "src/example/python", "fixed/root/jvm", ) options = { "pants_ignore": [], "root_patterns": [ "src/*", "src/example/*", "contrib/go/examples/src/go/src", # Dir does not exist, should not be listed as a root. "java", "fixed/root/jvm", ], } options.update(self.options[""]) # We need inherited values for pants_workdir etc. self.context( for_subsystems=[SourceRootConfig], options={SourceRootConfig.options_scope: options} ) source_root_config = SourceRootConfig.global_instance() # This function mocks out reading real directories off the file system. def provider_rule(path_globs: PathGlobs) -> Snapshot: return Snapshot(Digest("abcdef", 10), (), dirs) def source_root_mock_rule(req: SourceRootRequest) -> OptionalSourceRoot: for d in dirs: if str(req.path).startswith(d): return OptionalSourceRoot(SourceRoot(str(req.path))) return OptionalSourceRoot(None) output = run_rule( list_roots.all_roots, rule_args=[source_root_config], mock_gets=[ MockGet(product_type=Snapshot, subject_type=PathGlobs, mock=provider_rule), MockGet( product_type=OptionalSourceRoot, subject_type=SourceRootRequest, mock=source_root_mock_rule, ), ], ) self.assertEqual( { SourceRoot("contrib/go/examples/src/go/src"), SourceRoot("src/java"), SourceRoot("src/python"), SourceRoot("src/python/subdir/src/python"), SourceRoot("src/kotlin"), SourceRoot("src/example/java"), SourceRoot("src/example/python"), SourceRoot("my/project/src/java"), SourceRoot("fixed/root/jvm"), }, set(output), )
def test_source_root_trie_deprecated(self): trie = SourceRootTrie() self.assertIsNone(trie.find("src/java/org/pantsbuild/foo/Foo.java")) def root(path): return SourceRoot(path) # Wildcard at the end. trie.add_pattern("src/*") self.assertEqual(root("src/java"), trie.find("src/java/org/pantsbuild/foo/Foo.java")) self.assertEqual( root("my/project/src/java"), trie.find("my/project/src/java/org/pantsbuild/foo/Foo.java"), ) self.assertEqual(root("src/python"), trie.find("src/python/pantsbuild/foo/foo.py")) self.assertEqual( root("my/project/src/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"), trie.find("src/main/java/org/pantsbuild/foo/Foo.java")) self.assertEqual( root("my/project/src/main/java"), trie.find("my/project/src/main/java/org/pantsbuild/foo/Foo.java"), ) self.assertEqual(root("src/main/python"), trie.find("src/main/python/pantsbuild/foo/foo.py")) self.assertEqual( root("my/project/src/main/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"), trie.find("src/java/code/org/pantsbuild/foo/Foo.java")) self.assertEqual( root("my/project/src/java/code"), trie.find("my/project/src/java/code/org/pantsbuild/foo/Foo.java"), ) self.assertEqual(root("src/python/code"), trie.find("src/python/code/pantsbuild/foo/foo.py")) self.assertEqual( root("my/project/src/python/code"), 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"), trie.find("src/main/java/org/pantsbuild/foo/Foo.java")) self.assertEqual( root("my/project/src/main/java"), trie.find("my/project/src/main/java/org/pantsbuild/foo/Foo.java"), ) self.assertEqual(root("src/main/python"), trie.find("src/main/python/pantsbuild/foo/foo.py")) self.assertEqual( root("my/project/src/main/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"), trie.find("src/java/src/python/Foo.java")) # Test canonicalization. self.assertEqual(root("src/jvm"), trie.find("src/jvm/org/pantsbuild/foo/Foo.java")) self.assertEqual(root("src/jvm"), trie.find("src/jvm/org/pantsbuild/foo/Foo.scala")) self.assertEqual(root("src/py"), trie.find("src/py/pantsbuild/foo/foo.py")) # Non-canonicalized language names should also be detected. self.assertEqual(root("src/kotlin"), trie.find("src/kotlin/org/pantsbuild/foo/Foo.kotlin")) # Test fixed roots. trie.add_fixed("mysrc/scalastuff") self.assertEqual( SourceRoot("mysrc/scalastuff"), 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") self.assertEqual(root("src/go/src"), trie.find("src/go/src/foo/bar/baz.go")) # Verify that the repo root can be a fixed source root. trie.add_fixed("") self.assertEqual(root(""), trie.find("foo/bar/baz.py"))
def test_source_root_pattern_at_buildroot_deprecated(self): trie = SourceRootTrie() trie.add_pattern("*") self.assertEqual(SourceRoot("java"), trie.find("java/bar/baz.proto"))
def test_all_roots(self): SOURCE = SourceRootCategories.SOURCE TEST = SourceRootCategories.TEST THIRDPARTY = SourceRootCategories.THIRDPARTY options = { 'pants_ignore': [], 'source_root_patterns': ['src/*', 'src/example/*'], 'source_roots': { # Fixed roots should trump patterns which would detect contrib/go/examples/src/go here. 'contrib/go/examples/src/go/src': ['go'], # Dir does not exist, should not be listed as a root. 'java': ['java'] } } options.update(self.options[''] ) # We need inherited values for pants_workdir etc. self.context(for_subsystems=[SourceRootConfig], options={SourceRootConfig.options_scope: options}) source_root_config = SourceRootConfig.global_instance() source_roots = source_root_config.get_source_roots() # Ensure that we see any manually added roots. source_roots.add_source_root('fixed/root/jvm', ('java', 'scala'), TEST) # This function mocks out reading real directories off the file system def provider_rule(path_globs: PathGlobs) -> Snapshot: dirs = ( 'contrib/go/examples/3rdparty/go', 'contrib/go/examples/src/go/src', 'src/java', 'src/python', 'src/kotlin', 'my/project/src/java', 'src/example/java', 'src/example/python', 'fixed/root/jvm', # subdirectories of source roots should not show up in final output 'src/kotlin/additional/directories/that/might/get/matched/src/foo', ) return Snapshot(Digest('abcdef', 10), (), dirs) output = run_rule(list_roots.all_roots, source_root_config, {(Snapshot, PathGlobs): provider_rule}) self.assertEqual( { SourceRoot('contrib/go/examples/3rdparty/go', ('go', ), THIRDPARTY), SourceRoot('contrib/go/examples/src/go/src', ('go', ), SOURCE), SourceRoot('src/java', ('java', ), SOURCE), SourceRoot('src/python', ('python', ), SOURCE), SourceRoot('src/kotlin', ('kotlin', ), SOURCE), SourceRoot('src/example/java', ('java', ), SOURCE), SourceRoot('src/example/python', ('python', ), SOURCE), SourceRoot('my/project/src/java', ('java', ), SOURCE), SourceRoot('fixed/root/jvm', ('java', 'scala'), TEST) }, set(output))
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_deprecated(self): trie = SourceRootTrie() trie.add_fixed("", ) self.assertEqual(SourceRoot(""), trie.find("foo/proto/bar/baz.proto"))
def test_source_root_trie(self): trie = SourceRootTrie({ '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'))
def root(path, langs): return SourceRoot(path, langs, UNKNOWN)
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_all_roots() -> None: dirs = ( "contrib/go/examples/src/go/src", "src/java", "src/python", "src/python/subdir/src/python", # We allow source roots under source roots. "src/kotlin", "my/project/src/java", "src/example/java", "src/example/python", "fixed/root/jvm", ) source_root_config = create_subsystem( SourceRootConfig, root_patterns=[ "src/*", "src/example/*", "contrib/go/examples/src/go/src", # Dir does not exist, should not be listed as a root. "java", "fixed/root/jvm", ], marker_filenames=[], ) # This function mocks out reading real directories off the file system. def provider_rule(_: PathGlobs) -> Snapshot: return Snapshot(Digest("abcdef", 10), (), dirs) def source_root_mock_rule(req: SourceRootRequest) -> OptionalSourceRoot: for d in dirs: if str(req.path).startswith(d): return OptionalSourceRoot(SourceRoot(str(req.path))) return OptionalSourceRoot(None) output = run_rule_with_mocks( all_roots, rule_args=[source_root_config], mock_gets=[ MockGet(product_type=Snapshot, subject_type=PathGlobs, mock=provider_rule), MockGet( product_type=OptionalSourceRoot, subject_type=SourceRootRequest, mock=source_root_mock_rule, ), ], ) assert { SourceRoot("contrib/go/examples/src/go/src"), SourceRoot("src/java"), SourceRoot("src/python"), SourceRoot("src/python/subdir/src/python"), SourceRoot("src/kotlin"), SourceRoot("src/example/java"), SourceRoot("src/example/python"), SourceRoot("my/project/src/java"), SourceRoot("fixed/root/jvm"), } == set(output)
def test_all_roots(self): SOURCE = SourceRootCategories.SOURCE TEST = SourceRootCategories.TEST THIRDPARTY = SourceRootCategories.THIRDPARTY options = { "pants_ignore": [], "source_root_patterns": ["src/*", "src/example/*"], "source_roots": { # Fixed roots should trump patterns which would detect contrib/go/examples/src/go here. "contrib/go/examples/src/go/src": ["go"], # Dir does not exist, should not be listed as a root. "java": ["java"], }, } options.update(self.options[""] ) # We need inherited values for pants_workdir etc. self.context(for_subsystems=[SourceRootConfig], options={SourceRootConfig.options_scope: options}) source_root_config = SourceRootConfig.global_instance() source_roots = source_root_config.get_source_roots() # Ensure that we see any manually added roots. source_roots.add_source_root("fixed/root/jvm", ("java", "scala"), TEST) # This function mocks out reading real directories off the file system def provider_rule(path_globs: PathGlobs) -> Snapshot: dirs = ( "contrib/go/examples/3rdparty/go", "contrib/go/examples/src/go/src", "src/java", "src/python", "src/kotlin", "my/project/src/java", "src/example/java", "src/example/python", "fixed/root/jvm", # subdirectories of source roots should not show up in final output "src/kotlin/additional/directories/that/might/get/matched/src/foo", ) return Snapshot(Digest("abcdef", 10), (), dirs) output = run_rule( list_roots.all_roots, rule_args=[source_root_config], mock_gets=[ MockGet(product_type=Snapshot, subject_type=PathGlobs, mock=provider_rule) ], ) self.assertEqual( { SourceRoot("contrib/go/examples/3rdparty/go", ("go", ), THIRDPARTY), SourceRoot("contrib/go/examples/src/go/src", ("go", ), SOURCE), SourceRoot("src/java", ("java", ), SOURCE), SourceRoot("src/python", ("python", ), SOURCE), SourceRoot("src/kotlin", ("kotlin", ), SOURCE), SourceRoot("src/example/java", ("java", ), SOURCE), SourceRoot("src/example/python", ("python", ), SOURCE), SourceRoot("my/project/src/java", ("java", ), SOURCE), SourceRoot("fixed/root/jvm", ("java", "scala"), TEST), }, set(output), )
def source_root_mock_rule(req: SourceRootRequest) -> OptionalSourceRoot: for d in dirs: if str(req.path).startswith(d): return OptionalSourceRoot(SourceRoot(str(req.path))) return OptionalSourceRoot(None)