Refine command resolution and symlink creation (see ChatGPT conversation: https://chatgpt.com/share/6936be2d-952c-800f-a1cd-7ce5438014ff)
This commit is contained in:
132
tests/integration/test_install_repos_integration.md
Normal file
132
tests/integration/test_install_repos_integration.md
Normal file
@@ -0,0 +1,132 @@
|
||||
# Integration Test: Command Resolution & Link Creation
|
||||
|
||||
**File:** `tests/integration/test_install_repos_integration.py`
|
||||
|
||||
This integration test validates the *end-to-end* behavior of the pkgmgr installation pipeline:
|
||||
|
||||
1. Repository selection
|
||||
2. Verification
|
||||
3. **Command resolution (`resolve_command_for_repo`)**
|
||||
4. **Symlink creation (`create_ink`)**
|
||||
5. Installer execution order and skipping rules
|
||||
|
||||
The test sets up **two repositories**:
|
||||
|
||||
| Repository | Environment Condition | Expected Behavior |
|
||||
| ------------- | ------------------------------- | ------------------------------------ |
|
||||
| `repo-system` | `/usr/bin/tool-system` exists | System binary → **NO symlink** |
|
||||
| `repo-nix` | Nix profile contains `tool-nix` | Link → `~/.nix-profile/bin/tool-nix` |
|
||||
|
||||
This confirms that pkgmgr respects system package managers, prefers Nix over fallback logic, and creates or skips symlinks appropriately.
|
||||
|
||||
---
|
||||
|
||||
## 🔧 **Command Selection Flowchart**
|
||||
|
||||
The integration test verifies that pkgmgr follows this exact decision tree:
|
||||
|
||||
```mermaid
|
||||
flowchart TD
|
||||
|
||||
A[Start: install_repos()] --> B(resolve_command_for_repo)
|
||||
|
||||
B --> C{Explicit command in repo config?}
|
||||
C -- Yes --> C1[Return explicit command]
|
||||
C -- No --> D
|
||||
|
||||
D --> E{System binary under /usr/?}
|
||||
E -- Yes --> E1[Return None → NO symlink]
|
||||
E -- No --> F
|
||||
|
||||
F --> G{Nix profile binary exists?}
|
||||
G -- Yes --> G1[Return Nix binary]
|
||||
G -- No --> H
|
||||
|
||||
H --> I{Python/non-system PATH binary?}
|
||||
I -- Yes --> I1[Return PATH binary]
|
||||
I -- No --> J
|
||||
|
||||
J --> K{main.sh/main.py in repo?}
|
||||
K -- Yes --> K1[Return fallback script]
|
||||
K -- No --> L[Error: No command found]
|
||||
|
||||
L --> X[Abort installation of this repo]
|
||||
|
||||
C1 --> M[create_ink → create symlink]
|
||||
G1 --> M
|
||||
I1 --> M
|
||||
K1 --> M
|
||||
|
||||
E1 --> N[Skip symlink creation]
|
||||
```
|
||||
|
||||
The integration test specifically checks branches:
|
||||
|
||||
* **System binary → skip**
|
||||
* **Nix binary → create link**
|
||||
|
||||
---
|
||||
|
||||
## 📐 **Behavior Matrix (Simplified Priority Model)**
|
||||
|
||||
| Priority | Layer / Condition | Action | Link Created? |
|
||||
| -------- | ------------------------------------- | ----------- | ------------- |
|
||||
| 1 | Explicit `command` in repo config | Use it | ✅ Yes |
|
||||
| 2 | System binary under `/usr/bin/...` | Respect OS | ❌ No |
|
||||
| 3 | Nix profile binary exists | Use it | ✅ Yes |
|
||||
| 4 | Non-system PATH binary | Use it | ✅ Yes |
|
||||
| 5 | Repo fallback (`main.sh` / `main.py`) | Use it | ✅ Yes |
|
||||
| 6 | None of the above | Raise error | ❌ No |
|
||||
|
||||
The integration test hits row **2** and **3**.
|
||||
|
||||
---
|
||||
|
||||
## 🧪 What This Integration Test Ensures
|
||||
|
||||
### ✔ Correct orchestration
|
||||
|
||||
`install_repos()` calls components in the correct sequence and respects outputs from each stage.
|
||||
|
||||
### ✔ Correct command resolution
|
||||
|
||||
The test asserts that:
|
||||
|
||||
* System binaries suppress symlink creation.
|
||||
* Nix binaries produce symlinks even if PATH is empty.
|
||||
|
||||
### ✔ Correct linking behavior
|
||||
|
||||
For the Nix repo:
|
||||
|
||||
* A symlink is created under the `bin_dir`.
|
||||
* The symlink points exactly to `~/.nix-profile/bin/<identifier>`.
|
||||
|
||||
### ✔ Isolation
|
||||
|
||||
No real system binaries or actual Nix installation are required—the test uses deterministic patches.
|
||||
|
||||
---
|
||||
|
||||
## 🧩 Additional Notes
|
||||
|
||||
* The integration test covers only the *positive Nix case* and the *system binary skip case*.
|
||||
More tests can be added later for:
|
||||
|
||||
* Python binary resolution
|
||||
* Fallback to `main.py`
|
||||
* Error case when no command can be resolved
|
||||
* The test intentionally uses a **temporary HOME directory** to simulate isolated Nix profiles.
|
||||
|
||||
---
|
||||
|
||||
## ✅ Summary
|
||||
|
||||
This integration test validates that:
|
||||
|
||||
* **pkgmgr does not overwrite or override system binaries**
|
||||
* **Nix takes precedence over PATH-based tools**
|
||||
* **The symlink layer works correctly**
|
||||
* **The installer pipeline continues normally even when command resolution skips symlink creation**
|
||||
|
||||
The file provides a reliable foundation for confirming that command resolution, linking, and installation orchestration are functioning exactly as designed.
|
||||
179
tests/integration/test_install_repos_integration.py
Normal file
179
tests/integration/test_install_repos_integration.py
Normal file
@@ -0,0 +1,179 @@
|
||||
# tests/integration/test_install_repos_integration.py
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pkgmgr.install_repos as install_module
|
||||
from pkgmgr.install_repos import install_repos
|
||||
from pkgmgr.installers.base import BaseInstaller
|
||||
|
||||
|
||||
class DummyInstaller(BaseInstaller):
|
||||
"""
|
||||
Minimal installer used to ensure that the installation pipeline runs
|
||||
without executing any real external commands.
|
||||
"""
|
||||
|
||||
layer = None
|
||||
|
||||
def supports(self, ctx):
|
||||
return True
|
||||
|
||||
def run(self, ctx):
|
||||
return
|
||||
|
||||
|
||||
class TestInstallReposIntegration(unittest.TestCase):
|
||||
@patch("pkgmgr.install_repos.verify_repository")
|
||||
@patch("pkgmgr.install_repos.clone_repos")
|
||||
@patch("pkgmgr.install_repos.get_repo_dir")
|
||||
@patch("pkgmgr.install_repos.get_repo_identifier")
|
||||
def test_system_binary_vs_nix_binary(
|
||||
self,
|
||||
mock_get_repo_identifier,
|
||||
mock_get_repo_dir,
|
||||
mock_clone_repos,
|
||||
mock_verify_repository,
|
||||
):
|
||||
"""
|
||||
Full integration test for high-level command resolution + symlink creation.
|
||||
|
||||
We do NOT re-test all low-level file-system details of
|
||||
resolve_command_for_repo here (that is covered by unit tests).
|
||||
Instead, we assert that:
|
||||
|
||||
- If resolve_command_for_repo(...) returns None:
|
||||
→ install_repos() does NOT create a symlink.
|
||||
|
||||
- If resolve_command_for_repo(...) returns a path:
|
||||
→ install_repos() creates exactly one symlink in bin_dir
|
||||
that points to this path.
|
||||
|
||||
Concretely:
|
||||
|
||||
- repo-system:
|
||||
resolve_command_for_repo(...) → None
|
||||
→ no symlink in bin_dir for this repo.
|
||||
|
||||
- repo-nix:
|
||||
resolve_command_for_repo(...) → "/nix/profile/bin/repo-nix"
|
||||
→ exactly one symlink in bin_dir pointing to that path.
|
||||
"""
|
||||
# Repositories must have provider/account/repository so that get_repo_dir()
|
||||
# does not crash when called from create_ink().
|
||||
repo_system = {
|
||||
"name": "repo-system",
|
||||
"provider": "github.com",
|
||||
"account": "dummy",
|
||||
"repository": "repo-system",
|
||||
}
|
||||
repo_nix = {
|
||||
"name": "repo-nix",
|
||||
"provider": "github.com",
|
||||
"account": "dummy",
|
||||
"repository": "repo-nix",
|
||||
}
|
||||
|
||||
selected_repos = [repo_system, repo_nix]
|
||||
all_repos = selected_repos
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp_base, \
|
||||
tempfile.TemporaryDirectory() as tmp_bin:
|
||||
|
||||
# Fake repo directories (what get_repo_dir will return)
|
||||
repo_system_dir = os.path.join(tmp_base, "repo-system")
|
||||
repo_nix_dir = os.path.join(tmp_base, "repo-nix")
|
||||
os.makedirs(repo_system_dir, exist_ok=True)
|
||||
os.makedirs(repo_nix_dir, exist_ok=True)
|
||||
|
||||
# Identifiers and repo dirs used inside install_repos()
|
||||
mock_get_repo_identifier.side_effect = ["repo-system", "repo-nix"]
|
||||
mock_get_repo_dir.side_effect = [repo_system_dir, repo_nix_dir]
|
||||
|
||||
# Repository verification always succeeds
|
||||
mock_verify_repository.return_value = (True, [], "commit", "key")
|
||||
mock_clone_repos.return_value = None
|
||||
|
||||
# Pretend this is the "Nix binary" path for repo-nix
|
||||
nix_tool_path = "/nix/profile/bin/repo-nix"
|
||||
|
||||
# Patch resolve_command_for_repo at the install_repos module level
|
||||
with patch("pkgmgr.install_repos.resolve_command_for_repo") as mock_resolve, \
|
||||
patch("pkgmgr.install_repos.os.path.exists") as mock_exists_install:
|
||||
|
||||
def fake_resolve_command(repo, repo_identifier: str, repo_dir: str):
|
||||
"""
|
||||
High-level behavior stub:
|
||||
|
||||
- For repo-system: act as if a system package owns the command
|
||||
→ return None (no symlink).
|
||||
|
||||
- For repo-nix: act as if a Nix profile binary is the entrypoint
|
||||
→ return nix_tool_path (symlink should be created).
|
||||
"""
|
||||
if repo_identifier == "repo-system":
|
||||
return None
|
||||
if repo_identifier == "repo-nix":
|
||||
return nix_tool_path
|
||||
return None
|
||||
|
||||
def fake_exists_install(path: str) -> bool:
|
||||
"""
|
||||
Make _ensure_repo_dir() believe that the repo directories
|
||||
already exist so that it does not attempt cloning.
|
||||
"""
|
||||
if path in (repo_system_dir, repo_nix_dir):
|
||||
return True
|
||||
return False
|
||||
|
||||
mock_resolve.side_effect = fake_resolve_command
|
||||
mock_exists_install.side_effect = fake_exists_install
|
||||
|
||||
# Use only DummyInstaller so we focus on link creation, not installer behavior
|
||||
old_installers = install_module.INSTALLERS
|
||||
install_module.INSTALLERS = [DummyInstaller()]
|
||||
try:
|
||||
install_repos(
|
||||
selected_repos=selected_repos,
|
||||
repositories_base_dir=tmp_base,
|
||||
bin_dir=tmp_bin,
|
||||
all_repos=all_repos,
|
||||
no_verification=False,
|
||||
preview=False,
|
||||
quiet=False,
|
||||
clone_mode="shallow",
|
||||
update_dependencies=False,
|
||||
)
|
||||
finally:
|
||||
install_module.INSTALLERS = old_installers
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Inspect bin_dir: exactly one symlink must exist, pointing to Nix.
|
||||
# ------------------------------------------------------------------
|
||||
symlink_paths = []
|
||||
for entry in os.listdir(tmp_bin):
|
||||
full = os.path.join(tmp_bin, entry)
|
||||
if os.path.islink(full):
|
||||
symlink_paths.append(full)
|
||||
|
||||
# There must be exactly one symlink (for repo-nix)
|
||||
self.assertEqual(
|
||||
len(symlink_paths),
|
||||
1,
|
||||
f"Expected exactly one symlink in {tmp_bin}, found {symlink_paths}",
|
||||
)
|
||||
|
||||
target = os.readlink(symlink_paths[0])
|
||||
|
||||
# That symlink must point to the "Nix" path returned by the resolver stub
|
||||
self.assertEqual(
|
||||
target,
|
||||
nix_tool_path,
|
||||
f"Expected symlink target to be Nix binary {nix_tool_path}, got {target}",
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -98,6 +98,11 @@ class TestRecursiveCapabilitiesIntegration(unittest.TestCase):
|
||||
The installers' supports() are forced to True so that only the
|
||||
capability-shadowing logic decides whether they are skipped.
|
||||
The installers' run() methods are patched to avoid real commands.
|
||||
|
||||
NOTE:
|
||||
We patch resolve_command_for_repo() to always return a dummy
|
||||
command path so that command resolution does not interfere with
|
||||
capability-layering tests.
|
||||
"""
|
||||
if selected_repos is None:
|
||||
repo = {}
|
||||
@@ -128,7 +133,8 @@ class TestRecursiveCapabilitiesIntegration(unittest.TestCase):
|
||||
patch.object(install_mod, "get_repo_dir", return_value=repo_dir), \
|
||||
patch.object(install_mod, "verify_repository", return_value=(True, [], None, None)), \
|
||||
patch.object(install_mod, "create_ink"), \
|
||||
patch.object(install_mod, "clone_repos"):
|
||||
patch.object(install_mod, "clone_repos"), \
|
||||
patch.object(install_mod, "resolve_command_for_repo", return_value="/bin/dummy"):
|
||||
|
||||
install_repos(
|
||||
selected_repos=selected_repos,
|
||||
@@ -144,6 +150,7 @@ class TestRecursiveCapabilitiesIntegration(unittest.TestCase):
|
||||
|
||||
return called_installers
|
||||
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Scenario 1: Only Makefile with install target
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
102
tests/unit/pkgmgr/test_create_ink.py
Normal file
102
tests/unit/pkgmgr/test_create_ink.py
Normal file
@@ -0,0 +1,102 @@
|
||||
# tests/unit/pkgmgr/test_create_ink.py
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pkgmgr.create_ink as create_ink_module
|
||||
|
||||
|
||||
class TestCreateInk(unittest.TestCase):
|
||||
@patch("pkgmgr.create_ink.get_repo_dir")
|
||||
@patch("pkgmgr.create_ink.get_repo_identifier")
|
||||
def test_create_ink_skips_when_no_command(
|
||||
self,
|
||||
mock_get_repo_identifier,
|
||||
mock_get_repo_dir,
|
||||
):
|
||||
repo = {} # no 'command' key
|
||||
mock_get_repo_identifier.return_value = "test-id"
|
||||
mock_get_repo_dir.return_value = "/repos/test-id"
|
||||
|
||||
with patch("pkgmgr.create_ink.os.makedirs") as mock_makedirs, \
|
||||
patch("pkgmgr.create_ink.os.symlink") as mock_symlink, \
|
||||
patch("pkgmgr.create_ink.os.chmod") as mock_chmod:
|
||||
create_ink_module.create_ink(
|
||||
repo=repo,
|
||||
repositories_base_dir="/repos",
|
||||
bin_dir="/bin",
|
||||
all_repos=[repo],
|
||||
quiet=True,
|
||||
preview=False,
|
||||
)
|
||||
|
||||
mock_makedirs.assert_not_called()
|
||||
mock_symlink.assert_not_called()
|
||||
mock_chmod.assert_not_called()
|
||||
|
||||
@patch("pkgmgr.create_ink.get_repo_dir")
|
||||
@patch("pkgmgr.create_ink.get_repo_identifier")
|
||||
def test_create_ink_preview_only(
|
||||
self,
|
||||
mock_get_repo_identifier,
|
||||
mock_get_repo_dir,
|
||||
):
|
||||
repo = {"command": "/repos/test-id/main.py"}
|
||||
mock_get_repo_identifier.return_value = "test-id"
|
||||
mock_get_repo_dir.return_value = "/repos/test-id"
|
||||
|
||||
with patch("pkgmgr.create_ink.os.makedirs") as mock_makedirs, \
|
||||
patch("pkgmgr.create_ink.os.symlink") as mock_symlink, \
|
||||
patch("pkgmgr.create_ink.os.chmod") as mock_chmod:
|
||||
create_ink_module.create_ink(
|
||||
repo=repo,
|
||||
repositories_base_dir="/repos",
|
||||
bin_dir="/bin",
|
||||
all_repos=[repo],
|
||||
quiet=True,
|
||||
preview=True,
|
||||
)
|
||||
|
||||
mock_makedirs.assert_not_called()
|
||||
mock_symlink.assert_not_called()
|
||||
mock_chmod.assert_not_called()
|
||||
|
||||
@patch("pkgmgr.create_ink.get_repo_dir")
|
||||
@patch("pkgmgr.create_ink.get_repo_identifier")
|
||||
def test_create_ink_creates_symlink_and_alias(
|
||||
self,
|
||||
mock_get_repo_identifier,
|
||||
mock_get_repo_dir,
|
||||
):
|
||||
repo = {
|
||||
"command": "/repos/test-id/main.py",
|
||||
"alias": "alias-id",
|
||||
}
|
||||
mock_get_repo_identifier.return_value = "test-id"
|
||||
mock_get_repo_dir.return_value = "/repos/test-id"
|
||||
|
||||
with patch("pkgmgr.create_ink.os.makedirs") as mock_makedirs, \
|
||||
patch("pkgmgr.create_ink.os.symlink") as mock_symlink, \
|
||||
patch("pkgmgr.create_ink.os.chmod") as mock_chmod, \
|
||||
patch("pkgmgr.create_ink.os.path.exists", return_value=False), \
|
||||
patch("pkgmgr.create_ink.os.path.islink", return_value=False), \
|
||||
patch("pkgmgr.create_ink.os.remove") as mock_remove, \
|
||||
patch("pkgmgr.create_ink.os.path.realpath", side_effect=lambda p: p):
|
||||
create_ink_module.create_ink(
|
||||
repo=repo,
|
||||
repositories_base_dir="/repos",
|
||||
bin_dir="/bin",
|
||||
all_repos=[repo],
|
||||
quiet=True,
|
||||
preview=False,
|
||||
)
|
||||
|
||||
# main link + alias link
|
||||
self.assertEqual(mock_symlink.call_count, 2)
|
||||
mock_makedirs.assert_called_once()
|
||||
mock_chmod.assert_called_once()
|
||||
mock_remove.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -11,7 +11,7 @@ from pkgmgr.installers.base import BaseInstaller
|
||||
class DummyInstaller(BaseInstaller):
|
||||
"""Simple installer for testing orchestration."""
|
||||
|
||||
layer = None # keine speziellen Capabilities
|
||||
layer = None # no specific capabilities
|
||||
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
@@ -26,6 +26,7 @@ class DummyInstaller(BaseInstaller):
|
||||
|
||||
class TestInstallReposOrchestration(unittest.TestCase):
|
||||
@patch("pkgmgr.install_repos.create_ink")
|
||||
@patch("pkgmgr.install_repos.resolve_command_for_repo")
|
||||
@patch("pkgmgr.install_repos.verify_repository")
|
||||
@patch("pkgmgr.install_repos.get_repo_dir")
|
||||
@patch("pkgmgr.install_repos.get_repo_identifier")
|
||||
@@ -36,6 +37,7 @@ class TestInstallReposOrchestration(unittest.TestCase):
|
||||
mock_get_repo_identifier,
|
||||
mock_get_repo_dir,
|
||||
mock_verify_repository,
|
||||
mock_resolve_command_for_repo,
|
||||
mock_create_ink,
|
||||
):
|
||||
repo1 = {"name": "repo1"}
|
||||
@@ -50,6 +52,9 @@ class TestInstallReposOrchestration(unittest.TestCase):
|
||||
# Simulate verification success: (ok, errors, commit, key)
|
||||
mock_verify_repository.return_value = (True, [], "commit", "key")
|
||||
|
||||
# Resolve commands for both repos so create_ink will be called
|
||||
mock_resolve_command_for_repo.side_effect = ["/bin/cmd1", "/bin/cmd2"]
|
||||
|
||||
# Ensure directories exist (no cloning)
|
||||
with patch("os.path.exists", return_value=True):
|
||||
dummy_installer = DummyInstaller()
|
||||
@@ -75,6 +80,7 @@ class TestInstallReposOrchestration(unittest.TestCase):
|
||||
self.assertEqual(dummy_installer.calls, ["id1", "id2"])
|
||||
self.assertEqual(mock_create_ink.call_count, 2)
|
||||
self.assertEqual(mock_verify_repository.call_count, 2)
|
||||
self.assertEqual(mock_resolve_command_for_repo.call_count, 2)
|
||||
|
||||
@patch("pkgmgr.install_repos.verify_repository")
|
||||
@patch("pkgmgr.install_repos.get_repo_dir")
|
||||
@@ -100,6 +106,7 @@ class TestInstallReposOrchestration(unittest.TestCase):
|
||||
dummy_installer = DummyInstaller()
|
||||
with patch("os.path.exists", return_value=True), \
|
||||
patch("pkgmgr.install_repos.create_ink") as mock_create_ink, \
|
||||
patch("pkgmgr.install_repos.resolve_command_for_repo") as mock_resolve_cmd, \
|
||||
patch("builtins.input", return_value="n"):
|
||||
old_installers = install_module.INSTALLERS
|
||||
install_module.INSTALLERS = [dummy_installer]
|
||||
@@ -121,6 +128,7 @@ class TestInstallReposOrchestration(unittest.TestCase):
|
||||
# No installer run and no create_ink when user declines
|
||||
self.assertEqual(dummy_installer.calls, [])
|
||||
mock_create_ink.assert_not_called()
|
||||
mock_resolve_cmd.assert_not_called()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
166
tests/unit/pkgmgr/test_resolve_command.py
Normal file
166
tests/unit/pkgmgr/test_resolve_command.py
Normal file
@@ -0,0 +1,166 @@
|
||||
# tests/unit/pkgmgr/test_resolve_command.py
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pkgmgr.resolve_command as resolve_command_module
|
||||
|
||||
|
||||
class TestResolveCommandForRepo(unittest.TestCase):
|
||||
def test_explicit_command_wins(self):
|
||||
repo = {"command": "/custom/cmd"}
|
||||
result = resolve_command_module.resolve_command_for_repo(
|
||||
repo=repo,
|
||||
repo_identifier="tool",
|
||||
repo_dir="/repos/tool",
|
||||
)
|
||||
self.assertEqual(result, "/custom/cmd")
|
||||
|
||||
@patch("pkgmgr.resolve_command.shutil.which", return_value="/usr/bin/tool")
|
||||
def test_system_binary_returns_none_and_no_error(self, mock_which):
|
||||
repo = {}
|
||||
result = resolve_command_module.resolve_command_for_repo(
|
||||
repo=repo,
|
||||
repo_identifier="tool",
|
||||
repo_dir="/repos/tool",
|
||||
)
|
||||
# System binary → no link
|
||||
self.assertIsNone(result)
|
||||
|
||||
@patch("pkgmgr.resolve_command.os.access")
|
||||
@patch("pkgmgr.resolve_command.os.path.exists")
|
||||
@patch("pkgmgr.resolve_command.shutil.which", return_value=None)
|
||||
@patch("pkgmgr.resolve_command.os.path.expanduser", return_value="/fakehome")
|
||||
def test_nix_profile_binary(
|
||||
self,
|
||||
mock_expanduser,
|
||||
mock_which,
|
||||
mock_exists,
|
||||
mock_access,
|
||||
):
|
||||
"""
|
||||
No system/PATH binary, but a Nix profile binary exists:
|
||||
→ must return the Nix binary path.
|
||||
"""
|
||||
repo = {}
|
||||
fake_home = "/fakehome"
|
||||
nix_path = f"{fake_home}/.nix-profile/bin/tool"
|
||||
|
||||
def fake_exists(path):
|
||||
# Only the Nix binary exists
|
||||
return path == nix_path
|
||||
|
||||
def fake_access(path, mode):
|
||||
# Only the Nix binary is executable
|
||||
return path == nix_path
|
||||
|
||||
mock_exists.side_effect = fake_exists
|
||||
mock_access.side_effect = fake_access
|
||||
|
||||
result = resolve_command_module.resolve_command_for_repo(
|
||||
repo=repo,
|
||||
repo_identifier="tool",
|
||||
repo_dir="/repos/tool",
|
||||
)
|
||||
self.assertEqual(result, nix_path)
|
||||
|
||||
@patch("pkgmgr.resolve_command.os.access")
|
||||
@patch("pkgmgr.resolve_command.os.path.exists")
|
||||
@patch("pkgmgr.resolve_command.os.path.expanduser", return_value="/home/user")
|
||||
@patch("pkgmgr.resolve_command.shutil.which", return_value="/home/user/.local/bin/tool")
|
||||
def test_non_system_binary_on_path(
|
||||
self,
|
||||
mock_which,
|
||||
mock_expanduser,
|
||||
mock_exists,
|
||||
mock_access,
|
||||
):
|
||||
"""
|
||||
No system (/usr) binary and no Nix binary, but a non-system
|
||||
PATH binary exists (e.g. venv or ~/.local/bin):
|
||||
→ must return that PATH binary.
|
||||
"""
|
||||
repo = {}
|
||||
non_system_path = "/home/user/.local/bin/tool"
|
||||
nix_candidate = "/home/user/.nix-profile/bin/tool"
|
||||
|
||||
def fake_exists(path):
|
||||
# Only the non-system PATH binary "exists".
|
||||
return path == non_system_path
|
||||
|
||||
def fake_access(path, mode):
|
||||
# Only the non-system PATH binary is executable.
|
||||
return path == non_system_path
|
||||
|
||||
mock_exists.side_effect = fake_exists
|
||||
mock_access.side_effect = fake_access
|
||||
|
||||
result = resolve_command_module.resolve_command_for_repo(
|
||||
repo=repo,
|
||||
repo_identifier="tool",
|
||||
repo_dir="/repos/tool",
|
||||
)
|
||||
self.assertEqual(result, non_system_path)
|
||||
|
||||
@patch("pkgmgr.resolve_command.os.access")
|
||||
@patch("pkgmgr.resolve_command.os.path.exists")
|
||||
@patch("pkgmgr.resolve_command.shutil.which", return_value=None)
|
||||
@patch("pkgmgr.resolve_command.os.path.expanduser", return_value="/fakehome")
|
||||
def test_fallback_to_main_py(
|
||||
self,
|
||||
mock_expanduser,
|
||||
mock_which,
|
||||
mock_exists,
|
||||
mock_access,
|
||||
):
|
||||
"""
|
||||
No system/non-system PATH binary, no Nix binary, but main.py exists:
|
||||
→ must fall back to main.py in the repo.
|
||||
"""
|
||||
repo = {}
|
||||
main_py = "/repos/tool/main.py"
|
||||
|
||||
def fake_exists(path):
|
||||
return path == main_py
|
||||
|
||||
def fake_access(path, mode):
|
||||
return path == main_py
|
||||
|
||||
mock_exists.side_effect = fake_exists
|
||||
mock_access.side_effect = fake_access
|
||||
|
||||
result = resolve_command_module.resolve_command_for_repo(
|
||||
repo=repo,
|
||||
repo_identifier="tool",
|
||||
repo_dir="/repos/tool",
|
||||
)
|
||||
self.assertEqual(result, main_py)
|
||||
|
||||
@patch("pkgmgr.resolve_command.os.access", return_value=False)
|
||||
@patch("pkgmgr.resolve_command.os.path.exists", return_value=False)
|
||||
@patch("pkgmgr.resolve_command.shutil.which", return_value=None)
|
||||
@patch("pkgmgr.resolve_command.os.path.expanduser", return_value="/fakehome")
|
||||
def test_no_command_results_in_system_exit(
|
||||
self,
|
||||
mock_expanduser,
|
||||
mock_which,
|
||||
mock_exists,
|
||||
mock_access,
|
||||
):
|
||||
"""
|
||||
Nothing available at any layer:
|
||||
→ must raise SystemExit with a descriptive error message.
|
||||
"""
|
||||
repo = {}
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
resolve_command_module.resolve_command_for_repo(
|
||||
repo=repo,
|
||||
repo_identifier="tool",
|
||||
repo_dir="/repos/tool",
|
||||
)
|
||||
msg = str(cm.exception)
|
||||
self.assertIn("No executable command could be resolved for repository 'tool'", msg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user