unit test UnusedCleaner

now there is some expectation of its behavior
This commit is contained in:
boyska 2016-12-11 02:16:35 +01:00
parent f9385db597
commit 76babeaa97
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
3 changed files with 36 additions and 3 deletions

View file

@ -27,6 +27,7 @@ def get_conf(prefix='LARIGIRA_'):
conf['EVENT_TICK_SECS'] = 30 # period for scheduling events
conf['DEBUG'] = False
conf['LOG_CONFIG'] = False
conf['TMPDIR'] = os.getenv('TMPDIR', '/tmp/')
conf.update(from_envvars(prefix=prefix))
return conf

31
larigira/test_unused.py Normal file
View file

@ -0,0 +1,31 @@
import pytest
from .unused import UnusedCleaner
from .config import get_conf
@pytest.fixture
def unusedcleaner():
return UnusedCleaner(get_conf(prefix='LARIGIRATEST_'))
# this test suite heavily assumes that TMPDIR == /tmp/, which is the default
# indeed. However, the code does not rely on this assumption.
def test_watch_file(unusedcleaner):
# despite not existing, the file is added
unusedcleaner.watch('file:///tmp/gnam')
assert len(unusedcleaner.waiting_removal_files) == 1
assert list(unusedcleaner.waiting_removal_files)[0] == '/tmp/gnam'
def test_watch_path_error(unusedcleaner):
'''paths are not valid thing to watch. URIs only, thanks'''
unusedcleaner.watch('/tmp/foo')
assert len(unusedcleaner.waiting_removal_files) == 0
def test_watch_notmp_error(unusedcleaner):
'''Files not in TMPDIR are not added'''
unusedcleaner.watch('file:///not/in/tmp')
assert len(unusedcleaner.waiting_removal_files) == 0

View file

@ -5,7 +5,7 @@ This component will look for files to be removed. There are some assumptions:
* MPD URIs are parsed, and only file:/// is supported
'''
import os
import os.path
from os.path import commonpath, normpath
import logging
import mpd
@ -30,8 +30,9 @@ class UnusedCleaner:
if not uri.startswith('file:///'):
return # not a file URI
fpath = uri[len('file://'):]
if 'TMPDIR' in os.environ and os.environ['TMPDIR'] \
and not os.path.commonpath([os.environ['TMPDIR'], fpath]) == os.path.normpath(os.environ['TMPDIR']):
if 'TMPDIR' in self.conf and self.conf['TMPDIR'] \
and commonpath([self.conf['TMPDIR'], fpath]) != \
normpath(self.conf['TMPDIR']):
self.log.info('Not watching file {}: not in TMPDIR'.format(fpath))
return
if not os.path.exists(fpath):