Files
aqtinstall/tests/test_connection.py
David Dalcino d5213142a8 Fix misuse of disable_socket (#415)
In an earlier PR, I added calls to `disable_socket()` from
`pytest_socket` where I thought they were needed to prevent some tests
from accessing the network, in case they weren't monkeypatched properly.
Today, I discovered that `disable_socket()` disables sockets globally
for all tests, which means that the tests that use remote data cannot
run if they are executed after another test calls `disable_socket()`.

This change calls `disable_socket()` once from `conftest.py`, so that
no tests are allowed to use network data unless they are marked as ok
to use the network, with `@pytest.mark.enable_socket`. See example of
usage in `tests/test_connection.py`.

Changed return code for unexpected exceptions:

This allows us to write tests that can discover whether an unexpected
exception occurred just by checking the return code, rather than reading
stderr. This will allow us to write less friable tests that don't break
every time some insignificant output details change.

This change catches exceptions derived from Exception and
KeyboardInterrupt raised by `installer`, while run by multiple
processes, and propagates them back to earlier stack entries. This will
prevent any OSError and BrokenPipe exceptions that would otherwise be
raised when one process has an exception while the other processes are
still running.

This also handles the MemoryError exception we saw in #416, and offers
some suggestions for solving the issue.
2021-10-05 14:40:05 +09:00

48 lines
1.6 KiB
Python

import re
import sys
import pytest
import aqt
@pytest.mark.enable_socket
@pytest.mark.remote_data
def test_cli_unknown_version(capsys):
wrong_version = "5.16.0"
cli = aqt.installer.Cli()
assert cli.run(["install-qt", "mac", "desktop", wrong_version]) == 1
out, err = capsys.readouterr()
sys.stdout.write(out)
sys.stderr.write(err)
assert not out
"""
Expected result when no redirect occurs:
aqtinstall(aqt) v.* on Python 3.*
Specified Qt version is unknown: 5.16.0.
Failed to locate XML data for Qt version '5.16.0'.
==============================Suggested follow-up:==============================
* Please use 'aqt list-qt mac desktop' to show versions available.
Expected result when redirect occurs:
aqtinstall(aqt) v.* on Python 3.*
Specified Qt version is unknown: 5.16.0.
Connection to the download site failed and fallback to mirror site.
Failed to locate XML data for Qt version '5.16.0'.
==============================Suggested follow-up:==============================
* Please use 'aqt list-qt mac desktop' to show versions available.
"""
matcher = re.compile(
r"^aqtinstall\(aqt\) v.* on Python 3.*\n"
r".*Specified Qt version is unknown: " + re.escape(wrong_version) + r"\.\n"
r".*Failed to locate XML data for Qt version '" + re.escape(wrong_version) + r"'\.\n"
r"==============================Suggested follow-up:==============================\n"
r"\* Please use 'aqt list-qt mac desktop' to show versions available\.\n",
)
assert matcher.match(err)