| 8 | |
| 9 | |
| 10 | class TestGetPythonFileAsts(unittest.TestCase): |
| 11 | |
| 12 | def setUp(self): |
| 13 | # Create a temporary directory structure for tests |
| 14 | self.test_dir = tempfile.TemporaryDirectory() |
| 15 | self.base_path = Path(self.test_dir.name) |
| 16 | |
| 17 | # Valid python file |
| 18 | self.valid_file = self.base_path / "valid.py" |
| 19 | self.valid_file.write_text("x = 10", encoding="utf-8") |
| 20 | |
| 21 | # Syntax warning file |
| 22 | self.warning_syntax = self.base_path / "warning_err.py" |
| 23 | self.warning_syntax.write_bytes(b'path = "c:\windows"') |
| 24 | |
| 25 | # Invalid syntax file |
| 26 | self.invalid_syntax = self.base_path / "syntax_err.py" |
| 27 | self.invalid_syntax.write_text("def broken_function(:", encoding="utf-8") |
| 28 | |
| 29 | # Encoding error file |
| 30 | self.encoding_err = self.base_path / "encoding_err.py" |
| 31 | self.encoding_err.write_bytes(b"\xff\xfe\x00\x00") |
| 32 | |
| 33 | # Fixture file (should be skipped) |
| 34 | self.fixture_dir = self.base_path / "tests" / "fixtures" |
| 35 | self.fixture_dir.mkdir(parents=True) |
| 36 | self.fixture_file = self.fixture_dir / "fixture_file.py" |
| 37 | self.fixture_file.write_text("y = 20", encoding="utf-8") |
| 38 | |
| 39 | def tearDown(self): |
| 40 | self.test_dir.cleanup() |
| 41 | |
| 42 | # @patch('pyspector.cli.click.echo') |
| 43 | # @patch('pyspector.cli.click.style', side_effect=lambda msg, fg=None, **kwargs: msg) |
| 44 | def test_get_python_file_asts_handling_default(self): |
| 45 | """Test that by default SyntaxWarnings are ignored and files are included.""" |
| 46 | # Run function with default (enable_syntax_warnings=False) |
| 47 | results = get_python_file_asts(self.base_path) |
| 48 | |
| 49 | # We expect BOTH the valid python file AND the warning file to be in the result |
| 50 | # because the warning is ignored and parsing proceeds. |
| 51 | self.assertEqual(len(results), 2) |
| 52 | filenames = [r["file_path"] for r in results] |
| 53 | self.assertIn("valid.py", filenames) |
| 54 | self.assertIn("warning_err.py", filenames) |
| 55 | |
| 56 | def test_get_python_file_asts_handling_enabled(self): |
| 57 | """Test that when enabled, SyntaxWarnings are treated as errors and files are excluded.""" |
| 58 | # Run function with enable_syntax_warnings=True |
| 59 | results = get_python_file_asts(self.base_path, enable_syntax_warnings=True) |
| 60 | |
| 61 | # We expect ONLY the valid python file to be in the result |
| 62 | # because the warning_err.py triggers an exception and is caught. |
| 63 | self.assertEqual(len(results), 1) |
| 64 | self.assertEqual(results[0]["file_path"], "valid.py") |
| 65 | self.assertEqual(results[0]["content"], "x = 10") |
| 66 | self.assertIn("ast_json", results[0]) |
| 67 |
nothing calls this directly
no outgoing calls
no test coverage detected