Skip to content

Commit

Permalink
Consistently use in output.
Browse files Browse the repository at this point in the history
It's up to tests to use os.linesep when reading and testing outputs.
Many tests still are not fixed, but all production outputs are.

More work towards pex-tool#2658.
  • Loading branch information
jsirois committed Feb 21, 2025
1 parent 3e4a3df commit 9be6a98
Show file tree
Hide file tree
Showing 17 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pex/cli/commands/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2077,7 +2077,7 @@ def _sync(self):
)
if sync_target.command:
return Ok(
os.linesep.join(
"\n".join(
(
would_update_venv_msg + " and run the following command in it:",
" " + " ".join(shlex_quote(arg) for arg in sync_target.command),
Expand Down
2 changes: 1 addition & 1 deletion pex/dist_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def requires_dists(location):
project_name=metadata_files.metadata.project_name,
count=len(legacy_requires),
field=pluralize(legacy_requires, "field"),
requires=os.linesep.join(
requires="\n".join(
"{index}.) Requires: {req}".format(index=index, req=req)
for index, req in enumerate(legacy_requires, start=1)
),
Expand Down
4 changes: 2 additions & 2 deletions pex/docs/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _read_url(
while time.time() - start < timeout:
with open(server_log) as fp:
for line in fp:
if line.endswith(os.linesep):
if line.endswith(("\r", "\n")):
match = re.search(r"Serving HTTP on 0\.0\.0\.0 port (?P<port>\d+)", line)
if match:
port = match.group("port")
Expand Down Expand Up @@ -140,7 +140,7 @@ def __str__(self):
if self.additional_msg:
lines.append(self.additional_msg)
lines.append("See the log at {log} for more details.".format(log=self.log))
return os.linesep.join(lines)
return "\n".join(lines)


def launch(
Expand Down
3 changes: 1 addition & 2 deletions pex/pep_723.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import absolute_import

import os
import re
from collections import OrderedDict

Expand Down Expand Up @@ -53,7 +52,7 @@ def create_metadata_error(self, problem_clause):

def parse_metadata(self):
# type: () -> Mapping[str, Any]
stripped_content = os.linesep.join(
stripped_content = "\n".join(
line[2:] if line.startswith("# ") else line[1:] for line in self.content
)
try:
Expand Down
4 changes: 2 additions & 2 deletions pex/repl/pex_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def _create_repl_data(
"""
)
.format(
pex_header=os.linesep.join(pex_header),
pex_header="\n".join(pex_header),
python_version=sys.version,
platform=sys.platform,
more_info_footer=more_info_footer,
Expand All @@ -241,7 +241,7 @@ def _create_repl_data(

return _REPLData(
banner=banner,
pex_info_summary=os.linesep.join(pex_info_summary),
pex_info_summary="\n".join(pex_info_summary),
ps1=">>>",
ps2="...",
)
Expand Down
2 changes: 1 addition & 1 deletion pex/resolve/downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def download(
except Job.Error as e:
error_lines = list(e.contextualized_stderr()) or str(e).splitlines()
return Error(
os.linesep.join(error_lines)
"\n".join(error_lines)
if "See above for details" in error_lines[-1]
else error_lines[-1]
)
Expand Down
3 changes: 1 addition & 2 deletions pex/resolve/lockfile/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import itertools
import logging
import os
from collections import OrderedDict
from contextlib import contextmanager

Expand Down Expand Up @@ -423,7 +422,7 @@ def _calculate_requirement_configuration(
return

with named_temporary_file(prefix="lock_update.", suffix=".constraints.txt", mode="w") as fp:
fp.write(os.linesep.join(constraints))
fp.write("\n".join(constraints))
fp.flush()
constraint_files = [fp.name]
if self.requirement_configuration.constraint_files:
Expand Down
5 changes: 2 additions & 3 deletions pex/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,15 +416,14 @@ def collect_platforms(tags):
incompatible = not wheel_tag_match
if incompatible:
raise ValueError(
"No pre-built wheel was available for {project_name} {version}.{eol}"
"No pre-built wheel was available for {project_name} {version}.\n"
"Successfully built the wheel {wheel} from the sdist {sdist} but it is not "
"compatible with the requested foreign target {target}.{eol}"
"compatible with the requested foreign target {target}.\n"
"You'll need to build a wheel from {sdist} on the foreign target platform and "
"make it available to Pex via a `--find-links` repo or a custom "
"`--index`.".format(
project_name=wheel.project_name,
version=wheel.version,
eol=os.linesep,
wheel=os.path.basename(wheel_path),
sdist=os.path.basename(self.request.source_path),
target=self.request.target.render_description(),
Expand Down
4 changes: 1 addition & 3 deletions pex/venv/venv_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ def maybe_log(*message):
if ignored_pex_env_vars:
maybe_log(
"Ignoring the following environment variables in Pex venv mode:\n"
"{ignored_env_vars}".format(
ignored_env_vars=os.linesep.join(sorted(ignored_pex_env_vars))
)
"{ignored_env_vars}".format(ignored_env_vars="\n".join(sorted(ignored_pex_env_vars)))
)

os.environ["VIRTUAL_ENV"] = venv_dir
Expand Down
2 changes: 1 addition & 1 deletion pex/ziputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def __str__(self):
"Please file an issue at https://github.com/pex-tool/pex/issues/new that includes "
"this full backtrace if you need this support."
)
return os.linesep.join(message_lines)
return "\n".join(message_lines)


_MAX_2_BYTES = 0xFFFF
Expand Down
4 changes: 2 additions & 2 deletions scripts/analyze-CI-shard-timeout.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python3

import os
import re
import sys
from pathlib import Path
Expand Down Expand Up @@ -28,7 +27,8 @@ def analyze(log: Path) -> Any:

hung_tests = sorted(test for test, complete in tests.items() if not complete)
if hung_tests:
return f"The following tests never finished:\n{os.linesep.join(hung_tests)}"
hung_tests_lines = "\n".join(hung_tests)
return f"The following tests never finished:\n{hung_tests_lines}"


def main() -> Any:
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-cache-image.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def main() -> Any:
bad_tox_envs = selected_tox_envs - all_tox_envs
if bad_tox_envs:
return colors.red(
os.linesep.join(
"\n".join(
(
"The following selected tox envs are not used in Linux CI test shards:",
*(f" {bad_tox_env}" for bad_tox_env in sorted(bad_tox_envs)),
Expand Down
4 changes: 2 additions & 2 deletions scripts/create-packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ def build_pex_scies(
artifacts = glob.glob(f"{output_file}*")
scie_artifacts = [artifact for artifact in artifacts if not artifact.endswith(".sha256")]
if len(scie_artifacts) != 1:
artifact_lines = "\n".join(sorted(artifacts))
raise SystemExit(
f"Found unexpected artifacts after generating Pex scie:{os.linesep}"
f"{os.linesep.join(sorted(artifacts))}"
f"Found unexpected artifacts after generating Pex scie:\n{artifact_lines}"
)
scie_name = os.path.basename(scie_artifacts[0])
for artifact in artifacts:
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/cli/commands/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ def test_update_targeted_impossible(
pip_to="pip" if pip_version.version < PipVersion.v24_1.version else "pip to"
),
]
assert expected_lines == error_lines[12:], os.linesep.join(
assert expected_lines == error_lines[12:], "\n".join(
difflib.unified_diff(expected_lines, error_lines[12:])
)

Expand Down Expand Up @@ -1023,7 +1023,7 @@ def test_update_add_impossible(
pip_to="pip" if pip_version.version < PipVersion.v24_1.version else "pip to"
),
]
assert expected_lines == error_lines[13:], os.linesep.join(
assert expected_lines == error_lines[13:], "\n".join(
difflib.unified_diff(expected_lines, error_lines[12:])
)

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/scie/test_pex_scie.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def assert_output(
in output_lines
)
stderr_import_logging = any(line.startswith("import ") for line in output_lines)
assert expect_python_verbose is stderr_import_logging, os.linesep.join(output_lines)
assert expect_python_verbose is stderr_import_logging, "\n".join(output_lines)

assert_output(
args=[busybox, "foo-script-ad-hoc"],
Expand Down
4 changes: 3 additions & 1 deletion tests/resolve/test_pex_repository_resolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2022 Pex project contributors.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import absolute_import, print_function

import os
import re
from collections import defaultdict
Expand Down Expand Up @@ -62,7 +64,7 @@ def create_constraints_file(*requirements):
constraints_file = os.path.join(safe_mkdtemp(), "constraints.txt")
with open(constraints_file, "w") as fp:
for requirement in requirements:
fp.write(requirement + os.linesep)
print(requirement, file=fp)
return constraints_file


Expand Down
2 changes: 1 addition & 1 deletion tests/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def test_sys_path(python):
# type: (str) -> None

interp = PythonInterpreter.from_binary(python)
_, stdout, _ = interp.execute(args=["-c", "import os, sys; print(os.linesep.join(sys.path))"])
_, stdout, _ = interp.execute(args=["-c", r"import sys; print('\n'.join(sys.path))"])
assert tuple(entry for entry in stdout.splitlines() if entry) == interp.sys_path, (
'Its expected the sys_path matches the runtime sys.path with the exception of the PWD ("") '
"head entry."
Expand Down

0 comments on commit 9be6a98

Please sign in to comment.