Skip to content

Commit

Permalink
Fix some errors, monkeypatch install test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidev committed Jan 11, 2025
1 parent c8d86dc commit 399acf0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ Qt/
.eggs
qtaccount.ini
.pytest_cache
.run/
12 changes: 4 additions & 8 deletions aqt/commercial.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(

# Set OS-specific properties
self.os_name = self._get_os_name()
self.installer_filename = self.ALLOWED_INSTALLERS[self.os_name]
self._installer_filename = self.ALLOWED_INSTALLERS[self.os_name]
self.qt_account = self._get_qt_account_path()

def _get_os_name(self) -> str:
Expand All @@ -92,17 +92,14 @@ def _get_qt_account_path(self) -> Path:
return Path.home() / ".local" / "share" / "Qt" / "qtaccount.ini"

def _download_installer(self, target_path: Path) -> None:
url = f"{self.base_url}/official_releases/online_installers/{self.installer_filename}"
url = f"{self.base_url}/official_releases/online_installers/{self._installer_filename}"
try:
response = requests.get(url, stream=True, timeout=self.timeout)
response.raise_for_status()

with open(target_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)

if self.os_name != "windows":
os.chmod(target_path, 0o700)
except Exception as e:
raise RuntimeError(f"Failed to download installer: {e}")

Expand Down Expand Up @@ -175,9 +172,8 @@ def install(self) -> None:

with tempfile.TemporaryDirectory(prefix="qt_install_") as temp_dir:
temp_path = Path(temp_dir)
os.chmod(temp_dir, 0o700)

installer_path = temp_path / self.installer_filename
installer_path = temp_path / self._installer_filename
self.logger.info(f"Downloading Qt installer to {installer_path}")
self._download_installer(installer_path)

Expand All @@ -194,7 +190,7 @@ def install(self) -> None:
safe_cmd[email_index + 1] = "********"
self.logger.info(f"Running: {' '.join(safe_cmd)}")

subprocess.run(cmd, shell=False, check=True, cwd=temp_dir)
subprocess.run([self._installer_filename] + cmd, shell=False, check=True, cwd=temp_dir)

except subprocess.CalledProcessError as e:
self.logger.error(f"Installation failed with exit code {e.returncode}")
Expand Down
62 changes: 27 additions & 35 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,33 @@


def expected_help(actual, prefix=None):
if sys.version_info >= (3, 13):
expected = (
"usage: aqt [-h] [-c CONFIG]\n"
" {install-qt,install-tool,install-qt-commercial,install-doc,install-example,"
"install-src,list-qt,list-tool,list-doc,list-example,list-src,help,version}\n"
" ...\n"
"\n"
"Another unofficial Qt Installer.\n"
"aqt helps you install Qt SDK, tools, examples and others\n"
"\n"
"options:\n"
" -h, --help show this help message and exit\n"
" -c, --config CONFIG Configuration ini file.\n"
)
if prefix is not None:
return actual.startswith(prefix + expected)
return actual.startswith(expected)
else:
expected = (
"usage: aqt [-h] [-c CONFIG]\n"
" {install-qt,install-tool,install-qt-commercial,install-doc,install-example,"
"install-src,list-qt,list-tool,list-doc,list-example,list-src,help,version}\n"
" ...\n"
"\n"
"Another unofficial Qt Installer.\n"
"aqt helps you install Qt SDK, tools, examples and others\n"
"\n"
"option",
" -h, --help show this help message and exit\n"
" -c CONFIG, --config CONFIG\n"
" Configuration ini file.\n",
)
if prefix is not None:
return actual.startswith(prefix + expected[0]) and actual.endswith(expected[1])
return actual.startswith(expected[0]) and actual.endswith(expected[1])
expected = (
"usage: aqt [-h] [-c CONFIG]\n"
" {install-qt,install-tool,install-doc,install-example,install-src,"
"list-qt,list-tool,list-doc,list-example,list-src,help,version}\n"
" ...\n"
"\n"
"Another unofficial Qt Installer.\n"
"aqt helps you install Qt SDK, tools, examples and others\n"
"\n"
"option",
" -h, --help show this help message and exit\n"
" -c CONFIG, --config CONFIG\n"
" Configuration ini file.\n"
"\n"
"subcommands:\n"
" aqt accepts several subcommands:\n"
" install-* subcommands are commands that install components\n"
" list-* subcommands are commands that show available components\n"
"\n"
" {install-qt,install-tool,install-doc,install-example,install-src,list-qt,"
"list-tool,list-doc,list-example,list-src,help,version}\n"
" Please refer to each help message by using '--help' "
"with each subcommand\n",
)
if prefix is not None:
return actual.startswith(prefix + expected[0]) and actual.endswith(expected[1])
return actual.startswith(expected[0]) and actual.endswith(expected[1])


def test_cli_help(capsys):
Expand Down
12 changes: 9 additions & 3 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2061,9 +2061,9 @@ def mock_get_url(url: str, *args, **kwargs) -> str:
"cmd, arch_dict, details, expected_command",
[
(
"install-qt-commercial desktop {} 6.8.0 " "--outputdir /tmp/install-qt-commercial " "--user {} --password {}",
"install-qt-commercial desktop {} 6.8.0 " "--outputdir ./install-qt-commercial " "--user {} --password {}",
{"windows": "win64_msvc2022_64", "linux": "linux_gcc_64", "mac": "clang_64"},
["/tmp/install-qt-commercial", "qt6", "680"],
["./install-qt-commercial", "qt6", "680"],
"qt-unified-{}-x64-online.run --email ******** --pw ******** --root {} "
"--accept-licenses --accept-obligations "
"--confirm-command "
Expand All @@ -2077,11 +2077,17 @@ def test_install_qt_commercial(
capsys, monkeypatch, cmd: str, arch_dict: dict[str, str], details: list[str], expected_command: str
) -> None:
"""Test commercial Qt installation command"""

# Use monkeypatch to replace subprocess.run
monkeypatch.setattr(subprocess, "run", lambda *args, **kwargs: None)

current_platform = sys.platform.lower()
arch = arch_dict[current_platform]

abs_out = Path(details[0]).absolute()

formatted_cmd = cmd.format(arch, "[email protected]", "WxK43TdWCTmxsrrpnsWbjPfPXVq3mtLK")
formatted_expected = expected_command.format(current_platform, *details, arch)
formatted_expected = expected_command.format(current_platform, abs_out, *details[1:], arch)

cli = Cli()
cli._setup_settings()
Expand Down

0 comments on commit 399acf0

Please sign in to comment.