Repository-wide mechanical cleanup so `ruff check src tests` has a chance of passing; no behavioural changes. - Add `from __future__ import annotations` where PEP 604 unions are used. This has to come first: pyproject declares requires-python >= 3.9, where `X | None` is not evaluable at runtime unless annotations are stringified. - Replace typing.List/Dict/Tuple/Set with the builtin generics and Optional[X] with X | None, then drop the imports that became unused. The four actions/*/__init__.py files needed this by hand because ruff leaves unused imports in __init__.py alone (possible re-exports). - Strip shebangs from 72 importable modules. None of them are executable or invoked directly; the entry points are console_scripts and runpy. - Flatten nested `with` blocks, collapse needless-bool returns, and apply the remaining mechanical ruff fixes (PIE810, FLY002, PERF102, FURB192, RUF059, I001). - Pass check=False explicitly to the four subprocess.run() calls that inspect returncode themselves. That is the existing default. Two rewrites are visible to mocks, so their tests move with them: subprocess.run(stdout=PIPE, stderr=PIPE) became capture_output=True, and open(path, "r", ...) lost the redundant mode. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
159 lines
5.6 KiB
Python
159 lines
5.6 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
Repository = dict[str, Any]
|
|
|
|
|
|
class TestOpenVSCodeWorkspace(unittest.TestCase):
|
|
def test_no_selected_repos_prints_message_and_returns(self) -> None:
|
|
from pkgmgr.cli.tools.vscode import open_vscode_workspace
|
|
|
|
ctx = SimpleNamespace(config_merged={}, all_repositories=[])
|
|
|
|
with patch("builtins.print") as p:
|
|
open_vscode_workspace(ctx, [])
|
|
|
|
p.assert_called_once()
|
|
self.assertIn("No repositories selected.", str(p.call_args[0][0]))
|
|
|
|
def test_raises_if_code_cli_missing(self) -> None:
|
|
from pkgmgr.cli.tools.vscode import open_vscode_workspace
|
|
|
|
ctx = SimpleNamespace(config_merged={}, all_repositories=[])
|
|
selected: list[Repository] = [
|
|
{"provider": "github.com", "account": "x", "repository": "y"}
|
|
]
|
|
|
|
with (
|
|
patch("pkgmgr.cli.tools.vscode.shutil.which", return_value=None),
|
|
self.assertRaises(RuntimeError) as cm,
|
|
):
|
|
open_vscode_workspace(ctx, selected)
|
|
|
|
self.assertIn("VS Code CLI ('code') not found", str(cm.exception))
|
|
|
|
def test_raises_if_identifier_contains_slash(self) -> None:
|
|
from pkgmgr.cli.tools.vscode import open_vscode_workspace
|
|
|
|
ctx = SimpleNamespace(
|
|
config_merged={"directories": {"workspaces": "~/Workspaces"}},
|
|
all_repositories=[],
|
|
)
|
|
selected: list[Repository] = [
|
|
{"provider": "github.com", "account": "x", "repository": "y"}
|
|
]
|
|
|
|
with (
|
|
patch("pkgmgr.cli.tools.vscode.shutil.which", return_value="/usr/bin/code"),
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.get_repo_identifier",
|
|
return_value="github.com/x/y",
|
|
),
|
|
self.assertRaises(RuntimeError) as cm,
|
|
):
|
|
open_vscode_workspace(ctx, selected)
|
|
|
|
msg = str(cm.exception)
|
|
self.assertIn("not yet identified", msg)
|
|
self.assertIn("identifier contains '/'", msg)
|
|
|
|
def test_creates_workspace_file_and_calls_code(self) -> None:
|
|
from pkgmgr.cli.tools.vscode import open_vscode_workspace
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
workspaces_dir = os.path.join(tmp, "Workspaces")
|
|
repo_path = os.path.join(tmp, "Repos", "dotlinker")
|
|
|
|
ctx = SimpleNamespace(
|
|
config_merged={"directories": {"workspaces": workspaces_dir}},
|
|
all_repositories=[],
|
|
repositories_base_dir=os.path.join(tmp, "Repos"),
|
|
)
|
|
selected: list[Repository] = [
|
|
{
|
|
"provider": "github.com",
|
|
"account": "kevin",
|
|
"repository": "dotlinker",
|
|
}
|
|
]
|
|
|
|
with (
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.shutil.which", return_value="/usr/bin/code"
|
|
),
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.get_repo_identifier",
|
|
return_value="dotlinker",
|
|
),
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.resolve_repository_path",
|
|
return_value=repo_path,
|
|
),
|
|
patch("pkgmgr.cli.tools.vscode.run_command") as run_cmd,
|
|
):
|
|
open_vscode_workspace(ctx, selected)
|
|
|
|
workspace_file = os.path.join(workspaces_dir, "dotlinker.code-workspace")
|
|
self.assertTrue(os.path.exists(workspace_file))
|
|
|
|
with open(workspace_file, encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
self.assertEqual(data["folders"], [{"path": repo_path}])
|
|
self.assertEqual(data["settings"], {})
|
|
|
|
run_cmd.assert_called_once_with(f'code "{workspace_file}"')
|
|
|
|
def test_uses_existing_workspace_file_without_overwriting(self) -> None:
|
|
from pkgmgr.cli.tools.vscode import open_vscode_workspace
|
|
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
workspaces_dir = os.path.join(tmp, "Workspaces")
|
|
os.makedirs(workspaces_dir, exist_ok=True)
|
|
|
|
workspace_file = os.path.join(workspaces_dir, "dotlinker.code-workspace")
|
|
original = {"folders": [{"path": "/original"}], "settings": {"x": 1}}
|
|
with open(workspace_file, "w", encoding="utf-8") as f:
|
|
json.dump(original, f)
|
|
|
|
ctx = SimpleNamespace(
|
|
config_merged={"directories": {"workspaces": workspaces_dir}},
|
|
all_repositories=[],
|
|
)
|
|
selected: list[Repository] = [
|
|
{
|
|
"provider": "github.com",
|
|
"account": "kevin",
|
|
"repository": "dotlinker",
|
|
}
|
|
]
|
|
|
|
with (
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.shutil.which", return_value="/usr/bin/code"
|
|
),
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.get_repo_identifier",
|
|
return_value="dotlinker",
|
|
),
|
|
patch(
|
|
"pkgmgr.cli.tools.vscode.resolve_repository_path",
|
|
return_value="/new/path",
|
|
),
|
|
patch("pkgmgr.cli.tools.vscode.run_command") as run_cmd,
|
|
):
|
|
open_vscode_workspace(ctx, selected)
|
|
|
|
with open(workspace_file, encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
self.assertEqual(data, original)
|
|
run_cmd.assert_called_once_with(f'code "{workspace_file}"')
|