From e7f53fd0e466e99add146d8100dd1b5b04754517 Mon Sep 17 00:00:00 2001 From: Mateus Perfigao Domiciano Date: Mon, 7 Jul 2025 23:24:11 -0300 Subject: [PATCH 1/2] add GH_TOKEN --- .github/workflows/release.yml | 4 +- test_error_handling.py | 92 +++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 test_error_handling.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4d29520..2dab078 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -135,7 +135,7 @@ jobs: echo "Auto-merge enabled for PR: $PR_URL" env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GH_TOKEN }} - name: Build package run: poetry build @@ -150,7 +150,7 @@ jobs: files: dist/* generate_release_notes: true env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} - name: Publish to PyPI env: diff --git a/test_error_handling.py b/test_error_handling.py new file mode 100644 index 0000000..f64c24e --- /dev/null +++ b/test_error_handling.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Test script to verify error handling in CodeQL installer.""" + +from unittest.mock import patch +from urllib.error import URLError + +from codeql_wrapper.infrastructure.codeql_installer import CodeQLInstaller + +def test_get_latest_version_exception(): + """Test that get_latest_version raises proper exceptions.""" + installer = CodeQLInstaller() + + # Test with network error + with patch("codeql_wrapper.infrastructure.codeql_installer.urlopen") as mock_urlopen: + mock_urlopen.side_effect = URLError("Network error") + + try: + installer.get_latest_version() + print("❌ FAIL: Expected exception but none was raised") + return False + except Exception as e: + if "Unable to fetch latest CodeQL version" in str(e): + print("✅ PASS: Correct exception raised for network error") + else: + print(f"❌ FAIL: Wrong exception message: {e}") + return False + + # Test with HTTP error + from unittest.mock import Mock + with patch("codeql_wrapper.infrastructure.codeql_installer.urlopen") as mock_urlopen: + mock_response = Mock() + mock_response.status = 404 + mock_urlopen.return_value.__enter__ = Mock(return_value=mock_response) + mock_urlopen.return_value.__exit__ = Mock(return_value=None) + + try: + installer.get_latest_version() + print("❌ FAIL: Expected exception but none was raised") + return False + except Exception as e: + if "GitHub API returned status 404" in str(e): + print("✅ PASS: Correct exception raised for HTTP error") + else: + print(f"❌ FAIL: Wrong exception message: {e}") + return False + + return True + +def test_install_propagates_exception(): + """Test that install() propagates exceptions correctly.""" + installer = CodeQLInstaller() + + # Test with network error during version fetch + with patch("codeql_wrapper.infrastructure.codeql_installer.urlopen") as mock_urlopen: + mock_urlopen.side_effect = URLError("Network error") + + try: + installer.install(version=None) # This should trigger get_latest_version + print("❌ FAIL: Expected exception but none was raised") + return False + except Exception as e: + if "Unable to fetch latest CodeQL version" in str(e): + print("✅ PASS: Install correctly propagates version fetch exception") + else: + print(f"❌ FAIL: Wrong exception message: {e}") + return False + + return True + +def main(): + """Run all tests.""" + print("Testing CodeQL installer error handling...") + + tests = [ + test_get_latest_version_exception, + test_install_propagates_exception, + ] + + passed = 0 + total = len(tests) + + for test in tests: + if test(): + passed += 1 + print() + + print(f"Results: {passed}/{total} tests passed") + return passed == total + +if __name__ == "__main__": + import sys + sys.exit(0 if main() else 1) From 9204e595270f9ea76fdcbc9af1e8b5b1aa3b106e Mon Sep 17 00:00:00 2001 From: Mateus Perfigao Domiciano Date: Mon, 7 Jul 2025 23:26:39 -0300 Subject: [PATCH 2/2] Remove test script for CodeQL installer error handling --- test_error_handling.py | 92 ------------------------------------------ 1 file changed, 92 deletions(-) delete mode 100644 test_error_handling.py diff --git a/test_error_handling.py b/test_error_handling.py deleted file mode 100644 index f64c24e..0000000 --- a/test_error_handling.py +++ /dev/null @@ -1,92 +0,0 @@ -#!/usr/bin/env python3 -"""Test script to verify error handling in CodeQL installer.""" - -from unittest.mock import patch -from urllib.error import URLError - -from codeql_wrapper.infrastructure.codeql_installer import CodeQLInstaller - -def test_get_latest_version_exception(): - """Test that get_latest_version raises proper exceptions.""" - installer = CodeQLInstaller() - - # Test with network error - with patch("codeql_wrapper.infrastructure.codeql_installer.urlopen") as mock_urlopen: - mock_urlopen.side_effect = URLError("Network error") - - try: - installer.get_latest_version() - print("❌ FAIL: Expected exception but none was raised") - return False - except Exception as e: - if "Unable to fetch latest CodeQL version" in str(e): - print("✅ PASS: Correct exception raised for network error") - else: - print(f"❌ FAIL: Wrong exception message: {e}") - return False - - # Test with HTTP error - from unittest.mock import Mock - with patch("codeql_wrapper.infrastructure.codeql_installer.urlopen") as mock_urlopen: - mock_response = Mock() - mock_response.status = 404 - mock_urlopen.return_value.__enter__ = Mock(return_value=mock_response) - mock_urlopen.return_value.__exit__ = Mock(return_value=None) - - try: - installer.get_latest_version() - print("❌ FAIL: Expected exception but none was raised") - return False - except Exception as e: - if "GitHub API returned status 404" in str(e): - print("✅ PASS: Correct exception raised for HTTP error") - else: - print(f"❌ FAIL: Wrong exception message: {e}") - return False - - return True - -def test_install_propagates_exception(): - """Test that install() propagates exceptions correctly.""" - installer = CodeQLInstaller() - - # Test with network error during version fetch - with patch("codeql_wrapper.infrastructure.codeql_installer.urlopen") as mock_urlopen: - mock_urlopen.side_effect = URLError("Network error") - - try: - installer.install(version=None) # This should trigger get_latest_version - print("❌ FAIL: Expected exception but none was raised") - return False - except Exception as e: - if "Unable to fetch latest CodeQL version" in str(e): - print("✅ PASS: Install correctly propagates version fetch exception") - else: - print(f"❌ FAIL: Wrong exception message: {e}") - return False - - return True - -def main(): - """Run all tests.""" - print("Testing CodeQL installer error handling...") - - tests = [ - test_get_latest_version_exception, - test_install_propagates_exception, - ] - - passed = 0 - total = len(tests) - - for test in tests: - if test(): - passed += 1 - print() - - print(f"Results: {passed}/{total} tests passed") - return passed == total - -if __name__ == "__main__": - import sys - sys.exit(0 if main() else 1)