Ticket #57: testflags.patch

File testflags.patch, 3.8 KB (added by dustin, 5 years ago)

support for test flags

  • buildbot/test/runutils.py

    Patch to add 'flags' that can be set in BuildSteps and tested for in
    the test harness.  These are useful in determining which steps have
    and have not run.
    old new  
    413413    def filterArgs(self, args): 
    414414        # this can be overridden 
    415415        return args 
     416 
     417# ---------------------------------------- 
     418 
     419_flags = {} 
     420 
     421def setTestFlag(flagname, value): 
     422    _flags[flagname] = value 
     423 
     424class SetTestFlagStep(BuildStep): 
     425    """ 
     426    A special BuildStep to set a named flag; this can be used with the 
     427    TestFlagMixin to monitor what has and has not run in a particular 
     428    configuration. 
     429    """ 
     430    def __init__(self, flagname='flag', value=1, **kwargs): 
     431        BuildStep.__init__(self, **kwargs) 
     432        self.flagname = flagname 
     433        self.value = value 
     434 
     435    def start(self): 
     436        _flags[self.flagname] = self.value 
     437        self.finished(builder.SUCCESS) 
     438 
     439class TestFlagMixin: 
     440    def clearFlags(self): 
     441        """ 
     442        Set up for a test by clearing all flags; call this from your test 
     443        function. 
     444        """ 
     445        _flags.clear() 
     446 
     447    def failIfFlagSet(self, flagname, msg=None): 
     448        if not msg: msg = "flag '%s' is set" % flagname 
     449        self.failIf(_flags.has_key(flagname), msg=msg) 
     450 
     451    def failIfFlagNotSet(self, flagname, msg=None): 
     452        if not msg: msg = "flag '%s' is not set" % flagname 
     453        self.failUnless(_flags.has_key(flagname), msg=msg) 
     454 
     455    def getFlag(self, flagname): 
     456        self.failIfFlagNotSet(flagname, "flag '%s' not set" % flagname) 
     457        return _flags.get(flagname) 
  • buildbot/test/test_run.py

    old new  
    1010from buildbot.status import builder 
    1111from buildbot.process.base import BuildRequest 
    1212 
    13 from buildbot.test.runutils import RunMixin, rmtree 
     13from buildbot.test.runutils import RunMixin, TestFlagMixin, rmtree 
    1414 
    1515config_base = """ 
    1616from buildbot.process import factory 
     
    506506        d = self.master.loadConfig(config_4_newbuilder) 
    507507        return d 
    508508 
     509config_test_flag = config_base + """ 
     510from buildbot.scheduler import Scheduler 
     511c['schedulers'] = [Scheduler('quick', None, 0.1, ['dummy'])] 
     512 
     513from buildbot.test.runutils import SetTestFlagStep 
     514f3 = factory.BuildFactory([ 
     515    s(SetTestFlagStep, flagname='foo', value='bar'), 
     516    ]) 
     517 
     518c['builders'] = [{'name': 'dummy', 'slavename': 'bot1', 
     519                  'builddir': 'dummy', 'factory': f3}] 
     520""" 
     521 
     522class TestFlag(RunMixin, TestFlagMixin, unittest.TestCase): 
     523    """Test for the TestFlag functionality in runutils""" 
     524    def testTestFlag(self): 
     525        m = self.master 
     526        m.loadConfig(config_test_flag) 
     527        m.readConfig = True 
     528        m.startService() 
     529 
     530        c = changes.Change("bob", ["Makefile", "foo/bar.c"], "changed stuff") 
     531        m.change_svc.addChange(c) 
     532 
     533        d = self.connectSlave() 
     534        d.addCallback(self._testTestFlag_1) 
     535        return d 
     536 
     537    def _testTestFlag_1(self, res): 
     538        d = defer.Deferred() 
     539        reactor.callLater(0.5, d.callback, None) 
     540        d.addCallback(self._testTestFlag_2) 
     541        return d 
     542 
     543    def _testTestFlag_2(self, res): 
     544        self.failUnlessEqual(self.getFlag('foo'), 'bar') 
     545 
    509546# TODO: test everything, from Change submission to Scheduler to Build to 
    510547# Status. Use all the status types. Specifically I want to catch recurrences 
    511548# of the bug where I forgot to make Waterfall inherit from StatusReceiver