# How to debug a unit test with debug gem ## Way 1 Build the Ruby. ``` $ cd ~/git/ruby/ruby $ ./configure ... $ make -j$(nproc) ``` Run a unit test with `RUN_OPTS=-rdebug/start`. ``` $ make test-all V=1 TESTS="-v test/mkmf/test_flags.rb -n TestMkmfFlags#test_valid_warnflags" RUN_OPTS=-rdebug/start ... (rdbg) b TestMkmfFlags#test_valid_warnflags # break command Unknown constant name: "TestMkmfFlags" #0 BP - Method (pending) TestMkmfFlags#test_valid_warnflags (rdbg) c # continue command ... ``` ## Way 2 Prepare Ruby and the debug gem. ``` $ mkdir ~/git/ruby $ git clone https://github.com/ruby/ruby.git $ git clone https://github.com/ruby/debug.git ``` Then compile the debug gem. The debug.so is built. ``` $ cd ~/git/ruby/debug $ bundle install $ bundle exec rake compile $ ls -l lib/debug/debug.so -rwxr-xr-x. 1 jaruga jaruga 114768 May 25 15:26 lib/debug/debug.so* ``` Build the Ruby. ``` $ cd ~/git/ruby/ruby $ ./configure ... $ make -j$(nproc) ``` Here is an example of running a unit test. ``` $ make test-all V=1 TESTS="-v test/mkmf/test_flags.rb -n TestMkmfFlags#test_valid_warnflags" ``` Modify the unit test file to debug with "debug" gem like this. ``` $ git diff diff --git a/test/mkmf/test_flags.rb b/test/mkmf/test_flags.rb index aedf06b610..6da6e3bfb1 100644 --- a/test/mkmf/test_flags.rb +++ b/test/mkmf/test_flags.rb @@ -3,6 +3,9 @@ class TestMkmfFlags < TestMkmf def test_valid_warnflags + require_relative '/home/jaruga/git/ruby/debug/lib/debug' + binding.break + val = $extmk warnflags = $warnflags makefile = mkmf do ``` Then you can load the debug gem in the test case. ``` $ make test-all V=1 TESTS="-v test/mkmf/test_flags.rb -n TestMkmfFlags#test_valid_warnflags" exec ./miniruby -I./lib -I. -I.ext/common ./tool/runruby.rb --extout=.ext -- --disable-gems "./test/runner.rb" --ruby="./miniruby -I./lib -I. -I.ext/common ./tool/runruby.rb --extout=.ext -- --disable-gems" --excludes-dir=./test/excludes --name=!/memory_leak/ -v test/mkmf/test_flags.rb -n TestMkmfFlags#test_valid_warnflags Run options: --seed=34032 "--ruby=./miniruby -I./lib -I. -I.ext/common ./tool/runruby.rb --extout=.ext -- --disable-gems" --excludes-dir=./test/excludes -v -n TestMkmfFlags#test_valid_warnflags # Running tests: [1/0] TestMkmfFlags#test_valid_warnflags[2, 11] in ~/var/git/ruby/ruby/test/mkmf/test_flags.rb 2| require_relative 'base' 4| class TestMkmfFlags < TestMkmf 5| def test_valid_warnflags 6| require_relative '/home/jaruga/git/ruby/debug/lib/debug' => 7| binding.break 8| 9| val = $extmk 11| makefile = mkmf do =>#0 TestMkmfFlags#test_valid_warnflags at ~/var/git/ruby/ruby/test/mkmf/test_flags.rb:7 #1 Test::Unit::TestCase#run_test(name=:test_valid_warnflags) at ~/var/git/ruby/ruby/tool/lib/test/unit/testcase.rb:200 # and 24 frames (use `bt' command for all frames) ```