-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert app clean-up step into fixture
- Loading branch information
1 parent
67d08a9
commit 5dcdff4
Showing
4 changed files
with
200 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,60 @@ | ||
import itertools | ||
import os | ||
import subprocess | ||
import shutil | ||
import sys | ||
from pathlib import Path | ||
|
||
# TIP: You can debug the tests with this setup: | ||
# CONDA_STANDALONE=src/entry_point.py pytest ... | ||
CONDA_EXE = os.environ.get( | ||
"CONDA_STANDALONE", | ||
os.path.join(sys.prefix, "standalone_conda", "conda.exe"), | ||
) | ||
import pytest | ||
from utils import _get_shortcut_dirs | ||
|
||
|
||
menuinst_pkg_specs = [ | ||
( | ||
"conda-test/label/menuinst-tests::package_1", | ||
{ | ||
"win32": "Package 1/A.lnk", | ||
"darwin": "A.app", | ||
"linux": "package-1_a.desktop", | ||
}, | ||
), | ||
] | ||
if os.name == "nt": | ||
menuinst_pkg_specs.append( | ||
@pytest.fixture | ||
def menuinst_pkg_specs(): | ||
specs = [ | ||
( | ||
"conda-forge::miniforge_console_shortcut", | ||
{"win32": "{base}/{base} Prompt ({name}).lnk"}, | ||
"conda-test/label/menuinst-tests::package_1", | ||
{ | ||
"win32": "Package 1/A.lnk", | ||
"darwin": "A.app", | ||
"linux": "package-1_a.desktop", | ||
}, | ||
), | ||
) | ||
|
||
|
||
def run_conda(*args, **kwargs) -> subprocess.CompletedProcess: | ||
check = kwargs.pop("check", False) | ||
sudo = None | ||
if "needs_sudo" in kwargs: | ||
if kwargs["needs_sudo"]: | ||
if sys.platform == "win32": | ||
raise NotImplementedError( | ||
"Calling run_conda with elevated privileged is not available on Windows" | ||
) | ||
sudo = ["sudo", "-E"] | ||
del kwargs["needs_sudo"] | ||
cmd = [*sudo, CONDA_EXE] if sudo else [CONDA_EXE] | ||
|
||
process = subprocess.run([*cmd, *args], **kwargs) | ||
if check: | ||
if kwargs.get("capture_output"): | ||
print(process.stdout) | ||
print(process.stderr, file=sys.stderr) | ||
process.check_returncode() | ||
return process | ||
|
||
|
||
def _get_shortcut_dirs() -> list[Path]: | ||
if sys.platform == "win32": | ||
from menuinst.platforms.win_utils.knownfolders import dirs_src as win_locations | ||
|
||
return [ | ||
Path(win_locations["user"]["start"][0]), | ||
Path(win_locations["system"]["start"][0]), | ||
] | ||
if sys.platform == "darwin": | ||
return [ | ||
Path(os.environ["HOME"], "Applications"), | ||
Path("/Applications"), | ||
] | ||
if sys.platform == "linux": | ||
paths = [ | ||
Path(os.environ["HOME"], ".local", "share", "applications"), | ||
Path("/usr/share/applications"), | ||
] | ||
if os.name == "nt": | ||
specs.append( | ||
( | ||
"conda-forge::miniforge_console_shortcut", | ||
{"win32": "{base}/{base} Prompt ({name}).lnk"}, | ||
), | ||
) | ||
return specs | ||
|
||
|
||
# If the test app already exists, tests will fail, | ||
# so clean up before and after the run. | ||
def _clean_macos_apps(shortcuts: dict[str, list[Path]]): | ||
if not sys.platform == "darwin": | ||
return | ||
for shortcut in itertools.chain.from_iterable(shortcuts.values()): | ||
if shortcut.exists(): | ||
shutil.rmtree(shortcut) | ||
|
||
|
||
@pytest.fixture | ||
def clean_shortcuts( | ||
tmp_path: Path, menuinst_pkg_specs: list[tuple[str, dict[str, str]]] | ||
): | ||
# The shortcut will take 'root_prefix' as the base, but conda-standalone | ||
# sets that to its temporary 'sys.prefix' as provided by the pyinstaller | ||
# self-extraction. We override it via 'CONDA_ROOT_PREFIX' in the same | ||
# way 'constructor' will do it. | ||
variables = {"base": Path(sys.prefix).name, "name": tmp_path.name} | ||
shortcuts = {} | ||
for package, spec in menuinst_pkg_specs: | ||
shortcuts[package] = [ | ||
folder / spec[sys.platform].format(**variables) | ||
for folder in _get_shortcut_dirs() | ||
] | ||
if xdg_data_home := os.environ.get("XDG_DATA_HOME"): | ||
paths.append(Path(xdg_data_home, "applications")) | ||
return paths | ||
raise NotImplementedError(sys.platform) | ||
_clean_macos_apps(shortcuts) | ||
yield shortcuts | ||
_clean_macos_apps(shortcuts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.