Ejemplo n.º 1
0
    def testAgainstActualLoggingModule(self):
        """Test the collection against the actual logging module."""
        lo = logging.getLogger('TestGetFilenamesFromLoggers')
        h1 = logging.FileHandler(osutils.mktemp('.log'))
        lo.addHandler(h1)
        h2 = logging.FileHandler(osutils.mktemp('.log'))
        logging.root.addHandler(h2)

        #Can't test equality because logging may have picked up garbage.
        fns = logutils.get_filenames_from_loggers()
        self.assertTrue(h1.baseFilename in fns)
        self.assertTrue(h2.baseFilename in fns)

        lo.removeHandler(h1)
        logging.root.removeHandler(h2)
Ejemplo n.º 2
0
 def testWorking(self):
     """Runs of a battery of gets and sets that should all work."""
     fn = osutils.mktemp()
     p = self.create(fn)
     self.assertEqual(p.get('a', 'b', 3), 3)
     p.setdefault('a', 'b', 4)
     self.assertEqual(p.get('a', 'b', 5), 4)
Ejemplo n.º 3
0
 def testIsNotJsonAndIsCPickle(self):
     p = self.create(osutils.mktemp())
     p.set('hi', 'there', 'you')
     with open(p.filename, 'r') as f:
         self.assertRaises(ValueError, json.load, f)
     with open(p.filename, 'rb') as f:
         pickle.load(f)
Ejemplo n.º 4
0
 def testKnown(self):
     # Will this fail on other OSes?
     f = osutils.mktemp()
     self.assertEqual(osutils.crc_from_filename(f), 0)
     with open(f, 'w') as fd:
         fd.write('hello')
     self.assertEqual(osutils.crc_from_filename(f), 907060870)
Ejemplo n.º 5
0
 def mk(self, suffix=''):
     # TODO: Change mtime. Relying on sleep isn't reliable!
     self.counter += 1
     prefix = 'f%s_' % self.counter
     f = osutils.mktemp(prefix=prefix, suffix=suffix, dir=self.root)
     os.utime(f, (self.counter, self.counter))
     return f
Ejemplo n.º 6
0
 def testExtractDoesNotIncludeEmptyFilenames(self):
     """Test that no empty values are included in the returned log
     filenames."""
     f = osutils.mktemp()
     lo = self.createMockLogger(f)
     lo.handlers[0].baseFilename = ''  # nuke the filename
     fns = logutils.get_filenames_from_loggers((lo,))
     self.assertEqual(fns, ())
Ejemplo n.º 7
0
 def testDuplicateFilenamesDoesNotDuplicateFilenames(self):
     """Test that passing in unique handlers pointing to the same
     file does not return duplicate filenames."""
     f = osutils.mktemp()
     lo = self.createMockLogger(f)
     lo.addHandler(itertoolsext.Bundle(baseFilename=f))
     lo.addHandler(itertoolsext.Bundle(baseFilename=f))
     fns = logutils.get_filenames_from_loggers((lo,))
     self.assertEqual(fns, (f,))
Ejemplo n.º 8
0
 def testExtractsOnlyFromHandlersWithBaseFilename(self):
     """Ensures that method returns filenames from all handlers with
     baseFilename, regardless of type."""
     f = osutils.mktemp()
     lo = self.createMockLogger(f)
     lo.addHandler(itertoolsext.Bundle(baseFilename=__file__))
     fns = logutils.get_filenames_from_loggers((lo,))
     ideal = sorted([__file__, f])
     self.assertEqual(sorted(fns), ideal)
Ejemplo n.º 9
0
def get_yaml_filepath_str_and_stream(DICT, dumper=yaml.Dumper):
    yamlfp = osutils.mktemp('.yaml')
    with open(yamlfp, 'w') as f:
        yaml.dump(DICT, f, Dumper=dumper)
    atexit.register(os.remove, yamlfp)
    yamlstr = yaml.dump(DICT, None, dumper)
    yamlstream = compat.StringIO(yamlstr)
    yamlstream.getvalue()  # Force it to update internally
    return yamlfp, yamlstr, yamlstream
Ejemplo n.º 10
0
 def testInvalidData(self):
     """Test that the object does not raise when the data file does not
     contain a dictionary."""
     fn = osutils.mktemp()
     with open(fn, 'w') as f:
         f.write('abcd!!!~')
     p = self.create(fn)
     self.assertEqual(7, p.setdefault('test', 'test', 7))
     self.assertEqual(7, p.get('test', 'test', 0))
Ejemplo n.º 11
0
 def testDuplicateLoggersPassedInDoesNotDuplicateFilenames(self):
     """Test that passing in duplicate loggers does not return
     duplicate files."""
     f = osutils.mktemp()
     lo = self.createMockLogger(f)
     h = itertoolsext.Bundle(baseFilename=f)
     lo.addHandler(h)
     lo.addHandler(h)
     fns = logutils.get_filenames_from_loggers((lo,))
     self.assertEqual(fns, (f,))
Ejemplo n.º 12
0
 def testCorruptData(self):
     """Test that the object does not raise when the data file is corrupt.
     """
     fn = osutils.mktemp()
     with open(fn, 'wb') as f:
         f.write(b'yhgh5454][][.^^%()B')
     p = self.create(fn)
     self.assertEqual(7, p.setdefault('test', 'test', 7))
     self.assertEqual(7, p.get('test', 'test', 0))
     return p
Ejemplo n.º 13
0
 def setUp(self):
     self.writeable = osutils.mktemp()
     self.readonly = osutils.mktemp()
     os.chmod(self.readonly, stat.S_IREAD)
Ejemplo n.º 14
0
 def testExtractsFileHandlers(self):
     """Ensures that method extracts filename from log file handlers."""
     f = osutils.mktemp()
     lo = self.createMockLogger(f)
     fns = logutils.get_filenames_from_loggers((lo,))
     self.assertEqual(fns, (f,))
Ejemplo n.º 15
0
 def createZip(self, files=(('dir1/file1.txt', 'abc'),)):
     path = osutils.mktemp('.zip', dir=self.tempd)
     with zu.ZipFile(path, 'w') as z:
         for fpath, fstr in files:
             z.writestr(fpath, fstr)
     return path
Ejemplo n.º 16
0
 def CreateFile(self, readonlystate):
     p = osutils.mktemp('readonlystate_%s' % readonlystate)
     osutils.set_readonly(p, readonlystate)
     self._files.append(p)
     return p
Ejemplo n.º 17
0
 def _getTempfile(self):
     fp = osutils.mktemp()
     self.addCleanup(os.remove, fp)
     return fp
Ejemplo n.º 18
0
 def testDumpFile(self):
     path = osutils.mktemp('.yaml')
     self.cls().dumpfile(self.DICT, path)
     testhelpers.assertTextFilesEqual(self, path, self.FILE)