Skip to content

Commit

Permalink
Set default source root patterns. (#10043)
Browse files Browse the repository at this point in the history
These will work in a decent variety of cases.
  • Loading branch information
benjyw committed Jun 14, 2020
1 parent 5cb5379 commit 32a72a8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/python/pants/core/util_rules/strip_source_roots_test.py
Expand Up @@ -36,9 +36,14 @@ def get_stripped_files(
args: Optional[List[str]] = None,
) -> StrippedResponseData:
args = args or []
args.append("--source-root-patterns=src/python")
args.append("--source-root-patterns=src/java")
args.append("--source-root-patterns=tests/python")
has_source_root_patterns = False
for arg in args:
if arg.startswith("--source-root-patterns"):
has_source_root_patterns = True
break
if not has_source_root_patterns:
source_root_patterns = ["src/python", "src/java", "tests/python"]
args.append(f"--source-root-patterns={json.dumps(source_root_patterns)}")
result = self.request_single_product(
SourceRootStrippedSources, Params(request, create_options_bootstrapper(args=args)),
)
Expand Down
7 changes: 3 additions & 4 deletions src/python/pants/source/source_root.py
Expand Up @@ -145,6 +145,8 @@ class SourceRootConfig(Subsystem):

options_scope = "source"

DEFAULT_ROOT_PATTERNS = ["/", "src", "src/python", "src/py"]

@classmethod
def register_options(cls, register):
super().register_options(register)
Expand All @@ -164,10 +166,7 @@ def register_options(cls, register):
metavar='["pattern1", "pattern2", ...]',
type=list,
fingerprint=True,
# For Python a good default might be the repo root, but that would be a bad default
# for other languages, so best to have no default for now, and force users to be
# explicit about this when integrating Pants. It's a fairly trivial thing to do.
default=[],
default=cls.DEFAULT_ROOT_PATTERNS,
advanced=True,
help="A list of source root suffixes. A directory with this suffix will be considered "
"a potential source root. E.g., `src/python` will match `<buildroot>/src/python`, "
Expand Down
12 changes: 12 additions & 0 deletions src/python/pants/source/source_root_test.py
Expand Up @@ -100,6 +100,18 @@ def find_root(path):
assert find_root("prefix/project/python/foo/bar.py") is None


def test_source_root_default_patterns() -> None:
# Test that the default root patterns behave as expected.
def find_root(path):
return _find_root(path, tuple(SourceRootConfig.DEFAULT_ROOT_PATTERNS))

assert "src/python" == find_root("src/python/foo/bar.py")
assert "src" == find_root("src/baz/qux.py")
assert "project1/src/python" == find_root("project1/src/python/foo/bar.py")
assert "project2/src" == find_root("project2/src/baz/qux.py")
assert "." == find_root("corge/grault.py")


def test_marker_file() -> None:
def find_root(path):
return _find_root(
Expand Down
4 changes: 3 additions & 1 deletion tests/python/pants_test/test_maven_layout.py
Expand Up @@ -19,7 +19,9 @@ def alias_groups(cls):

def setUp(self):
super().setUp()
init_subsystems([SourceRootConfig, JUnit])
init_subsystems(
[SourceRootConfig, JUnit], {"source": {"root_patterns": ["src/main/*", "src/test/*"]}}
)
self.create_file("projectB/src/test/scala/a/source")
self.add_to_build_file(
"projectB/src/test/scala", 'junit_tests(name="test", sources=["a/source"])'
Expand Down

0 comments on commit 32a72a8

Please sign in to comment.