def test_cli_determine_qt_version(monkeypatch, host, target, arch, version_or_spec: str, expected_version: Version, is_bad_spec: bool): _html = (Path(__file__).parent / "data" / f"{host}-{target}.html").read_text("utf-8") monkeypatch.setattr(MetadataFactory, "fetch_http", lambda *args, **kwargs: _html) cli = Cli() cli._setup_settings() if is_bad_spec: with pytest.raises(CliInputError) as e: Cli._determine_qt_version(version_or_spec, host, target, arch) assert e.type == CliInputError assert format( e.value ) == f"Invalid version or SimpleSpec: '{version_or_spec}'\n" + SimpleSpec.usage( ) elif not expected_version: with pytest.raises(CliInputError) as e: Cli._determine_qt_version(version_or_spec, host, target, arch) assert e.type == CliInputError expect_msg = f"No versions of Qt exist for spec={version_or_spec} with host={host}, target={target}, arch={arch}" actual_msg = format(e.value) assert actual_msg == expect_msg else: ver = Cli._determine_qt_version(version_or_spec, host, target, arch) assert ver == expected_version
def test_cli_check_module(): cli = Cli() cli._setup_settings() assert cli._check_modules_arg("5.11.3", ["qtcharts", "qtwebengine"]) assert not cli._check_modules_arg("5.7", ["not_exist"]) assert cli._check_modules_arg("5.14.0", None) assert not cli._check_modules_arg("5.15.0", ["Unknown"])
def test_cli_invalid_version(capsys, invalid_version): """Checks that invalid version strings are handled properly""" # Ensure that invalid_version cannot be a semantic_version.Version with pytest.raises(ValueError): Version(invalid_version) cli = Cli() cli._setup_settings() matcher = re.compile(r"^INFO : aqtinstall\(aqt\) v.* on Python 3.*\n" r"(.*\n)*" r"ERROR :.*Invalid version: '" + invalid_version + r"'! Please use the form '5\.X\.Y'\.\n.*") for cmd in ( ("install", invalid_version, "mac", "desktop"), ("doc", invalid_version, "mac", "desktop"), ("list-qt", "mac", "desktop", "--arch", invalid_version), ): cli = Cli() assert cli.run(cmd) == 1 out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert matcher.match(err)
def test_cli_set_7zip(monkeypatch): cli = Cli() cli._setup_settings() with pytest.raises(CliInputError) as err: cli._set_sevenzip("some_nonexistent_binary") assert err.type == CliInputError assert format(err.value) == "Specified 7zip command executable does not exist: 'some_nonexistent_binary'"
def test_install_nonexistent_archives(monkeypatch, capsys, cmd, xml_file: Optional[str], expected): xml = (Path(__file__).parent / "data" / xml_file).read_text("utf-8") if xml_file else "" def mock_get_url(url, *args, **kwargs): if not xml_file: raise ArchiveDownloadError( f"Failed to retrieve file at {url}\nServer response code: 404, reason: Not Found" ) return xml monkeypatch.setattr("aqt.archives.getUrl", mock_get_url) monkeypatch.setattr( "aqt.archives.get_hash", lambda *args, **kwargs: hashlib.sha256( bytes(xml, "utf-8")).hexdigest()) monkeypatch.setattr( "aqt.metadata.get_hash", lambda *args, **kwargs: hashlib.sha256( bytes(xml, "utf-8")).hexdigest()) monkeypatch.setattr("aqt.metadata.getUrl", mock_get_url) cli = Cli() cli._setup_settings() assert cli.run(cmd.split()) == 1 out, err = capsys.readouterr() actual = err[err.index("\n") + 1:] assert actual == expected, "{0} != {1}".format(actual, expected)
def test_cli_legacy_commands_with_correct_syntax(monkeypatch, cmd): # Pretend to install correctly when any command is run for func in ("run_install_qt", "run_install_src", "run_install_doc", "run_install_example", "run_install_tool"): monkeypatch.setattr(Cli, func, lambda *args, **kwargs: 0) cli = Cli() cli._setup_settings() assert 0 == cli.run(cmd.split())
def test_cli_check_mirror(): cli = Cli() cli._setup_settings() assert cli._check_mirror(None) arg = ["install-qt", "linux", "desktop", "5.11.3", "-b", "https://download.qt.io/"] args = cli.parser.parse_args(arg) assert args.base == "https://download.qt.io/" assert cli._check_mirror(args.base)
def test_cli_check_combination(): cli = Cli() cli._setup_settings() assert cli._check_qt_arg_combination("5.11.3", "linux", "desktop", "gcc_64") assert cli._check_qt_arg_combination("5.11.3", "mac", "desktop", "clang_64") assert not cli._check_qt_arg_combination("5.14.0", "android", "desktop", "clang_64")
def test_cli_input_errors(capsys, cmd, expect_msg, should_show_help): cli = Cli() cli._setup_settings() assert 1 == cli.run(cmd.split()) out, err = capsys.readouterr() if should_show_help: assert expected_help(out) else: assert out == "" assert err.rstrip().endswith(expect_msg)
def test_install( monkeypatch, capsys, cmd: List[str], host: str, target: str, version: str, arch: str, arch_dir: str, updates_url: str, archives: List[MockArchive], expect_out, # type: re.Pattern ): # For convenience, fill in version and arch dir: prevents repetitive data declarations for i in range(len(archives)): archives[i].version = version archives[i].arch_dir = arch_dir mock_get_url, mock_download_archive = make_mock_geturl_download_archive( archives, arch, host, updates_url) monkeypatch.setattr("aqt.archives.getUrl", mock_get_url) monkeypatch.setattr("aqt.installer.getUrl", mock_get_url) monkeypatch.setattr("aqt.installer.downloadBinaryFile", mock_download_archive) with TemporaryDirectory() as output_dir: cli = Cli() cli._setup_settings() assert 0 == cli.run(cmd + ["--outputdir", output_dir]) out, err = capsys.readouterr() sys.stdout.write(out) sys.stderr.write(err) assert expect_out.match(err) installed_path = Path(output_dir) / version / arch_dir if version == "5.9.0": installed_path = Path(output_dir) / "5.9" / arch_dir assert installed_path.is_dir() for archive in archives: if not archive.should_install: continue for patched_file in archive.contents: file_path = installed_path / patched_file.filename assert file_path.is_file() expect_content = patched_file.expected_content( base_dir=output_dir, sep=os.sep) actual_content = file_path.read_text(encoding="utf_8") assert actual_content == expect_content
def test_cli_legacy_tool_new_syntax(monkeypatch, capsys, cmd): # These incorrect commands cannot be filtered out directly by argparse because # they have the correct number of arguments. command = cmd.split() expected = ( "WARNING : The command 'tool' is deprecated and marked for removal in a future version of aqt.\n" "In the future, please use the command 'install-tool' instead.\n" "ERROR : Invalid version: 'tools_ifw'! Please use the form '5.X.Y'.\n" ) cli = Cli() cli._setup_settings() assert 1 == cli.run(command) out, err = capsys.readouterr() actual = err[err.index("\n") + 1:] assert actual == expected
def test_cli_unexpected_error(monkeypatch, capsys): def _mocked_run(*args): raise RuntimeError("Some unexpected error") monkeypatch.setattr("aqt.installer.Cli.run_install_qt", _mocked_run) cli = Cli() cli._setup_settings() assert Cli.UNHANDLED_EXCEPTION_CODE == cli.run(["install-qt", "mac", "ios", "6.2.0"]) out, err = capsys.readouterr() assert err.startswith("Some unexpected error") assert err.rstrip().endswith( "===========================PLEASE FILE A BUG REPORT===========================\n" "You have discovered a bug in aqt.\n" "Please file a bug report at https://github.com/miurahr/aqtinstall/issues.\n" "Please remember to include a copy of this program's output in your report." )
def test_cli_check_version(): cli = Cli() cli._setup_settings() assert cli._check_qt_arg_versions("5.12.0") assert not cli._check_qt_arg_versions("5.12")
def test_cli_legacy_commands_with_wrong_syntax(cmd): cli = Cli() cli._setup_settings() with pytest.raises(SystemExit) as e: cli.run(cmd.split()) assert e.type == SystemExit