def testGetStat(self): """ Tests the getStat() method. 1. Get the stat of an existing file. 2. Get the stat of a non-existing file. """ existingFilePath=ufsi.Path(self.existingFilePathStr) nonExistingFilePath=ufsi.Path(self.nonExistingFilePathStr) # 1 f=existingFilePath.getFile() s1=f.getStat() # TODO: validate returned data # 2 f=nonExistingFilePath.getFile() self.assertRaises(ufsi.PathNotFoundError,f.getStat)
def testGetDirList(self): """ Tests the getDirList() method. 1. getDirList of existing directory 2. getDirList of non-existing directory TODO: test re filtering """ existingDirPath=ufsi.Path(self.existingDirPathStr) nonExistingDirPath=ufsi.Path(self.nonExistingDirPathStr) # 1 d=existingDirPath.getDir() self.assertEquals(d.getDirList(),self.existingDirDirList, 'Returned dir list was not correct') # 2 d=nonExistingDirPath.getDir() self.assertRaises(ufsi.PathNotFoundError,d.getDirList)
def getSymlinkPath(self): """ Returns a Path object that has the path that the symlink refers to. """ if not self.isSymlink(): raise ufsi.NotASymlinkError('%r is not a symlink' % self._path) try: return ufsi.Path(os.readlink(self._path)) except Exception, e: NativeUtils.handleException(e, self._path)
def testWriteLines(self): """ Tests the writeLines method. 1. Write a list of lines to a file. """ writeFilePath=ufsi.Path(self.writeFilePathStr) # 1 f=writeFilePath.getFile() self.assertRaises(ufsi.UnsupportedOperationError,f.open,'w') self.assertRaises(ufsi.UnsupportedOperationError,f.write,['data\n'])
def testOpen(self): """ Tests the open() method. 1. open existing file for read 2. open non-existing file for read 3. open existing file for write 4. open non-existing file for write """ existingFilePath = ufsi.Path(self.existingFilePathStr) nonExistingFilePath = ufsi.Path(self.nonExistingFilePathStr) # 1 f = existingFilePath.getFile() f.open('r') f.close() # 2 f = nonExistingFilePath.getFile() self.assertRaises(ufsi.PathNotFoundError, f.open, 'r') # 3 f = existingFilePath.getFile() f.open('w') f.close() self.assertEqual( os.stat(self.existingFilePathStr)[stat.ST_SIZE], 0, 'File was not truncated') # 4 f = nonExistingFilePath.getFile() f.open('w') f.close() self.assert_(os.path.exists(self.nonExistingFilePathStr), 'File was not created') self.assertEqual( os.stat(self.nonExistingFilePathStr)[stat.ST_SIZE], 0, 'File was not truncated')
def testGetStat(self): """ Tests the getStat() method. 1. getStat on existing directory 2. getStat on non-existing directory """ existingDirPath=ufsi.Path(self.existingDirPathStr) nonExistingDirPath=ufsi.Path(self.nonExistingDirPathStr) # 1 d=existingDirPath.getDir() s1=d.getStat() s2=os.stat(self.existingDirPathStr) self.assertEquals(s1['size'],s2[stat.ST_SIZE],'Size not correct') self.assertEquals(s1['accessTime'],s2[stat.ST_ATIME], 'Access time not correct') self.assertEquals(s1['modificationTime'],s2[stat.ST_MTIME], 'Modification time not correct') self.assertEquals(s1['creationTime'],s2[stat.ST_CTIME], 'Creation time not correct') self.assertEquals(s1['userId'],s2[stat.ST_UID], 'UserID not correct') self.assertEquals(s1['groupId'],s2[stat.ST_GID], 'GroupID not correct') self.assertEquals(s1['permissions'],s2[stat.ST_MODE], 'Mode not correct') self.assertEquals(s1['inodeNumber'],s2[stat.ST_INO], 'Inode number not correct') self.assertEquals(s1['inodeDevice'],s2[stat.ST_DEV], 'Inode device not correct') self.assertEquals(s1['inodeLinks'],s2[stat.ST_NLINK], 'Inode links count not correct') # 2 d=nonExistingDirPath.getDir() self.assertRaises(ufsi.PathNotFoundError,d.getStat)
def setUp(self): """ """ # location of the testing server host = 'localhost' server = 'ftp://' + host + '/' self.testDataDir = 'ufsiTd/' # authentication details readUser = '******' readPassword = '******' writeUser = '******' writePassword = '******' # to save from creating another account, simply assign # readUserAuth to None for anonymous login self.readUserAuth = ufsi.UserPasswordAuthentication( readUser, readPassword) self.writeUserAuth = ufsi.UserPasswordAuthentication( writeUser, writePassword) self.anonUserAuth = None # files self.existingFile = 'existing' self.existingFilePath = ufsi.FtpPath(server + self.testDataDir + self.existingFile) self.existingFileContents=\ '12345678901234567890\nSecondLine\nThirdLine\n' self.nonExistingFile = 'nonExisting' self.nonExistingFilePath = ufsi.FtpPath(server + self.testDataDir + self.nonExistingFile) # dirs self.existingDir = 'existingDir' self.existingDirPath = ufsi.FtpPath(server + self.testDataDir + self.existingDir) self.nonExistingDir = 'nonExistingDir' self.nonExistingDirPath = ufsi.FtpPath(server + self.testDataDir + self.nonExistingDir) # write file self.writeFile = 'write' self.writeFilePath = ufsi.Path(server + self.testDataDir + self.writeFile) self.writeFileContents = 'a couple\nof\nlines' # TODO: test symlinks (when implemented) self.server = server self.host = host
def testReadLines(self): """ Tests the readLines() method 1. Read all lines. """ existingFilePath=ufsi.Path(self.existingFilePathStr) # 1 f=existingFilePath.getFile() f.open('r') lines=self.existingFileContents.splitlines(True) self.assertEqual(f.readLines(),lines,'Contents read are not correct') f.close()
def testReadLine(self): """ Tests the readLine() method. 1. Read each line to the end of the file. """ existingFilePath=ufsi.Path(self.existingFilePathStr) # 1 f=existingFilePath.getFile() f.open('r') lines=self.existingFileContents.splitlines(True) for l in lines: self.assertEqual(f.readLine(),l,'Contents read are not correct') self.assertEqual(f.readLine(),'','Too much content in the file') f.close()
def setUp(self): """ Creates the test data: * existing dir, containing files: test1,test2 * non existing dir """ # tar file self.tarFilePath = ufsi.Path('data/tarfile.tar') # paths self.existingDirPathStr = 'existingDir/' self.existingDirPath = ufsi.TarPath(self.tarFilePath, self.existingDirPathStr) self.existingDirDirList = ['test1', 'test2'] self.nonExistingDirPathStr = 'nonexistingDir/' self.nonExistingDirPath = ufsi.TarPath(self.tarFilePath, self.nonExistingDirPathStr)
def testWriteLines(self): """ Tests the writeLines method. 1. Write a list of lines to a file. """ writeFilePath = ufsi.Path(self.writeFilePathStr) lines = self.writeFileContents.splitlines(True) # 1 f = writeFilePath.getFile() f.open('w') f.writeLines(lines) f.close() f = file(self.writeFilePathStr, 'r') self.assertEqual(f.readlines(), lines, 'Contents were not written correctly') f.close()
def testWrite(self): """ Tests the write() method. 1. Write to a file. """ writeFilePath = ufsi.Path(self.writeFilePathStr) # 1 f = writeFilePath.getFile() f.open('w') f.write(self.writeFileContents) f.close() f = file(self.writeFilePathStr, 'r') self.assertEqual(f.read(), self.writeFileContents, 'Contents were not written correctly') f.close()
def join(self, other): if not isinstance(other, ufsi.PathInterface): other = ufsi.Path(other) if other.isAbsolute(): return other otherStr = str(other) otherSep = other.getSeparator() pathSep = self.getSeparator() pathStr = self.__tarPath otherStr = otherStr.replace(otherSep, pathSep) if otherStr.startswith(pathSep): otherStr = otherStr[len(pathStr):] if not pathStr.endswith(pathSep): pathStr += pathSep return TarPath(self.__tarFilePath, pathStr + otherStr)
def testJoin(self): """ Tests the join() method. 1. append a relative path 2. append an absolute path 3. append to a path not terminated by a separator character 4. append an empty path """ server=self.server P=lambda p:ufsi.Path(p) data={ # 1 'relativePath': [server+'dir1/',P('dir2/fileBase.ext'), server+'dir1/dir2/fileBase.ext'], # 2 'absolutePath': [server+'dir1/',P('/dir2/fileBase.ext'), str(P('/dir2/fileBase.ext'))], # 3 'notSeparatorTerminatedPath': [server+'dir1',P('dir2/fileBase.ext'), server+'dir1/dir2/fileBase.ext'], # 4 'emptyPath': [server+'dir1',P(''),server+'dir1/'], } for k in data.iterkeys(): p1=ufsi.FtpPath(data[k][0]) p2=data[k][1] r1=str(p1.join(p2)) r2=data[k][2] self.assertEquals(r1,r2, '%s: join result was %r but should have been %r' %(k,r1,r2))
def join(self, other): """ Joins ``other`` onto the end of this path and returns a new Path object. ``other`` must be a string or a Path object. """ # TODO: should we take a list of other, as os.path does if not isinstance(other, ufsi.PathInterface): other = ufsi.Path(other) if other.isAbsolute(): return other # Sort out separators selfSep = self.getSeparator() otherStr = str(other).replace(other.getSeparator(), selfSep) selfStr = self._path if not selfStr.endswith(selfSep) and selfStr != '': selfStr = selfStr + selfSep if otherStr.startswith(selfSep): otherStr = otherStr[len(selfSep):] return self.__class__(selfStr + otherStr)
def setUp(self): """ Creates the test data: * existing file, content '12345678901234567890\nSecondLine\nThirdLine' * non existing file """ self.tarFilePath = ufsi.Path('data/tarfile.tar') # file paths self.existingFilePathStr = 'existing' self.existingFilePath = ufsi.TarPath(self.tarFilePath, self.existingFilePathStr) self.existingFileContents=\ '12345678901234567890\nSecondLine\nThirdLine\n' self.nonExistingFilePathStr = 'nonExisting' self.nonExistingFilePath = ufsi.TarPath(self.tarFilePath, self.nonExistingFilePathStr) self.writeFilePathStr = 'write' self.writeFilePath = ufsi.TarPath(self.tarFilePath, self.writeFilePathStr) self.writeFileContents = 'test content\nsecond line\n'