FIX: mostrecent was doing the least recent!

This commit is contained in:
boyska 2018-01-23 10:43:11 +01:00
parent a862fe3e1e
commit 1b9232efb1
3 changed files with 32 additions and 2 deletions

View file

@ -30,7 +30,7 @@ def recent_choose(paths, howmany, minepoch):
if mtime >= minepoch]
return [fname for fname, mtime in
sorted(found_files, key=lambda x: x[1])[:howmany]]
sorted(found_files, key=lambda x: x[1], reverse=True)[:howmany]]
def generate(spec):

View file

@ -47,7 +47,7 @@ def generate(spec):
os.close(tmp[0])
shutil.copy(path, tmp[1])
log.info("copying %s -> %s", path, os.path.basename(tmp[1]))
yield 'file://{}'.format(tmp[1])
yield "file://{}".format(tmp[1])
generate.description = 'Picks random files from a specified directory'

View file

@ -12,6 +12,11 @@ def now(request):
return int(time.time())
@pytest.fixture
def yesterday(request):
return int(time.time()) - 24*60*60
@pytest.fixture
def empty_dir():
dirpath = tempfile.mkdtemp(prefix='mostrecent.')
@ -28,6 +33,15 @@ def dir_with_old_file(empty_dir):
os.unlink(fname)
@pytest.fixture
def dir_with_yesterday_file(empty_dir, yesterday):
fd, fname = tempfile.mkstemp(prefix='yesterday.', dir=empty_dir)
os.close(fd)
os.utime(fname, times=(yesterday, yesterday))
yield empty_dir
os.unlink(fname)
@pytest.fixture
def dir_with_new_file(dir_with_old_file, now):
fd, fname = tempfile.mkstemp(prefix='new.', dir=dir_with_old_file)
@ -37,6 +51,15 @@ def dir_with_new_file(dir_with_old_file, now):
os.unlink(fname)
@pytest.fixture
def dir_with_two_recent_files(dir_with_yesterday_file, now):
fd, fname = tempfile.mkstemp(prefix='new.', dir=dir_with_yesterday_file)
os.close(fd)
os.utime(fname, times=(now, now))
yield dir_with_yesterday_file
os.unlink(fname)
def test_empty_is_empty(empty_dir, now):
'''nothing can be picked from a empty dir'''
picked = recent_choose([empty_dir], 1, now)
@ -58,3 +81,10 @@ def test_only_new_files_found(dir_with_new_file):
picked = recent_choose([dir_with_new_file], 2, 1)
assert len(picked) == 1
assert os.path.basename(picked[0]).startswith('new.')
def test_correct_sorting(dir_with_two_recent_files):
picked = recent_choose([dir_with_two_recent_files], 1, 1)
assert len(picked) == 1
assert not os.path.basename(picked[0]).startswith('yesterday.')
assert os.path.basename(picked[0]).startswith('new.')