Quellcode durchsuchen

Modified to_dir function to create full paths.

Taddeus Kroes vor 14 Jahren
Ursprung
Commit
f1d3b12160
2 geänderte Dateien mit 24 neuen und 26 gelöschten Zeilen
  1. 6 12
      cache.py
  2. 18 14
      tests/test_cache.py

+ 6 - 12
cache.py

@@ -3,7 +3,7 @@ import cPickle as pickle
 from time import gmtime
 from datetime import datetime, timedelta
 from os import mkdir
-from os.path import getmtime, exists
+from os.path import getmtime, exists, dirname, realpath
 from hashlib import md5
 from copy import copy
 
@@ -13,16 +13,10 @@ ADMINISTRATION_FILE = 'administration.py'
 
 def to_dir(path):
     """
-    Transform a path to a directory, that can be used as prefix before a
-    filename.
+    Transform a path to an absolute directory, that can be used as prefix
+    before a filename.
     """
-    if not path:
-        return ''
-
-    if path[-1] != '/':
-        return path + '/'
-
-    return path
+    return realpath(dirname(path)) + '/'
 
 
 def assert_file_exists(path):
@@ -51,8 +45,8 @@ class Cache:
         4. A dictionary containing arguments to the timedelta constructor, that
            indicates how long the cache object sould live.
         """
-        self.root = to_dir(root)
-        self.cached = to_dir(cached)
+        self.root = to_dir(root + '/')
+        self.cached = to_dir(cached + '/')
         self.files = map(lambda f: self.root + f, files)
         self.expires = expires
 

+ 18 - 14
tests/test_cache.py

@@ -7,6 +7,9 @@ from cPickle import dumps
 from cache import ADMINISTRATION_FILE, assert_file_exists, to_dir, Cache
 
 
+d = os.path.realpath('') + '/'
+
+
 class TestCache(unittest.TestCase):
     def setUp(self):
         if os.path.exists('baz'):
@@ -32,34 +35,35 @@ class TestCache(unittest.TestCase):
         self.assertRaises(IOError, assert_file_exists, 'foobar')
 
     def test_to_dir(self):
-        self.assertEqual(to_dir(''), '')
-        self.assertEqual(to_dir('foo'), 'foo/')
-        self.assertEqual(to_dir('foo/'), 'foo/')
+        self.assertEqual(to_dir(''), d)
+        self.assertEqual(to_dir('foo'), d)
+        self.assertEqual(to_dir('foo/'), d + 'foo/')
+        self.assertEqual(to_dir('foo/bar.txt'), d + 'foo/')
 
     def test___init__(self):
         self.assertTrue(hasattr(self.c, 'root'))
-        self.assertEqual(self.c.root, 'baz/')
+        self.assertEqual(self.c.root, d + 'baz/')
         self.assertTrue(hasattr(self.c, 'files'))
-        self.assertEqual(self.c.files, ['baz/foo', 'baz/bar'])
+        self.assertEqual(self.c.files, [d + 'baz/foo', d + 'baz/bar'])
         self.assertTrue(hasattr(self.c, 'cached'))
-        self.assertEqual(self.c.cached, 'cached/')
+        self.assertEqual(self.c.cached, d + 'cached/')
 
     def test___init___custom_root(self):
         c = Cache(root='foo')
         self.assertTrue(hasattr(c, 'root'))
-        self.assertEqual(c.root, 'foo/')
+        self.assertEqual(c.root, d + 'foo/')
 
     def test___init___custom_cached_dir(self):
         c = Cache(cached='foo')
         self.assertTrue(hasattr(c, 'cached'))
-        self.assertEqual(c.cached, 'foo/')
+        self.assertEqual(c.cached, d + 'foo/')
 
     def test___init___non_existant_file(self):
         self.assertRaises(IOError, Cache, files=['foo'])
 
     def test_add(self):
         self.c.add('baz')
-        self.assertIn('baz/baz', self.c.files)
+        self.assertIn(d + 'baz/baz', self.c.files)
 
     def test_add_non_relative(self):
         self.c.add('baz', absolute=True)
@@ -67,16 +71,16 @@ class TestCache(unittest.TestCase):
 
     def test_remove(self):
         self.c.remove('foo')
-        self.assertNotIn('baz/foo', self.c.files)
+        self.assertNotIn(d + 'baz/foo', self.c.files)
 
     def test_remove_non_relative(self):
-        self.c.remove('baz/foo', absolute=True)
-        self.assertNotIn('baz/foo', self.c.files)
+        self.c.remove(d + 'baz/foo', absolute=True)
+        self.assertNotIn(d + 'baz/foo', self.c.files)
 
     def test_assert_modification_dates_exist(self):
         self.c.assert_modification_dates_exist()
-        self.assertIn('baz/foo', self.c.modified)
-        self.assertEqual(self.c.modified['baz/foo'],
+        self.assertIn(d + 'baz/foo', self.c.modified)
+        self.assertEqual(self.c.modified[d + 'baz/foo'],
                          os.path.getmtime('baz/foo'))
         modified = copy(self.c.modified)