Skip to content

Conversation

Perdiga
Copy link
Collaborator

@Perdiga Perdiga commented Jul 8, 2025

No description provided.

@Copilot Copilot AI review requested due to automatic review settings July 8, 2025 02:24
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a manual test script for CodeQL installer error handling and updates the GitHub Actions workflow to use a custom GH_TOKEN secret.

  • Introduces test_error_handling.py with manual test functions for network and HTTP error scenarios.
  • Modifies .github/workflows/release.yml to replace GITHUB_TOKEN with GH_TOKEN.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test_error_handling.py Added manual tests for get_latest_version and install error handling.
.github/workflows/release.yml Switched environment variables from GITHUB_TOKEN to GH_TOKEN.
Comments suppressed due to low confidence (2)

.github/workflows/release.yml:138

  • Ensure that the secret GH_TOKEN is defined in the repository settings or switch back to the built-in GITHUB_TOKEN to avoid authentication failures during auto-merge steps.
        GH_TOKEN: ${{ secrets.GH_TOKEN }}

.github/workflows/release.yml:153

  • Mapping secrets.GH_TOKEN to GITHUB_TOKEN may be confusing and could lead to a missing token; consider aligning the environment variable name with your secret or using the default GITHUB_TOKEN secret.
        GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}

Comment on lines 4 to 92
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)
Copy link
Preview

Copilot AI Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using a testing framework like pytest or unittest with assert statements instead of manual prints and return values to integrate with standard test runners and improve readability.

Suggested change
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)
import unittest
from unittest.mock import patch
from urllib.error import URLError
from codeql_wrapper.infrastructure.codeql_installer import CodeQLInstaller
class TestCodeQLInstaller(unittest.TestCase):
"""Unit tests for CodeQLInstaller error handling."""
def test_get_latest_version_exception(self):
"""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")
with self.assertRaises(Exception) as context:
installer.get_latest_version()
self.assertIn("Unable to fetch latest CodeQL version", str(context.exception))
# 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)
with self.assertRaises(Exception) as context:
installer.get_latest_version()
self.assertIn("GitHub API returned status 404", str(context.exception))
def test_install_propagates_exception(self):
"""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")
with self.assertRaises(Exception) as context:
installer.install(version=None) # This should trigger get_latest_version
self.assertIn("Unable to fetch latest CodeQL version", str(context.exception))
if __name__ == "__main__":
import unittest
unittest.main()

Copilot uses AI. Check for mistakes.

@Perdiga Perdiga merged commit 26f2579 into main Jul 8, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant