test/file_cache.py: ported to pytest (#3429)
This commit is contained in:
parent
64bd7adefa
commit
8c3edfd36f
1 changed files with 36 additions and 44 deletions
|
@ -22,62 +22,54 @@
|
||||||
# License along with this program; if not, write to the Free Software
|
# License along with this program; if not, write to the Free Software
|
||||||
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
##############################################################################
|
##############################################################################
|
||||||
"""
|
"""Test Spack's FileCache."""
|
||||||
Test Spack's FileCache.
|
|
||||||
"""
|
|
||||||
import os
|
import os
|
||||||
import shutil
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
|
import pytest
|
||||||
from spack.file_cache import FileCache
|
from spack.file_cache import FileCache
|
||||||
|
|
||||||
|
|
||||||
class FileCacheTest(unittest.TestCase):
|
@pytest.fixture()
|
||||||
"""Ensure that a file cache can properly write to a file and recover its
|
def file_cache(tmpdir):
|
||||||
contents."""
|
"""Returns a properly initialized FileCache instance"""
|
||||||
|
return FileCache(str(tmpdir))
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.scratch_dir = tempfile.mkdtemp()
|
|
||||||
self.cache = FileCache(self.scratch_dir)
|
|
||||||
|
|
||||||
def tearDown(self):
|
def test_write_and_read_cache_file(file_cache):
|
||||||
shutil.rmtree(self.scratch_dir)
|
|
||||||
|
|
||||||
def test_write_and_read_cache_file(self):
|
|
||||||
"""Test writing then reading a cached file."""
|
"""Test writing then reading a cached file."""
|
||||||
with self.cache.write_transaction('test.yaml') as (old, new):
|
with file_cache.write_transaction('test.yaml') as (old, new):
|
||||||
self.assertTrue(old is None)
|
assert old is None
|
||||||
self.assertTrue(new is not None)
|
assert new is not None
|
||||||
new.write("foobar\n")
|
new.write("foobar\n")
|
||||||
|
|
||||||
with self.cache.read_transaction('test.yaml') as stream:
|
with file_cache.read_transaction('test.yaml') as stream:
|
||||||
text = stream.read()
|
text = stream.read()
|
||||||
self.assertEqual("foobar\n", text)
|
assert text == "foobar\n"
|
||||||
|
|
||||||
def test_remove(self):
|
|
||||||
"""Test removing an entry from the cache."""
|
|
||||||
self.test_write_and_write_cache_file()
|
|
||||||
|
|
||||||
self.cache.remove('test.yaml')
|
def test_write_and_remove_cache_file(file_cache):
|
||||||
|
"""Test two write transactions on a cached file. Then try to remove an
|
||||||
|
entry from it.
|
||||||
|
"""
|
||||||
|
|
||||||
self.assertFalse(os.path.exists(self.cache.cache_path('test.yaml')))
|
with file_cache.write_transaction('test.yaml') as (old, new):
|
||||||
self.assertFalse(os.path.exists(self.cache._lock_path('test.yaml')))
|
assert old is None
|
||||||
|
assert new is not None
|
||||||
def test_write_and_write_cache_file(self):
|
|
||||||
"""Test two write transactions on a cached file."""
|
|
||||||
with self.cache.write_transaction('test.yaml') as (old, new):
|
|
||||||
self.assertTrue(old is None)
|
|
||||||
self.assertTrue(new is not None)
|
|
||||||
new.write("foobar\n")
|
new.write("foobar\n")
|
||||||
|
|
||||||
with self.cache.write_transaction('test.yaml') as (old, new):
|
with file_cache.write_transaction('test.yaml') as (old, new):
|
||||||
self.assertTrue(old is not None)
|
assert old is not None
|
||||||
text = old.read()
|
text = old.read()
|
||||||
self.assertEqual("foobar\n", text)
|
assert text == "foobar\n"
|
||||||
self.assertTrue(new is not None)
|
assert new is not None
|
||||||
new.write("barbaz\n")
|
new.write("barbaz\n")
|
||||||
|
|
||||||
with self.cache.read_transaction('test.yaml') as stream:
|
with file_cache.read_transaction('test.yaml') as stream:
|
||||||
text = stream.read()
|
text = stream.read()
|
||||||
self.assertEqual("barbaz\n", text)
|
assert text == "barbaz\n"
|
||||||
|
|
||||||
|
file_cache.remove('test.yaml')
|
||||||
|
|
||||||
|
# After removal both the file and the lock file should not exist
|
||||||
|
assert not os.path.exists(file_cache.cache_path('test.yaml'))
|
||||||
|
assert not os.path.exists(file_cache._lock_path('test.yaml'))
|
||||||
|
|
Loading…
Reference in a new issue