From 86b48d295bf237367824dc2f282ed4aba24a6bef Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Mon, 11 Jun 2018 08:29:31 -0400 Subject: [PATCH] avocado.Test: remove once deprecated datadir The `avocado.Test.datadir` attribute has been deprecated, and it's now time to have it removed for good. Signed-off-by: Cleber Rosa --- avocado/core/test.py | 53 +++++++++++++++--------------------- docs/source/WritingTests.rst | 17 ++++++++---- selftests/unit/test_test.py | 6 ++-- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/avocado/core/test.py b/avocado/core/test.py index a40ede56..e03250e9 100644 --- a/avocado/core/test.py +++ b/avocado/core/test.py @@ -190,15 +190,20 @@ class TestData(object): DATA_SOURCES = ["variant", "test", "file"] def __init__(self): + # Maximal allowed file name length is 255 + file_datadir = None + if (self.filename is not None and + len(os.path.basename(self.filename)) < 251): + file_datadir = self.filename + '.data' self._data_sources_mapping = { - "variant": [lambda: self.datadir, + "variant": [lambda: file_datadir, lambda: "%s.%s" % (self.__class__.__name__, self._testMethodName), lambda: self.name.variant], - "test": [lambda: self.datadir, + "test": [lambda: file_datadir, lambda: "%s.%s" % (self.__class__.__name__, self._testMethodName)], - "file": [lambda: self.datadir] + "file": [lambda: file_datadir] } def _check_valid_data_source(self, source): @@ -273,7 +278,13 @@ class TestData(object): for attempt_source in sources: datadir = self._get_datadir(attempt_source) if datadir is not None: - path = os.path.join(datadir, filename) + # avoid returning a slash after the data directory name + # when a file was not requested (thus return the data + # directory itself) + if not filename: + path = datadir + else: + path = os.path.join(datadir, filename) if not must_exist: self.log.debug(log_fmt, filename, path, ("assumed to be located at %s source " @@ -479,29 +490,6 @@ class Test(unittest.TestCase, TestData): else: return None - @property - def datadir(self): - """ - Returns the path to the directory that may contain test data files - - For test a test file hosted at /usr/share/doc/avocado/tests/sleeptest.py - the datadir is /usr/share/doc/avocado/tests/sleeptest.py.data. - - Note that this directory has no specific relation to the test - name, only to the file that contains the test. It can be used to - host data files that are generic enough to be used for all tests - contained in a given test file. - - This property is deprecated and will be removed in the future. - The :meth:`get_data` function should be used instead. - """ - # Maximal allowed file name length is 255 - if (self.filename is not None and - len(os.path.basename(self.filename)) < 251): - return self.filename + '.data' - else: - return None - @property def filename(self): """ @@ -954,8 +942,6 @@ class Test(unittest.TestCase, TestData): os.environ['AVOCADO_VERSION'] = VERSION if self.basedir is not None: os.environ['AVOCADO_TEST_BASEDIR'] = self.basedir - if self.datadir is not None: - os.environ['AVOCADO_TEST_DATADIR'] = self.datadir os.environ['AVOCADO_TEST_WORKDIR'] = self.workdir os.environ['AVOCADO_TEST_LOGDIR'] = self.logdir os.environ['AVOCADO_TEST_LOGFILE'] = self.logfile @@ -1094,9 +1080,14 @@ class SimpleTest(Test): self._filename = executable super(SimpleTest, self).__init__(name=name, params=params, base_logdir=base_logdir, job=job) - self._data_sources_mapping = {"variant": [lambda: self.datadir, + # Maximal allowed file name length is 255 + file_datadir = None + if (self.filename is not None and + len(os.path.basename(self.filename)) < 251): + file_datadir = self.filename + '.data' + self._data_sources_mapping = {"variant": [lambda: file_datadir, lambda: self.name.variant], - "file": [lambda: self.datadir]} + "file": [lambda: file_datadir]} self._command = None if self.filename is not None: self._command = pipes.quote(self.filename) diff --git a/docs/source/WritingTests.rst b/docs/source/WritingTests.rst index 69d1bf8d..86cb579f 100644 --- a/docs/source/WritingTests.rst +++ b/docs/source/WritingTests.rst @@ -227,10 +227,12 @@ you intend to create it. for that specific test and execution conditions (such as with or without variants). Look for "Test data directories" in the test logs. -.. note:: An older API, :attr:`avocado.core.test.Test.datadir`, allows access - to the data directory based on the test file location only. This API - is limited, deprecated and will be removed. All new users should rely - on ``get_data()`` instead. +.. note:: The previously existing API ``avocado.core.test.Test.datadir``, + used to allow access to the data directory based on the test file + location only. This API has been removed. If, for whatever reason + you still need to access the data directory based on the test file + location only, you can use + ``get_data(filename='', source='file', must_exist=False)`` instead. .. _accessing-test-parameters: @@ -1672,8 +1674,6 @@ tests: +-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+ | AVOCADO_TEST_BASEDIR | Base directory of Avocado tests | $HOME/Downloads/avocado-source/avocado | +-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+ -| AVOCADO_TEST_DATADIR | Data directory for the test | $AVOCADO_TEST_BASEDIR/my_test.sh.data | -+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+ | AVOCADO_TEST_WORKDIR | Work directory for the test | /var/tmp/avocado_Bjr_rd/my_test.sh | +-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+ | AVOCADO_TESTS_COMMON_TMPDIR | Temporary directory created by the | /var/tmp/avocado_XhEdo/ | @@ -1697,6 +1697,11 @@ tests: version 62.0. Please use ``AVOCADO_TEST_WORKDIR`` instead. +.. warning:: ``AVOCADO_TEST_DATADIR`` was present in earlier versions, + but has been deprecated on version 60.0, and removed on + version 62.0. The test data files (and directories) are + now dynamically evaluated and are not available as + environment variables SIMPLE Tests BASH extensions ============================ diff --git a/selftests/unit/test_test.py b/selftests/unit/test_test.py index 297578e0..0c8dc15e 100644 --- a/selftests/unit/test_test.py +++ b/selftests/unit/test_test.py @@ -98,12 +98,12 @@ class TestClassTestUnit(unittest.TestCase): def test_data_dir(self): """ - Tests that a valid datadir exists following the test filename + Checks `get_data()` won't report fs-unfriendly data dir name """ max_length_name = os.path.join(self.tmpdir, "a" * 250) tst = self._get_fake_filename_test(max_length_name) self.assertEqual(os.path.join(self.tmpdir, max_length_name + ".data"), - tst.datadir) + tst.get_data('', 'file', False)) def test_no_data_dir(self): """ @@ -111,7 +111,7 @@ class TestClassTestUnit(unittest.TestCase): """ above_limit_name = os.path.join(self.tmpdir, "a" * 251) tst = self._get_fake_filename_test(above_limit_name) - self.assertFalse(tst.datadir) + self.assertFalse(tst.get_data('', 'file', False)) tst._record_reference # Should do nothing tst._record_reference('stdout', 'stdout.expected') tst._record_reference('stderr', 'stderr.expected') -- GitLab