|
5 | 5 | from builtins import open
|
6 | 6 |
|
7 | 7 | import os
|
8 |
| -from tempfile import mkstemp |
| 8 | +import time |
| 9 | +from tempfile import mkstemp, mkdtemp |
| 10 | +import shutil |
9 | 11 | import warnings
|
10 | 12 |
|
11 | 13 | import pytest
|
|
15 | 17 | hash_rename, check_forhash,
|
16 | 18 | copyfile, copyfiles,
|
17 | 19 | filename_to_list, list_to_filename,
|
| 20 | + check_depends, |
18 | 21 | split_filename, get_related_files)
|
19 | 22 |
|
20 | 23 | import numpy as np
|
@@ -271,6 +274,41 @@ def test_list_to_filename(list, expected):
|
271 | 274 | assert x == expected
|
272 | 275 |
|
273 | 276 |
|
| 277 | +def test_check_depends(): |
| 278 | + def touch(fname): |
| 279 | + with open(fname, 'a'): |
| 280 | + os.utime(fname, None) |
| 281 | + |
| 282 | + tmpdir = mkdtemp() |
| 283 | + |
| 284 | + dependencies = [os.path.join(tmpdir, str(i)) for i in range(3)] |
| 285 | + targets = [os.path.join(tmpdir, str(i)) for i in range(3, 6)] |
| 286 | + |
| 287 | + # Targets newer than dependencies |
| 288 | + for dep in dependencies: |
| 289 | + touch(dep) |
| 290 | + time.sleep(1) |
| 291 | + for tgt in targets: |
| 292 | + touch(tgt) |
| 293 | + assert check_depends(targets, dependencies) |
| 294 | + |
| 295 | + # Targets older than newest dependency |
| 296 | + time.sleep(1) |
| 297 | + touch(dependencies[0]) |
| 298 | + assert not check_depends(targets, dependencies) |
| 299 | + |
| 300 | + # Missing dependency |
| 301 | + os.unlink(dependencies[0]) |
| 302 | + try: |
| 303 | + check_depends(targets, dependencies) |
| 304 | + except OSError as e: |
| 305 | + pass |
| 306 | + else: |
| 307 | + assert False, "Should raise OSError on missing dependency" |
| 308 | + |
| 309 | + shutil.rmtree(tmpdir) |
| 310 | + |
| 311 | + |
274 | 312 | def test_json():
|
275 | 313 | # Simple roundtrip test of json files, just a sanity check.
|
276 | 314 | adict = dict(a='one', c='three', b='two')
|
|
0 commit comments