Skip to content

Commit

Permalink
Add even more additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matoking committed Nov 3, 2019
1 parent d62d922 commit ec6ebfc
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 18 deletions.
6 changes: 0 additions & 6 deletions src/protontricks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ def main(args=None):

# 2. Find Steam Runtime if enabled
steam_runtime_path = None
runtime_enabled = False

if os.environ.get("STEAM_RUNTIME", "") != "0" and not args.no_runtime:
runtime_enabled = True
steam_runtime_path = find_steam_runtime_path(steam_root=steam_root)

if not steam_runtime_path:
Expand All @@ -134,10 +132,6 @@ def main(args=None):
else:
logger.info("Steam Runtime disabled.")

if runtime_enabled and not steam_runtime_path:
print("Steam Runtime not found!")
sys.exit(-1)

# 3. Find Winetricks
winetricks_path = get_winetricks_path()
if not winetricks_path:
Expand Down
24 changes: 12 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ def home_dir(monkeypatch, tmp_path):
home_dir = tmp_path / "home" / "fakeuser"
home_dir.mkdir(parents=True)

# Create fake Winetricks binary that just prints the arguments
# and the environment variables
# Create fake Winetricks executable
(home_dir / ".local" / "bin").mkdir(parents=True)
(home_dir / ".local" / "bin" / "winetricks").touch()
(home_dir / ".local" / "bin" / "winetricks").chmod(0o744)

monkeypatch.setenv("HOME", str(home_dir))
monkeypatch.setenv(
"PATH",
"{}{}{}".format(
str(home_dir / ".local" / "bin"),
os.pathsep,
os.environ["PATH"]
)
)
monkeypatch.setenv("PATH", str(home_dir / ".local" / "bin"))

yield home_dir

Expand All @@ -46,8 +38,6 @@ def steam_dir(home_dir):
steam_path = home_dir / ".steam"
steam_path.mkdir()

# Steam Runtime
(steam_path / "root" / "ubuntu12_32").mkdir(parents=True)
(steam_path / "root" / "compatibilitytools.d").mkdir(parents=True)

(steam_path / "steam" / "appcache").mkdir(parents=True)
Expand All @@ -57,6 +47,16 @@ def steam_dir(home_dir):
yield steam_path


@pytest.fixture(scope="function")
def steam_runtime_dir(steam_dir):
"""
Fake Steam Runtime installation
"""
(steam_dir / "root" / "ubuntu12_32").mkdir(parents=True)

yield steam_dir / "root" / "ubuntu12_32"


@pytest.fixture(scope="function", autouse=True)
def steam_config_path(steam_dir):
"""
Expand Down
106 changes: 106 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
from pathlib import Path

import pytest
Expand Down Expand Up @@ -44,6 +45,111 @@ def test_run_winetricks_game_not_found(

assert "Steam app with the given app ID could not be found" in result

def test_run_no_command(self, cli):
"""
Run only the 'protontricks' command.
"""
result = cli([])

# Help will be printed if no specific command is given
assert result.startswith("usage: ")

def test_run_multiple_commands(self, cli):
"""
Try performing multiple commands at once
"""
result = cli(["--gui", "-s", "game"])

assert "Only one action can be performed" in result

def test_run_steam_not_found(self, cli, steam_dir):
"""
Try performing a command with a missing Steam directory
"""
shutil.rmtree(steam_dir)

result = cli(["10", "winecfg"], expect_exit=True)

assert "Steam installation directory could not be found" in result

def test_run_winetricks_not_found(
self, cli, default_proton, home_dir, steam_app_factory):
"""
Try performing a command with missing Winetricks executable
"""
steam_app_factory(name="Fake game 1", appid=10)
(home_dir / ".local" / "bin" / "winetricks").unlink()

result = cli(["10", "winecfg"], expect_exit=True)

assert "Winetricks isn't installed" in result

def test_run_steam_runtime_not_found(
self, cli, steam_dir, steam_app_factory):
"""
Try performing a command with Steam Runtime enabled but no
available Steam Runtime installation
"""
steam_app_factory(name="Fake game 1", appid=10)
result = cli(
["10", "winecfg"], env={"STEAM_RUNTIME": "invalid/path"},
expect_exit=True
)

assert "Steam Runtime was enabled but couldn't be found" in result

def test_run_proton_not_found(self, cli, steam_dir, steam_app_factory):
steam_app_factory(name="Fake game 1", appid=10)
result = cli(["10", "winecfg"], expect_exit=True)

assert "Proton installation could not be found" in result


class TestCLIGUI:
def test_run_gui(
self, cli, default_proton, steam_app_factory, zenity, command,
home_dir):
"""
Start the GUI and fake selecting a game
"""
steam_app_factory(name="Fake game 1", appid=10)

# Fake the user selecting the game
zenity.mock_stdout = "Fake game 1: 10"

cli(["--gui"])

# 'winetricks --gui' was run for the game selected by user
assert command.args[0] == str(
home_dir / ".local" / "bin" / "winetricks")
assert command.args[1] == "--gui"

def test_run_gui_no_games(self, cli, default_proton):
"""
Try starting the GUI when no games are installed
"""
result = cli(["--gui"], expect_exit=True)

assert "Found no games" in result


class TestCLICommand:
def test_run_command(
self, cli, default_proton, steam_app_factory, zenity, command,
home_dir):
"""
Run a shell command for a given game
"""
steam_app = steam_app_factory(name="Fake game", appid=10)

cli(["-c", "bash", "10"])

# The command is just 'bash'
assert command.args == "bash"

assert command.kwargs["cwd"] == str(steam_app.install_path)
assert command.kwargs["shell"] is True


class TestCLISearch:
def test_search_case_insensitive(self, cli, steam_app_factory):
Expand Down
50 changes: 50 additions & 0 deletions tests/test_steam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest

from protontricks.steam import SteamApp

from pathlib import Path


class TestSteamApp:
def test_steam_app_from_appmanifest(self, steam_app_factory, steam_dir):
"""
Create a SteamApp from an appmanifest file
"""
steam_app = steam_app_factory(name="Fake game", appid=10)

appmanifest_path = \
Path(steam_app.install_path).parent.parent / "appmanifest_10.acf"

steam_app = SteamApp.from_appmanifest(
path=str(appmanifest_path),
steam_lib_paths=[str(steam_dir / "steam" / "steamapps")]
)

assert steam_app.name == "Fake game"
assert steam_app.appid == 10

def test_steam_app_from_appmanifest_invalid(self, steam_app_factory):
steam_app = steam_app_factory(name="Fake game", appid=10)

appmanifest_path = \
Path(steam_app.install_path).parent.parent / "appmanifest_10.acf"
appmanifest_path.write_text("invalid")

# Invalid appmanifest file is ignored
assert not SteamApp.from_appmanifest(
path=str(appmanifest_path),
steam_lib_paths=[]
)

def test_steam_app_from_appmanifest_empty(self, steam_app_factory):
steam_app = steam_app_factory(name="Fake game", appid=10)

appmanifest_path = \
Path(steam_app.install_path).parent.parent / "appmanifest_10.acf"
appmanifest_path.write_text("")

# Empty appmanifest file is ignored
assert not SteamApp.from_appmanifest(
path=str(appmanifest_path),
steam_lib_paths=[]
)

0 comments on commit ec6ebfc

Please sign in to comment.