Skip to content

Commit

Permalink
Set arguments explicitly in CLI entrypoints
Browse files Browse the repository at this point in the history
Set arguments explicitly in CLI entrypoints instead of letting
`argparse` handle it.

This allows us to determine whether any arguments were actually
provided without having to resort to checking `sys.argv`. This is done
more for the sake of tests, which relied on monkeypatching `sys.argv`
which is *very* flaky and broke tests in the Nix build sandbox and
potentially other environments as well.

Fixes #346
  • Loading branch information
Matoking committed Sep 24, 2024
1 parent 8b4e44d commit 826d6b7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/protontricks/cli/desktop_install.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import sys
from pathlib import Path
from subprocess import run

Expand Down Expand Up @@ -41,6 +42,9 @@ def main(args=None):
"""
'protontricks-desktop-install' script entrypoint
"""
if args is None:
args = sys.argv[1:]

parser = CustomArgumentParser(
description=(
"Install Protontricks application shortcuts for the local user\n"
Expand Down
4 changes: 4 additions & 0 deletions src/protontricks/cli/launch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import logging
import shlex
import sys
from pathlib import Path

from ..gui import (prompt_filesystem_access, select_steam_app_with_gui,
Expand All @@ -23,6 +24,9 @@ def main(args=None):
"""
'protontricks-launch' script entrypoint
"""
if args is None:
args = sys.argv[1:]

parser = CustomArgumentParser(
description=(
"Utility for launching Windows executables using Protontricks\n"
Expand Down
11 changes: 7 additions & 4 deletions src/protontricks/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def main(args=None, steam_path=None, steam_root=None):
"""
'protontricks' script entrypoint
"""
if args is None:
args = sys.argv[1:]

parser = CustomArgumentParser(
description=(
"Wrapper for running Winetricks commands for "
Expand Down Expand Up @@ -154,11 +157,11 @@ def main(args=None, steam_path=None, steam_root=None):
version=f"%(prog)s ({__version__})"
)

args = parser.parse_args(args)

if len(sys.argv) < 2:
if len(args) == 0:
# No arguments were provided, default to GUI
args.gui = True
args = ["--gui"]

args = parser.parse_args(args)

# 'cli_error_handler' relies on this to know whether to use error dialog or
# not
Expand Down
6 changes: 1 addition & 5 deletions tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,14 +774,10 @@ def test_run_gui_proton_incomplete(

@pytest.mark.usefixtures("default_proton", "gui_provider")
def test_run_no_args(
self, cli, steam_app_factory, command_mock, gui_provider,
monkeypatch):
self, cli, steam_app_factory, command_mock, gui_provider):
"""
Run only the 'protontricks' command. This will default to GUI.
"""
# Monkeypatch 'sys.argv', as that seems to be the only way to determine
# whether no arguments were provided
monkeypatch.setattr(sys, "argv", ["protontricks"])
steam_app_factory(name="Fake game", appid=10)

result = cli([], expect_returncode=1)
Expand Down

0 comments on commit 826d6b7

Please sign in to comment.