Answer stability round 1: 2 🔴, 3 🟡; anchor .gitattributes to the root settings.json, sort the new imports, isolate the checkout tests from the host's git, start a new install's Update at git fetch, fix the filter's docstring
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import json
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from settings_filter import cleaned
|
||||
from git_repo import git
|
||||
from repo_checks.test_python_floor import ROOT
|
||||
from settings_filter import cleaned
|
||||
|
||||
INSTALL = "git config filter.settings.clean bin/settings_filter.py"
|
||||
|
||||
|
||||
class Settings(unittest.TestCase):
|
||||
@@ -14,8 +17,9 @@ class Settings(unittest.TestCase):
|
||||
# The index, not the working file: the working file holds this machine's model in Claude Code's key order.
|
||||
stored = git("show", ":settings.json", cwd=str(ROOT))
|
||||
self.assertIsInstance(stored, str, stored)
|
||||
self.assertEqual(stored, cleaned(stored))
|
||||
self.assertNotIn("model", json.loads(stored))
|
||||
self.assertEqual(stored, cleaned(stored), f"commit settings.json with the filter on: {INSTALL}")
|
||||
|
||||
def test_the_readme_installs_the_filter_git_attributes_name(self):
|
||||
self.assertIn("settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8"))
|
||||
self.assertIn("git config filter.settings.clean bin/settings_filter.py\n", (ROOT / "README.md").read_text(encoding="utf-8"))
|
||||
def test_the_readme_installs_the_filter_git_attributes_names_for_the_root_settings_only(self):
|
||||
self.assertEqual("/settings.json filter=settings\n", (ROOT / ".gitattributes").read_text(encoding="utf-8"))
|
||||
self.assertIn(f"{INSTALL}\n", (ROOT / "README.md").read_text(encoding="utf-8"))
|
||||
|
||||
@@ -15,5 +15,5 @@ class Types(unittest.TestCase):
|
||||
self.assertIsNotNone(floor, "README.md's Install names `Python 3.N or newer`")
|
||||
if shutil.which("uv") is None:
|
||||
self.fail("uv on PATH fetches mypy; see README.md's Test line")
|
||||
ran = subprocess.run(["uv", "tool", "run", "--python", "%d.%d" % floor, "mypy==2.3.1", "--cache-dir=/dev/null", "--check-untyped-defs", "--disallow-untyped-defs", "--exclude", "/test_", "-p", "chunk_preflight", "-p", "claude_code", "-m", "comprehension_history", "-m", "git_repo", "-p", "judge_replay", "-p", "larv_review", "-p", "loop_claude", "-m", "markdown_text", "-p", "metric_bench", "-m", "project_key", "-m", "rank_todo", "-m", "round_titles", "-p", "reading_cost", "-p", "review_replay", "-p", "rule_hook", "-p", "rung_replay", "-p", "score_outcomes", "-p", "subagent_rung", "-m", "settings_filter", "-m", "shell_command", "-m", "statusline", "-m", "todo_table", "-p", "transcript_archive", "-m", "utc_stamp"], capture_output=True, cwd=ROOT / "bin", text=True)
|
||||
ran = subprocess.run(["uv", "tool", "run", "--python", "%d.%d" % floor, "mypy==2.3.1", "--cache-dir=/dev/null", "--check-untyped-defs", "--disallow-untyped-defs", "--exclude", "/test_", "-p", "chunk_preflight", "-p", "claude_code", "-m", "comprehension_history", "-m", "git_repo", "-p", "judge_replay", "-p", "larv_review", "-p", "loop_claude", "-m", "markdown_text", "-p", "metric_bench", "-m", "project_key", "-m", "rank_todo", "-p", "reading_cost", "-p", "review_replay", "-m", "round_titles", "-p", "rule_hook", "-p", "rung_replay", "-p", "score_outcomes", "-m", "settings_filter", "-m", "shell_command", "-m", "statusline", "-p", "subagent_rung", "-m", "todo_table", "-p", "transcript_archive", "-m", "utc_stamp"], capture_output=True, cwd=ROOT / "bin", text=True)
|
||||
self.assertEqual(ran.returncode, 0, ran.stdout + ran.stderr)
|
||||
|
||||
+13
-5
@@ -3,22 +3,30 @@
|
||||
|
||||
Claude Code rewrites settings.json, in its own key order, whenever a model is
|
||||
picked or a plugin installed. Each machine owns its model, so git stores the
|
||||
file without it, and neither write shows as a change.
|
||||
file without it, and neither write reaches a commit.
|
||||
"""
|
||||
import json
|
||||
import sys
|
||||
|
||||
|
||||
def cleaned(written: str) -> str:
|
||||
"""settings.json as git stores it: without "model", every key sorted."""
|
||||
"""settings.json as git stores it: without "model", every key sorted. ValueError where it holds no JSON object."""
|
||||
settings = json.loads(written)
|
||||
if not isinstance(settings, dict):
|
||||
raise ValueError("settings.json holds no JSON object")
|
||||
settings.pop("model", None)
|
||||
return json.dumps(settings, ensure_ascii=False, indent=2, sort_keys=True) + "\n"
|
||||
|
||||
|
||||
def main() -> None:
|
||||
sys.stdout.write(cleaned(sys.stdin.read()))
|
||||
def main() -> int:
|
||||
# On a non-zero exit git warns and stores the file as written.
|
||||
try:
|
||||
sys.stdout.write(cleaned(sys.stdin.read()))
|
||||
except ValueError as error:
|
||||
print(f"settings_filter.py: {error}", file=sys.stderr)
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
sys.exit(main())
|
||||
|
||||
+55
-27
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
@@ -8,8 +9,11 @@ import unittest
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||||
|
||||
from settings_filter import cleaned
|
||||
from test_env import isolated_env
|
||||
|
||||
FILTER = pathlib.Path(__file__).resolve().parent / "settings_filter.py"
|
||||
ROOT = FILTER.parents[1]
|
||||
PICKED = json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2)
|
||||
|
||||
|
||||
class Clean(unittest.TestCase):
|
||||
@@ -20,36 +24,60 @@ class Clean(unittest.TestCase):
|
||||
def test_keeps_text_outside_ascii(self):
|
||||
self.assertEqual(cleaned('{"name": "Lilleman på Larv"}'), '{\n "name": "Lilleman på Larv"\n}\n')
|
||||
|
||||
def test_refuses_json_that_is_no_object(self):
|
||||
with self.assertRaises(ValueError):
|
||||
cleaned("[]")
|
||||
|
||||
def test_a_broken_file_exits_1_naming_the_tool(self):
|
||||
ran = subprocess.run([sys.executable, FILTER], input="{", capture_output=True, text=True)
|
||||
self.assertEqual((ran.returncode, ran.stdout), (1, ""))
|
||||
self.assertIn("settings_filter.py: ", ran.stderr)
|
||||
|
||||
|
||||
class Checkout(unittest.TestCase):
|
||||
def test_a_model_pick_in_claude_codes_key_order_leaves_nothing_to_commit_and_outlasts_an_update(self):
|
||||
with tempfile.TemporaryDirectory() as repo:
|
||||
def git(*args):
|
||||
return subprocess.run(["git", "-C", repo, "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, text=True).stdout
|
||||
"""A repo as README → Install leaves it, at tag v1, and v2, which leaves settings.json alone."""
|
||||
|
||||
git("init", "-q")
|
||||
(pathlib.Path(repo) / "bin").mkdir()
|
||||
(pathlib.Path(repo) / "bin" / "settings_filter.py").symlink_to(FILTER)
|
||||
(pathlib.Path(repo) / ".gitattributes").write_text("settings.json filter=settings\n")
|
||||
git("config", "filter.settings.clean", "bin/settings_filter.py")
|
||||
settings = pathlib.Path(repo) / "settings.json"
|
||||
settings.write_text(cleaned('{"env": {}, "theme": "dark"}'))
|
||||
git("add", ".")
|
||||
git("commit", "-qm", "settings")
|
||||
git("tag", "v1")
|
||||
(pathlib.Path(repo) / "other").write_text("")
|
||||
git("add", "other")
|
||||
git("commit", "-qm", "a tag that leaves settings.json alone")
|
||||
git("tag", "v2")
|
||||
git("checkout", "-q", "v1")
|
||||
picked = json.dumps({"theme": "dark", "model": "opus[1m]", "env": {}}, indent=2)
|
||||
settings.write_text(picked)
|
||||
self.assertEqual(git("diff"), "")
|
||||
# README → Update: git status lists a file whose size changed until it is staged; staging adds no change, and keeps the model through the move.
|
||||
git("add", "settings.json")
|
||||
self.assertEqual(git("status", "--porcelain"), "")
|
||||
git("checkout", "-q", "-f", "v2")
|
||||
self.assertEqual(settings.read_text(), picked)
|
||||
def setUp(self):
|
||||
temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temp.cleanup)
|
||||
self.repo = pathlib.Path(temp.name)
|
||||
self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1"}
|
||||
self.git("init", "-q")
|
||||
(self.repo / "bin").mkdir()
|
||||
(self.repo / "bin" / "settings_filter.py").symlink_to(FILTER)
|
||||
(self.repo / ".gitattributes").write_text((ROOT / ".gitattributes").read_text())
|
||||
self.git("config", "filter.settings.clean", "bin/settings_filter.py")
|
||||
self.settings = self.repo / "settings.json"
|
||||
self.settings.write_text(cleaned('{"env": {}, "theme": "dark"}'))
|
||||
(self.repo / ".claude").mkdir()
|
||||
(self.repo / ".claude" / "settings.json").write_text('{\n "model": "claude-opus-5-5"\n}\n')
|
||||
self.git("add", ".")
|
||||
self.git("commit", "-qm", "settings")
|
||||
self.git("tag", "v1")
|
||||
(self.repo / "other").write_text("")
|
||||
self.git("add", "other")
|
||||
self.git("commit", "-qm", "a tag that leaves settings.json alone")
|
||||
self.git("tag", "v2")
|
||||
self.git("checkout", "-q", "v1")
|
||||
|
||||
def git(self, *args):
|
||||
return subprocess.run(["git", "-C", self.repo, "-c", "user.name=t", "-c", "user.email=t@t", *args], capture_output=True, check=True, env=self.env, text=True).stdout
|
||||
|
||||
def test_a_model_pick_in_claude_codes_key_order_leaves_nothing_to_commit(self):
|
||||
self.settings.write_text(PICKED)
|
||||
self.assertEqual(self.git("diff"), "")
|
||||
# git status lists a file whose size changed until it is staged; staging adds no change.
|
||||
self.git("add", "settings.json")
|
||||
self.assertEqual(self.git("status", "--porcelain"), "")
|
||||
|
||||
def test_a_staged_pick_outlasts_an_update_to_a_tag_that_leaves_settings_json_alone(self):
|
||||
self.settings.write_text(PICKED)
|
||||
self.git("add", "settings.json")
|
||||
self.git("checkout", "-q", "-f", "v2")
|
||||
self.assertEqual(self.settings.read_text(), PICKED)
|
||||
|
||||
def test_the_repos_own_harness_settings_keep_their_model(self):
|
||||
self.assertEqual(self.git("show", ":.claude/settings.json"), '{\n "model": "claude-opus-5-5"\n}\n')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user