Skip to content

Commit

Permalink
[ci skip-rust-tests]
Browse files Browse the repository at this point in the history
[ci skip-jvm-tests]
  • Loading branch information
benjyw committed Jun 24, 2020
1 parent 7549c8e commit 56f7b00
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
PythonAwsLambdaHandler,
PythonAwsLambdaRuntime,
)
from pants.backend.python.rules import (
download_pex_bin,
pex,
pex_from_targets,
python_sources,
)
from pants.backend.python.rules import download_pex_bin, pex, pex_from_targets, python_sources
from pants.backend.python.rules.pex import (
Pex,
PexInterpreterConstraints,
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/lint/mypy/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Tuple

from pants.backend.python.lint.mypy.subsystem import MyPy
from pants.backend.python.rules import download_pex_bin, python_sources, pex
from pants.backend.python.rules import download_pex_bin, pex, python_sources
from pants.backend.python.rules.pex import (
Pex,
PexInterpreterConstraints,
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/lint/pylint/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Iterable, Tuple

from pants.backend.python.lint.pylint.subsystem import Pylint
from pants.backend.python.rules import download_pex_bin, python_sources, pex
from pants.backend.python.rules import download_pex_bin, pex, python_sources
from pants.backend.python.rules.pex import (
Pex,
PexInterpreterConstraints,
Expand Down
8 changes: 6 additions & 2 deletions src/python/pants/backend/python/rules/inject_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import os
from dataclasses import dataclass

from pants.engine.fs import Digest, FileContent, InputFilesContent, MergeDigests, Snapshot
Expand Down Expand Up @@ -41,7 +41,11 @@ async def inject_missing_init_files(request: InjectInitRequest) -> InitInjectedS
for init_file in missing_init_files_list
)
for optional_src_root, init_file in zip(optional_src_roots, missing_init_files_list):
if optional_src_root.source_root is None:
# If the identified-as-missing __init__.py file is above or at a source root, remove it.
if (
optional_src_root.source_root is None
or optional_src_root.source_root.path == os.path.dirname(init_file)
):
missing_init_files.remove(init_file)

generated_inits_digest = await Get[Digest](
Expand Down
26 changes: 12 additions & 14 deletions src/python/pants/backend/python/rules/inject_init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def rules(cls):
RootRule(Digest),
)

def assert_injected(self, *, original_files: List[str], expected_added: List[str],
sources_stripped=True) -> None:
def assert_injected(
self, *, original_files: List[str], expected_added: List[str], sources_stripped=True
) -> None:
request = InjectInitRequest(
self.make_snapshot({fp: "# python code" for fp in original_files}),
sources_stripped=sources_stripped,
Expand Down Expand Up @@ -55,11 +56,7 @@ def test_no_inits_present(self) -> None:
)
self.assert_injected(
original_files=["a/b/lib.py", "a/b/subdir/lib.py"],
expected_added=[
"a/__init__.py",
"a/b/__init__.py",
"a/b/subdir/__init__.py",
],
expected_added=["a/__init__.py", "a/b/__init__.py", "a/b/subdir/__init__.py",],
)

def test_preserves_original_inits(self) -> None:
Expand All @@ -83,12 +80,13 @@ def test_preserves_original_inits(self) -> None:
)

def test_source_roots_unstripped(self):
self.assert_injected(original_files=[
"src/python/lib.py",
"src/python/subdir/lib.py",
"src/python/subdir/__init__.py",
"src/python/anothersubdir/lib.py",
],
expected_added=["src/python/__init__.py", "src/python/anothersubdir/__init__.py"],
self.assert_injected(
original_files=[
"src/python/lib.py",
"src/python/subdir/lib.py",
"src/python/subdir/__init__.py",
"src/python/anothersubdir/lib.py",
],
expected_added=["src/python/anothersubdir/__init__.py"],
sources_stripped=False,
)
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/rules/pex_from_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from dataclasses import dataclass
from typing import Iterable, Optional, Tuple

from pants.backend.python.rules.python_sources import StrippedPythonSources
from pants.backend.python.rules.pex import (
PexInterpreterConstraints,
PexPlatforms,
PexRequest,
PexRequirements,
TwoStepPexRequest,
)
from pants.backend.python.rules.python_sources import StrippedPythonSources
from pants.backend.python.target_types import (
PythonInterpreterCompatibility,
PythonRequirementsField,
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/backend/python/rules/pytest_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
CoverageSubsystem,
PytestCoverageData,
)
from pants.backend.python.rules.python_sources import UnstrippedPythonSources
from pants.backend.python.rules.pex import (
Pex,
PexInterpreterConstraints,
PexRequest,
PexRequirements,
)
from pants.backend.python.rules.pex_from_targets import PexFromTargetsRequest
from pants.backend.python.rules.python_sources import UnstrippedPythonSources
from pants.backend.python.subsystems.pytest import PyTest
from pants.backend.python.subsystems.subprocess_environment import SubprocessEncodingEnvironment
from pants.backend.python.target_types import (
Expand Down
73 changes: 70 additions & 3 deletions src/python/pants/backend/python/rules/python_sources_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import List, Type

from pants.backend.python.rules.python_sources import StrippedPythonSources
from pants.backend.python.rules.python_sources import StrippedPythonSources, UnstrippedPythonSources
from pants.backend.python.rules.python_sources import rules as python_sources_rules
from pants.backend.python.target_types import PythonSources
from pants.core.target_types import Files, Resources
Expand Down Expand Up @@ -52,7 +52,7 @@ def test_adds_missing_inits_and_strips_source_roots(self) -> None:
StrippedPythonSources,
Params(
Targets([target_with_init, target_without_init, files_target]),
create_options_bootstrapper(args=["--source-root-patterns=src/python"]),
create_options_bootstrapper(args=["--source-root-patterns=['src/python']"]),
),
)
assert sorted(result.snapshot.files) == sorted(
Expand Down Expand Up @@ -83,7 +83,74 @@ def test_filters_out_irrelevant_targets(self) -> None:
StrippedPythonSources,
Params(
Targets(targets),
create_options_bootstrapper(args=["--source-root-patterns=src/python"]),
create_options_bootstrapper(args=["--source-root-patterns=['src/python']"]),
),
)
assert sorted(result.snapshot.files) == sorted(["p.py", "src/python/f.txt", "r.txt"])


class UnstrippedPythonSourcesTest(TestBase):
@classmethod
def rules(cls):
return (*super().rules(), *python_sources_rules())

def create_target(
self, *, parent_directory: str, files: List[str], target_cls: Type[Target] = PythonTarget
) -> Target:
self.create_files(parent_directory, files=files)
address = Address(spec_path=parent_directory, target_name="target")
return target_cls({Sources.alias: files}, address=address)

def test_adds_missing_inits(self) -> None:
target_with_init = self.create_target(
parent_directory="src/python/project", files=["lib.py", "__init__.py"]
)
target_without_init = self.create_target(
parent_directory="src/python/test_project", files=["f1.py", "f2.py"]
)
files_target = self.create_target(
parent_directory="src/python/project/resources",
files=["loose_file.txt"],
target_cls=Files,
)
result = self.request_single_product(
UnstrippedPythonSources,
Params(
Targets([target_with_init, target_without_init, files_target]),
create_options_bootstrapper(args=["--source-root-patterns=['src/python']"]),
),
)
assert sorted(result.snapshot.files) == sorted(
[
"src/python/project/lib.py",
"src/python/project/__init__.py",
"src/python/test_project/f1.py",
"src/python/test_project/f2.py",
"src/python/test_project/__init__.py",
"src/python/project/resources/loose_file.txt",
]
)

def test_filters_out_irrelevant_targets(self) -> None:
targets = [
self.create_target(
parent_directory="src/python", files=["p.py"], target_cls=PythonTarget
),
self.create_target(parent_directory="src/python", files=["f.txt"], target_cls=Files),
self.create_target(
parent_directory="src/python", files=["r.txt"], target_cls=Resources
),
self.create_target(
parent_directory="src/python", files=["j.java"], target_cls=NonPythonTarget
),
]
result = self.request_single_product(
UnstrippedPythonSources,
Params(
Targets(targets),
create_options_bootstrapper(args=["--source-root-patterns=['src/python']"]),
),
)
assert sorted(result.snapshot.files) == sorted(
["src/python/p.py", "src/python/f.txt", "src/python/r.txt"]
)
7 changes: 1 addition & 6 deletions src/python/pants/core/goals/repl_integration_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.backend.python.rules import (
download_pex_bin,
pex,
pex_from_targets,
python_sources,
)
from pants.backend.python.rules import download_pex_bin, pex, pex_from_targets, python_sources
from pants.backend.python.rules import repl as python_repl
from pants.backend.python.rules.repl import PythonRepl
from pants.backend.python.subsystems import python_native_code, subprocess_environment
Expand Down

0 comments on commit 56f7b00

Please sign in to comment.