Browse Source

FIX: mostrecent was doing the least recent!

boyska 6 years ago
parent
commit
1b9232efb1

+ 1 - 1
larigira/audiogen_mostrecent.py

@@ -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):

+ 1 - 1
larigira/audiogen_randomdir.py

@@ -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'

+ 30 - 0
larigira/tests/test_audiogen_mostrecent.py

@@ -13,6 +13,11 @@ def now(request):
 
 
 @pytest.fixture
+def yesterday(request):
+    return int(time.time()) - 24*60*60
+
+
+@pytest.fixture
 def empty_dir():
     dirpath = tempfile.mkdtemp(prefix='mostrecent.')
     yield dirpath
@@ -29,6 +34,15 @@ def dir_with_old_file(empty_dir):
 
 
 @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)
     os.close(fd)
@@ -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.')