Differences between revisions 3 and 4
Revision 3 as of 2008-06-13 07:51:40
Size: 4013
Editor: abuehl
Comment: using RFC 2606, Section 3, reserved example documentation domain
Revision 4 as of 2009-05-19 19:31:00
Size: 4021
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 40: Line 40:
With the open-source [http://pydev.sourceforge.net/ Eclipse Pydev plugin], you can only launch `hg` directly from within [http://eclipse.org/ Eclipse]. So you need to stop the script before the command you want to debug (as described above) and setup a corresponding launch configuration in Eclipse. With the open-source [[http://pydev.sourceforge.net/|Eclipse Pydev plugin]], you can only launch `hg` directly from within [[http://eclipse.org/|Eclipse]]. So you need to stop the script before the command you want to debug (as described above) and setup a corresponding launch configuration in Eclipse.
Line 66: Line 66:
With the commercially licensed [http://www.fabioz.com/pydev/ Pydev Extensions], you can attach to a running instance of `hg`. This relies on a Pydev supplied module, whose path you need to set up. Amend the Python path in `debug-test` as follows: With the commercially licensed [[http://www.fabioz.com/pydev/|Pydev Extensions]], you can attach to a running instance of `hg`. This relies on a Pydev supplied module, whose path you need to set up. Amend the Python path in `debug-test` as follows:
Line 73: Line 73:
Then you can proceed as described in [http://www.fabioz.com/pydev/manual_adv_remote_debugger.html Pydev's remote debugging instructions]. Beware, though, that it did not work for me when placing `pydevd.settrace()` top-level in `dispatch.py`. It did work within actual commands in `commands.py` and also deeper within the code. Don't despair if Pydev tries to locate `demandimport.py` and cannot find it. Simply set a breakpoint at your desired location and continue the debugger. Then you can proceed as described in [[http://www.fabioz.com/pydev/manual_adv_remote_debugger.html|Pydev's remote debugging instructions]]. Beware, though, that it did not work for me when placing `pydevd.settrace()` top-level in `dispatch.py`. It did work within actual commands in `commands.py` and also deeper within the code. Don't despair if Pydev tries to locate `demandimport.py` and cannot find it. Simply set a breakpoint at your desired location and continue the debugger.

Debugging Tests

It's quite handy to be able to develop test scripts in parallel with the code (test-driven development). Hg's default tests/run-tests.py script described in WritingTests is a little slow for running a single test repeatedly while refining your test and code. And it runs the test in a temporary dir with an ever changing name. This is not useful for manually inspecting the state left by the test, or to run hg from within a debugger on an interim state.

Debugging Shell Tests Scripts

The following script, if placed in hg's tests folder, will run a single, simple shell test script much more quickly and in a predictable location, namely tests/tmp. It does not support test scripts involving servers, however. It assumes that it is run from within tests. Example:

~/dev/hg/tests$ ./debug-test test-guessrenames
...

And here is the debug-test script:

TESTDIR=`pwd`
cd ..; HGDIR=`pwd`; cd tests # want an absolute path
HGRCPATH=$TESTDIR/tmp/hgrc
PYTHONPATH=$HGDIR:$PYTHONPATH
PATH=$HGDIR:$PATH
export HGRCPATH
export PYTHONPATH
export PATH
rm -rf tmp; mkdir tmp; cd tmp
echo "[ui]" >$HGRCPATH
echo "username = John Doe <john@example.com>" >>$HGRCPATH
. ../$1

Hg provides a number of DebuggingFeatures you can use here. For example, you might want to change a particular call from hg to hg --debugger in your testscript.

You can also add echo DEBUG; return instructions anywhere in your test script to stop and inspect the state manually or to run a particular hg command under control of an external debugger (see below). The echo DEBUG part is just a reminder so you don't forget to remove the return again. The hg --cwd option is particularly handy for switching the debuggee to tests/tmp/mytestrepo or wherever.

Using Eclipse Pydev

With the open-source Eclipse Pydev plugin, you can only launch hg directly from within Eclipse. So you need to stop the script before the command you want to debug (as described above) and setup a corresponding launch configuration in Eclipse.

Main module:

${workspace_loc:my-hg-project/hg}

Arguments (... is the command in question):

--cwd ~/dev/hg/tests/tmp/mytestrepo
...

Working dir (I had to switch from the default settings to this to make the debugger work):

${workspace_loc:my-hg-project}

Environment:

HGRCPATH = ~/dev/hg/tests/tmp/hgrc

Using Eclipse Pydev Extensions

With the commercially licensed Pydev Extensions, you can attach to a running instance of hg. This relies on a Pydev supplied module, whose path you need to set up. Amend the Python path in debug-test as follows:

PYDEVDIR=/path/to/eclipse/plugins/org.python.pydev.debug_1.3.17/pysrc
PYTHONPATH=$HGDIR:$PYDEVDIR:$PYTHONPATH

Then you can proceed as described in Pydev's remote debugging instructions. Beware, though, that it did not work for me when placing pydevd.settrace() top-level in dispatch.py. It did work within actual commands in commands.py and also deeper within the code. Don't despair if Pydev tries to locate demandimport.py and cannot find it. Simply set a breakpoint at your desired location and continue the debugger.

If your test script contains something like:

hg stat
...
hg stat

and you want to break on the second instance of hg stat only, you could use a flag file to signal this. I have the following in my debug-test script:

rm /tmp/enable-hg-debugger
DBG() {
    touch /tmp/enable-hg-debugger
}

In the code I then do:

   1 if os.path.exists('/tmp/enable-hg-debugger'):
   2     import pydevd; pydevd.settrace()

and in the script I do:

hg stat
...
DBG; hg stat


CategoryContributing CategoryTesting CategoryHowTo

DebuggingTests (last edited 2011-05-03 21:59:12 by DaleWijnand)