Say why trust cannot read ~/.claude.json and look up trust without inserting entries

This commit is contained in:
2026-09-25 17:24:16 +02:00
parent e4f6361e19
commit 40f0b73a19
4 changed files with 33 additions and 5 deletions
+6 -4
View File
@@ -219,15 +219,17 @@ def checkouts():
def trust(paths):
config = os.path.expanduser("~/.claude.json")
try:
data = json.load(open(config, encoding="utf-8"))
except (OSError, ValueError):
with open(config, encoding="utf-8") as handle:
data = json.load(handle)
except (OSError, ValueError) as failure:
print(f"could not read {config}: {failure}", file=sys.stderr)
return
projects = data.setdefault("projects", {})
granted = [path for path in paths if not projects.setdefault(path, {}).get("hasTrustDialogAccepted")]
granted = [path for path in paths if not projects.get(path, {}).get("hasTrustDialogAccepted")]
if not granted:
return
for path in granted:
projects[path]["hasTrustDialogAccepted"] = True
projects.setdefault(path, {})["hasTrustDialogAccepted"] = True
staged = f"{config}.loop-claude"
try:
with open(staged, "w", encoding="utf-8") as handle:
+26
View File
@@ -1,6 +1,7 @@
import contextlib
import importlib.machinery
import importlib.util
import io
import json
import os
import pathlib
@@ -199,6 +200,31 @@ class Interrupt(unittest.TestCase):
self.assertEqual(loop.interrupt(SESSION_ID, transcript, "q?"), "")
class Trust(unittest.TestCase):
def test_says_why_the_config_cannot_be_read_and_records_only_what_is_new(self):
with tempfile.TemporaryDirectory() as home, unittest.mock.patch.dict(os.environ, {"HOME": home}):
config = pathlib.Path(home, ".claude.json")
with contextlib.redirect_stderr(io.StringIO()) as err:
loop.trust(["/a"])
self.assertIn(f"could not read {config}: ", err.getvalue())
self.assertIn("No such file or directory", err.getvalue())
config.write_text("{")
with contextlib.redirect_stderr(io.StringIO()) as err:
loop.trust(["/a"])
self.assertIn(f"could not read {config}: ", err.getvalue())
data = {"other": 1, "projects": {"/a": {"hasTrustDialogAccepted": True, "kept": 2}}}
config.write_text(json.dumps(data))
with contextlib.redirect_stderr(io.StringIO()) as err:
loop.trust(["/a"])
self.assertEqual(err.getvalue(), "")
self.assertEqual(json.loads(config.read_text()), data)
with contextlib.redirect_stderr(io.StringIO()) as err:
loop.trust(["/a", "/b"])
self.assertEqual(err.getvalue(), "trusted for the interrupt: /b\n")
data["projects"]["/b"] = {"hasTrustDialogAccepted": True}
self.assertEqual(json.loads(config.read_text()), data)
class Parse(unittest.TestCase):
def test_yields_only_json_objects(self):
self.assertEqual(loop.parse('{"type": "user"}'), {"type": "user"})