Skip to content

Commit

Permalink
Merge branch 'main' into reduce-test-warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu authored Feb 29, 2024
2 parents f2dd8cc + e5f03e0 commit bb32a4a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
fail-fast: true
matrix:
os: ["ubuntu-latest"]
python-version: ["3.9", "3.10", "3.11"]
python-version: ["3.10", "3.11", "3.12"]
experimental: [false]
include:
- python-version: "3.11"
- python-version: "3.12"
os: "ubuntu-latest"
experimental: true

Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
pytest --cov=trollflow2 trollflow2/tests --cov-report=xml
- name: Upload unittest coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
flags: unittests
file: ./coverage.xml
Expand Down
4 changes: 3 additions & 1 deletion trollflow2/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ def setup_queued_logging(log_queue, config=None):
def remove_handlers_from_config(config):
"""Remove handlers from config."""
config.pop("handlers", None)
for logger in config["loggers"]:
for logger in config.get("loggers", []):
config["loggers"][logger].pop("handlers", None)
if config.get("root", None):
config["root"].pop("handlers", None)


def queued_logging(func):
Expand Down
26 changes: 13 additions & 13 deletions trollflow2/tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ def setUp(self):
def test_run_does_not_call_process_directly(self):
"""Test that process is called through Process."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load'),\
mock.patch('trollflow2.launcher.open'),\
mock.patch('trollflow2.launcher.generate_messages') as generate_messages,\
mock.patch('trollflow2.launcher.process') as process,\
mock.patch('trollflow2.launcher.check_results'),\
with mock.patch('trollflow2.launcher.yaml.load'), \
mock.patch('trollflow2.launcher.open'), \
mock.patch('trollflow2.launcher.generate_messages') as generate_messages, \
mock.patch('trollflow2.launcher.process') as process, \
mock.patch('trollflow2.launcher.check_results'), \
mock.patch('multiprocessing.get_context'):
generate_messages.side_effect = ['foo', KeyboardInterrupt]
prod_list = {'product_list': {}}
Expand All @@ -400,8 +400,8 @@ def test_run_does_not_call_process_directly(self):
def test_run_relies_on_listener(self):
"""Test running relies on listener."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load,\
mock.patch('trollflow2.launcher.open'),\
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load, \
mock.patch('trollflow2.launcher.open'), \
mock.patch('multiprocessing.get_context') as get_context, \
mock.patch('trollflow2.launcher.ListenerContainer') as lc_:
msg = mock.MagicMock()
Expand All @@ -420,7 +420,7 @@ def test_run_relies_on_listener(self):
runner.run()
except KeyboardInterrupt:
pass
listener.output_queue.called_once()
listener.output_queue.get.assert_called_once()
lc_.assert_called_with(addresses=None, nameserver='localhost',
topics=['/topic1', '/topic2'])
# Subscriber topics are removed from config
Expand Down Expand Up @@ -451,9 +451,9 @@ def test_subprocess_is_spawned(self):
def run_on_a_simple_product_list(config):
"""Run a simple (fake) product list."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load,\
mock.patch('trollflow2.launcher.open'),\
mock.patch('multiprocessing.get_context') as get_context,\
with mock.patch('trollflow2.launcher.yaml.load') as yaml_load, \
mock.patch('trollflow2.launcher.open'), \
mock.patch('multiprocessing.get_context') as get_context, \
mock.patch('trollflow2.launcher.ListenerContainer') as lc_:

msg = mock.MagicMock()
Expand Down Expand Up @@ -486,8 +486,8 @@ def setUp(self):
def test_run_keyboard_interrupt(self):
"""Test interrupting the run with a ctrl-C."""
from trollflow2.launcher import Runner
with mock.patch('trollflow2.launcher.yaml.load'),\
mock.patch('trollflow2.launcher.open'),\
with mock.patch('trollflow2.launcher.yaml.load'), \
mock.patch('trollflow2.launcher.open'), \
mock.patch('trollflow2.launcher.ListenerContainer') as lc_:
listener = mock.MagicMock()
get = mock.Mock()
Expand Down
44 changes: 42 additions & 2 deletions trollflow2/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

import pytest

from trollflow2.logging import (create_logged_process, logging_on,
queued_logging)
from trollflow2.logging import (DEFAULT_LOG_CONFIG, create_logged_process,
logging_on, queued_logging)


def test_queued_logging_has_a_listener():
Expand Down Expand Up @@ -129,6 +129,7 @@ def run_subprocess(loggers):
proc = create_logged_process(target=fun, args=(loggers,))
proc.start()
proc.join()
return proc


@queued_logging
Expand Down Expand Up @@ -182,3 +183,42 @@ def duplicate_lines(contents):
"""Make sure there are no duplicate lines."""
lines = contents.strip().split("\n")
return len(lines) != len(set(lines))


def test_logging_config_without_loggers(tmp_path):
"""Test that the log configs without loggers work."""
logfile = tmp_path / "mylog"
LOG_CONFIG_TO_FILE = {'version': 1,
'formatters': {'simple': {'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'}},
'handlers': {'file': {'class': 'logging.FileHandler',
'filename': logfile,
'formatter': 'simple'}},
"root": {"level": "DEBUG", "handlers": ["file"]}
}

with logging_on(LOG_CONFIG_TO_FILE):
run_subprocess(["foo1", "foo2"])
with open(logfile) as fd:
file_contents = fd.read()

assert not duplicate_lines(file_contents)
assert "root debug" in file_contents
assert "root info" in file_contents
assert "root warning" in file_contents


def test_default_logging_config_works_with_subprocesses(capsys):
"""Test that the default log config works."""
LOG_CONFIG_TO_FILE = DEFAULT_LOG_CONFIG
captured = capsys.readouterr()
with logging_on(LOG_CONFIG_TO_FILE):
proc = run_subprocess(["foo1", "foo2"])
captured = capsys.readouterr()
assert proc.exitcode == 0
err = captured.err
assert not duplicate_lines(err)
assert "root debug" in err
assert "root info" in err
assert "root warning" in err
assert "foo1 debug" in err
assert "foo2 debug" in err

0 comments on commit bb32a4a

Please sign in to comment.