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 Jan 10, 2025
1 parent 3d99761 commit 3f2fae1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 41 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ jobs:
ln -s umu-protonfixes protonfixes
cd protonfixes
python3 .github/scripts/check_imports.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
42 changes: 33 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
"""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
7 changes: 2 additions & 5 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
"""Load configuration settings for protonfixes"""

import os
from configparser import ConfigParser

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


CONF_FILE = '~/.config/protonfixes/config.ini'
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


Expand Down
12 changes: 4 additions & 8 deletions fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import re
import sys
import csv

from functools import lru_cache
from importlib import import_module

try:
from . import config
from .checks import run_checks
from .logger import log
except ImportError:
import config
from checks import run_checks
from logger import log
from . import config
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: 4 additions & 7 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Utilities to make gamefixes easier"""

import configparser
from io import TextIOWrapper
import os
import sys
import re
Expand All @@ -12,16 +11,14 @@
import subprocess
import urllib.request
import functools

from io import TextIOWrapper
from socket import socket, AF_INET, SOCK_DGRAM
from typing import Literal, Any, Callable, Union
from collections.abc import Mapping, Generator

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

try:
import __main__ as protonmain
Expand Down

0 comments on commit 3f2fae1

Please sign in to comment.