Skip to content

Commit

Permalink
make test_log_path_printed less fragile
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Feb 14, 2021
1 parent 2410e9a commit a1c2b58
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions tests/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import subprocess
import sys
import threading
import time
import types
from datetime import datetime

import pytest

from porcupine import _logs


Expand Down Expand Up @@ -52,17 +53,13 @@ def test_remove_old_logs(monkeypatch, caplog):
assert f'logs{os.sep}1987-06-05T04-03-02_3.txt is more than 7 days old, removing' in text


def test_log_path_printed():
# -u for unbuffered, helps get printed output when python is killed
process = subprocess.Popen([sys.executable, '-u', '-m', 'porcupine'], stdout=subprocess.PIPE)
try:
time.sleep(1)
finally:
process.kill()
def test_log_path_printed(mocker):
mocker.patch('porcupine._logs.print')
_logs.print.side_effect = ZeroDivisionError # to make it stop when it prints
with pytest.raises(ZeroDivisionError):
_logs.setup(None)

line = process.stdout.readline()
if sys.platform == 'win32':
assert line.startswith(b'log file: ')
else:
assert line.startswith(b'log file: /') # absolute path
assert line.endswith((b'.txt\n', b'.txt\r\n'))
_logs.print.assert_called_once()
printed = _logs.print.call_args[0][0]
assert printed.startswith('log file: ')
assert os.path.isfile(printed[len('log file: '):])

0 comments on commit a1c2b58

Please sign in to comment.