def testWriteToReadFrom(self): newConfig = Configuration() newConfig.addTask(Task('TestTest1', ['config1', 'config2'])) newConfig.addTask(Task('TestTest2', ['config1', 'config2'])) newConfig.definitions.append( Definition('name1', 'dir', 'C:/temp1', './C/temp1')) newConfig.definitions.append( Definition('name2', 'dir', 'C:/temp2', './C/temp2')) newConfig.definitions.append( Definition('name3', 'dir', 'C:/temp3', './C/temp3')) newConfig.retainStrategies.append(RetainStrategy('reatinDefault')) newConfig.backupDirectory = 'c:/temp/backup' Environment.cleanupTestFolder() Environment.setupExamplesDir() testFile = os.path.join(Environment.examplesFolder(), 'test_config.brit') newConfig.writeTo(testFile) self.assert_(os.path.isfile(testFile), 'No config file written') newConfig2 = Configuration.readFrom(testFile) self.assert_(newConfig2 <> None, 'No configuration has been read from Json') self.assert_( len(newConfig2.tasks) == 2, 'Length of list of tasks not OK') self.assert_(newConfig2.tasks[0].name == 'TestTest1', 'Name of first task not OK') self.assert_(newConfig2.tasks[0].configuration == newConfig2, 'Configuration of task not set') self.assert_( len(newConfig2.definitions) == 3, 'Length of list of Definitions not OK') self.assert_( len(newConfig2.retainStrategies) == 1, 'Length of list of RetainStrategies not OK') self.assert_(newConfig2.backupDirectory == 'c:/temp/backup', 'backupDirectory not OK') Environment.cleanupTestFolder()
def testfilesSize(self): Environment.cleanupTestFolder() exampleFile = Environment.setupExamplesDir() someFiles = Environment.extendExamplesDir() someFiles.append(exampleFile) myDef = Definition('Name1', 'dir', Environment.examplesFolder(), 'Test/Dir') self.assert_(myDef.filesSize() == 514, 'FilesSize not OK') Environment.cleanupTestFolder()
def testRun(self): Environment.cleanupTestFolder() Environment.setupExamplesDir() newConfig = Configuration() newConfig.definitions.append( Definition('Name1', 'dir', Environment.examplesFolder(), './C/temp1')) newTask = Task('myName', ['Name1'], targetFolder=Environment.targetFolder()) newConfig.addTask(newTask) filename = newTask.run() self.assert_(os.path.exists(Environment.targetFolder()), 'Target folder not created') self.assert_(filename <> None and filename <> '', 'No file name returned on run') Environment.cleanupTestFolder()
def testSaveToReadFrom(self): newMapping = FileMapping() newMapping.addItem(u'c:/temp/x1.txt', u'Temp/x1.txt') newMapping.addItem(u'c:/temp/x2.txt', u'Temp/x2.txt') Environment.cleanupTestFolder() Environment.setupExamplesDir() testFile = os.path.join(Environment.examplesFolder(), 'test_mapping.json') newMapping.saveTo(testFile) self.assert_(os.path.isfile(testFile), 'No mapping file written') newMapping2 = FileMapping.readFrom(testFile) self.assert_(newMapping2 <> None, 'No mapping read') self.assert_( len(newMapping2.itemMappings) == 2, 'Nb of items mappings not OK') self.assert_( newMapping2.itemMappings[u'c:/temp/x1.txt'] == u'Temp/x1.txt', 'Mapping item not added') Environment.cleanupTestFolder()
def testFileInfos_forFile(self): Environment.cleanupTestFolder() exampleFile = Environment.setupExamplesDir() myDef = Definition('Name1', 'file', exampleFile, 'Test/example.txt') infos = [] for info in myDef.fileInfos(): infos.append(info) self.assert_(len(infos) == 1, 'There should be exactly one fileInfo') self.assert_(infos[0]['source'] == exampleFile, 'Source not OK') self.assert_(infos[0]['target'] == 'Test/example.txt', 'Target not OK') Environment.cleanupTestFolder()
def test_doArchiveFileFail(self): filename = Environment.setupExamplesDir() zipTargetFile = 'dir/example' config = Configuration() config.backupDirectory = Environment.targetFolder() definition = Definition('name1', 'file', filename, zipTargetFile) config.definitions.append(definition) task = Task('myName', ['name1']) config.addTask(task) task._prepareTargetFolder() targetfile = task._getTargetFilename() archiveFile = task._prepareArchive(targetfile) # Now the actual test # I open and lock the example file to provoke an I/O error file_ = open(filename, 'a') if os.name == 'nt': msvcrt.locking(file_.fileno(), msvcrt.LK_LOCK, 0x7fffffff) with LogCapture(level=logging.WARNING) as l: task._doArchiveFile(archiveFile, filename, zipTargetFile) l.check(( 'root', 'WARNING', 'File could not be read: C:\\ProgramData\\brit_test\\examples\\example.txt Reason: I/O error(13): Permission denied' )) archiveFile.close() # Unlock and close the file if os.name == 'nt': msvcrt.locking(file_.fileno(), msvcrt.LK_UNLCK, 0x7fffffff) file_.close() self.assert_(not self.fileIsZipped(zipTargetFile, targetfile), 'File placed in zip file even though it was locked') Environment.cleanupTestFolder()
def testfileInfos(self): Environment.cleanupTestFolder() exampleFile = Environment.setupExamplesDir() myDef = Definition('Name1', 'dir', Environment.examplesFolder(), 'Test/Dir') infos = [] for info in myDef.fileInfos(): infos.append(info) self.assert_(infos <> None, 'No fileInfos created') self.assert_( infos[0]['source'] == os.path.join(Environment.examplesFolder(), 'example.txt'), 'Example source file not OK') self.assert_( infos[0]['target'] == os.path.join('Test/Dir', 'examples', 'example.txt'), 'Example source file not OK') someFiles = Environment.extendExamplesDir() someFiles.append(exampleFile) infos = [] for info in myDef.fileInfos(): infos.append(info) self.assert_(len(infos) == len(someFiles), 'Not all files found') myDef.includeDirPattern = ['subfolder'] infos = [] for info in myDef.fileInfos(): infos.append(info) # Note that I get all files of the base dir, independent if that matches included dirrs! self.assert_(len(infos) == 4, 'Nb of files not OK for include dirs') Environment.cleanupTestFolder()
def test_doArchiveFile(self): filename = Environment.setupExamplesDir() zipTargetFile = 'dir/example' config = Configuration() config.backupDirectory = Environment.targetFolder() definition = Definition('name1', 'file', filename, zipTargetFile) config.definitions.append(definition) task = Task('myName', ['name1']) config.addTask(task) task._prepareTargetFolder() targetfile = task._getTargetFilename() archiveFile = task._prepareArchive(targetfile) # Now the actual test task._doArchiveFile(archiveFile, filename, zipTargetFile) archiveFile.close() self.assert_(self.fileIsZipped(zipTargetFile, targetfile), 'File not placed in zip file') Environment.cleanupTestFolder()