def test_type(self): """Unit test for type attribute""" dirname = tempfile.mkdtemp(prefix='bleachbit-action-type') filename = os.path.join(dirname, 'file') # this should not delete anything common.touch_file(filename) action_str = '<action command="delete" search="file" type="d" path="%s" />' % filename self._test_action_str(action_str) self.assertExists(filename) # should delete file action_str = '<action command="delete" search="file" type="f" path="%s" />' % filename self._test_action_str(action_str) self.assertNotExists(filename) # should delete file common.touch_file(filename) action_str = '<action command="delete" search="file" path="%s" />' % filename self._test_action_str(action_str) self.assertNotExists(filename) # should not delete anything action_str = '<action command="delete" search="file" type="f" path="%s" />' % dirname self._test_action_str(action_str) self.assertExists(dirname) # should delete directory action_str = '<action command="delete" search="file" type="d" path="%s" />' % dirname self._test_action_str(action_str) self.assertNotExists(dirname)
def benchmark_filter(this_filter): """Measure how fast listing files is with and without filter""" n_files = 100000 print 'benchmark of %d files' % n_files # make a directory with many files dirname = tempfile.mkdtemp(prefix='bleachbit-action-bench') for x in xrange(0, n_files): common.touch_file(os.path.join(dirname, str(x))) # scan directory import time start = time.time() filter_code = '' if 'regex' == this_filter: # This regex matches everything, so the "no filter" and regex # are comparable filter_code = 'regex="."' action_str = '<action command="delete" search="glob" path="%s/*" %s />' % \ (dirname, filter_code) results = _action_str_to_results(action_str) end = time.time() elapsed_seconds = end - start rate = n_files / elapsed_seconds print 'filter %s: elapsed: %.2f seconds, %.2f files/second' % ( this_filter, elapsed_seconds, rate) # clean up shutil.rmtree(dirname) return rate
def test_children_in_directory(self): """Unit test for function children_in_directory()""" # test an existing directory that usually exists dirname = os.path.expanduser("~/.config") for filename in children_in_directory(dirname, True): self.assert_(isinstance(filename, str)) self.assert_(os.path.isabs(filename)) for filename in children_in_directory(dirname, False): self.assert_(isinstance(filename, str)) self.assert_(os.path.isabs(filename)) self.assert_(not os.path.isdir(filename)) # test a constructed file in a constructed directory dirname = tempfile.mkdtemp(prefix='bleachbit-test-children') filename = os.path.join(dirname, "somefile") common.touch_file(filename) for loopfilename in children_in_directory(dirname, True): self.assertEqual(loopfilename, filename) for loopfilename in children_in_directory(dirname, False): self.assertEqual(loopfilename, filename) os.remove(filename) # test subdirectory subdirname = os.path.join(dirname, "subdir") os.mkdir(subdirname) for filename in children_in_directory(dirname, True): self.assertEqual(filename, subdirname) for filename in children_in_directory(dirname, False): self.assert_(False) os.rmdir(subdirname) os.rmdir(dirname)
def benchmark_filter(this_filter): """Measure how fast listing files is with and without filter""" n_files = 100000 print 'benchmark of %d files' % n_files # make a directory with many files dirname = tempfile.mkdtemp(prefix='bleachbit-action-bench') for x in xrange(0, n_files): common.touch_file(os.path.join(dirname, str(x))) # scan directory import time start = time.time() filter_code = '' if 'regex' == this_filter: # This regex matches everything, so the "no filter" and regex # are comparable filter_code = 'regex="."' action_str = '<action command="delete" search="glob" path="%s/*" %s />' % \ (dirname, filter_code) results = _action_str_to_results(action_str) end = time.time() elapsed_seconds = end - start rate = n_files / elapsed_seconds print 'filter %s: elapsed: %.2f seconds, %.2f files/second' % (this_filter, elapsed_seconds, rate) # clean up shutil.rmtree(dirname) return rate
def put_files_into_recycle_bin(): """Put a file and a folder into the recycle bin""" # make a file and move it to the recycle bin import tempfile tests = (('regular', u'unicode-emdash-u\u2014', 'long' + 'x' * 100)) for test in tests: (fd, filename) = tempfile.mkstemp(prefix='bleachbit-recycle-file', suffix=test) os.close(fd) move_to_recycle_bin(filename) # make a folder and move it to the recycle bin dirname = tempfile.mkdtemp(prefix='bleachbit-recycle-folder') common.touch_file(os.path.join(dirname, 'file')) move_to_recycle_bin(dirname)
def put_files_into_recycle_bin(): """Put a file and a folder into the recycle bin""" # make a file and move it to the recycle bin import tempfile tests = (('regular', u'unicode-emdash-u\u2014', 'long' + 'x' * 100)) for test in tests: (fd, filename) = tempfile.mkstemp( prefix='bleachbit-recycle-file', suffix=test) os.close(fd) move_to_recycle_bin(filename) # make a folder and move it to the recycle bin dirname = tempfile.mkdtemp(prefix='bleachbit-recycle-folder') common.touch_file(os.path.join(dirname, 'file')) move_to_recycle_bin(dirname)
def test_delete_special_filenames(self): """Unit test for deleting special filenames""" dirname = tempfile.mkdtemp(prefix='bleachbit-action-delete-special') tests = [ 'normal', 'space in name', 'sigil$should-not-be-expanded', ] for test in tests: pathname = os.path.join(dirname, test) common.touch_file(pathname) action_str = '<action command="delete" search="file" path="%s" />' % pathname self._test_action_str(action_str) self.assertNotExists(pathname) os.rmdir(dirname)
def test_walk_all(self): """Unit test for walk.all""" dirname = tempfile.mkdtemp(prefix='bleachbit-walk-all') # this sub-directory should be deleted subdir = os.path.join(dirname, 'sub') os.mkdir(subdir) self.assert_(os.path.exists(subdir)) # this file should be deleted too filename = os.path.join(subdir, 'file') common.touch_file(filename) action_str = '<action command="delete" search="walk.all" path="%s" />' % dirname self._test_action_str(action_str) self.assert_(not os.path.exists(subdir))
def _test_encoding(self, fn): """Test encoding""" tempd = tempfile.mkdtemp(prefix='bleachbit-test-deepscan') self.assert_(os.path.exists(tempd)) fullpath = os.path.join(tempd, fn) common.touch_file(fullpath) ds = DeepScan() ds.add_search(tempd, '^%s$' % fn) found = False for ret in ds.scan(): if True == ret: continue self.assert_(ret == fullpath) found = True self.assert_(found, "Did not find '%s'" % fullpath) os.unlink(fullpath) self.assert_(not os.path.exists(fullpath)) os.rmdir(tempd) self.assert_(not os.path.exists(tempd))
def test_create_simple_cleaner(self): """Unit test for method create_simple_cleaner""" dirname = tempfile.mkdtemp( prefix='bleachbit-test-create-simple-cleaner') filename1 = os.path.join(dirname, '1') common.touch_file(filename1) # test Cyrillic for https://bugs.launchpad.net/bleachbit/+bug/1541808 filename2 = os.path.join(dirname, u'чистый') common.touch_file(filename2) self.assert_(os.path.exists(filename1)) self.assert_(os.path.exists(filename2)) cleaner = create_simple_cleaner([filename1, filename2, dirname]) for cmd in cleaner.get_commands('files'): # preview for result in cmd.execute(False): common.validate_result(self, result) # delete for result in cmd.execute(True): pass self.assert_(not os.path.exists(filename1)) self.assert_(not os.path.exists(filename2)) self.assert_(not os.path.exists(dirname))
def benchmark_regex(): """Measure how fast the regex option is""" n_files = 100000 print 'benchmark of %d files' % n_files # make a directory with many files dirname = tempfile.mkdtemp(prefix='bleachbit-action-bench') for x in xrange(0, n_files): common.touch_file(os.path.join(dirname, str(x))) # scan directory import time start = time.time() action_str = '<action command="delete" search="glob" path="%s/*" regex="^12$"/>' % dirname results = _action_str_to_results(action_str) end = time.time() elapsed_seconds = end - start rate = n_files / elapsed_seconds print 'elapsed: %.2f seconds, %.2f files/second' % (elapsed_seconds, rate) # clean up shutil.rmtree(dirname) return rate