Skip to content

Commit

Permalink
Print available Proton versions on missing value
Browse files Browse the repository at this point in the history
Print available Proton versions when $PROTON_VERSION has been set but
the installation in question cannot be found, as the list of allowed
values is otherwise hard to determine.

Also refactor the Proton app discovery in CLI entrypoint, as it was
already duplicated and needed to be updated to print the new error
message.
  • Loading branch information
Matoking committed Sep 29, 2024
1 parent 426c188 commit 8a7a78a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
58 changes: 36 additions & 22 deletions src/protontricks/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,45 @@ def cli(args=None):
main(args)



@cli_error_handler
def main(args=None, steam_path=None, steam_root=None):
"""
'protontricks' script entrypoint
"""
def _find_proton_app_or_exit(steam_path, steam_apps, appid):
"""
Attempt to find a Proton app. Fail with an appropriate CLI error
message if one cannot be found.
"""
proton_app = find_proton_app(
steam_path=steam_path, steam_apps=steam_apps, appid=appid
)

if not proton_app:
if os.environ.get("PROTON_VERSION"):
# Print an error listing accepted values if PROTON_VERSION was
# set, as the user is trying to use a certain Proton version
proton_names = sorted(set([
app.name for app in steam_apps if app.is_proton
]))
exit_(
"Protontricks installation could not be found with given "
"$PROTON_VERSION!\n\n"
f"Valid values include: {", ".join(proton_names)}"
)
else:
exit_("Proton installation could not be found!")

if not proton_app.is_proton_ready:
exit_(
"Proton installation is incomplete. Have you launched a Steam "
"app using this Proton version at least once to finish the "
"installation?"
)

return proton_app

if args is None:
args = sys.argv[1:]

Expand Down Expand Up @@ -297,19 +331,9 @@ def exit_(error):
cwd = str(steam_app.install_path) if args.cwd_app else None

# 6. Find Proton version of selected app
proton_app = find_proton_app(
proton_app = _find_proton_app_or_exit(
steam_path=steam_path, steam_apps=steam_apps, appid=steam_app.appid
)
if not proton_app:
exit_("Proton installation could not be found!")

if not proton_app.is_proton_ready:
exit_(
"Proton installation is incomplete. Have you launched a Steam "
"app using this Proton version at least once to finish the "
"installation?"
)


run_command(
winetricks_path=winetricks_path,
Expand Down Expand Up @@ -361,19 +385,9 @@ def exit_(error):
return

# 6. Find globally active Proton version now
proton_app = find_proton_app(
proton_app = _find_proton_app_or_exit(
steam_path=steam_path, steam_apps=steam_apps, appid=args.appid)

if not proton_app:
exit_("Proton installation could not be found!")

if not proton_app.is_proton_ready:
exit_(
"Proton installation is incomplete. Have you launched a Steam app "
"using this Proton version at least once to finish the "
"installation?"
)

# If neither search or GUI are set, do a normal Winetricks command
# Find game by appid
steam_appid = int(args.appid)
Expand Down
1 change: 1 addition & 0 deletions src/protontricks/steam.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ def find_proton_app(steam_path, steam_apps, appid=None):
"$PROTON_VERSION was set but matching Proton installation "
"could not be found."
)

return None

tool_app = find_steam_compat_tool_app(
Expand Down
21 changes: 21 additions & 0 deletions tests/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ def test_run_winetricks_select_proton(
assert command_mock.commands[-1].env["PROTON_PATH"] \
== str(custom_proton.install_path)

def test_run_winetricks_select_proton_accepted_values(
self, cli, steam_app_factory, custom_proton_factory, command_mock):
"""
Perform a Protonrticks command while selecting a non-existent Proton
version using PROTON_VERSION env var. Ensure list of allowed values
is printed in the error message
"""
steam_app_factory(name="Fake game", appid=10)
custom_proton_factory(name="Custom Proton C")
custom_proton_factory(name="Custom Proton A")

result = cli(
["10", "winecfg"],
env={"PROTON_VERSION": "Nonexistent Proton"},
expect_returncode=1
)

assert "Protontricks installation could not be found" in result
assert \
"Valid values include: Custom Proton A, Custom Proton C" in result

def test_run_winetricks_select_steam(
self, cli, steam_app_factory, default_proton, command_mock,
home_dir):
Expand Down

0 comments on commit 8a7a78a

Please sign in to comment.