Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kidev committed Jan 7, 2025
1 parent 3d0d352 commit ca67f43
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
15 changes: 5 additions & 10 deletions aqt/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,8 +1725,6 @@ def install(self) -> None:
if any(arg.startswith(";") or arg.startswith("&") for arg in sanitized_cmd):
raise ValueError("Invalid command argument detected")

subprocess.check_call(sanitized_cmd, shell=False, env=os.environ.copy(), cwd=temp_dir, timeout=3600)

# Log sanitized command (exclude password)
safe_cmd = list(sanitized_cmd)
if "--pw" in safe_cmd:
Expand All @@ -1739,18 +1737,15 @@ def install(self) -> None:
safe_cmd[email_index + 1] = "********"
self.logger.info(f"Running: {' '.join(safe_cmd)}")

subprocess.run(safe_cmd, shell=False, check=True, cwd=temp_dir)

Check failure on line 1740 in aqt/installer.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

aqt/installer.py#L1740

Detected subprocess function 'run' without a static string.

Check warning on line 1740 in aqt/installer.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

aqt/installer.py#L1740

subprocess call - check for execution of untrusted input.
except subprocess.CalledProcessError as e:
error_msg = f"Qt installation failed with code {e.returncode}"
if e.stderr:
error_msg += f": {e.stderr.decode('utf-8', errors='replace')}"
raise CliInputError(error_msg)
self.logger.error(f": {e.stderr.decode('utf-8', errors='replace')}")
self.logger.error(f"Installation failed with exit code {e.returncode}")
except subprocess.TimeoutExpired:
raise CliInputError("Installation timed out")
except (ValueError, OSError) as e:
raise CliInputError(f"Installation preparation failed: {str(e)}")
raise RuntimeError("Installation timed out")
finally:
# Ensure cleanup
if installer_path.exists():
installer_path.unlink()

self.logger.info("Qt installation completed successfully")
self.logger.info("Qt installation completed successfully")
4 changes: 4 additions & 0 deletions tests/data/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ concurrency: 3
blacklist:
http://mirror.example.com
http://mirrors.geekpie.club

[authentication]
user: [email protected]
password: WxK43TdWCTmxsrrpnsWbjPfPXVq3mtLK
6 changes: 2 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def test_get_autodesktop_dir_and_arch_non_android(
),
],
)
def test_cli_install_qt_commercial(capsys, monkeypatch, cmd, expected_arch, expected_err):
def test_cli_login_qt_commercial(capsys, monkeypatch, cmd, expected_arch, expected_err):
"""Test commercial Qt installation command"""
# Detect current platform
current_platform = platform.system().lower()
Expand All @@ -555,12 +555,10 @@ def mock_subprocess(*args, **kwargs):

monkeypatch.setattr("subprocess.check_call", mock_subprocess)

# Run the command
cli = Cli()
cli._setup_settings()
result = cli.run(cmd.split())

# Check outputs
out, err = capsys.readouterr()
assert expected_err in err
assert result == 1 # Should fail due to missing credentials
assert result == 1
44 changes: 44 additions & 0 deletions tests/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2054,3 +2054,47 @@ def mock_get_url(url: str, *args, **kwargs) -> str:
sys.stderr.write(err)

assert expect_out.match(err), err


@pytest.mark.parametrize(
"cmd, arch_dict, details, expected_command",
[
(
"install-qt-commercial desktop {} 6.8.0 "
"--outputdir ./install-qt-commercial "
"--user {} --password {}",
{"windows": "win64_msvc2022_64", "linux": "linux_gcc_64", "mac": "clang_64"},
["./install-qt-commercial", "qt6", "681"],
"qt-unified-{}-online.run --root {} --accept-licenses --accept-obligations --confirm-command "
"--auto-answer OperationDoesNotExistError=Ignore,OverwriteTargetDirectory=No,"
"stopProcessesForUpdates=Cancel,installationErrorWithCancel=Cancel,installationErrorWithIgnore=Ignore,"
"AssociateCommonFiletypes=Yes,telemetry-question=No install qt.{}.{}.{}",
),
],
)
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"""
current_platform = sys.platform.lower()
arch = arch_dict[current_platform]

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

cli = Cli()
cli._setup_settings()

def mock_exists(*args, **kwargs):
return False

def mock_subprocess(*args, **kwargs):
return 0

monkeypatch.setattr(Path, "exists", mock_exists)
monkeypatch.setattr("subprocess.check_call", mock_subprocess)

cli.run(formatted_cmd.split())

[out, _] = capsys.readouterr()
assert str(out).find(formatted_expected)

0 comments on commit ca67f43

Please sign in to comment.