Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21de9fc

Browse files
committed
Add tests for ignore and unignore module debugger commands
1 parent 0ab1113 commit 21de9fc

1 file changed

Lines changed: 313 additions & 1 deletion

File tree

tests/test_debugger.py

Lines changed: 313 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
import os
88
import sys
99
import platform
10+
from pathlib import Path
1011

11-
from tempfile import NamedTemporaryFile
12+
from tempfile import NamedTemporaryFile, TemporaryDirectory
1213
from textwrap import dedent
1314
from unittest.mock import patch
1415

@@ -625,3 +626,314 @@ def simple_f():
625626
child.expect("ipdb>")
626627

627628
child.close()
629+
630+
631+
@skip_win32
632+
def test_ignore_module_basic_functionality():
633+
"""Test basic ignore/unignore functionality and error handling."""
634+
import pexpect
635+
636+
env = os.environ.copy()
637+
env["IPY_TEST_SIMPLE_PROMPT"] = "1"
638+
639+
with TemporaryDirectory() as temp_dir:
640+
main_path = create_test_modules(temp_dir)
641+
642+
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir)
643+
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE
644+
child.expect("ipdb>")
645+
646+
# Test listing modules when none are ignored
647+
child.sendline("ignore_module")
648+
child.expect_exact("No modules are currently ignored.")
649+
child.expect("ipdb>")
650+
651+
# Test ignoring a module
652+
child.sendline("ignore_module level2_module")
653+
child.expect("ipdb>")
654+
655+
# Test listing ignored modules
656+
child.sendline("ignore_module")
657+
child.expect_exact("Currently ignored modules: ['level2_module']")
658+
child.expect("ipdb>")
659+
660+
# Test wildcard pattern
661+
child.sendline("ignore_module testpkg.*")
662+
child.expect("ipdb>")
663+
664+
child.sendline("ignore_module")
665+
child.expect_exact("Currently ignored modules: ['level2_module', 'testpkg.*']")
666+
child.expect("ipdb>")
667+
668+
# Test error handling - removing non-existent module
669+
child.sendline("unignore_module nonexistent")
670+
child.expect_exact("Module nonexistent is not currently ignored")
671+
child.expect("ipdb>")
672+
673+
# Test successful removal
674+
child.sendline("unignore_module level2_module")
675+
child.expect("ipdb>")
676+
677+
child.sendline("ignore_module")
678+
child.expect_exact("Currently ignored modules: ['testpkg.*']")
679+
child.expect("ipdb>")
680+
681+
# Test removing already removed module
682+
child.sendline("unignore_module level2_module")
683+
child.expect_exact("Module level2_module is not currently ignored")
684+
child.expect("ipdb>")
685+
686+
# Remove wildcard pattern
687+
child.sendline("unignore_module testpkg.*")
688+
child.expect("ipdb>")
689+
690+
child.sendline("ignore_module")
691+
child.expect_exact("No modules are currently ignored.")
692+
child.expect("ipdb>")
693+
694+
child.sendline("continue")
695+
child.close()
696+
697+
698+
# Helper function for creating temporary modules
699+
def create_test_modules(temp_dir):
700+
"""Create a comprehensive module hierarchy for testing all debugger commands."""
701+
702+
temp_path = Path(temp_dir)
703+
704+
# Create package structure for wildcard testing
705+
package_dir = temp_path / "testpkg"
706+
package_dir.mkdir()
707+
708+
# Package __init__.py
709+
(package_dir / "__init__.py").write_text("# Test package")
710+
711+
# testpkg/submod1.py
712+
(package_dir / "submod1.py").write_text(
713+
dedent(
714+
"""
715+
def submod1_func():
716+
x = 1
717+
y = 2
718+
return x + y
719+
"""
720+
)
721+
)
722+
723+
# testpkg/submod2.py
724+
(package_dir / "submod2.py").write_text(
725+
dedent(
726+
"""
727+
def submod2_func():
728+
z = 10
729+
return z * 2
730+
"""
731+
)
732+
)
733+
734+
# Level 1 (top level module)
735+
(temp_path / "level1_module.py").write_text(
736+
dedent(
737+
"""
738+
from level2_module import level2_func
739+
740+
def level1_func():
741+
return level2_func()
742+
"""
743+
)
744+
)
745+
746+
# Level 2 (middle level module)
747+
(temp_path / "level2_module.py").write_text(
748+
dedent(
749+
"""
750+
from level3_module import level3_func
751+
from testpkg.submod1 import submod1_func
752+
from testpkg.submod2 import submod2_func
753+
754+
def level2_func():
755+
# Call package functions for step/next testing
756+
result1 = submod1_func()
757+
result2 = submod2_func()
758+
return level3_func() + result1 + result2
759+
"""
760+
)
761+
)
762+
763+
# Level 3 (bottom level with debugger)
764+
(temp_path / "level3_module.py").write_text(
765+
dedent(
766+
"""
767+
from level4_module import level4_func
768+
769+
from IPython.core.debugger import set_trace
770+
771+
def level3_func():
772+
set_trace()
773+
pass
774+
result = level4_func()
775+
return result
776+
"""
777+
)
778+
)
779+
780+
# Level 4 (bottom level with debugger)
781+
(temp_path / "level4_module.py").write_text(
782+
dedent(
783+
"""
784+
def level4_func():
785+
a = 70
786+
b = 30
787+
return a + b
788+
"""
789+
)
790+
)
791+
792+
# Main runner
793+
main_path = temp_path / "main_runner.py"
794+
main_path.write_text(
795+
dedent(
796+
"""
797+
import sys
798+
sys.path.insert(0, '.')
799+
from level1_module import level1_func
800+
801+
if __name__ == "__main__":
802+
result = level1_func()
803+
print(f"Final result: {result}")
804+
"""
805+
)
806+
)
807+
808+
return str(main_path)
809+
810+
811+
@skip_win32
812+
def test_ignore_module_all_commands():
813+
"""Comprehensive test for all debugger commands (up/down/step/next) with ignore functionality."""
814+
import pexpect
815+
816+
env = os.environ.copy()
817+
env["IPY_TEST_SIMPLE_PROMPT"] = "1"
818+
819+
with TemporaryDirectory() as temp_dir:
820+
main_path = create_test_modules(temp_dir)
821+
822+
# Test UP and DOWN commands
823+
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir)
824+
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE
825+
child.expect("ipdb>")
826+
827+
# Test up without ignores (baseline)
828+
child.sendline("up")
829+
child.expect("ipdb>")
830+
child.sendline("__name__")
831+
child.expect_exact("level2_module")
832+
child.expect("ipdb>")
833+
834+
# Reset position
835+
child.sendline("down")
836+
child.expect("ipdb>")
837+
838+
# Test up with single module ignore
839+
child.sendline("ignore_module level2_module")
840+
child.expect("ipdb>")
841+
child.sendline("up")
842+
child.expect_exact(
843+
"[... skipped 1 frame(s): 0 hidden frames + 1 ignored modules]"
844+
)
845+
child.expect("ipdb>")
846+
child.sendline("__name__")
847+
child.expect_exact("level1_module")
848+
child.expect("ipdb>")
849+
850+
# Test up with wildcard ignore
851+
child.sendline("down")
852+
child.expect_exact(
853+
"[... skipped 1 frame(s): 0 hidden frames + 1 ignored modules]"
854+
)
855+
child.expect("ipdb>")
856+
child.sendline("unignore_module level2_module")
857+
child.expect("ipdb>")
858+
child.sendline("ignore_module level*")
859+
child.expect("ipdb>")
860+
child.sendline("up")
861+
child.expect_exact(
862+
"[... skipped 2 frame(s): 0 hidden frames + 2 ignored modules]"
863+
)
864+
child.expect("ipdb>")
865+
child.sendline("__name__")
866+
child.expect_exact("__main__")
867+
child.expect("ipdb>")
868+
869+
child.sendline("continue")
870+
child.close()
871+
872+
# Test STEP command
873+
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir)
874+
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE
875+
child.expect("ipdb>")
876+
877+
# Test step without ignores (should step into module)
878+
child.sendline("until 9")
879+
child.expect("ipdb>")
880+
child.sendline("step")
881+
child.expect("ipdb>")
882+
child.sendline("__name__")
883+
child.expect_exact("level4_module")
884+
child.expect("ipdb>")
885+
886+
child.sendline("continue")
887+
child.close()
888+
889+
# Test step with single module ignore
890+
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir)
891+
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE
892+
child.expect("ipdb>")
893+
894+
child.sendline("ignore_module level4_module")
895+
child.expect("ipdb>")
896+
child.sendline("until 9")
897+
child.expect("ipdb>")
898+
child.sendline("step")
899+
child.expect_exact("[... skipped 1 ignored module(s)]")
900+
child.expect("ipdb>")
901+
child.sendline("__name__")
902+
child.expect_exact("level3_module")
903+
child.expect("ipdb>")
904+
905+
child.sendline("continue")
906+
child.close()
907+
908+
# Test NEXT command
909+
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir)
910+
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE
911+
child.expect("ipdb>")
912+
913+
# Test next without ignores
914+
child.sendline("until 9")
915+
child.expect("ipdb>")
916+
child.sendline("next")
917+
child.expect("ipdb>")
918+
child.sendline("__name__")
919+
child.expect_exact("level3_module")
920+
child.expect("ipdb>")
921+
922+
child.sendline("continue")
923+
child.close()
924+
925+
# Test next with module ignore
926+
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir)
927+
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE
928+
child.expect("ipdb>")
929+
930+
child.sendline("ignore_module level2_module")
931+
child.expect("ipdb>")
932+
child.sendline("return")
933+
child.expect("ipdb>")
934+
child.sendline("next")
935+
child.expect_exact("[... skipped 1 ignored module(s)]")
936+
child.expect("ipdb>")
937+
938+
child.sendline("continue")
939+
child.close()

0 commit comments

Comments
 (0)