summaryrefslogtreecommitdiff
path: root/silx/test/test_resources.py
diff options
context:
space:
mode:
authorPicca Frédéric-Emmanuel <picca@debian.org>2017-10-07 07:59:01 +0200
committerPicca Frédéric-Emmanuel <picca@debian.org>2017-10-07 07:59:01 +0200
commitbfa4dba15485b4192f8bbe13345e9658c97ecf76 (patch)
treefb9c6e5860881fbde902f7cbdbd41dc4a3a9fb5d /silx/test/test_resources.py
parentf7bdc2acff3c13a6d632c28c4569690ab106eed7 (diff)
New upstream version 0.6.0+dfsg
Diffstat (limited to 'silx/test/test_resources.py')
-rw-r--r--silx/test/test_resources.py212
1 files changed, 191 insertions, 21 deletions
diff --git a/silx/test/test_resources.py b/silx/test/test_resources.py
index 9d3e277..eaf65c1 100644
--- a/silx/test/test_resources.py
+++ b/silx/test/test_resources.py
@@ -26,17 +26,52 @@
__authors__ = ["T. Vincent"]
__license__ = "MIT"
-__date__ = "20/04/2017"
+__date__ = "06/09/2017"
import os
import unittest
+import shutil
+import tempfile
+from silx.third_party import six
import silx.resources
from .utils import utilstest
+import socket
class TestResources(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+ super(TestResources, cls).setUpClass()
+
+ cls.tmpDirectory = tempfile.mkdtemp(prefix="resource_")
+ os.mkdir(os.path.join(cls.tmpDirectory, "gui"))
+ destination_dir = os.path.join(cls.tmpDirectory, "gui", "icons")
+ os.mkdir(destination_dir)
+ source = silx.resources.resource_filename("gui/icons/zoom-in.png")
+ destination = os.path.join(destination_dir, "foo.png")
+ shutil.copy(source, destination)
+ source = silx.resources.resource_filename("gui/icons/zoom-out.svg")
+ destination = os.path.join(destination_dir, "close.png")
+ shutil.copy(source, destination)
+
+ @classmethod
+ def tearDownClass(cls):
+ super(TestResources, cls).tearDownClass()
+ shutil.rmtree(cls.tmpDirectory)
+
+ def setUp(self):
+ # Store the original configuration
+ self._oldResources = dict(silx.resources._RESOURCE_DIRECTORIES)
+ unittest.TestCase.setUp(self)
+
+ def tearDown(self):
+ unittest.TestCase.tearDown(self)
+ # Restiture the original configuration
+ silx.resources._RESOURCE_DIRECTORIES = self._oldResources
+
def test_resource_dir(self):
"""Get a resource directory"""
icons_dirname = silx.resources.resource_filename('gui/icons/')
@@ -52,44 +87,179 @@ class TestResources(unittest.TestCase):
filename = silx.resources.resource_filename('non_existent_file.txt')
self.assertFalse(os.path.exists(filename))
+ def test_isdir(self):
+ self.assertTrue(silx.resources.is_dir('gui/icons'))
+
+ def test_not_isdir(self):
+ self.assertFalse(silx.resources.is_dir('gui/icons/colormap.png'))
+
+ def test_list_dir(self):
+ result = silx.resources.list_dir('gui/icons')
+ self.assertTrue(len(result) > 10)
+
+ # With prefixed resources
+
+ def test_resource_dir_with_prefix(self):
+ """Get a resource directory"""
+ icons_dirname = silx.resources.resource_filename('silx:gui/icons/')
+ self.assertTrue(os.path.isdir(icons_dirname))
+
+ def test_resource_file_with_prefix(self):
+ """Get a resource file name"""
+ filename = silx.resources.resource_filename('silx:gui/icons/colormap.png')
+ self.assertTrue(os.path.isfile(filename))
+
+ def test_resource_nonexistent_with_prefix(self):
+ """Get a non existent resource"""
+ filename = silx.resources.resource_filename('silx:non_existent_file.txt')
+ self.assertFalse(os.path.exists(filename))
+
+ def test_isdir_with_prefix(self):
+ self.assertTrue(silx.resources.is_dir('silx:gui/icons'))
+
+ def test_not_isdir_with_prefix(self):
+ self.assertFalse(silx.resources.is_dir('silx:gui/icons/colormap.png'))
+
+ def test_list_dir_with_prefix(self):
+ result = silx.resources.list_dir('silx:gui/icons')
+ self.assertTrue(len(result) > 10)
+
+ # Test new repository
+
+ def test_repository_not_exists(self):
+ """The resource from 'test' is available"""
+ self.assertRaises(ValueError, silx.resources.resource_filename, 'test:foo.png')
+
+ def test_adding_test_directory(self):
+ """The resource from 'test' is available"""
+ silx.resources.register_resource_directory("test", "silx.test.resources", forced_path=self.tmpDirectory)
+ path = silx.resources.resource_filename('test:gui/icons/foo.png')
+ self.assertTrue(os.path.exists(path))
+
+ def test_adding_test_directory_no_override(self):
+ """The resource from 'silx' is still available"""
+ silx.resources.register_resource_directory("test", "silx.test.resources", forced_path=self.tmpDirectory)
+ filename1 = silx.resources.resource_filename('gui/icons/close.png')
+ filename2 = silx.resources.resource_filename('silx:gui/icons/close.png')
+ filename3 = silx.resources.resource_filename('test:gui/icons/close.png')
+ self.assertTrue(os.path.isfile(filename1))
+ self.assertTrue(os.path.isfile(filename2))
+ self.assertTrue(os.path.isfile(filename3))
+ self.assertEqual(filename1, filename2)
+ self.assertNotEqual(filename1, filename3)
+
+ def test_adding_test_directory_non_existing(self):
+ """A resource while not exists in test is not available anyway it exists
+ in silx"""
+ silx.resources.register_resource_directory("test", "silx.test.resources", forced_path=self.tmpDirectory)
+ resource_name = "gui/icons/colormap.png"
+ path = silx.resources.resource_filename('test:' + resource_name)
+ path2 = silx.resources.resource_filename('silx:' + resource_name)
+ self.assertFalse(os.path.exists(path))
+ self.assertTrue(os.path.exists(path2))
+
+
+class TestResourcesWithoutPkgResources(TestResources):
+
+ @classmethod
+ def setUpClass(cls):
+ super(TestResourcesWithoutPkgResources, cls).setUpClass()
+ cls._old = silx.resources.pkg_resources
+ silx.resources.pkg_resources = None
+
+ @classmethod
+ def tearDownClass(cls):
+ silx.resources.pkg_resources = cls._old
+ del cls._old
+ super(TestResourcesWithoutPkgResources, cls).tearDownClass()
+
+
+class TestResourcesWithCustomDirectory(TestResources):
+
+ @classmethod
+ def setUpClass(cls):
+ super(TestResourcesWithCustomDirectory, cls).setUpClass()
+ cls._old = silx.resources._RESOURCES_DIR
+ base = os.path.dirname(silx.resources.__file__)
+ silx.resources._RESOURCES_DIR = base
+
+ @classmethod
+ def tearDownClass(cls):
+ silx.resources._RESOURCES_DIR = cls._old
+ del cls._old
+ super(TestResourcesWithCustomDirectory, cls).tearDownClass()
+
+
+def isSilxWebsiteAvailable():
+ try:
+ six.moves.urllib.request.urlopen('http://www.silx.org', timeout=1)
+ return True
+ except six.moves.urllib.error.URLError:
+ return False
+ except socket.timeout:
+ # This exception is still received in Python 2.7
+ return False
+
class TestExternalResources(unittest.TestCase):
- "This is a test for the TestResources"
+ """This is a test for the ExternalResources"""
+
+ @classmethod
+ def setUpClass(cls):
+ if not isSilxWebsiteAvailable():
+ raise unittest.SkipTest("Network or silx website not available")
+
+ def setUp(self):
+ self.utilstest = silx.resources.ExternalResources("toto", "http://www.silx.org/pub/silx/")
+
+ def tearDown(self):
+ if self.utilstest.data_home:
+ shutil.rmtree(self.utilstest.data_home)
+ self.utilstest = None
+
def test_tempdir(self):
"test the temporary directory creation"
- myutilstest = silx.resources.ExternalResources("toto", "http://www.silx.org")
- d = myutilstest.tempdir
+ d = self.utilstest.tempdir
self.assertTrue(os.path.isdir(d))
- self.assertEqual(d, myutilstest.tempdir, 'tmpdir is stable')
- myutilstest.clean_up()
+ self.assertEqual(d, self.utilstest.tempdir, 'tmpdir is stable')
+ self.utilstest.clean_up()
self.assertFalse(os.path.isdir(d))
- e = myutilstest.tempdir
+ e = self.utilstest.tempdir
self.assertTrue(os.path.isdir(e))
- self.assertEqual(e, myutilstest.tempdir, 'tmpdir is stable')
+ self.assertEqual(e, self.utilstest.tempdir, 'tmpdir is stable')
self.assertNotEqual(d, e, "tempdir changed")
- myutilstest.clean_up()
+ self.utilstest.clean_up()
def test_download(self):
"test the download from silx.org"
- f = utilstest.getfile("lena.png")
+ f = self.utilstest.getfile("lena.png")
self.assertTrue(os.path.exists(f))
- f = utilstest.getdir("source.tar.gz")
- self.assertTrue(os.path.isfile(f))
- self.assertTrue(os.path.isdir(f[:-7]))
+ di = utilstest.getdir("source.tar.gz")
+ for fi in di:
+ self.assertTrue(os.path.exists(fi))
- def test_dowload_all(self):
+ def test_download_all(self):
"test the download of all files from silx.org"
- l = utilstest.download_all()
- self.assertGreater(len(l), 1, "At least 2 items were downloaded")
+ filename = self.utilstest.getfile("lena.png")
+ directory = "source.tar.gz"
+ _filelist = self.utilstest.getdir(directory)
+ # download file and remove it to create a json mapping file
+ os.remove(filename)
+ directory_path = os.path.join(self.utilstest.data_home, "source")
+ shutil.rmtree(directory_path)
+ directory_path = os.path.join(self.utilstest.data_home, directory)
+ os.remove(directory_path)
+ filelist = self.utilstest.download_all()
+ self.assertGreater(len(filelist), 1, "At least 2 items were downloaded")
def suite():
+ loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
test_suite = unittest.TestSuite()
- test_suite.addTest(
- unittest.defaultTestLoader.loadTestsFromTestCase(TestResources))
- test_suite.addTest(TestExternalResources("test_tempdir"))
- test_suite.addTest(TestExternalResources("test_download")) # order matters !
- test_suite.addTest(TestExternalResources("test_dowload_all"))
+ test_suite.addTest(loadTests(TestResources))
+ test_suite.addTest(loadTests(TestResourcesWithoutPkgResources))
+ test_suite.addTest(loadTests(TestResourcesWithCustomDirectory))
+ test_suite.addTest(loadTests(TestExternalResources))
return test_suite