Skip to content

Commit

Permalink
fix #871967, can not use an instance method in uptodate.
Browse files Browse the repository at this point in the history
--HG--
extra : convert_revision : schettino72-20111011013250-e1d4fbxnv50vmydp
  • Loading branch information
schettino72 committed Oct 11, 2011
1 parent 2fd6b51 commit 5f3a5f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
10 changes: 5 additions & 5 deletions doit/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ def get_status(self, task):

# check uptodate bool/callables
checked_uptodate = False
for uptodate in task.uptodate:
for utd, utd_args, utd_kwargs in task.uptodate:
# FIXME control verbosity, check error messages
if hasattr(uptodate, '__call__'):
args = [task, self.get_values(task.name)] + uptodate.args
uptodate_result = uptodate(*args, **uptodate.kwargs)
if hasattr(utd, '__call__'):
args = [task, self.get_values(task.name)] + utd_args
uptodate_result = utd(*args, **utd_kwargs)
else:
uptodate_result = uptodate
uptodate_result = utd

# None means uptodate was not really calculated and should be
# just ignored
Expand Down
14 changes: 6 additions & 8 deletions doit/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,14 @@ def _init_uptodate(self, items):
uptodate = []
for item in items:
if isinstance(item, bool) or item is None:
uptodate.append(item)
uptodate.append((item, None, None))
elif hasattr(item, '__call__'):
item.args = []
item.kwargs = {}
uptodate.append(item)
uptodate.append((item, [], {}))
elif isinstance(item, tuple):
call = item[0]
call.args = list(item[1]) if len(item)>1 else []
call.kwargs = item[2] if len(item)>2 else {}
uptodate.append(call)
args = list(item[1]) if len(item)>1 else []
kwargs = item[2] if len(item)>2 else {}
uptodate.append((call, args, kwargs))
else:
msg = ("%s. task invalid 'uptodate' item '%r'. " +
"Must be bool, None, callable or tuple " +
Expand Down Expand Up @@ -222,7 +220,7 @@ def _expand_calc_dep(self, calc_dep):

def _extend_uptodate(self, uptodate):
"""add/extend uptodate values"""
self.uptodate.extend(uptodate)
self.uptodate.extend(self._init_uptodate(uptodate))


# FIXME should support setup also
Expand Down
23 changes: 13 additions & 10 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,29 @@ class TestTaskUpToDate(object):

def test_FalseRunalways(self):
t = task.Task("Task X", ["taskcmd"], uptodate=[False])
assert t.uptodate == [False]
assert t.uptodate == [(False, None, None)]

def test_NoneIgnored(self):
t = task.Task("Task X", ["taskcmd"], uptodate=[None])
assert t.uptodate == [None]
assert t.uptodate == [(None, None, None)]

def test_callable(self):
def test_callable_function(self):
def custom_check(): return True
t = task.Task("Task X", ["taskcmd"], uptodate=[custom_check])
assert t.uptodate[0] == custom_check
assert t.uptodate[0].args == []
assert t.uptodate[0].kwargs == {}
assert t.uptodate[0] == (custom_check, [], {})

def test_callable_instance_method(self):
class Base(object):
def check(): return True
base = Base()
t = task.Task("Task X", ["taskcmd"], uptodate=[base.check])
assert t.uptodate[0] == (base.check, [], {})

def test_tuple(self):
def custom_check(pos_arg, xxx=None): return True
t = task.Task("Task X", ["taskcmd"],
uptodate=[(custom_check, [123], {'xxx':'yyy'})])
assert t.uptodate[0] == custom_check
assert t.uptodate[0].args == [123]
assert t.uptodate[0].kwargs == {'xxx':'yyy'}
assert t.uptodate[0] == (custom_check, [123], {'xxx':'yyy'})

def test_invalid(self):
pytest.raises(task.InvalidTask,
Expand Down Expand Up @@ -156,7 +159,7 @@ def test_update_deps(self):
assert ['resultX', 'taskY', 'resultY'] == my_task.task_dep
assert set(['calcX', 'calcY']) == my_task.calc_dep
assert ['resultX', 'resultY'] == my_task.result_dep
assert [None, True] == my_task.uptodate
assert [(None, None, None), (True, None, None)] == my_task.uptodate


class TestTask_Getargs(object):
Expand Down
27 changes: 15 additions & 12 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,49 +73,52 @@ def test_invalid(self):

def test_int(self, monkeypatch):
monkeypatch.setattr(tools.time_module, 'time', lambda: 100)
t = task.Task("TaskX", None, uptodate=[tools.timeout(5)])
uptodate = tools.timeout(5)
t = task.Task("TaskX", None, uptodate=[uptodate])

assert False == t.uptodate[0](t, t.values)
assert False == uptodate(t, t.values)
t.execute()
assert 100 == t.values['success-time']

monkeypatch.setattr(tools.time_module, 'time', lambda: 103)
assert True == t.uptodate[0](t, t.values)
assert True == uptodate(t, t.values)

monkeypatch.setattr(tools.time_module, 'time', lambda: 106)
assert False == t.uptodate[0](t, t.values)
assert False == uptodate(t, t.values)


def test_timedelta(self, monkeypatch):
monkeypatch.setattr(tools.time_module, 'time', lambda: 10)
limit = datetime.timedelta(minutes=2)
t = task.Task("TaskX", None, uptodate=[tools.timeout(limit)])
uptodate = tools.timeout(limit)
t = task.Task("TaskX", None, uptodate=[uptodate])

assert False == t.uptodate[0](t, t.values)
assert False == uptodate(t, t.values)
t.execute()
assert 10 == t.values['success-time']

monkeypatch.setattr(tools.time_module, 'time', lambda: 100)
assert True == t.uptodate[0](t, t.values)
assert True == uptodate(t, t.values)

monkeypatch.setattr(tools.time_module, 'time', lambda: 200)
assert False == t.uptodate[0](t, t.values)
assert False == uptodate(t, t.values)


def test_timedelta_big(self, monkeypatch):
monkeypatch.setattr(tools.time_module, 'time', lambda: 10)
limit = datetime.timedelta(days=2, minutes=5)
t = task.Task("TaskX", None, uptodate=[tools.timeout(limit)])
uptodate = tools.timeout(limit)
t = task.Task("TaskX", None, uptodate=[uptodate])

assert False == t.uptodate[0](t, t.values)
assert False == uptodate(t, t.values)
t.execute()
assert 10 == t.values['success-time']

monkeypatch.setattr(tools.time_module, 'time', lambda: 3600 * 30)
assert True == t.uptodate[0](t, t.values)
assert True == uptodate(t, t.values)

monkeypatch.setattr(tools.time_module, 'time', lambda: 3600 * 49)
assert False == t.uptodate[0](t, t.values)
assert False == uptodate(t, t.values)



Expand Down

0 comments on commit 5f3a5f4

Please sign in to comment.