summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMassimiliano Culpo <massimiliano.culpo@googlemail.com>2017-03-14 17:07:04 +0100
committerTodd Gamblin <tgamblin@llnl.gov>2017-03-14 09:07:04 -0700
commit8c3edfd36ffe668e4e82c8e01b0a6e7918707266 (patch)
treef27cefdf9e1e3b7c07a83d7b5f6841240a2b2913 /lib
parent64bd7adefabc76a6b1337ce3b9c678c4af84ae6e (diff)
downloadspack-8c3edfd36ffe668e4e82c8e01b0a6e7918707266.tar.gz
spack-8c3edfd36ffe668e4e82c8e01b0a6e7918707266.tar.bz2
spack-8c3edfd36ffe668e4e82c8e01b0a6e7918707266.tar.xz
spack-8c3edfd36ffe668e4e82c8e01b0a6e7918707266.zip
test/file_cache.py: ported to pytest (#3429)
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/test/file_cache.py80
1 files changed, 36 insertions, 44 deletions
diff --git a/lib/spack/spack/test/file_cache.py b/lib/spack/spack/test/file_cache.py
index cc66beda2e..af52380e34 100644
--- a/lib/spack/spack/test/file_cache.py
+++ b/lib/spack/spack/test/file_cache.py
@@ -22,62 +22,54 @@
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################
-"""
-Test Spack's FileCache.
-"""
+"""Test Spack's FileCache."""
import os
-import shutil
-import tempfile
-import unittest
+import pytest
from spack.file_cache import FileCache
-class FileCacheTest(unittest.TestCase):
- """Ensure that a file cache can properly write to a file and recover its
- contents."""
+@pytest.fixture()
+def file_cache(tmpdir):
+ """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):
- shutil.rmtree(self.scratch_dir)
+def test_write_and_read_cache_file(file_cache):
+ """Test writing then reading a cached file."""
+ with file_cache.write_transaction('test.yaml') as (old, new):
+ assert old is None
+ assert new is not None
+ new.write("foobar\n")
- def test_write_and_read_cache_file(self):
- """Test writing then reading 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")
+ with file_cache.read_transaction('test.yaml') as stream:
+ text = stream.read()
+ assert text == "foobar\n"
- with self.cache.read_transaction('test.yaml') as stream:
- text = stream.read()
- self.assertEqual("foobar\n", text)
- def test_remove(self):
- """Test removing an entry from the cache."""
- self.test_write_and_write_cache_file()
+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.cache.remove('test.yaml')
+ with file_cache.write_transaction('test.yaml') as (old, new):
+ assert old is None
+ assert new is not None
+ new.write("foobar\n")
- self.assertFalse(os.path.exists(self.cache.cache_path('test.yaml')))
- self.assertFalse(os.path.exists(self.cache._lock_path('test.yaml')))
+ with file_cache.write_transaction('test.yaml') as (old, new):
+ assert old is not None
+ text = old.read()
+ assert text == "foobar\n"
+ assert new is not None
+ new.write("barbaz\n")
- 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")
+ with file_cache.read_transaction('test.yaml') as stream:
+ text = stream.read()
+ assert text == "barbaz\n"
- with self.cache.write_transaction('test.yaml') as (old, new):
- self.assertTrue(old is not None)
- text = old.read()
- self.assertEqual("foobar\n", text)
- self.assertTrue(new is not None)
- new.write("barbaz\n")
+ file_cache.remove('test.yaml')
- with self.cache.read_transaction('test.yaml') as stream:
- text = stream.read()
- self.assertEqual("barbaz\n", text)
+ # 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'))