From 3b017d788db63b9d0a413b46300f85502ca581fa Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Mon, 12 Oct 2026 02:29:15 -0600 Subject: [PATCH] Say MAX_TAG_LENGTH bounds the install ID too; close leaked test clients MAX_TAG_LENGTH's comment named only the program version, though it also bounds install_id= and the ID lines install_id_from_file accepts. In the tests, close the three enabled clients the blank-install-ID loop left running, and set DO_NOT_TRACK through mock.patch.dict rather than writing os.environ directly. Closes #12 Closes #13 Co-Authored-By: Claude Opus 5.5 --- tests/test_trace_client.py | 11 ++++++----- trace_client/trace_client.py | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/test_trace_client.py b/tests/test_trace_client.py index 2645a20..bd00837 100644 --- a/tests/test_trace_client.py +++ b/tests/test_trace_client.py @@ -486,10 +486,9 @@ def test_install_id_from_file_never_raises_for_a_bad_path(self): def test_a_disabled_client_never_makes_up_or_writes_an_install_id(self): directory = self._tmpdir() path = os.path.join(directory, "install-id") - os.environ["DO_NOT_TRACK"] = "1" - by_environment = TraceClient(self.base_url, "MyGame", "1.2.3", key="k", install_id_file=path, - install_id="explicit") - del os.environ["DO_NOT_TRACK"] + with mock.patch.dict(os.environ, {"DO_NOT_TRACK": "1"}): + by_environment = TraceClient(self.base_url, "MyGame", "1.2.3", key="k", install_id_file=path, + install_id="explicit") by_config = TraceClient(self.base_url, "MyGame", "1.2.3", key="k", enabled=False, install_id_file=path) by_no_key = TraceClient(self.base_url, "MyGame", "1.2.3", install_id_file=path) for client in (by_environment, by_config, by_no_key): @@ -507,7 +506,9 @@ def test_explicit_install_id_is_trimmed_sent_and_wins_over_the_file(self): def test_blank_install_id_is_none_and_an_overlong_one_is_rejected(self): for blank in (None, "", " "): - self.assertIsNone(TraceClient(self.base_url, "MyGame", "1.2.3", key="k", install_id=blank).install_id) + client = TraceClient(self.base_url, "MyGame", "1.2.3", key="k", install_id=blank) + self.assertIsNone(client.install_id, repr(blank)) + client.close() with self.assertRaises(ValueError): TraceClient(self.base_url, "MyGame", "1.2.3", key="k", install_id="x" * (MAX_TAG_LENGTH + 1)) with self.assertRaises(ValueError, msg="rejected even on a disabled client, like the version"): diff --git a/trace_client/trace_client.py b/trace_client/trace_client.py index 6d92048..3515ac2 100644 --- a/trace_client/trace_client.py +++ b/trace_client/trace_client.py @@ -38,8 +38,9 @@ REASON_CONFIG = "config" REASON_NO_KEY = "no key" -#: The longest a program version may be, after trimming: the trace server's -#: limit on a tag value. +#: The longest a tag value the client sets itself -- the program version, the +#: installation ID -- may be, after trimming: the trace server's limit on a +#: tag value. MAX_TAG_LENGTH = 255 #: The most tags one event carries: the trace server's limit. The ``install``