Skip to content

Commit

Permalink
Always disable capturing in pytest setup
Browse files Browse the repository at this point in the history
  • Loading branch information
noliveleger committed Jul 27, 2020
1 parent b801ad6 commit 62c47db
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,36 @@
'peter',
]

try:
INTERRUPTED = pytest.ExitCode.INTERRUPTED # pytest 5
except AttributeError:
INTERRUPTED = 2


def stderr_prompt(message):
sys.stderr.write(message)
# FIXME: Python 3 compatibility
return raw_input().strip()


def toggle_capturing(capture_manager, stop):
if stop:
capture_manager.suspend_global_capture()
capture_manager.stop_global_capturing()
else:
capture_manager.start_global_capturing()
capture_manager.resume_global_capture()


@pytest.fixture(scope="session", autouse=True)
def setup(request):
# We need to disable global capturing in case `-s` is not passed to `pytest`
# by the users to force print the safeguard messages about data loss.
capture_manager = request.config.pluginmanager.getplugin("capturemanager")
is_global_capturing = capture_manager.is_globally_capturing()

if is_global_capturing:
toggle_capturing(capture_manager, stop=True)

for username in TEST_USERNAMES:
if user_storage_exists(username):
Expand All @@ -39,7 +60,9 @@ def setup(request):
'cancel: '.format(username)
)
if response.lower() != 'yes':
sys.exit(1)
if is_global_capturing:
toggle_capturing(capture_manager, stop=False)
pytest.exit('User interrupted tests', INTERRUPTED)

if 'instances' in settings.MONGO_DB.collection_names():
response = stderr_prompt(
Expand All @@ -51,12 +74,18 @@ def setup(request):
if response.lower() == 'yes':
settings.MONGO_DB.instances.drop()
else:
sys.exit(1)
if is_global_capturing:
toggle_capturing(capture_manager, stop=False)
pytest.exit('User interrupted tests', INTERRUPTED)

if is_global_capturing:
toggle_capturing(capture_manager, stop=False)

request.addfinalizer(_tear_down)


def _tear_down():
return
print("\nCleaning testing environment...")
print('Removing MongoDB...')
settings.MONGO_DB.instances.drop()
Expand Down

0 comments on commit 62c47db

Please sign in to comment.