| 1 |
import os |
| 2 |
import datetime |
| 3 |
import json |
| 4 |
import operator |
| 5 |
from sys import executable |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from doit import exceptions |
| 10 |
from doit import tools |
| 11 |
from doit import task |
| 12 |
|
| 13 |
|
| 14 |
class TestCreateFolder(object): |
| 15 |
def test_create_folder(self): |
| 16 |
def rm_dir(): |
| 17 |
if os.path.exists(DIR_DEP): |
| 18 |
os.removedirs(DIR_DEP) |
| 19 |
|
| 20 |
DIR_DEP = os.path.join(os.path.dirname(__file__),"parent/child/") |
| 21 |
rm_dir() |
| 22 |
tools.create_folder(DIR_DEP) |
| 23 |
assert os.path.exists(DIR_DEP) |
| 24 |
rm_dir() |
| 25 |
|
| 26 |
def test_error_if_path_is_a_file(self): |
| 27 |
def rm_file(path): |
| 28 |
if os.path.exists(path): |
| 29 |
os.remove(path) |
| 30 |
|
| 31 |
path = os.path.join(os.path.dirname(__file__), "test_create_folder") |
| 32 |
with open(path, 'w') as fp: |
| 33 |
fp.write('testing') |
| 34 |
pytest.raises(OSError, tools.create_folder, path) |
| 35 |
rm_file(path) |
| 36 |
|
| 37 |
|
| 38 |
class TestTitleWithActions(object): |
| 39 |
def test_actions(self): |
| 40 |
t = task.Task("MyName",["MyAction"], title=tools.title_with_actions) |
| 41 |
assert "MyName => Cmd: MyAction" == t.title() |
| 42 |
|
| 43 |
def test_group(self): |
| 44 |
t = task.Task("MyName", None, file_dep=['file_foo'], |
| 45 |
task_dep=['t1','t2'], title=tools.title_with_actions) |
| 46 |
assert "MyName => Group: t1, t2" == t.title() |
| 47 |
|
| 48 |
|
| 49 |
class TestRunOnce(object): |
| 50 |
def test_run(self): |
| 51 |
t = task.Task("TaskX", None, uptodate=[tools.run_once]) |
| 52 |
assert False == tools.run_once(t, t.values) |
| 53 |
t.save_extra_values() |
| 54 |
assert True == tools.run_once(t, t.values) |
| 55 |
|
| 56 |
|
| 57 |
class TestConfigChanged(object): |
| 58 |
def test_invalid_type(self): |
| 59 |
class NotValid(object):pass |
| 60 |
uptodate = tools.config_changed(NotValid()) |
| 61 |
pytest.raises(Exception, uptodate, None, None) |
| 62 |
|
| 63 |
def test_string(self): |
| 64 |
ua = tools.config_changed('a') |
| 65 |
ub = tools.config_changed('b') |
| 66 |
t1 = task.Task("TaskX", None, uptodate=[ua]) |
| 67 |
assert False == ua(t1, t1.values) |
| 68 |
assert False == ub(t1, t1.values) |
| 69 |
t1.save_extra_values() |
| 70 |
assert True == ua(t1, t1.values) |
| 71 |
assert False == ub(t1, t1.values) |
| 72 |
|
| 73 |
def test_unicode(self): |
| 74 |
ua = tools.config_changed({'x': "中文"}) |
| 75 |
ub = tools.config_changed('b') |
| 76 |
t1 = task.Task("TaskX", None, uptodate=[ua]) |
| 77 |
assert False == ua(t1, t1.values) |
| 78 |
assert False == ub(t1, t1.values) |
| 79 |
t1.save_extra_values() |
| 80 |
assert True == ua(t1, t1.values) |
| 81 |
assert False == ub(t1, t1.values) |
| 82 |
|
| 83 |
def test_dict(self): |
| 84 |
ua = tools.config_changed({'x':'a', 'y':1}) |
| 85 |
ub = tools.config_changed({'x':'b', 'y':1}) |
| 86 |
t1 = task.Task("TaskX", None, uptodate=[ua]) |
| 87 |
assert False == ua(t1, t1.values) |
| 88 |
assert False == ub(t1, t1.values) |
| 89 |
t1.save_extra_values() |
| 90 |
assert True == ua(t1, t1.values) |
| 91 |
assert False == ub(t1, t1.values) |
| 92 |
|
| 93 |
def test_nested_dict(self): |
| 94 |
|
| 95 |
|
| 96 |
c1a = tools.config_changed({'x':'a', 'y':{'one':1, 'two':2}}) |
| 97 |
c1b = tools.config_changed({'y':{'two':2, 'one':1}, 'x':'a'}) |
| 98 |
|
| 99 |
t1 = task.Task("TaskX", None, uptodate=[c1a]) |
| 100 |
assert False == c1a(t1, t1.values) |
| 101 |
t1.save_extra_values() |
| 102 |
assert True == c1a(t1, t1.values) |
| 103 |
assert True == c1b(t1, t1.values) |
| 104 |
|
| 105 |
def test_using_custom_encoder(self): |
| 106 |
|
| 107 |
class DatetimeJSONEncoder(json.JSONEncoder): |
| 108 |
def default(self, o): |
| 109 |
if isinstance(o, datetime.datetime): |
| 110 |
return o.isoformat() |
| 111 |
|
| 112 |
ua = tools.config_changed({'a': datetime.datetime(2018, 12, 10, 10, 33, 55, 478421), 'b': 'bb'}, encoder=DatetimeJSONEncoder) |
| 113 |
ub = tools.config_changed({'a': datetime.datetime.now(), 'b': 'bb'}, encoder=DatetimeJSONEncoder) |
| 114 |
t1 = task.Task("TaskX", None, uptodate=[ua]) |
| 115 |
assert ua(t1, t1.values) is False |
| 116 |
assert ub(t1, t1.values) is False |
| 117 |
t1.save_extra_values() |
| 118 |
assert ua(t1, t1.values) is True |
| 119 |
assert ub(t1, t1.values) is False |
| 120 |
|
| 121 |
|
| 122 |
class TestTimeout(object): |
| 123 |
def test_invalid(self): |
| 124 |
pytest.raises(Exception, tools.timeout, "abc") |
| 125 |
|
| 126 |
def test_int(self, monkeypatch): |
| 127 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 100) |
| 128 |
uptodate = tools.timeout(5) |
| 129 |
t = task.Task("TaskX", None, uptodate=[uptodate]) |
| 130 |
|
| 131 |
assert False == uptodate(t, t.values) |
| 132 |
t.save_extra_values() |
| 133 |
assert 100 == t.values['success-time'] |
| 134 |
|
| 135 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 103) |
| 136 |
assert True == uptodate(t, t.values) |
| 137 |
|
| 138 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 106) |
| 139 |
assert False == uptodate(t, t.values) |
| 140 |
|
| 141 |
|
| 142 |
def test_timedelta(self, monkeypatch): |
| 143 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 10) |
| 144 |
limit = datetime.timedelta(minutes=2) |
| 145 |
uptodate = tools.timeout(limit) |
| 146 |
t = task.Task("TaskX", None, uptodate=[uptodate]) |
| 147 |
|
| 148 |
assert False == uptodate(t, t.values) |
| 149 |
t.save_extra_values() |
| 150 |
assert 10 == t.values['success-time'] |
| 151 |
|
| 152 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 100) |
| 153 |
assert True == uptodate(t, t.values) |
| 154 |
|
| 155 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 200) |
| 156 |
assert False == uptodate(t, t.values) |
| 157 |
|
| 158 |
|
| 159 |
def test_timedelta_big(self, monkeypatch): |
| 160 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 10) |
| 161 |
limit = datetime.timedelta(days=2, minutes=5) |
| 162 |
uptodate = tools.timeout(limit) |
| 163 |
t = task.Task("TaskX", None, uptodate=[uptodate]) |
| 164 |
|
| 165 |
assert False == uptodate(t, t.values) |
| 166 |
t.save_extra_values() |
| 167 |
assert 10 == t.values['success-time'] |
| 168 |
|
| 169 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 3600 * 30) |
| 170 |
assert True == uptodate(t, t.values) |
| 171 |
|
| 172 |
monkeypatch.setattr(tools.time_module, 'time', lambda: 3600 * 49) |
| 173 |
assert False == uptodate(t, t.values) |
| 174 |
|
| 175 |
|
| 176 |
@pytest.fixture |
| 177 |
def checked_file(request): |
| 178 |
fname = 'mytmpfile' |
| 179 |
file_ = open(fname, 'a') |
| 180 |
file_.close() |
| 181 |
def remove(): |
| 182 |
os.remove(fname) |
| 183 |
request.addfinalizer(remove) |
| 184 |
return fname |
| 185 |
|
| 186 |
|
| 187 |
class TestCheckTimestampUnchanged(object): |
| 188 |
|
| 189 |
def test_time_selection(self): |
| 190 |
check = tools.check_timestamp_unchanged('check_atime', 'atime') |
| 191 |
assert 'st_atime' == check._timeattr |
| 192 |
|
| 193 |
check = tools.check_timestamp_unchanged('check_ctime', 'ctime') |
| 194 |
assert 'st_ctime' == check._timeattr |
| 195 |
|
| 196 |
check = tools.check_timestamp_unchanged('check_mtime', 'mtime') |
| 197 |
assert 'st_mtime' == check._timeattr |
| 198 |
|
| 199 |
pytest.raises( |
| 200 |
ValueError, |
| 201 |
tools.check_timestamp_unchanged, 'check_invalid_time', 'foo') |
| 202 |
|
| 203 |
def test_file_missing(self): |
| 204 |
check = tools.check_timestamp_unchanged('no_such_file') |
| 205 |
t = task.Task("TaskX", None, uptodate=[check]) |
| 206 |
|
| 207 |
task_values = {check._key: 1} |
| 208 |
pytest.raises(OSError, check, t, task_values) |
| 209 |
|
| 210 |
def test_op_ge(self, monkeypatch, checked_file): |
| 211 |
check = tools.check_timestamp_unchanged(checked_file,cmp_op=operator.ge) |
| 212 |
t = task.Task("TaskX", None, uptodate=[check]) |
| 213 |
|
| 214 |
|
| 215 |
assert False == check(t, t.values) |
| 216 |
|
| 217 |
|
| 218 |
t.save_extra_values() |
| 219 |
assert True == check(t, t.values) |
| 220 |
|
| 221 |
|
| 222 |
future_time = list(t.values.values())[0] + 100 |
| 223 |
monkeypatch.setattr(check, '_get_time', lambda: future_time) |
| 224 |
assert False == check(t, t.values) |
| 225 |
|
| 226 |
|
| 227 |
def test_op_bad_custom(self, monkeypatch, checked_file): |
| 228 |
|
| 229 |
def bad_op(prev_time, current_time): |
| 230 |
raise Exception('oops') |
| 231 |
|
| 232 |
check = tools.check_timestamp_unchanged(checked_file, cmp_op=bad_op) |
| 233 |
t = task.Task("TaskX", None, uptodate=[check]) |
| 234 |
|
| 235 |
task_values = {check._key: 1} |
| 236 |
pytest.raises(Exception, check, t, task_values) |
| 237 |
|
| 238 |
def test_multiple_checks(self): |
| 239 |
|
| 240 |
|
| 241 |
check_a = tools.check_timestamp_unchanged('check_multi', 'atime') |
| 242 |
check_m = tools.check_timestamp_unchanged('check_multi', 'mtime') |
| 243 |
assert check_a._key != check_m._key |
| 244 |
|
| 245 |
|
| 246 |
class TestLongRunning(object): |
| 247 |
def test_success(self): |
| 248 |
TEST_PATH = os.path.dirname(__file__) |
| 249 |
PROGRAM = "%s %s/sample_process.py" % (executable, TEST_PATH) |
| 250 |
my_action = tools.LongRunning(PROGRAM + " please fail") |
| 251 |
got = my_action.execute() |
| 252 |
assert got is None |
| 253 |
|
| 254 |
def test_ignore_keyboard_interrupt(self, monkeypatch): |
| 255 |
my_action = tools.LongRunning('') |
| 256 |
class FakeRaiseInterruptProcess(object): |
| 257 |
def __init__(self, *args, **kwargs): |
| 258 |
pass |
| 259 |
def wait(self): |
| 260 |
raise KeyboardInterrupt() |
| 261 |
monkeypatch.setattr(tools.subprocess, 'Popen', FakeRaiseInterruptProcess) |
| 262 |
got = my_action.execute() |
| 263 |
assert got is None |
| 264 |
|
| 265 |
class TestInteractive(object): |
| 266 |
def test_fail(self): |
| 267 |
TEST_PATH = os.path.dirname(__file__) |
| 268 |
PROGRAM = "%s %s/sample_process.py" % (executable, TEST_PATH) |
| 269 |
my_action = tools.Interactive(PROGRAM + " please fail") |
| 270 |
got = my_action.execute() |
| 271 |
assert isinstance(got, exceptions.TaskFailed) |
| 272 |
|
| 273 |
def test_success(self): |
| 274 |
TEST_PATH = os.path.dirname(__file__) |
| 275 |
PROGRAM = "%s %s/sample_process.py" % (executable, TEST_PATH) |
| 276 |
my_action = tools.Interactive(PROGRAM + " ok") |
| 277 |
got = my_action.execute() |
| 278 |
assert got is None |
| 279 |
|
| 280 |
|
| 281 |
class TestPythonInteractiveAction(object): |
| 282 |
def test_success(self): |
| 283 |
def hello(): print('hello') |
| 284 |
my_action = tools.PythonInteractiveAction(hello) |
| 285 |
got = my_action.execute() |
| 286 |
assert got is None |
| 287 |
|
| 288 |
def test_ignore_keyboard_interrupt(self, monkeypatch): |
| 289 |
def raise_x(): raise Exception('x') |
| 290 |
my_action = tools.PythonInteractiveAction(raise_x) |
| 291 |
got = my_action.execute() |
| 292 |
assert isinstance(got, exceptions.TaskError) |
| 293 |
|
| 294 |
def test_returned_dict_saved_result_values(self): |
| 295 |
def val(): return {'x': 3} |
| 296 |
my_action = tools.PythonInteractiveAction(val) |
| 297 |
got = my_action.execute() |
| 298 |
assert got is None |
| 299 |
assert my_action.result == {'x': 3} |
| 300 |
assert my_action.values == {'x': 3} |
| 301 |
|
| 302 |
def test_returned_string_saved_result(self): |
| 303 |
def val(): return 'hello' |
| 304 |
my_action = tools.PythonInteractiveAction(val) |
| 305 |
got = my_action.execute() |
| 306 |
assert got is None |
| 307 |
assert my_action.result == 'hello' |