hooks: fix shared mutable list bug in ProjectResults The ProjectResults class used a mutable default list `results: List[HookResult] = []`. In Python, this creates a single list instance shared across all ProjectResults objects that do not explicitly provide a list. This led to cross-contamination of results across different projects during `repo upload`. This change removes the default value to prevent accidental state sharing and requires explicit initialization. It also updates callers to pass an empty list and adds a unit test to verify instance isolation. Bug: 499052751 Test: ./rh/results_unittest.py Change-Id: Icaa52eed649b9ccfb6e42e1c68505e5ee086878a Reviewed-on: https://gerrit-review.googlesource.com/c/git-repohooks/+/570141 Reviewed-by: Mike Frysinger <vapier@google.com> Tested-by: Marty Heavey <mheavey@google.com> Commit-Queue: Marty Heavey <mheavey@google.com>
diff --git a/pre-upload.py b/pre-upload.py index 48b9fc9..5cdcb31 100755 --- a/pre-upload.py +++ b/pre-upload.py
@@ -368,7 +368,7 @@ Returns: All the results for this project. """ - ret = rh.results.ProjectResults(project_name, proj_dir) + ret = rh.results.ProjectResults(project_name, proj_dir, []) try: config = _get_project_config(from_git)
diff --git a/rh/results.py b/rh/results.py index 1efdf33..f1d1688 100644 --- a/rh/results.py +++ b/rh/results.py
@@ -99,7 +99,7 @@ workdir: str # All the results from running all the hooks. - results: List[HookResult] = [] + results: List[HookResult] # Whether there were any non-hook related errors. For example, trying to # parse the project configuration.
diff --git a/rh/results_unittest.py b/rh/results_unittest.py index fe09162..9699e47 100755 --- a/rh/results_unittest.py +++ b/rh/results_unittest.py
@@ -86,7 +86,7 @@ def test_error_warning(self): """Check error & warning handling.""" # No errors. - result = rh.results.ProjectResults("project", "workdir") + result = rh.results.ProjectResults("project", "workdir", []) self.assertFalse(result) # Warnings are not errors. @@ -108,6 +108,18 @@ ) self.assertTrue(result) + def test_shared_results_list(self): + """Check that ProjectResults instances do not share the results list.""" + result1 = rh.results.ProjectResults("project1", "workdir1", []) + result2 = rh.results.ProjectResults("project2", "workdir2", []) + + result1.add_results( + [rh.results.HookResult("hook", "project", "HEAD", True)] + ) + + self.assertEqual(len(result1.results), 1) + self.assertEqual(len(result2.results), 0) + if __name__ == "__main__": unittest.main()