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

Make log persistency behavior optional #122

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
0.22.0 (UNRELEASED)
-------------------

- Make log files persistency, added in `0.21.0`, optional, defaulting to True. The previous logging behavior (prior to `0.21.0`) can be
enabled by setting `persist_logs` flag to `False` when calling `XProcess.ensure`.
- Fix resource warnings due to leaked internal file handles (`#121 <https://github.com/pytest-dev/pytest-xprocess/issues/119>`_)
- Ignore zombie processes which are erroneously considered alive with python 3.11
(`#117 <https://github.com/pytest-dev/pytest-xprocess/issues/117>`_)
Expand Down
42 changes: 22 additions & 20 deletions xprocess/xprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def getinfo(self, name):

return XProcessInfo(self.rootdir, name)

def ensure(self, name, preparefunc, restart=False):
def ensure(self, name, preparefunc, restart=False, persist_logs=True):
"""Returns (PID, logfile) from a newly started or already
running process.

Expand All @@ -217,7 +217,6 @@ def ensure(self, name, preparefunc, restart=False):
@return: (PID, logfile) logfile will be seeked to the end if the
server was running, otherwise seeked to the line after
where the waitpattern matched."""

from subprocess import Popen, STDOUT

xresource = XProcessResources(self.proc_wait_timeout)
Expand All @@ -235,8 +234,11 @@ def ensure(self, name, preparefunc, restart=False):
starter = preparefunc(controldir, self)
args = [str(x) for x in starter.args]
self.log.debug("%s$ %s", controldir, " ".join(args))
stdout = open(str(info.logpath), "a+b", 0)
stdout.write(bytes(f"{XPROCESS_BLOCK_DELIMITER}\n", "utf8"))
if persist_logs:
stdout = open(str(info.logpath), "a+b", 0)
stdout.write(bytes(f"{XPROCESS_BLOCK_DELIMITER}\n", "utf8"))
else:
stdout = open(str(info.logpath), "wb", 0)

# is env still necessary? we could pass all in popen_kwargs
kwargs = {"env": starter.env}
Expand Down Expand Up @@ -269,25 +271,25 @@ def ensure(self, name, preparefunc, restart=False):
self.log.debug("process %r started pid=%s", name, pid)
stdout.close()

log_file_handle = info.logpath.open()

# skip previous process logs
process_log_block_handle = info.logpath.open()
lines = process_log_block_handle.readlines()
if lines:
proc_block_counter = sum(
1 for line in lines if XPROCESS_BLOCK_DELIMITER in line
)
for line in log_file_handle:
if XPROCESS_BLOCK_DELIMITER in line:
proc_block_counter -= 1
if proc_block_counter <= 0:
break

log_file_handle = open(info.logpath, errors="ignore")
# keep track of all file handles so we can
# cleanup later during teardown phase
xresource.fhandles.append(log_file_handle)
xresource.fhandles.append(process_log_block_handle)

if persist_logs:
# skip previous process logs
process_log_block_handle = info.logpath.open()
lines = process_log_block_handle.readlines()
if lines:
proc_block_counter = sum(
1 for line in lines if XPROCESS_BLOCK_DELIMITER in line
)
for line in log_file_handle:
if XPROCESS_BLOCK_DELIMITER in line:
proc_block_counter -= 1
if proc_block_counter <= 0:
break
xresource.fhandles.append(process_log_block_handle)

self.resources.append(xresource)

Expand Down