Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pants package :pex doesn't remove source root prefix for generated resources #21438

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
from __future__ import annotations

import json
from pathlib import Path
from textwrap import dedent
from typing import Callable, Optional

import pytest

from pants.backend.python.target_types import PexExecutionMode, PexLayout
from pants.testutil.pants_integration_test import PantsResult, run_pants, setup_tmpdir
from pants.base.build_environment import get_buildroot
from pants.testutil.pants_integration_test import (
PantsResult,
run_pants,
setup_tmpdir,
)


def run_generic_test(
Expand Down Expand Up @@ -309,3 +315,68 @@ def test_pass_extra_pex_cli_subsystem_global_args() -> None:
stderr = result.stderr.strip()
assert "unrecognized arguments" in stderr
assert "non-existing-flag-name" in stderr


def test_relocated_resources() -> None:
sources = {
"assets/query.sql": "SELECT 1",
"assets/BUILD": dedent(
"""\
files(name='files', sources=['query.sql'])
"""
),
"src/py/project/__init__.py": "",
"src/py/project/app.py": dedent(
"""\
from importlib.resources import files
import project

def main():
assert files(project).joinpath("query.sql").read_text() == "SELECT 1"
"""
),
"src/py/project/BUILD": dedent(
"""\
python_sources()
relocated_files(
name='relocated',
files_targets=['assets:files'],
src='assets',
dest='src/py/project',
)
experimental_wrap_as_resources(name='resources', inputs=[':relocated'])
pex_binary(
name="binary",
dependencies=[':resources'],
entry_point="project.app:main",
)
"""
),
"pants.toml": "",
}

for relpath, content in sources.items():
path = Path(get_buildroot()).joinpath(relpath)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)

args = [
"--backend-packages=pants.backend.python",
"--source-root-patterns=['src/py']",
"run",
"src/py/project:binary",
]
result = run_pants(args, use_pantsd=False)

assert "Hola, mundo.\n" in result.stderr
file = result.stdout.strip()
assert "src_root2" not in file
assert file.endswith("utils/strutil.py")
if layout == PexLayout.LOOSE:
# Loose PEXs execute their own code directly
assert "pants-sandbox-" in file
else:
assert "pants-sandbox-" not in file
assert result.exit_code == 23

return run
Loading