views:

184

answers:

3

I have an emergent bug that I've got to track down tomorrow. I know a previous hg revision which was good so I'm thinking about using hg bisect.

However, I'm on Windows and don't want to get into DOS scripting.

Ideally, I'd be able to write a Python unit test and have hg bisect use that. This is my first attempt.

bisector.py

#!/usr/bin/env python

import sys
import unittest

class TestCase(unittest.TestCase):

    def test(self):
        #raise Exception('Exception for testing.')
        #self.fail("Failure for testing.")
        pass


def main():
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestCase)
    result = unittest.TestResult()
    suite.run(result)

    if result.errors:
        # Skip the revision
        return 125

    if result.wasSuccessful():
        return 0
    else:
        return 1


if '__main__' == __name__:
    sys.exit(main())

Perhaps I could then run:

hg bisect --reset
hg bisect --bad
hg bisect --good -r 1
hg bisect --command=bisector.py

Is there a better way of doing it? Thanks for any advice.

+3  A: 

I think you can remove your main() function and use the following block to run the tests:

if __name__ == '__main__':
   unittest.main()

The call to unittest.main() will run the tests it finds in this file and exit with an appropriate status code depending on whether all the tests pass or fail.

Will McCutchen
+5  A: 

Thanks to all, especially to Will McCutchen. The solution that worked best is below.

bisector.py

#!/usr/bin/env python

import unittest

class TestCase(unittest.TestCase):

    def test(self):
        # Raise an assertion error to mark the revision as bad
        pass


if '__main__' == __name__:
    unittest.main()

The hard part was getting the hg bisect commands right:

hg update tip
hg bisect --reset
hg bisect --bad
hg bisect --good 0
hg bisect --command ./bisector.py

or on Windows, the last command is:

hg bisect --command bisector.py
blokeley
Exactly what I was searching for, thanks! :)
mizipzor
A: 

In case you have some unix tools at your disposal, note that 'grep' sets its exit status in a useful way. So if your unit test prints "PASS" when it passes, you can do:

hg bisect -c './unittest | grep PASS'

and that'll work pretty darn well.

awwaiid
Good idea. Unfortunately this project is on Windows but your suggestion is worth knowing. Thanks.
blokeley
There are windows equivalents (cygwin and standalone) that will do the same thing :)
awwaiid