|
1 | 1 | import os
|
| 2 | +import tempfile |
2 | 3 | from contextlib import contextmanager
|
3 | 4 | from pathlib import Path
|
4 | 5 |
|
@@ -32,16 +33,69 @@ def is_cursor_visible(self):
|
32 | 33 |
|
33 | 34 | @contextmanager
|
34 | 35 | def prepare_paths(self, *, custom):
|
35 |
| - if custom: |
36 |
| - pytest.xfail("This backend doesn't implement app path customization.") |
37 |
| - |
38 |
| - yield { |
39 |
| - "config": Path.home() / ".config/testbed", |
40 |
| - "data": Path.home() / ".local/share/testbed", |
41 |
| - "cache": Path.home() / ".cache/testbed", |
42 |
| - "logs": Path.home() / ".local/state/testbed/log", |
| 36 | + # Backup environment variables for later restoration |
| 37 | + backup = { |
| 38 | + "XDG_CONFIG_HOME": os.getenv("XDG_CONFIG_HOME"), |
| 39 | + "XDG_DATA_HOME": os.getenv("XDG_DATA_HOME"), |
| 40 | + "XDG_CACHE_HOME": os.getenv("XDG_CACHE_HOME"), |
| 41 | + "XDG_STATE_HOME": os.getenv("XDG_STATE_HOME"), |
43 | 42 | }
|
44 | 43 |
|
| 44 | + # Creating this variable here so it can be checked during cleanup |
| 45 | + temp_custom_dir = None |
| 46 | + |
| 47 | + try: |
| 48 | + if custom: |
| 49 | + # This will be cleaned up later |
| 50 | + temp_custom_dir = tempfile.TemporaryDirectory() |
| 51 | + |
| 52 | + custom_root = Path(temp_custom_dir.name) |
| 53 | + app_paths = { |
| 54 | + "config": custom_root / "config", |
| 55 | + "data": custom_root / "data", |
| 56 | + "cache": custom_root / "cache", |
| 57 | + "state": custom_root / "state", |
| 58 | + } |
| 59 | + |
| 60 | + # Set the custom paths |
| 61 | + os.environ["XDG_CONFIG_HOME"] = str(app_paths["config"]) |
| 62 | + os.environ["XDG_DATA_HOME"] = str(app_paths["data"]) |
| 63 | + os.environ["XDG_CACHE_HOME"] = str(app_paths["cache"]) |
| 64 | + os.environ["XDG_STATE_HOME"] = str(app_paths["state"]) |
| 65 | + else: |
| 66 | + # Delete existing environment variables to replicate the |
| 67 | + # default state. |
| 68 | + for envvar in backup: |
| 69 | + if envvar in os.environ: |
| 70 | + del os.environ[envvar] |
| 71 | + |
| 72 | + # The default paths |
| 73 | + app_paths = { |
| 74 | + "config": Path.home() / ".config", |
| 75 | + "data": Path.home() / ".local/share", |
| 76 | + "cache": Path.home() / ".cache", |
| 77 | + "state": Path.home() / ".local/state", |
| 78 | + } |
| 79 | + |
| 80 | + yield { |
| 81 | + "config": app_paths["config"] / "testbed", |
| 82 | + "data": app_paths["data"] / "testbed", |
| 83 | + "cache": app_paths["cache"] / "testbed", |
| 84 | + "logs": app_paths["state"] / "testbed" / "log", |
| 85 | + } |
| 86 | + finally: |
| 87 | + # Restore environment variables |
| 88 | + for envvar, value in backup.items(): |
| 89 | + if value is not None: |
| 90 | + os.environ[envvar] = value |
| 91 | + else: |
| 92 | + if envvar in os.environ: |
| 93 | + del os.environ[envvar] |
| 94 | + |
| 95 | + # Clean up temporary custom directory if it was created |
| 96 | + if temp_custom_dir: |
| 97 | + temp_custom_dir.cleanup() |
| 98 | + |
45 | 99 | def unhide(self):
|
46 | 100 | pytest.xfail("This platform doesn't have an app level unhide.")
|
47 | 101 |
|
|
0 commit comments