def test_filepath_tar_open(self): # open tar file path = getTestPath('nest2.tar') fd = s_filepath.openfile(path, mode='rb') fs_fd = open(getTestPath('nest2.tar'), 'rb') self.eq(fd.read(), fs_fd.read()) fs_fd.close() fd.close() # open inner tar file path = getTestPath('nest2.tar', 'nndir0', 'nndir1', 'nest1.tar') fd = s_filepath.openfile(path, mode='rb') fs_fd = open(getTestPath('nest1.tar'), 'rb') self.eq(fd.read(), fs_fd.read()) fs_fd.close() fd.close() # open inner file path = getTestPath('nest2.tar', 'nnfoo') fd = s_filepath.openfile(path, mode='rb') buf = b'A' * 20 buf += b'\n' self.eq(fd.read(), buf) fd.close()
def test_filepath_zip_open(self): temp_fd = tempfile.NamedTemporaryFile() zip_fd = zipfile.ZipFile(temp_fd.name, 'w') abuf = 'A'*20 bbuf = b'A'*20 zip_fd.writestr('dir0/foo', abuf) fbuf2 = 'B'*20 zip_fd.writestr('bar', fbuf2) zip_fd.close() # file that exists in a directory path = os.path.join(temp_fd.name, 'dir0', 'foo') self.assertTrue(s_filepath.isfile(path)) # open zip file path = temp_fd.name fd0 = s_filepath.openfile(path, mode='rb') fd1 = open(path, mode='rb') self.assertEqual(fd0.read(), fd1.read()) # open inner zip file path = os.path.join(temp_fd.name, 'dir0', 'foo') fd = s_filepath.openfile(path, mode='r') self.assertEqual(fd.read(), bbuf) temp_fd.close()
def test_filepath_regular(self): temp_fd = tempfile.NamedTemporaryFile() temp_dir = tempfile.mkdtemp() fbuf = b'A' * 20 temp_fd.write(fbuf) temp_fd.flush() # file and dir that exist self.true(s_filepath.exists(temp_fd.name)) self.true(s_filepath.exists(temp_dir)) self.true(s_filepath.exists('/')) self.false(s_filepath.isfile(temp_dir)) self.false(s_filepath.isdir(temp_fd.name)) # DNE in a real directory path = os.path.join(temp_dir, 'dne') self.false(s_filepath.exists(path)) self.raises(s_exc.NoSuchPath, s_filepath._pathClass, path) # open regular file fd = s_filepath.openfile(temp_fd.name, mode='rb') self.eq(fd.read(), fbuf) fd.close() # dne path self.raises(s_exc.NoSuchPath, s_filepath.openfile, '%s%s' % (temp_fd.name, '_DNE'), mode='rb') self.raises(s_exc.NoSuchPath, s_filepath.openfile, None) self.raises(s_exc.NoSuchPath, s_filepath.openfile, '') self.none(s_filepath.openfile(None, req=False)) # open a directory self.none(s_filepath.openfile('/tmp', mode='rb', req=False)) self.none(s_filepath.openfile('/', req=False)) temp_fd.close() shutil.rmtree(temp_dir)