Skip to content

Commit

Permalink
Fixed unit tests
Browse files Browse the repository at this point in the history
- Fixed relative imports
- Removed import handling via ImportError exception
- Fixed CI by running via module, instead of file
- Added / improved handling in __init__.py for unit tests
  • Loading branch information
Root-Core committed Dec 17, 2024
1 parent 54c18c2 commit 71eb0da
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 42 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ jobs:
python3 .github/scripts/check_gamefixes.py
python3 .github/scripts/check_verbs.py
# We need a known parent package in the unit tests, so we need to change to the parent dir
# As "umu-protonfixes" is not a valid package name, we create a symbolic link to "protonfixes"
- name: Test with unittest
run: |
python3 protonfixes_test.py
cd ..
ln -s umu-protonfixes protonfixes
python3 -m protonfixes.protonfixes_test
40 changes: 31 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
"""Starts the protonfix module"""
"""Starts the protonfix module and runs fixes after pre-flight-checks"""

import os
import sys
import traceback

RUN_CONDITIONS = [
'STEAM_COMPAT_DATA_PATH' in os.environ,
'PROTONFIXES_DISABLE' not in os.environ,
'waitforexitandrun' in sys.argv[1],
]
from . import fix
from .logger import log

if all(RUN_CONDITIONS):
import traceback
from . import fix

def check_conditions() -> bool:
"""Determine, if the actual game was executed and protonfixes isn't deactivated.
Returns:
bool: True, if the fix should be executed.
"""
return len(sys.argv) >= 1 and \
'STEAM_COMPAT_DATA_PATH' in os.environ and \
'PROTONFIXES_DISABLE' not in os.environ and \
'waitforexitandrun' in sys.argv[1]


def check_iscriptevaluator() -> bool:
"""Determine, if we were invoked while running "iscriptevaluator.exe".
Returns:
bool: True, if we were invoked while running "iscriptevaluator.exe".
"""
return len(sys.argv) >= 2 and \
'iscriptevaluator.exe' in sys.argv[2]


if check_iscriptevaluator():
log.debug('Skipping fix execution. We are running "iscriptevaluator.exe".')
elif not check_conditions():
log.warn('Skipping fix execution. We are probably running an unit test.')
else:
try:
fix.main()

Expand Down
5 changes: 1 addition & 4 deletions checks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"""Run some tests and generate warnings for proton configuration issues"""

try:
from .logger import log
except ImportError:
from logger import log
from .logger import log


def esync_file_limits() -> bool:
Expand Down
4 changes: 3 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Load configuration settings for protonfixes"""

from config_base import ConfigBase
from dataclasses import dataclass
from pathlib import Path

from .config_base import ConfigBase


class Config(ConfigBase):
"""Configuration for umu-protonfix"""

Expand Down
4 changes: 2 additions & 2 deletions config_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from configparser import ConfigParser
from dataclasses import is_dataclass
from pathlib import Path

from typing import Any
from collections.abc import Callable

from logger import log, LogLevel
from .logger import log, LogLevel


class ConfigBase:
"""Base class for configuration objects.
Expand Down
1 change: 1 addition & 0 deletions engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import sys

from .logger import log, LogLevel


Expand Down
15 changes: 5 additions & 10 deletions fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
import re
import sys
import csv

from functools import lru_cache
from importlib import import_module
from typing import Optional

try:
from .config import config
from .util import proton_version
from .checks import run_checks
from .logger import log
except ImportError:
from config import config
from util import proton_version
from checks import run_checks
from logger import log
from .config import config
from .util import proton_version
from .checks import run_checks
from .logger import log

try:
import __main__ as protonmain
Expand Down
16 changes: 9 additions & 7 deletions protonfixes_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import unittest
import io
import os
import tempfile
import unittest
import urllib.request

from pathlib import Path
from unittest.mock import patch, mock_open
import io
import urllib.request
import fix

from . import fix


class TestProtonfixes(unittest.TestCase):
Expand Down Expand Up @@ -315,7 +317,7 @@ def testGetGameNameDBFileNotFound(self):

with patch('builtins.open', mock_open()) as mocked_open:
mocked_open.side_effect = FileNotFoundError
with patch('fix.log') as mocked_log: # Mock the logger separately
with patch('protonfixes.fix.log') as mocked_log: # Mock the logger separately
func = fix.get_game_name.__wrapped__ # Do not reference the cache
result = func()
self.assertEqual(result, 'UNKNOWN')
Expand All @@ -329,7 +331,7 @@ def testGetGameNameDbOS(self):

with patch('builtins.open', mock_open()) as mocked_open:
mocked_open.side_effect = OSError
with patch('fix.log') as mocked_log: # Mock the logger separately
with patch('protonfixes.fix.log') as mocked_log: # Mock the logger separately
func = fix.get_game_name.__wrapped__ # Do not reference the cache
result = func()
self.assertEqual(result, 'UNKNOWN')
Expand Down Expand Up @@ -357,7 +359,7 @@ def testGetGameNameDbUnicode(self):

with patch('builtins.open', mock_open()) as mocked_open:
mocked_open.side_effect = UnicodeDecodeError('utf-8', b'', 0, 1, '')
with patch('fix.log') as mocked_log: # Mock the logger separately
with patch('protonfixes.fix.log') as mocked_log: # Mock the logger separately
func = fix.get_game_name.__wrapped__ # Do not reference the cache
result = func()
self.assertEqual(result, 'UNKNOWN')
Expand Down
11 changes: 3 additions & 8 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@
from typing import Any, Union, Optional
from collections.abc import Mapping, Callable

try:
from .logger import log
from .config import config
from .steamhelper import install_app
except ImportError:
from logger import log
from config import config
from steamhelper import install_app
from .logger import log
from .config import config
from .steamhelper import install_app

try:
import __main__ as protonmain
Expand Down

0 comments on commit 71eb0da

Please sign in to comment.