diff options
author | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2012-12-01 16:13:08 +0000 |
---|---|---|
committer | willmcgugan@gmail.com <willmcgugan@gmail.com@67cdc799-7952-0410-af00-57a81ceafa0f> | 2012-12-01 16:13:08 +0000 |
commit | 9ff3da0cf28eab28d5dd0e6dcc1714707350aea5 (patch) | |
tree | 2a1f457399a4257ae03e122d50dc322930f5836a /fs/tests | |
parent | eac172574eb68f8bf7b4000163698c2e9898c02f (diff) | |
download | pyfilesystem-git-9ff3da0cf28eab28d5dd0e6dcc1714707350aea5.tar.gz |
Implemented generic validatepath method and optimized normpath
Diffstat (limited to 'fs/tests')
-rw-r--r-- | fs/tests/__init__.py | 53 | ||||
-rw-r--r-- | fs/tests/test_fs.py | 9 | ||||
-rw-r--r-- | fs/tests/test_path.py | 9 |
3 files changed, 49 insertions, 22 deletions
diff --git a/fs/tests/__init__.py b/fs/tests/__init__.py index bbfeff1..73a6795 100644 --- a/fs/tests/__init__.py +++ b/fs/tests/__init__.py @@ -41,7 +41,7 @@ class FSTestCases(object): To apply the tests to your own FS implementation, simply use FSTestCase as a mixin for your own unittest.TestCase subclass and have the setUp method set self.fs to an instance of your FS implementation. - + NB. The Filesystem being tested must have a capacity of at least 3MB. This class is designed as a mixin so that it's not detected by test @@ -52,10 +52,19 @@ class FSTestCases(object): """Check that a file exists within self.fs""" return self.fs.exists(p) + def test_invalid_chars(self): + """Check paths validate ok""" + # Will have to be overriden selectively for custom validepath methods + self.assertEqual(self.fs.validatepath(''), None) + self.assertEqual(self.fs.validatepath('.foo'), None) + self.assertEqual(self.fs.validatepath('foo'), None) + self.assertEqual(self.fs.validatepath('foo/bar'), None) + self.assert_(self.fs.isvalidpath('foo/bar')) + def test_meta(self): """Checks getmeta / hasmeta are functioning""" # getmeta / hasmeta are hard to test, since there is no way to validate - # the implementations response + # the implementation's response meta_names = ["read_only", "network", "unicode_paths"] @@ -70,7 +79,7 @@ class FSTestCases(object): self.assertTrue(self.fs.hasmeta(meta_name)) except NoMetaError: self.assertFalse(self.fs.hasmeta(meta_name)) - + def test_root_dir(self): self.assertTrue(self.fs.isdir("")) @@ -108,7 +117,7 @@ class FSTestCases(object): else: f.close() assert False, "ResourceInvalidError was not raised" - + def test_writefile(self): self.assertRaises(ResourceNotFoundError,self.fs.open,"test1.txt") f = self.fs.open("test1.txt","wb") @@ -152,7 +161,7 @@ class FSTestCases(object): self.assertEquals(self.fs.getcontents("hello", "rb"), b("world")) # ...and a file-like object self.fs.setcontents_async("hello", StringIO(b("to you, good sir!")), chunk_size=2).wait() - self.assertEquals(self.fs.getcontents("hello", "rb"), b("to you, good sir!")) + self.assertEquals(self.fs.getcontents("hello", "rb"), b("to you, good sir!")) def test_isdir_isfile(self): self.assertFalse(self.fs.exists("dir1")) @@ -236,7 +245,7 @@ class FSTestCases(object): for (nm,info) in items: self.assertTrue(isinstance(nm,unicode)) def check_equal(items,target): - names = [nm for (nm,info) in items] + names = [nm for (nm,info) in items] self.assertEqual(sorted(names),sorted(target)) self.fs.setcontents(u"a", b('')) self.fs.setcontents("b", b('')) @@ -318,7 +327,7 @@ class FSTestCases(object): if "c" in files: found_c = True if "a.txt" in files: - break + break assert found_c, "depth search order was wrong: " + str(list(self.fs.walk(search="depth"))) def test_walk_wildcard(self): @@ -730,18 +739,18 @@ class FSTestCases(object): f.truncate() checkcontents("hello",b("12345")) - def test_truncate_to_larger_size(self): - with self.fs.open("hello","wb") as f: + def test_truncate_to_larger_size(self): + with self.fs.open("hello","wb") as f: f.truncate(30) - + self.assertEquals(self.fs.getsize("hello"), 30) - + # Some file systems (FTPFS) don't support both reading and writing if self.fs.getmeta('file.read_and_write', True): with self.fs.open("hello","rb+") as f: f.seek(25) f.write(b("123456")) - + with self.fs.open("hello","rb") as f: f.seek(25) self.assertEquals(f.read(),b("123456")) @@ -788,10 +797,10 @@ class FSTestCases(object): else: # Just make sure it doesn't throw an exception fs2 = pickle.loads(pickle.dumps(self.fs)) - + def test_big_file(self): - """Test handling of a big file (1MB)""" + """Test handling of a big file (1MB)""" chunk_size = 1024 * 256 num_chunks = 4 def chunk_stream(): @@ -821,19 +830,19 @@ class FSTestCases(object): finally: f.close() - def test_settimes(self): + def test_settimes(self): def cmp_datetimes(d1, d2): """Test datetime objects are the same to within the timestamp accuracy""" dts1 = time.mktime(d1.timetuple()) dts2 = time.mktime(d2.timetuple()) - return int(dts1) == int(dts2) + return int(dts1) == int(dts2) d1 = datetime.datetime(2010, 6, 20, 11, 0, 9, 987699) - d2 = datetime.datetime(2010, 7, 5, 11, 0, 9, 500000) - self.fs.setcontents('/dates.txt', b('check dates')) + d2 = datetime.datetime(2010, 7, 5, 11, 0, 9, 500000) + self.fs.setcontents('/dates.txt', b('check dates')) # If the implementation supports settimes, check that the times # can be set and then retrieved try: - self.fs.settimes('/dates.txt', d1, d2) + self.fs.settimes('/dates.txt', d1, d2) except UnsupportedError: pass else: @@ -847,7 +856,7 @@ class FSTestCases(object): # May be disabled - see end of file class ThreadingTestCases(object): """Testcases for thread-safety of FS implementations.""" - + # These are either too slow to be worth repeating, # or cannot possibly break cross-thread. _dont_retest = ("test_pickling","test_multiple_overwrite",) @@ -1026,7 +1035,7 @@ class ThreadingTestCases(object): self.fs.copydir("a","copy of a") def copydir_overwrite(): self._yield() - self.fs.copydir("a","copy of a",overwrite=True) + self.fs.copydir("a","copy of a",overwrite=True) # This should error out since we're not overwriting self.assertRaises(DestinationExistsError,self._runThreads,copydir,copydir) # This should run to completion and give a valid state, unless @@ -1059,4 +1068,4 @@ class ThreadingTestCases(object): # Uncomment to temporarily disable threading tests #class ThreadingTestCases(object): -# _dont_retest = () +# _dont_retest = () diff --git a/fs/tests/test_fs.py b/fs/tests/test_fs.py index 6414b2d..dd6c4de 100644 --- a/fs/tests/test_fs.py +++ b/fs/tests/test_fs.py @@ -31,7 +31,16 @@ class TestOSFS(unittest.TestCase,FSTestCases,ThreadingTestCases): return os.path.exists(os.path.join(self.temp_dir, relpath(p))) def test_invalid_chars(self): + self.assertEqual(self.fs.validatepath(''), None) + self.assertEqual(self.fs.validatepath('.foo'), None) + self.assertEqual(self.fs.validatepath('foo'), None) + self.assertEqual(self.fs.validatepath('foo/bar'), None) + self.assert_(self.fs.isvalidpath('foo/bar')) + self.assertRaises(errors.InvalidCharsInPathError, self.fs.open, 'invalid\0file', 'wb') + self.assertFalse(self.fs.isvalidpath('invalid\0file')) + self.assert_(self.fs.isvalidpath('validfile')) + self.assert_(self.fs.isvalidpath('completely_valid/path/foo.bar')) class TestSubFS(unittest.TestCase,FSTestCases,ThreadingTestCases): diff --git a/fs/tests/test_path.py b/fs/tests/test_path.py index 40ec742..b4e062f 100644 --- a/fs/tests/test_path.py +++ b/fs/tests/test_path.py @@ -138,6 +138,15 @@ class TestPathFunctions(unittest.TestCase): for path, test_basename in tests: self.assertEqual(basename(path), test_basename) + def test_iswildcard(self): + self.assert_(iswildcard('*')) + self.assert_(iswildcard('*.jpg')) + self.assert_(iswildcard('foo/*')) + self.assert_(iswildcard('foo/{}')) + self.assertFalse(iswildcard('foo')) + self.assertFalse(iswildcard('img.jpg')) + self.assertFalse(iswildcard('foo/bar')) + class Test_PathMap(unittest.TestCase): |