You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@climate.apache.org by jo...@apache.org on 2014/02/02 00:57:27 UTC
svn commit: r1563526 - in /incubator/climate/trunk/ocw-ui/backend:
directory_helpers.py tests/test_directory_helpers.py
Author: joyce
Date: Sat Feb 1 23:57:27 2014
New Revision: 1563526
URL: http://svn.apache.org/r1563526
Log:
CLIMATE-326 - Update directory_helper docs and improve tests
- Add docstring to _get_clean_directory_path helper.
- Remove the isdir check in _get_clean_directory_path. The purpose of
the helper is to 'clean' a provided path, not check if the result is a
valid directory. Additionally, the calling code was already checking
if the directory was valid, so this was just duplicated functionality.
- Remove the _get_clean_directory_path_helper that was asserting that
invalid directories caused an Exception. A modified version has been
added under the /dir/list tests to check for a similar result.
Modified:
incubator/climate/trunk/ocw-ui/backend/directory_helpers.py
incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py
Modified: incubator/climate/trunk/ocw-ui/backend/directory_helpers.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw-ui/backend/directory_helpers.py?rev=1563526&r1=1563525&r2=1563526&view=diff
==============================================================================
--- incubator/climate/trunk/ocw-ui/backend/directory_helpers.py (original)
+++ incubator/climate/trunk/ocw-ui/backend/directory_helpers.py Sat Feb 1 23:57:27 2014
@@ -115,7 +115,20 @@ def getPathLeader():
return returnJSON
def _get_clean_directory_path(path_leader, dir_path):
- ''''''
+ ''' Return a cleaned directory path with a defined path prefix.
+
+ 'Clean' dir_path to remove any relative path components or duplicate
+ slashes that could cause problems. The final clean path is then the
+ path_leader + dir_path.
+
+ :param path_leader: The path prefix that will be prepended to the cleaned
+ dir_path.
+ :type path_leader: String
+ :param dir_path: The path to clean.
+ :type path_leader: String
+
+ :returns: The cleaned directory path with path_leader prepended.
+ '''
# Strip out any .. or . relative directories and remove duplicate slashes
dir_path = re.sub('/[\./]*/?', '/', dir_path)
dir_path = re.sub('//+', '/', dir_path)
@@ -125,14 +138,4 @@ def _get_clean_directory_path(path_leade
# which could allow access to unacceptable paths. This also means that
if dir_path[0] == '/': dir_path = dir_path[1:]
- dir_path = os.path.join(path_leader, dir_path)
- if not os.path.isdir(dir_path):
- cur_frame = sys._getframe().f_code
- err = "{}.{}: Created path is not a valid directory {}".format(
- cur_frame.co_filename,
- cur_frame.co_name,
- dir_path
- )
- raise ValueError(err)
-
- return dir_path
+ return os.path.join(path_leader, dir_path)
Modified: incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py
URL: http://svn.apache.org/viewvc/incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py?rev=1563526&r1=1563525&r2=1563526&view=diff
==============================================================================
--- incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py (original)
+++ incubator/climate/trunk/ocw-ui/backend/tests/test_directory_helpers.py Sat Feb 1 23:57:27 2014
@@ -28,6 +28,11 @@ class TestDirectoryPathList(unittest.Tes
response = test_app.get('http://localhost:8082/dir/list//usr/local')
self.assertDictEqual(response.json, expected_return)
+ def test_nonexistent_path_listing(self):
+ expected_return = {'listing': []}
+ response = test_app.get('http://localhost:8082/dir/list//fake/path')
+ self.assertDictEqual(response.json, expected_return)
+
class TestDirectoryPathCleaner(unittest.TestCase):
PATH_LEADER = '/tmp/foo'
VALID_CLEAN_DIR = '/tmp/foo/bar'
@@ -55,11 +60,3 @@ class TestDirectoryPathCleaner(unittest.
clean_path = _get_clean_directory_path(self.PATH_LEADER, '/.././bar')
self.assertEquals(clean_path, self.VALID_CLEAN_DIR)
-
- def test_directory_validity_check(self):
- self.assertRaises(
- ValueError,
- _get_clean_directory_path,
- self.PATH_LEADER,
- '/bar/path/to/missing/directory'
- )