From 79c2d36049bc695f37c983be95198fe36c9ef4f1 Mon Sep 17 00:00:00 2001 From: ashvinctrl Date: Wed, 10 Jun 2026 11:38:12 +0530 Subject: [PATCH] fix: move result counter from module global to instance variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The module-level `globvar` in notify.py persisted across all username scans in a single process, causing `finish()` to print a cumulative count instead of a per-username count. Additionally, `finish()` called `self.countResults() - 1` which incremented the counter one extra time before subtracting 1 — a fragile pattern that masked the off-by-one. Replace `globvar` with `self._result_count = 0` initialised in `__init__`. Each `QueryNotifyPrint` instance now tracks its own scan independently and `finish()` reads the counter directly without an extra increment. Fixes #2990 --- sherlock_project/notify.py | 11 +++----- tests/test_notify.py | 53 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 tests/test_notify.py diff --git a/sherlock_project/notify.py b/sherlock_project/notify.py index ab6f5a3866..2e41765240 100644 --- a/sherlock_project/notify.py +++ b/sherlock_project/notify.py @@ -7,9 +7,6 @@ from colorama import Fore, Style import webbrowser -# Global variable to count the number of results. -globvar = 0 - class QueryNotify: """Query Notify Object. @@ -132,6 +129,7 @@ def __init__(self, result=None, verbose=False, print_all=False, browse=False): self.verbose = verbose self.print_all = print_all self.browse = browse + self._result_count = 0 def start(self, message): @@ -169,9 +167,8 @@ def countResults(self): Return Value: The number of results by the time we call the function. """ - global globvar - globvar += 1 - return globvar + self._result_count += 1 + return self._result_count def update(self, result): """Notify Update. @@ -258,7 +255,7 @@ def finish(self, message="The processing has been finished."): Return Value: Nothing. """ - NumberOfResults = self.countResults() - 1 + NumberOfResults = self._result_count print(Style.BRIGHT + Fore.GREEN + "[" + Fore.YELLOW + "*" + diff --git a/tests/test_notify.py b/tests/test_notify.py new file mode 100644 index 0000000000..285923babd --- /dev/null +++ b/tests/test_notify.py @@ -0,0 +1,53 @@ +"""Tests for QueryNotifyPrint result counter behaviour.""" +import pytest +from sherlock_project.notify import QueryNotifyPrint +from sherlock_project.result import QueryResult, QueryStatus + + +def _make_result(status): + return QueryResult( + username="testuser", + site_name="TestSite", + site_url_user="https://testsite.com/testuser", + status=status, + ) + + +class TestResultCounter: + def test_count_starts_at_zero(self): + notify = QueryNotifyPrint() + assert notify._result_count == 0 + + def test_count_increments_on_claimed(self): + notify = QueryNotifyPrint() + notify.update(_make_result(QueryStatus.CLAIMED)) + notify.update(_make_result(QueryStatus.CLAIMED)) + assert notify._result_count == 2 + + def test_count_does_not_increment_on_available(self): + notify = QueryNotifyPrint() + notify.update(_make_result(QueryStatus.AVAILABLE)) + assert notify._result_count == 0 + + def test_independent_instances_do_not_share_count(self): + """Two QueryNotifyPrint objects must not share a global counter.""" + n1 = QueryNotifyPrint() + n2 = QueryNotifyPrint() + n1.update(_make_result(QueryStatus.CLAIMED)) + n1.update(_make_result(QueryStatus.CLAIMED)) + n1.update(_make_result(QueryStatus.CLAIMED)) + # n2 was never updated — its count must still be 0 + assert n2._result_count == 0 + + def test_second_username_count_is_not_cumulative(self): + """Simulates two separate username scans using separate notify objects.""" + notify1 = QueryNotifyPrint() + notify1.update(_make_result(QueryStatus.CLAIMED)) + notify1.update(_make_result(QueryStatus.CLAIMED)) + + notify2 = QueryNotifyPrint() + notify2.update(_make_result(QueryStatus.CLAIMED)) + + # Each object tracks its own scan independently + assert notify1._result_count == 2 + assert notify2._result_count == 1