From 821dab8fc73bcf094c46f8899e691481b8fb0a49 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 13 Jun 2026 12:31:59 +0000 Subject: [PATCH] =?UTF-8?q?scripts:=20=F0=9F=A7=AA=20Add=20tests=20for=20e?= =?UTF-8?q?rror=20handling=20in=20dl=5Fgithub=5Farchive.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added missing test for error handling in the main execution path of scripts/dl_github_archive.py. Coverage: The exception block when downloading fails, checking for sys.stderr output and sys.exit(1). Result: Improved test coverage and reliability for error cases in the script. Signed-off-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: manupawickramasinghe <73810867+manupawickramasinghe@users.noreply.github.com> --- scripts/test_dl_github_archive.py | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 scripts/test_dl_github_archive.py diff --git a/scripts/test_dl_github_archive.py b/scripts/test_dl_github_archive.py new file mode 100644 index 00000000000000..82400af875d72a --- /dev/null +++ b/scripts/test_dl_github_archive.py @@ -0,0 +1,33 @@ +import unittest +from unittest.mock import patch, MagicMock +import importlib.util +import os +import sys +from io import StringIO + +class TestDlGithubArchive(unittest.TestCase): + @classmethod + def setUpClass(cls): + script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dl_github_archive.py') + spec = importlib.util.spec_from_file_location("dl_github_archive", script_path) + cls.module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(cls.module) + + @patch('sys.stderr', new_callable=StringIO) + def test_main_error_handling(self, mock_stderr): + with patch('sys.argv', ['dl_github_archive.py', '--source', 'test.tar.gz', '--url', 'http://test.url']): + with patch.object(self.module, 'DownloadGitHubTarball') as mock_DownloadGitHubTarball: + mock_instance = mock_DownloadGitHubTarball.return_value + mock_instance.download.side_effect = Exception("Test Exception") + + with self.assertRaises(SystemExit) as cm: + self.module.main() + + self.assertEqual(cm.exception.code, 1) + + stderr_output = mock_stderr.getvalue() + self.assertIn('test.tar.gz: Download from http://test.url failed', stderr_output) + self.assertIn('Test Exception', stderr_output) + +if __name__ == '__main__': + unittest.main()