-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(debug): fix --debug flag and associated tests
- Fix --debug flag - Remove duplicate block - Undo linting - Remove test_debug file - Tests with runner.invoke 1st pass - Passing tests with patchings - Fix patchings - Modify fetch_database - Merge branch 'main' into fix_debug_flag
- Loading branch information
1 parent
fcd8102
commit 7bebee7
Showing
3 changed files
with
102 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import json | ||
import logging | ||
import os | ||
import sys | ||
import shutil | ||
import tempfile | ||
import unittest | ||
|
@@ -15,7 +17,8 @@ | |
from safety import cli | ||
from safety.models import CVE, SafetyRequirement, Severity, Vulnerability | ||
from safety.util import Package, SafetyContext | ||
|
||
from safety.auth.models import Auth | ||
from safety_schemas.models.base import AuthenticationType | ||
|
||
def get_vulnerability(vuln_kwargs=None, cve_kwargs=None, pkg_kwargs=None): | ||
vuln_kwargs = {} if vuln_kwargs is None else vuln_kwargs | ||
|
@@ -69,6 +72,8 @@ def setUp(self): | |
self.runner = CliRunner(mix_stderr=False) | ||
self.output_options = ['screen', 'text', 'json', 'bare'] | ||
self.dirname = os.path.dirname(__file__) | ||
# Set up logging to capture debug output for tests | ||
logging.basicConfig(level=logging.DEBUG, format='%(name)s - %(levelname)s - %(message)s') | ||
|
||
def test_command_line_interface(self): | ||
runner = CliRunner() | ||
|
@@ -513,4 +518,48 @@ def test_license_with_file(self, fetch_database_url): | |
test_filename = os.path.join(dirname, "reqs_4.txt") | ||
result = self.runner.invoke(cli.cli, ['license', '--key', 'foo', '--file', test_filename]) | ||
print(result.stdout) | ||
self.assertEqual(result.exit_code, 0) | ||
self.assertEqual(result.exit_code, 0) | ||
|
||
@patch('safety.auth.cli.get_auth_info', return_value={'email': '[email protected]'}) | ||
@patch.object(Auth, 'is_valid', return_value=True) | ||
@patch('safety.auth.utils.SafetyAuthSession.get_authentication_type', return_value=AuthenticationType.TOKEN) | ||
@patch('builtins.input', lambda *args: '') | ||
@patch('safety.safety.fetch_database', return_value={'vulnerable_packages': []}) | ||
def test_debug_flag(self, mock_get_auth_info, mock_is_valid, mock_get_auth_type, mock_fetch_database): | ||
result = self.runner.invoke(cli.cli, ['--debug', 'scan']) | ||
assert result.exit_code == 0, f"CLI exited with code {result.exit_code} and output: {result.output} and error: {result.stderr}" | ||
assert "for known security issues using default" in result.output | ||
|
||
@patch('safety.auth.cli.get_auth_info', return_value={'email': '[email protected]'}) | ||
@patch.object(Auth, 'is_valid', return_value=True) | ||
@patch('safety.auth.utils.SafetyAuthSession.get_authentication_type', return_value=AuthenticationType.TOKEN) | ||
@patch('builtins.input', lambda *args: '') | ||
@patch('safety.safety.fetch_database', return_value={'vulnerable_packages': []}) | ||
def test_debug_flag_with_value_1(self, mock_get_auth_info, mock_is_valid, mock_get_auth_type, mock_fetch_database): | ||
# Simulate the command line arguments including the preprocessing | ||
sys.argv = ['safety', '--debug', '1', 'scan'] | ||
cli.preprocess_args() # Run the preprocess function to adjust the arguments | ||
|
||
# Extract the preprocessed arguments from sys.argv | ||
preprocessed_args = sys.argv[1:] # Exclude the script name 'safety' | ||
|
||
result = self.runner.invoke(cli.cli, preprocessed_args) | ||
assert result.exit_code == 0, f"CLI exited with code {result.exit_code} and output: {result.output} and error: {result.stderr}" | ||
assert "for known security issues using default" in result.output | ||
|
||
@patch('safety.auth.cli.get_auth_info', return_value={'email': '[email protected]'}) | ||
@patch.object(Auth, 'is_valid', return_value=True) | ||
@patch('safety.auth.utils.SafetyAuthSession.get_authentication_type', return_value=AuthenticationType.TOKEN) | ||
@patch('builtins.input', lambda *args: '') | ||
@patch('safety.safety.fetch_database', return_value={'vulnerable_packages': []}) | ||
def test_debug_flag_with_value_true(self, mock_get_auth_info, mock_is_valid, mock_get_auth_type, mock_fetch_database): | ||
# Simulate the command line arguments including the preprocessing | ||
sys.argv = ['safety', '--debug', 'true', 'scan'] | ||
cli.preprocess_args() # Run the preprocess function to adjust the arguments | ||
|
||
# Extract the preprocessed arguments from sys.argv | ||
preprocessed_args = sys.argv[1:] # Exclude the script name 'safety' | ||
|
||
result = self.runner.invoke(cli.cli, preprocessed_args) | ||
assert result.exit_code == 0, f"CLI exited with code {result.exit_code} and output: {result.output} and error: {result.stderr}" | ||
assert "for known security issues using default" in result.output |