From 32a72a83420817305a21e6b1237cb93aefbf203f Mon Sep 17 00:00:00 2001 From: Benjy Weinberger Date: Sun, 14 Jun 2020 14:45:33 -0700 Subject: [PATCH] Set default source root patterns. (#10043) These will work in a decent variety of cases. --- .../pants/core/util_rules/strip_source_roots_test.py | 11 ++++++++--- src/python/pants/source/source_root.py | 7 +++---- src/python/pants/source/source_root_test.py | 12 ++++++++++++ tests/python/pants_test/test_maven_layout.py | 4 +++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/python/pants/core/util_rules/strip_source_roots_test.py b/src/python/pants/core/util_rules/strip_source_roots_test.py index bffafecf017..8b0a9884878 100644 --- a/src/python/pants/core/util_rules/strip_source_roots_test.py +++ b/src/python/pants/core/util_rules/strip_source_roots_test.py @@ -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)), ) diff --git a/src/python/pants/source/source_root.py b/src/python/pants/source/source_root.py index 5b802f7e5e5..c2bbf280f1e 100644 --- a/src/python/pants/source/source_root.py +++ b/src/python/pants/source/source_root.py @@ -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) @@ -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 `/src/python`, " diff --git a/src/python/pants/source/source_root_test.py b/src/python/pants/source/source_root_test.py index 2860e5e6ec7..ee57a68657b 100644 --- a/src/python/pants/source/source_root_test.py +++ b/src/python/pants/source/source_root_test.py @@ -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( diff --git a/tests/python/pants_test/test_maven_layout.py b/tests/python/pants_test/test_maven_layout.py index 0deaa47f299..1f115ba20b0 100644 --- a/tests/python/pants_test/test_maven_layout.py +++ b/tests/python/pants_test/test_maven_layout.py @@ -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"])'