Skip to content

Commit 234ec52

Browse files
zzacharolnielsen
authored andcommitted
tests: add Flask.logging.default_handler if not present
1 parent 92c7cb0 commit 234ec52

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

tests/test_cmd.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525

2626
from __future__ import absolute_import, print_function
2727

28+
import logging
2829
import os
2930
from subprocess import call
3031

3132
import pkg_resources
33+
import pytest
3234
from click.testing import CliRunner
3335
from flask.cli import ScriptInfo
3436
from mock import MagicMock, Mock, patch
@@ -37,6 +39,23 @@
3739
from invenio_base.cli import instance
3840

3941

42+
@pytest.fixture(autouse=True)
43+
def force_logging():
44+
"""Flask 0.13/1.0 changed logging to not add the default
45+
handler in case a handler is already installed. pytest
46+
automatically adds a handler to the root logger, causing
47+
Flask not to add a handler. This is an issue when testing
48+
Click output which uses the logger to output to the console.
49+
"""
50+
try:
51+
from flask.logging import default_handler
52+
logger = logging.getLogger('flask.app')
53+
if default_handler not in logger.handlers:
54+
logger.handlers.append(default_handler)
55+
except:
56+
pass
57+
58+
4059
def test_instance_create():
4160
"""Test ``instance create`` command."""
4261
runner = CliRunner()
@@ -69,22 +88,27 @@ def test_list_entry_points():
6988
assert lines[1] == ' inveniomanage = invenio_base.__main__:cli'
7089

7190
# Test no entry point matching
72-
result = runner.invoke(
73-
instance, ['entrypoints', '-e', 'nothing_here'])
91+
result = runner.invoke(instance, ['entrypoints', '-e', 'nothing_here'])
7492
assert result.exit_code == 0
7593
assert result.output == ""
7694

7795
# By default we only show entry points groups starting with "invenio"
78-
dist.get_entry_map = Mock(return_value={
79-
'invenio_base.apps': {
80-
'myapp': 'myapp = myapp:MyApp',
81-
'app1': 'app1 = app1:MyApp'},
82-
'invenio_base.api_apps': {
83-
'myapi': 'myapi = myapi:MyApp'},
84-
'console_scripts': {
85-
'mycli': 'mycli = cli:main'},
86-
})
87-
result = runner.invoke(instance, ['entrypoints', ])
96+
dist.get_entry_map = Mock(
97+
return_value={
98+
'invenio_base.apps': {
99+
'myapp': 'myapp = myapp:MyApp',
100+
'app1': 'app1 = app1:MyApp'
101+
},
102+
'invenio_base.api_apps': {
103+
'myapi': 'myapi = myapi:MyApp'
104+
},
105+
'console_scripts': {
106+
'mycli': 'mycli = cli:main'
107+
},
108+
})
109+
result = runner.invoke(instance, [
110+
'entrypoints',
111+
])
88112
assert result.exit_code == 0
89113
print(result.output.splitlines())
90114
lines = result.output.splitlines()
@@ -103,16 +127,15 @@ def _config_loader(app, **kwargs):
103127

104128
create_app = create_app_factory('test', config_loader=_config_loader)
105129
app = create_app(KWARGS_TEST=True)
130+
106131
script_info = ScriptInfo(create_app=lambda info: app)
107132

108133
# Check that CLI command fails when the SECRET_KEY is not set.
109134
with app.app_context():
110135
runner = CliRunner()
111-
result = runner.invoke(instance,
112-
['migrate-secret-key',
113-
'--old-key',
114-
'OLD_SECRET_KEY'],
115-
obj=script_info)
136+
result = runner.invoke(
137+
instance, ['migrate-secret-key', '--old-key', 'OLD_SECRET_KEY'],
138+
obj=script_info)
116139
assert result.exit_code == 1
117140
assert 'Error: SECRET_KEY is not set in the configuration.' in \
118141
result.output

0 commit comments

Comments
 (0)