Skip to content

Commit

Permalink
Add tests for dcs.installation.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanAlbert committed May 9, 2023
1 parent da35d1b commit ba144c3
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ jobs:
flake8 . --isolated --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Test with pytest
run: |
pip install pytest
python setup.py test
13 changes: 9 additions & 4 deletions dcs/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
print("WARNING : Trying to run pydcs on non Windows machine")


# Note: Steam App ID for DCS World is 223750
STEAM_REGISTRY_KEY_NAME = "Software\\Valve\\Steam\\Apps\\223750"
DCS_STABLE_REGISTRY_KEY_NAME = "Software\\Eagle Dynamics\\DCS World"
DCS_BETA_REGISTRY_KEY_NAME = "Software\\Eagle Dynamics\\DCS World OpenBeta"


def is_using_dcs_steam_edition():
"""
Check if DCS World : Steam Edition version is installed on this computer
Expand All @@ -28,8 +34,7 @@ def is_using_dcs_steam_edition():
if not is_windows_os:
return False
try:
# Note : Steam App ID for DCS World is 223750
dcs_steam_app_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Valve\\Steam\\Apps\\223750")
dcs_steam_app_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, STEAM_REGISTRY_KEY_NAME)
installed = winreg.QueryValueEx(dcs_steam_app_key, "Installed")
winreg.CloseKey(dcs_steam_app_key)
if installed[0] == 1:
Expand All @@ -48,12 +53,12 @@ def is_using_dcs_standalone_edition():
if not is_windows_os:
return False
try:
dcs_path_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Eagle Dynamics\\DCS World")
dcs_path_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, DCS_STABLE_REGISTRY_KEY_NAME)
winreg.CloseKey(dcs_path_key)
return True
except FileNotFoundError:
try:
dcs_path_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Eagle Dynamics\\DCS World OpenBeta")
dcs_path_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, DCS_BETA_REGISTRY_KEY_NAME)
winreg.CloseKey(dcs_path_key)
return True
except FileNotFoundError:
Expand Down
269 changes: 269 additions & 0 deletions tests/test_installation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
import sys

try:
import winreg
except ImportError:
pass
from pathlib import Path
from unittest.mock import Mock, call, patch

import pytest

from dcs.installation import (
DCS_BETA_REGISTRY_KEY_NAME,
DCS_STABLE_REGISTRY_KEY_NAME,
STEAM_REGISTRY_KEY_NAME,
get_dcs_install_directory,
get_dcs_saved_games_directory,
is_using_dcs_standalone_edition,
is_using_dcs_steam_edition,
)

pytestmark = pytest.mark.skipif(
sys.platform != "win32", reason="dcs.installation is windows only"
)


@patch("winreg.CloseKey")
@patch("winreg.QueryValueEx")
@patch("winreg.OpenKey")
def test_is_using_dcs_steam_edition(
mock_openkey: Mock, mock_queryvalueex: Mock, mock_closekey: Mock
) -> None:
mock_openkey.return_value = "key"
mock_queryvalueex.return_value = (1, 4)

assert is_using_dcs_steam_edition()
mock_openkey.assert_called_once_with(
winreg.HKEY_CURRENT_USER, STEAM_REGISTRY_KEY_NAME
)
mock_queryvalueex.assert_called_once_with("key", "Installed")
mock_closekey.assert_called_once_with("key")


@patch("winreg.CloseKey")
@patch("winreg.OpenKey")
def test_is_using_dcs_steam_edition_no_key(
mock_openkey: Mock, mock_closekey: Mock
) -> None:
mock_openkey.side_effect = FileNotFoundError

assert not is_using_dcs_steam_edition()
mock_openkey.assert_called_once_with(
winreg.HKEY_CURRENT_USER, STEAM_REGISTRY_KEY_NAME
)
mock_closekey.assert_not_called()


@pytest.mark.xfail(strict=True) # Does not close the key.
@patch("winreg.CloseKey")
@patch("winreg.QueryValueEx")
@patch("winreg.OpenKey")
def test_is_using_dcs_steam_edition_no_value(
mock_openkey: Mock, mock_queryvalueex: Mock, mock_closekey: Mock
) -> None:
mock_openkey.return_value = "key"
mock_queryvalueex.side_effect = FileNotFoundError

assert not is_using_dcs_steam_edition()
mock_openkey.assert_called_once_with(
winreg.HKEY_CURRENT_USER, STEAM_REGISTRY_KEY_NAME
)
mock_queryvalueex.assert_called_once_with("key", "Installed")
mock_closekey.assert_called_once_with("key")


@patch("winreg.CloseKey")
@patch("winreg.QueryValueEx")
@patch("winreg.OpenKey")
def test_is_using_dcs_steam_edition_not_installed(
mock_openkey: Mock, mock_queryvalueex: Mock, mock_closekey: Mock
) -> None:
mock_openkey.return_value = "key"
mock_queryvalueex.return_value = (0, 0)

assert not is_using_dcs_steam_edition()
mock_openkey.assert_called_once_with(
winreg.HKEY_CURRENT_USER, STEAM_REGISTRY_KEY_NAME
)
mock_queryvalueex.assert_called_once_with("key", "Installed")
mock_closekey.assert_called_once_with("key")


@patch("winreg.CloseKey")
@patch("winreg.OpenKey")
def test_is_using_dcs_standalone_edition_stable(
mock_openkey: Mock, mock_closekey: Mock
) -> None:
def stable_installed(key_type: int, name: str) -> str:
if (
key_type == winreg.HKEY_CURRENT_USER
and name == DCS_STABLE_REGISTRY_KEY_NAME
):
return "key"
raise FileNotFoundError

mock_openkey.side_effect = stable_installed

assert is_using_dcs_standalone_edition()
mock_openkey.assert_has_calls(
[call(winreg.HKEY_CURRENT_USER, DCS_STABLE_REGISTRY_KEY_NAME)]
)
mock_closekey.assert_called_once_with("key")


@patch("winreg.CloseKey")
@patch("winreg.OpenKey")
def test_is_using_dcs_standalone_edition_beta(
mock_openkey: Mock, mock_closekey: Mock
) -> None:
def stable_installed(key_type: int, name: str) -> str:
if key_type == winreg.HKEY_CURRENT_USER and name == DCS_BETA_REGISTRY_KEY_NAME:
return "key"
raise FileNotFoundError

mock_openkey.side_effect = stable_installed

assert is_using_dcs_standalone_edition()
mock_openkey.assert_has_calls(
[call(winreg.HKEY_CURRENT_USER, DCS_BETA_REGISTRY_KEY_NAME)]
)
mock_closekey.assert_called_once_with("key")


@patch("winreg.CloseKey")
@patch("winreg.OpenKey")
def test_is_using_dcs_standalone_not_installed(
mock_openkey: Mock, mock_closekey: Mock
) -> None:
mock_openkey.side_effect = FileNotFoundError

assert not is_using_dcs_standalone_edition()
mock_openkey.assert_has_calls(
[
call(winreg.HKEY_CURRENT_USER, DCS_STABLE_REGISTRY_KEY_NAME),
call(winreg.HKEY_CURRENT_USER, DCS_BETA_REGISTRY_KEY_NAME),
],
any_order=True,
)
mock_closekey.assert_not_called()


@patch("dcs.installation.is_using_dcs_steam_edition")
@patch("dcs.installation.is_using_dcs_standalone_edition")
@patch("winreg.CloseKey")
@patch("winreg.QueryValueEx")
@patch("winreg.OpenKey")
def test_get_dcs_install_directory_stable(
mock_openkey: Mock,
mock_queryvalueex: Mock,
mock_closekey: Mock,
mock_standalone_edition: Mock,
mock_steam_edition: Mock,
) -> None:
def stable_installed(key_type: int, name: str) -> str:
if (
key_type == winreg.HKEY_CURRENT_USER
and name == DCS_STABLE_REGISTRY_KEY_NAME
):
return "key"
raise FileNotFoundError

mock_openkey.side_effect = stable_installed
mock_queryvalueex.return_value = ("path",)
mock_standalone_edition.return_value = True
mock_steam_edition.return_value = False

assert get_dcs_install_directory() == "path\\"

mock_queryvalueex.assert_called_once_with("key", "Path")
mock_closekey.assert_called_once_with("key")


@patch("dcs.installation.is_using_dcs_steam_edition")
@patch("dcs.installation.is_using_dcs_standalone_edition")
@patch("winreg.CloseKey")
@patch("winreg.QueryValueEx")
@patch("winreg.OpenKey")
def test_get_dcs_install_directory_beta(
mock_openkey: Mock,
mock_queryvalueex: Mock,
mock_closekey: Mock,
mock_standalone_edition: Mock,
mock_steam_edition: Mock,
) -> None:
def stable_installed(key_type: int, name: str) -> str:
if key_type == winreg.HKEY_CURRENT_USER and name == DCS_BETA_REGISTRY_KEY_NAME:
return "key"
raise FileNotFoundError

mock_openkey.side_effect = stable_installed
mock_queryvalueex.return_value = ("path",)
mock_standalone_edition.return_value = True
mock_steam_edition.return_value = False

assert get_dcs_install_directory() == "path\\"

mock_queryvalueex.assert_called_once_with("key", "Path")
mock_closekey.assert_called_once_with("key")


@patch("dcs.installation._get_steam_library_folders")
@patch("dcs.installation.is_using_dcs_steam_edition")
@patch("dcs.installation.is_using_dcs_standalone_edition")
def test_get_dcs_install_directory_steam(
mock_standalone_edition: Mock,
mock_steam_edition: Mock,
mock_get_steam_library_folders: Mock,
tmp_path: Path,
) -> None:
install_dir = tmp_path / "steamapps/common/DCSWorld"
install_dir.mkdir(parents=True)

mock_standalone_edition.return_value = False
mock_steam_edition.return_value = True
mock_get_steam_library_folders.return_value = ["foo", str(tmp_path), "bar"]

assert get_dcs_install_directory() == f"{install_dir}\\"


@patch("dcs.installation.is_using_dcs_steam_edition")
@patch("dcs.installation.is_using_dcs_standalone_edition")
def test_get_dcs_install_directory_not_installed(
mock_standalone_edition: Mock,
mock_steam_edition: Mock,
) -> None:
mock_standalone_edition.return_value = False
mock_steam_edition.return_value = False

assert get_dcs_install_directory() == ""


@patch("os.path.expanduser")
@patch("dcs.installation.get_dcs_install_directory")
def test_get_dcs_saved_games_directory_no_variant_file(
mock_get_dcs_install_directory: Mock, mock_expanduser: Mock, tmp_path: Path
) -> None:
home_dir = tmp_path / "home"

mock_expanduser.return_value = str(home_dir)
mock_get_dcs_install_directory.return_value = str(tmp_path)

assert get_dcs_saved_games_directory() == str(home_dir / "Saved Games/DCS")


@patch("os.path.expanduser")
@patch("dcs.installation.get_dcs_install_directory")
def test_get_dcs_saved_games_directory_beta_variant(
mock_get_dcs_install_directory: Mock, mock_expanduser: Mock, tmp_path: Path
) -> None:
install_dir = tmp_path / "install/steamapps/common/DCSWorld"
install_dir.mkdir(parents=True)
home_dir = tmp_path / "home"

(install_dir / "dcs_variant.txt").write_text("openbeta")

mock_expanduser.return_value = str(home_dir)
mock_get_dcs_install_directory.return_value = install_dir

assert get_dcs_saved_games_directory() == str(home_dir / "Saved Games/DCS.openbeta")

0 comments on commit ba144c3

Please sign in to comment.