File: test_main.py

package info (click to toggle)
protontricks 1.13.1%2Bds-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 716 kB
  • sloc: python: 5,237; sh: 247; xml: 68; makefile: 9
file content (1025 lines) | stat: -rw-r--r-- 36,856 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
import os
import sys
import shutil
from pathlib import Path

import pytest


class TestCLIRun:
    def test_run_winetricks(
            self, cli, steam_app_factory, default_proton, command_mock,
            home_dir):
        """
        Perform a Protontricks command directly for a certain game
        """
        proton_install_path = Path(default_proton.install_path)

        steam_app = steam_app_factory(name="Fake game 1", appid=10)

        cli(["10", "winecfg"], env={"STEAM_RUNTIME": "0"})

        # winecfg was actually run
        command = command_mock.commands[-1]
        assert str(command.args[0]).endswith(".local/bin/winetricks")
        assert command.args[1] == "winecfg"

        # Correct environment vars were set
        assert command.env["PROTON_PATH"] == str(proton_install_path)
        assert command.env["PROTON_DIST_PATH"] == \
            str(proton_install_path / "dist")
        assert command.env["WINETRICKS"] == str(
            home_dir / ".local" / "bin" / "winetricks")
        assert command.env["WINEPREFIX"] == str(steam_app.prefix_path)
        assert command.env["WINELOADER"] == command.env["WINE"]
        assert command.env["WINEDLLPATH"] == "{}{}{}".format(
            str(proton_install_path / "dist" / "lib64" / "wine"),
            os.pathsep,
            str(proton_install_path / "dist" / "lib" / "wine")
        )

    def test_run_winetricks_shortcut(
            self, cli, shortcut_factory, default_proton, command_mock,
            steam_dir):
        """
        Perform a Protontricks command for a non-Steam shortcut
        """
        proton_install_path = Path(default_proton.install_path)
        shortcut_factory(install_dir="fake/path/", name="fakegame.exe")

        cli(["4149337689", "winecfg"])

        # Default Proton is used
        command = command_mock.commands[-1]
        assert command.env["PROTON_PATH"] == str(proton_install_path)
        assert command.env["WINEPREFIX"] == str(
            steam_dir / "steamapps" / "compatdata" / "4149337689" / "pfx")

    def test_run_winetricks_select_proton(
            self, cli, steam_app_factory, default_proton,
            custom_proton_factory, command_mock, home_dir):
        """
        Perform a Protontricks command while selecting a specific
        Proton version using PROTON_VERSION env var
        """
        steam_app_factory(name="Fake game", appid=10)
        custom_proton = custom_proton_factory(name="Custom Proton")
        cli(["10", "winecfg"], env={"PROTON_VERSION": "Custom 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):
        """
        Perform a Protontricks command while selecting a specific
        Steam installation directory
        """
        steam_app_factory(name="Fake game", appid=10)
        os.rename(
            str(home_dir / ".steam" / "steam"),
            str(home_dir / ".steam_new")
        )
        os.rename(
            str(home_dir / ".steam" / "root" / "ubuntu12_32"),
            str(home_dir / ".steam_new" / "ubuntu12_32")
        )

        cli(
            ["10", "winecfg"],
            env={"STEAM_DIR": str(home_dir / ".steam_new")}
        )

        command = command_mock.commands[-1]
        assert command.env["WINE"] == str(
            home_dir / ".cache" / "protontricks" / "proton"
            / "Proton 4.20" / "bin" / "wine"
        )
        assert command.env["PROTON_PATH"] == str(
            home_dir / ".steam_new" / "steamapps" / "common"
            / "Proton 4.20"
        )

    def test_run_winetricks_steam_runtime_v1(
            self, cli, steam_app_factory, steam_runtime_dir, default_proton,
            command_mock, home_dir):
        """
        Perform a Protontricks command using the older Steam Runtime
        bundled with Steam
        """
        steam_app_factory(name="Fake game 1", appid=10)

        cli(["10", "winecfg"], env={"STEAM_RUNTIME": "1"})

        wine_bin_dir = (
            home_dir / ".cache" / "protontricks" / "proton" / "Proton 4.20"
            / "bin"
        )

        # winecfg was actually run
        command = command_mock.commands[-1]
        assert str(command.args[0]).endswith(".local/bin/winetricks")
        assert command.args[1] == "winecfg"
        assert command.env["PATH"].startswith(str(wine_bin_dir))
        assert (
            "fake_steam_runtime/lib64" in command.env["PROTON_LD_LIBRARY_PATH"]
        )
        assert command.env["WINE"] == str(wine_bin_dir / "wine")
        assert command.env["WINELOADER"] == str(wine_bin_dir / "wine")
        assert command.env["WINESERVER"] == str(wine_bin_dir / "wineserver")

        assert command.env["LEGACY_STEAM_RUNTIME_PATH"] == \
            str(steam_runtime_dir / "steam-runtime")
        assert command.env["PROTONTRICKS_STEAM_RUNTIME"] == "legacy"
        assert "STEAM_RUNTIME_PATH" not in command.env

        for name in ("wine", "wineserver"):
            # The helper scripts are created that point towards the real
            # Wine binaries
            path = wine_bin_dir / name
            assert path.is_file()

            content = path.read_text()

            # Correct binary names used in the scripts
            assert f"\"$PROTON_DIST_PATH\"/bin/{name}" in content

    def test_run_winetricks_steam_runtime_v2(
            self, cli, home_dir, steam_app_factory, steam_runtime_dir,
            steam_runtime_soldier, command_mock, proton_factory, caplog):
        """
        Perform a Protontricks command using a newer Steam Runtime that is
        installed as its own application
        """
        proton_app = proton_factory(
            name="Proton 5.13", appid=10, compat_tool_name="proton_513",
            is_default_proton=True, required_tool_app=steam_runtime_soldier
        )
        steam_app_factory(name="Fake game 1", appid=20)

        cli(["20", "winecfg"], env={"STEAM_RUNTIME": "1"})

        wine_bin_dir = (
            home_dir / ".cache" / "protontricks" / "proton" / "Proton 5.13"
            / "bin"
        )

        # Launcher process was launched to handle launching processes
        # inside the sandbox
        assert command_mock.commands[0].args[0] \
            == str(wine_bin_dir / "bwrap-launcher")

        # winecfg was run
        command = command_mock.commands[-1]

        assert str(command.args[0]).endswith(".local/bin/winetricks")
        assert command.args[1] == "winecfg"
        assert command.env["PATH"].startswith(str(wine_bin_dir))

        # Compared to the traditional Steam Runtime, PROTON_LD_LIBRARY_PATH
        # will be different
        proton_install_path = Path(proton_app.install_path)
        assert command.env["PROTON_LD_LIBRARY_PATH"] == "".join([
            str(proton_install_path / "dist" / "lib"), os.pathsep,
            str(proton_install_path / "dist" / "lib64"), os.pathsep
        ])

        # Environment variables for both legacy and new Steam Runtime exist
        assert command.env["LEGACY_STEAM_RUNTIME_PATH"] == \
            str(steam_runtime_dir / "steam-runtime")
        assert command.env["STEAM_RUNTIME_PATH"] == \
            str(steam_runtime_soldier.install_path)
        assert command.env["PROTONTRICKS_STEAM_RUNTIME"] == "bwrap"

        # No warning will be created since Steam Runtime Soldier is recognized
        # by Protontricks
        assert len([
            record for record in caplog.records
            if record.levelname == "WARNING"
            and "Steam Runtime not recognized" in record.message
        ]) == 0

        for name in ("wine", "wineserver"):
            # The helper scripts are created that point towards the real
            # Wine binaries
            path = wine_bin_dir / name
            assert path.is_file()

            content = path.read_text()

            # Correct binary names used in the scripts
            assert f"\"$PROTON_DIST_PATH\"/bin/{name}" in content

    def test_run_winetricks_steam_runtime_v2_no_bwrap(
            self, cli, home_dir, steam_app_factory, steam_runtime_dir,
            steam_runtime_soldier, command_mock, proton_factory, caplog):
        """
        Perform a Protontricks command using a newer Steam Runtime
        *without* bwrap that is installed as its own application
        """
        proton_app = proton_factory(
            name="Proton 5.13", appid=10, compat_tool_name="proton_513",
            is_default_proton=True, required_tool_app=steam_runtime_soldier
        )
        steam_app_factory(name="Fake game 1", appid=20)

        cli(["--no-bwrap", "20", "winecfg"], env={"STEAM_RUNTIME": "1"})

        wine_bin_dir = (
            home_dir / ".cache" / "protontricks" / "proton" / "Proton 5.13"
            / "bin"
        )

        command = command_mock.commands[-1]
        # winecfg was run
        assert str(command.args[0]).endswith(".local/bin/winetricks")
        assert command.args[1] == "winecfg"
        assert command.env["PATH"].startswith(str(wine_bin_dir))

        # Compared to the traditional Steam Runtime, PROTON_LD_LIBRARY_PATH
        # will be different
        proton_install_path = Path(proton_app.install_path)
        assert command.env["PROTON_LD_LIBRARY_PATH"].startswith("".join([
            str(proton_install_path / "dist" / "lib"), os.pathsep,
            str(proton_install_path / "dist" / "lib64"), os.pathsep
        ]))

        runtime_root = \
            steam_runtime_soldier.install_path / "soldier" / "files"
        assert command.env["PROTON_LD_LIBRARY_PATH"].endswith("".join([
            str(runtime_root / "lib" / "i386-linux-gnu"), os.pathsep,
            str(runtime_root / "lib" / "x86_64-linux-gnu")
        ]))

        # Environment variables for both legacy and new Steam Runtime exist
        assert command.env["LEGACY_STEAM_RUNTIME_PATH"] == \
            str(steam_runtime_dir / "steam-runtime")
        assert command.env["STEAM_RUNTIME_PATH"] == \
            str(steam_runtime_soldier.install_path)
        assert command.env["PROTONTRICKS_STEAM_RUNTIME"] == "legacy"

        # No warning will be created since Steam Runtime Soldier is recognized
        # by Protontricks
        assert len([
            record for record in caplog.records
            if record.levelname == "WARNING"
            and "Steam Runtime not recognized" in record.getMessage()
        ]) == 0

        for name in ("wine", "wineserver"):
            # The helper scripts are created that point towards the real
            # Wine binaries
            path = wine_bin_dir / name
            assert path.is_file()

            content = path.read_text()

            assert f"\"$PROTON_DIST_PATH\"/bin/{name}" in content

    @pytest.mark.parametrize(
        "args,wineserver_launched",
        [
            # background wineserver disabled for bwrap by default
            (["-c", "'echo nothing'", "20"], False),

            # background wineserver also disabled by default for everything
            # else
            (["--no-bwrap", "-c", "'echo nothing'", "20"], False),

            # Manually disable background wineserver
            (
                ["--no-background-wineserver", "-c", "'echo nothing'", "20"],
                False
            ),

            # Manually enable background wineserver
            (
                [
                    "--background-wineserver", "--no-bwrap",
                    "-c", "'echo nothing'", "20"
                ],
                True
            )
        ]
    )
    def test_run_background_wineserver_toggle(
            self, cli, steam_app_factory, default_new_proton, command_mock,
            args, wineserver_launched, home_dir):
        """
        Try running a Protontricks command with different arguments
        and ensure background wineserver is (not) launched
        depending on the scenario
        """
        steam_app_factory(name="Fake game 1", appid=20)

        cli(args)

        wineserver_found = any(
            True for command in command_mock.commands
            if isinstance(command.args, str)
            and command.args == str(
                home_dir / ".cache/protontricks/proton/Proton 7.0/bin"
                / "wineserver-keepalive"
            )
        )

        assert wineserver_found == wineserver_launched

    def test_run_winetricks_game_not_found(
            self, cli, steam_app_factory, default_proton):
        """
        Try running a Protontricks command for a non-existing app
        """
        result = cli(["100", "winecfg"], expect_returncode=1)

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

    @pytest.mark.usefixtures("default_proton")
    def test_run_returncode_passed(self, cli, steam_app_factory):
        """
        Run a command that returns a specific exit code and ensure it is
        returned
        """
        steam_app_factory(name="Fake game", appid=10)
        cli(["-c", "exit 5", "10"], expect_returncode=5)

    def test_run_multiple_command_mock(self, cli):
        """
        Try performing multiple command_mock 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(str(steam_dir))

        result = cli(["10", "winecfg"], expect_returncode=1)

        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_returncode=1)

        assert "Winetricks isn't installed" in result

    def test_run_winetricks_from_desktop(
            self, cli, default_proton, home_dir, steam_app_factory,
            monkeypatch, gui_provider):
        """
        Try performing a command with missing Winetricks executable.

        Run command using --no-term and ensure error dialog is shown
        with the expected error message
        """
        steam_app_factory(name="Fake game 1", appid=10)
        (home_dir / ".local" / "bin" / "winetricks").unlink()

        cli(["--no-term", "10", "winecfg"], expect_returncode=1)

        assert gui_provider.args[0] == "yad"
        assert gui_provider.args[1] == "--text-info"

        message = gui_provider.kwargs["input"]

        assert b"Winetricks isn't installed" in message

        # Also ensure log messages are included in the error message
        assert b"Found Steam directory at" in message
        assert b"Using default Steam Runtime" in message

    def test_run_gui_provider_not_found(self, cli, home_dir, steam_app_factory):
        """
        Try performing a command with missing YAD or Zenity executable
        """
        steam_app_factory(name="Fake game 1", appid=10)
        (home_dir / ".local" / "bin" / "yad").unlink()
        (home_dir / ".local" / "bin" / "zenity").unlink()

        result = cli(["--gui"], expect_returncode=1)

        assert "YAD or Zenity is not 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_returncode=1
        )

        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_returncode=1)

        assert "Proton installation could not be found" in result

    def test_run_compat_tool_not_proton(
            self, cli, steam_dir, default_proton, custom_proton_factory,
            steam_app_factory, caplog):
        """
        Try performing a Protontricks command for a Steam app that
        uses a compatibility tool that isn't Proton.

        Regression test for https://github.com/Matoking/protontricks/issues/113
        """
        # Create a compatibility tool that isn't actually Proton
        tool_app = custom_proton_factory(name="Not Proton")
        (tool_app.install_path / "proton").unlink()

        steam_app_factory(
            name="Fake game", appid=10, compat_tool_name="Not Proton"
        )

        result = cli(["10", "winecfg"], expect_returncode=1)

        assert "Proton installation could not be found" in result

        record = caplog.records[-1]
        assert (
            "Active compatibility tool was found, but it's not a Proton" in
            record.getMessage()
        )

    def test_run_command_proton_incomplete(
            self, cli, steam_app_factory, default_proton):
        """
        Try performing a Protontricks command using a Proton installation that
        is incomplete because it hasn't been launched yet.

        Regression test for
        https://github.com/flathub/com.github.Matoking.protontricks/issues/10
        """
        # Remove the 'dist' directory to make the Proton installation
        # incomplete
        shutil.rmtree(str(default_proton.install_path / "dist"))

        steam_app_factory(name="Fake game", appid=10)

        result = cli(["10", "winecfg"], expect_returncode=1)

        assert "Proton installation is incomplete" in result

    def test_run_command_runtime_incomplete(
            self, cli, steam_app_factory, steam_runtime_soldier,
            proton_factory, steam_dir):
        """
        Try performing a Protontricks command using a Proton installation that
        is still missing a Steam Runtime installation.

        Regression test for https://github.com/Matoking/protontricks/issues/75
        """
        proton_factory(
            name="Proton 5.13", appid=10, compat_tool_name="proton_513",
            is_default_proton=True, required_tool_app=steam_runtime_soldier
        )
        steam_app_factory(name="Fake game 1", appid=20)

        # Delete the Steam Runtime installation to simulate an incomplete
        # Proton installation that's missing the required Steam Runtime
        shutil.rmtree(str(steam_runtime_soldier.install_path))
        (steam_dir / "steamapps" / "appmanifest_1391110.acf").unlink()

        with pytest.raises(RuntimeError) as exc:
            cli(["20", "winecfg"])

        assert "Proton 5.13 is missing the required Steam Runtime" \
            in str(exc.value)

    def test_old_flatpak_detected(self, cli, monkeypatch, caplog):
        """
        Try performing a Protontricks command when running inside an older
        Flatpak environment and ensure bwrap is disabled.
        """
        cli(["-s", "nothing"])

        # No warning is printed since we're not running inside Flatpak
        assert len([
            record for record in caplog.records
            if record.levelname == "WARNING"
        ]) == 0

        # Fake a Flatpak environment
        monkeypatch.setattr(
            "protontricks.cli.main.get_running_flatpak_version",
            # Mock version 1.12.0. 1.12.1 is new enough to not require
            # disabling bwrap.
            lambda: (1, 12, 0)
        )

        cli(["-s", "nothing"])

        assert len([
            record for record in caplog.records
            if record.levelname == "WARNING"
        ]) == 1
        record = next(
            record for record in caplog.records
            if record.levelname == "WARNING"
        )

        assert record.levelname == "WARNING"
        assert "Flatpak version is too old" \
            in record.message

    def test_new_flatpak_detected(self, cli, monkeypatch, caplog):
        """
        Try performing a Protontricks command when running inside a newer
        Flatpak environment and ensure Flatpak is detected correctly.
        """
        # Fake a newer Flatpak environment
        monkeypatch.setattr(
            "protontricks.cli.main.get_running_flatpak_version",
            lambda: (1, 12, 1)
        )

        cli(["-s", "nothing"])

        # Flatpak is new enough not to generate a warning.
        assert len([
            record for record in caplog.records
            if record.levelname == "WARNING"
        ]) == 0
        assert any([
            record for record in caplog.records
            if record.levelname == "INFO"
            and "Running inside Flatpak sandbox, version 1.12.1"
            in record.message
        ])

    def test_cli_error_handler_uncaught_exception(
            self, cli, default_proton, steam_app_factory, monkeypatch,
            gui_provider):
        """
        Ensure that 'cli_error_handler' correctly catches any uncaught
        exception and includes a stack trace in the error dialog.
        """
        def _mock_from_appmanifest(*args, **kwargs):
            raise ValueError("Test appmanifest error")

        steam_app_factory(name="Fake game", appid=10)

        monkeypatch.setattr(
            "protontricks.steam.SteamApp.from_appmanifest",
            _mock_from_appmanifest
        )

        cli(["--no-term", "-s", "Fake"], expect_returncode=1)

        assert gui_provider.args[0] == "yad"
        assert gui_provider.args[1] == "--text-info"

        message = gui_provider.kwargs["input"]

        assert b"Test appmanifest error" in message

    @pytest.mark.usefixtures("flatpak_sandbox")
    def test_run_filesystem_permission_missing(
            self, cli, steam_library_factory, caplog):
        """
        Try performing a command in a Flatpak sandbox where the user
        hasn't provided adequate fileystem permissions. Ensure warning is
        printed.
        """
        path = steam_library_factory(name="GameDrive")

        cli(["-s", "fake"])

        record = next(
            record for record in caplog.records
            if "grant access to the required directories" in record.message
        )
        assert record.levelname == "WARNING"
        assert str(path) in record.message

    @pytest.mark.usefixtures("command_mock")
    def test_run_bwrap_default(
            self, cli, steam_app_factory, steam_runtime_soldier,
            proton_factory, command_mock, caplog):
        """
        Perform command_mock for two Proton apps, one using a Proton version
        using the legacy Steam Runtime and another app using newer Steam
        Runtime with bwrap. Ensure that the correct default for `use_bwrap`
        is used in both cases.

        Regression test for #150
        """
        proton_factory(
            name="Old Proton", appid=123450, compat_tool_name="old_proton",
        )
        proton_factory(
            name="New Proton", appid=543210, compat_tool_name="new_proton",
            required_tool_app=steam_runtime_soldier
        )

        steam_app_factory(
            name="Fake game", appid=10, compat_tool_name="old_proton"
        )
        steam_app_factory(
            name="Fake game 2", appid=20, compat_tool_name="new_proton"
        )

        # bwrap is disabled for the old app by default
        cli(["-v", "-c", "bash", "10"])
        assert any(
            filter(lambda msg: "Using 'bwrap = False'" in msg, caplog.messages)
        )

        caplog.clear()

        # bwrap is enabled for the new app by default.
        cli(["-v", "-c", "bash", "20"])
        assert any(
            filter(lambda msg: "Using 'bwrap = True'" in msg, caplog.messages)
        )

    @pytest.mark.usefixtures("flatpak_sandbox")
    def test_select_steam_installation(
            self, cli, steam_dir, flatpak_steam_dir, steam_app_factory,
            proton_factory, gui_provider):
        """
        Test that the user is prompted to select the Steam installation,
        and that the correct Steam installation is used in both cases
        """
        # Only the Flatpak installation has an app
        steam_app_factory(
            name="Native Steam app", appid=10
        )

        proton_factory(
            name="Flatpak Proton", appid=123450,
            compat_tool_name="flatpak_proton"
        )
        steam_app_factory(
            name="Flatpak Steam app", appid=10,
            compat_tool_name="flatpak_proton",
            library_dir=flatpak_steam_dir,
        )

        # Mock the user choosing the Flatpak installation.
        # Only the index is actually checked in the actual function.
        gui_provider.mock_stdout = "1: Native - /home/fake/.steam"

        result = cli(["-s", "app"])

        assert "Native Steam app (10)" in result
        assert "Flatpak Steam app (10)" not in result

        # This time mock the Flatpak installation
        gui_provider.mock_stdout = "2: Flatpak - /home/fake/.var/app/something"

        result = cli(["-s", "app"])

        assert "Flatpak Steam app (10)" in result
        assert "Native Steam app (10)" not in result

    @pytest.mark.usefixtures(
        "flatpak_sandbox", "steam_dir", "flatpak_steam_dir"
    )
    def test_steam_installation_not_selected(self, cli, gui_provider):
        """
        Test that not selecting a Steam installation results in the correct
        exit message
        """
        # Mock the user choosing the Flatpak installation.
        # Only the index is actually checked in the actual function.
        gui_provider.mock_stdout = ""
        gui_provider.mock_returncode = 1

        result = cli(["-s", "app"], expect_returncode=1)

        assert "No Steam installation was selected" in result


class TestCLIGUI:
    def test_run_gui(
            self, cli, default_proton, steam_app_factory, gui_provider,
            command_mock, home_dir):
        """
        Start the GUI and fake selecting a game
        """
        steam_app = steam_app_factory(name="Fake game 1", appid=10)
        proton_install_path = Path(default_proton.install_path)

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

        cli(["--gui"])

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

        # Correct environment vars were set
        assert command.env["WINE"] == str(
            home_dir / ".cache" / "protontricks" / "proton" / "Proton 4.20"
            / "bin" / "wine"
        )
        assert command.env["PROTON_PATH"] == str(proton_install_path)
        assert command.env["WINETRICKS"] == str(
            home_dir / ".local" / "bin" / "winetricks")
        assert command.env["WINEPREFIX"] == str(steam_app.prefix_path)
        assert command.env["WINELOADER"] == command.env["WINE"]
        assert command.env["WINEDLLPATH"] == "{}{}{}".format(
            str(proton_install_path / "dist" / "lib64" / "wine"),
            os.pathsep,
            str(proton_install_path / "dist" / "lib" / "wine")
        )

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

        assert "Found no games" in result

    def test_run_gui_proton_incomplete(
            self, cli, steam_app_factory, default_proton, gui_provider):
        """
        Try running Protontricks GUI using a Proton installation that
        is incomplete because it hasn't been launched yet.
        """
        # Remove the 'dist' directory to make the Proton installation
        # incomplete
        shutil.rmtree(str(default_proton.install_path / "dist"))

        steam_app_factory(name="Fake game", appid=10)

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

        result = cli(["--gui"], expect_returncode=1)

        assert "Proton installation is incomplete" in result

    @pytest.mark.usefixtures("default_proton", "gui_provider")
    def test_run_no_args(
            self, cli, steam_app_factory, command_mock, gui_provider):
        """
        Run only the 'protontricks' command. This will default to GUI.
        """
        steam_app_factory(name="Fake game", appid=10)

        result = cli([], expect_returncode=1)

        # Help will be printed if no specific command is given
        assert "No game was selected" in result


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

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

        command = command_mock.commands[-1]

        # The command is just 'bash'
        assert command.args == "bash"
        assert command.cwd is None
        assert command.shell is True

        # Correct environment vars were set
        assert command.env["WINE"] == str(
            home_dir / ".cache" / "protontricks" / "proton" / "Proton 4.20"
            / "bin" / "wine"
        )
        assert command.env["PROTON_PATH"] == str(proton_install_path)
        assert command.env["WINETRICKS"] == str(
            home_dir / ".local" / "bin" / "winetricks")
        assert command.env["WINEPREFIX"] == str(steam_app.prefix_path)
        assert command.env["WINELOADER"] == command.env["WINE"]
        assert command.env["WINEDLLPATH"] == "{}{}{}".format(
            str(proton_install_path / "dist" / "lib64" / "wine"),
            os.pathsep,
            str(proton_install_path / "dist" / "lib" / "wine")
        )

    @pytest.mark.usefixtures("default_proton")
    def test_run_command_cwd_app(self, cli, steam_app_factory, command_mock):
        """
        Run a shell command for a given game using `--cwd-app` flag and
        ensure the working directory was set to the game's installation
        directory
        """
        steam_app = steam_app_factory(name="Fake game", appid=10)

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

        command = command_mock.commands[-1]

        assert command.args == "bash"
        assert command.cwd == str(steam_app.install_path)


class TestCLISearch:
    def test_search_case_insensitive(self, cli, steam_app_factory):
        """
        Do a case-insensitive search
        """
        steam_app_factory(name="FaKe GaMe 1", appid=10)
        steam_app_factory(name="FAKE GAME 2", appid=20)

        # Search is case-insensitive
        stdout = cli(["-s", "game"])

        assert "FaKe GaMe 1 (10)" in stdout
        assert "FAKE GAME 2 (20)" in stdout

    def test_search_pfx_lock_required(self, cli, steam_app_factory):
        """
        Do a search for a game that doesn't have a complete prefix yet
        """
        steam_app = steam_app_factory(name="Fake game", appid=10)

        # Delete the pfx.lock file that signifies that the game has been
        # launched at least once. Protontricks requires that this file
        # exists
        (Path(steam_app.prefix_path).parent / "pfx.lock").unlink()

        stdout = cli(["-s", "game"])

        assert "Found no games" in stdout
        assert "Fake game" not in stdout

    def test_search_multiple_keywords(self, cli, steam_app_factory):
        """
        Do a search for games with multiple subsequent words from the entire
        name
        """
        steam_app_factory(name="Apple banana cinnamon", appid=10)
        steam_app_factory(name="Apple banana", appid=20)

        stdout = cli(["-s", "apple", "banana"])

        # First game is found, second is not
        assert "Apple banana cinnamon (10)" in stdout
        assert "Apple banana (20)" in stdout

        # Having the keywords in one parameter is also valid
        stdout = cli(["-s", "apple banana"])

        assert "Apple banana cinnamon (10)" in stdout
        assert "Apple banana (20)" in stdout

    def test_search_strip_non_ascii(self, cli, steam_app_factory):
        """
        Do a search for a game with various symbols that are ignored
        when doing the search
        """
        steam_app_factory(
            name="Frog™ Simulator®: Year of the 🐸 Edition", appid=10
        )

        # Non-ASCII symbols are not checked for when doing the search
        stdout = cli([
            "-s", "frog", "simulator", "year", "of", "the", "edition"
        ])

        assert "Frog™ Simulator®: Year of the 🐸 Edition (10)" in stdout

    def test_search_multiple_library_folders(
            self, cli, steam_app_factory, steam_library_factory):
        """
        Create three games in three different locations and ensure
        all are found when searched for
        """
        library_dir_a = steam_library_factory("LibraryA")
        library_dir_b = steam_library_factory("LibraryB")

        steam_app_factory(name="Fake game 1", appid=10)
        steam_app_factory(
            name="Fake game 2", appid=20, library_dir=library_dir_a
        )
        steam_app_factory(
            name="Fake game 3", appid=30, library_dir=library_dir_b
        )

        # All three games should be found automatically
        result = cli(["-s", "game"])

        assert "Fake game 1" in result
        assert "Fake game 2" in result
        assert "Fake game 3" in result

    def test_search_shortcut(
            self, cli, shortcut_factory):
        """
        Create two non-Steam shortcut and ensure they can be found
        """
        shortcut_factory(install_dir="fake/path/", name="fakegame.exe")
        shortcut_factory(install_dir="fake/path2/", name="fakegame.exe")

        result = cli(["-v", "-s", "steam"])

        assert "Non-Steam shortcut: fakegame.exe (4149337689)" in result
        assert "Non-Steam shortcut: fakegame.exe (4136117770)" in result

    def test_list_all_apps(self, cli, steam_app_factory):
        """
        List all apps using `-l` CLI flag
        """
        steam_app_factory(name="Game number one", appid=10)
        steam_app_factory(name="Fake game", appid=20)

        result = cli(["-l"])

        assert "Game number one" in result
        assert "Fake game" in result


def test_cli_error_help(cli):
    """
    Ensure that the full help message is printed when an incorrect argument
    is provided
    """
    _, stderr = cli(
        ["--nothing"],
        expect_returncode=2,  # Returned for CLI syntax error
        include_stderr=True
    )

    # Usage message
    assert "[-h] [--verbose]" in stderr
    # Help message
    assert "positional arguments:" in stderr


@pytest.mark.parametrize(
    "parameter,log_levels",
    [
        (None, []),
        ("-v", ["INFO"]),
        ("-vv", ["INFO", "DEBUG"])
    ]
)
def test_cli_enable_logging(cli, parameter, log_levels):
    """
    Run the CLI interface with different logging levels and ensure
    that log messages with corresponding log levels are printed
    """
    if parameter:
        _, stderr = cli(
            [parameter, "-s", "nothing"],
            expect_returncode=1,  # We don't care whether the command succeeds
            include_stderr=True
        )

        for log_level in log_levels:
            assert log_level in stderr
    elif not parameter:
        _, stderr = cli(
            ["-s", "nothing"],
            expect_returncode=1,
            include_stderr=True
        )

        assert "DEBUG" not in stderr
        assert "INFO" not in stderr