def test_get_commands(self): for key in sorted(backends): logger.debug("test_get_commands: key='%s'", key) for (option_id, __name) in backends[key].get_options(): for cmd in backends[key].get_commands(option_id): for result in cmd.execute(really_delete=False): if result != True: break common.validate_result(self, result) # make sure trash and tmp don't return the same results if 'nt' == os.name: return def get_files(option_id): ret = [] list(register_cleaners()) for cmd in backends['system'].get_commands(option_id): result = next(cmd.execute(False)) ret.append(result['path']) return ret trash_paths = get_files('trash') tmp_paths = get_files('tmp') for tmp_path in tmp_paths: self.assertNotIn(tmp_path, trash_paths)
def test_walk_files(self): """Unit test for walk.files""" paths = {'posix': '/var', 'nt': '$WINDIR\\system32'} action_str = u'<action command="delete" search="walk.files" path="%s" />' % paths[os.name] results = 0 for cmd in _action_str_to_commands(action_str): result = cmd.execute(False).next() common.validate_result(self, result) path = result['path'] self.assertFalse(os.path.isdir(path), "%s is a directory" % path) results += 1 self.assertGreater(results, 0)
def test_walk_files(self): """Unit test for walk.files""" paths = {'posix': '/var', 'nt': '$WINDIR\\system32'} action_str = u'<action command="delete" search="walk.files" path="%s" />' % paths[os.name] results = 0 for cmd in _action_str_to_commands(action_str): result = cmd.execute(False).next() common.validate_result(self, result) path = result['path'] self.assertFalse(os.path.isdir(path), "%s is a directory" % path) results += 1 self.assertGreater(results, 0)
def test_create_simple_cleaner(self): """Unit test for method create_simple_cleaner""" dirname = self.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) targets = [filename1, filename2, dirname] cleaner = create_simple_cleaner(targets) for cmd in cleaner.get_commands('files'): # preview for result in cmd.execute(False): common.validate_result(self, result) # delete list(cmd.execute(True)) for target in targets: self.assertNotExists(target)
def _test_action_str(self, action_str, expect_exists=True): """Parse <action> and test it""" dom = parseString(action_str) action_node = dom.childNodes[0] command = action_node.getAttribute('command') filename = action_node.getAttribute('path') search = action_node.getAttribute('search') provider = None for actionplugin in ActionProvider.plugins: if actionplugin.action_key == command: provider = actionplugin(action_node) self.assertNotEqual(provider, None) for cmd in provider.get_commands(): self.assertIsInstance( cmd, (Command.Delete, Command.Ini, Command.Json, Command.Function)) if 'process' != command and not has_glob( filename) and expect_exists: # process does not have a filename self.assertLExists(filename) # preview result = next(cmd.execute(really_delete=False)) common.validate_result(self, result) self.assertNotEqual('/', result['path']) # delete ret = next(cmd.execute(really_delete=True)) if 'delete' == command: self.assertNotLExists(cmd.path) elif 'truncate' == command: self.assertLExists(filename) os.remove(filename) self.assertNotLExists(filename) elif command in 'process': pass elif command in ('ini', 'json'): self.assertLExists(filename) else: raise RuntimeError("Unknown command '%s'" % command) if 'walk.all' == search: if expect_exists: self.assertTrue( dir_is_empty(filename), 'directory not empty after walk.all: %s' % filename)
def test_create_simple_cleaner(self): """Unit test for method create_simple_cleaner""" dirname = self.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, 'чистый') common.touch_file(filename2) targets = [filename1, filename2, dirname] cleaner = create_simple_cleaner(targets) for cmd in cleaner.get_commands('files'): # preview for result in cmd.execute(False): common.validate_result(self, result) # delete list(cmd.execute(True)) for target in targets: self.assertNotExists(target)
def test_add_action(self): """Unit test for Cleaner.add_action()""" self.actions = [] if 'nt' == os.name: self.actions += [ '<action command="delete" search="file" path="$WINDIR\\explorer.exe"/>', '<action command="delete" search="glob" path="$WINDIR\\system32\\*.dll"/>', '<action command="delete" search="walk.files" path="$WINDIR\\system32\\"/>', '<action command="delete" search="walk.all" path="$WINDIR\\system32\\"/>' ] elif 'posix' == os.name: print(__file__) self.actions += [ '<action command="delete" search="file" path="%s"/>' % __file__, '<action command="delete" search="glob" path="/bin/*sh"/>', '<action command="delete" search="walk.files" path="/bin/"/>', '<action command="delete" search="walk.all" path="/var/log/"/>' ] else: raise AssertionError('Unknown OS.') self.assertGreater(len(self.actions), 0) for action_str in self.actions: cleaner = action_to_cleaner(action_str) count = 0 for cmd in cleaner.get_commands('option1'): for result in cmd.execute(False): self.assertEqual(result['n_deleted'], 1) pathname = result['path'] self.assertLExists(pathname, "Does not exist: '%s'" % pathname) count += 1 common.validate_result(self, result) self.assertGreater(count, 0, "No files found for %s" % action_str) # should yield nothing cleaner.add_option('option2', 'name2', 'description2') for cmd in cleaner.get_commands('option2'): print(cmd) raise AssertionError('option2 should yield nothing') # should fail self.assertRaises(RuntimeError, cleaner.get_commands('option3').__next__)
def test_system_recent_documents(self): """Clean recent documents in GTK""" mgr = Gtk.RecentManager().get_default() fn = self.mkstemp(suffix='.txt') self.assertExists(fn) uri = 'file:///' + fn print(uri) self.assertTrue(mgr.add_item(uri)) from gi.repository import GLib GLib.idle_add(Gtk.main_quit) Gtk.main() # process the addition self.assertTrue(mgr.has_item(uri)) self.assertGreater(len(mgr.get_items()), 0) list(register_cleaners()) for cmd in backends['system'].get_commands('recent_documents'): for result in cmd.execute(really_delete=True): common.validate_result(self, result, True) self.assertEqual(len(mgr.get_items()), 0)
def test_add_action(self): """Unit test for Cleaner.add_action()""" self.actions = [] if 'nt' == os.name: self.actions += [ '<action command="delete" search="file" path="$WINDIR\\explorer.exe"/>', '<action command="delete" search="glob" path="$WINDIR\\system32\\*.dll"/>', '<action command="delete" search="walk.files" path="$WINDIR\\system32\\"/>', '<action command="delete" search="walk.all" path="$WINDIR\\system32\\"/>'] elif 'posix' == os.name: print(__file__) self.actions += [ '<action command="delete" search="file" path="%s"/>' % __file__, '<action command="delete" search="glob" path="/bin/*sh"/>', '<action command="delete" search="walk.files" path="/bin/"/>', '<action command="delete" search="walk.all" path="/var/log/"/>'] else: raise AssertionError('Unknown OS.') self.assertGreater(len(self.actions), 0) for action_str in self.actions: cleaner = action_to_cleaner(action_str) count = 0 for cmd in cleaner.get_commands('option1'): for result in cmd.execute(False): self.assertEqual(result['n_deleted'], 1) pathname = result['path'] self.assertLExists(pathname, "Does not exist: '%s'" % pathname) count += 1 common.validate_result(self, result) self.assertGreater(count, 0, "No files found for %s" % action_str) # should yield nothing cleaner.add_option('option2', 'name2', 'description2') for cmd in cleaner.get_commands('option2'): print(cmd) raise AssertionError('option2 should yield nothing') # should fail self.assertRaises(RuntimeError, cleaner.get_commands('option3').next)
def _test_action_str(self, action_str): """Parse <action> and test it""" dom = parseString(action_str) action_node = dom.childNodes[0] command = action_node.getAttribute('command') filename = action_node.getAttribute('path') search = action_node.getAttribute('search') provider = None for actionplugin in ActionProvider.plugins: if actionplugin.action_key == command: provider = actionplugin(action_node) self.assertNotEqual(provider, None) for cmd in provider.get_commands(): self.assertIsInstance(cmd, (Command.Delete, Command.Ini, Command.Json, Command.Function)) if 'process' != command: # process does not have a filename self.assertLExists(filename) # preview result = cmd.execute(really_delete=False).next() common.validate_result(self, result) self.assertNotEqual('/', result['path']) # delete ret = cmd.execute(really_delete=True).next() if 'delete' == command: self.assertNotLExists(cmd.path) elif 'truncate' == command: self.assertLExists(filename) os.remove(filename) self.assertNotLExists(filename) elif command in 'process': pass elif command in ('ini', 'json'): self.assertLExists(filename) else: raise RuntimeError("Unknown command '%s'" % command) if 'walk.all' == search: self.assertTrue(dir_is_empty(filename), 'directory not empty after walk.all: %s' % filename)
def test_no_files_exist(self): """Verify only existing files are returned""" _exists = os.path.exists _iglob = glob.iglob _lexists = os.path.lexists _oswalk = os.walk glob.iglob = lambda path: [] os.path.exists = lambda path: False os.path.lexists = lambda path: False os.walk = lambda top, topdown=True, onerror=None, followlinks=False: [] for key in sorted(backends): for (option_id, __name) in backends[key].get_options(): for cmd in backends[key].get_commands(option_id): for result in cmd.execute(really_delete=False): if result != True: break msg = "Expected no files to be deleted but got '%s'" % str( result) self.assertNotIsInstance(cmd, Command.Delete, msg) common.validate_result(self, result) glob.iglob = _iglob os.path.exists = _exists os.path.lexists = _lexists os.walk = _oswalk
def test_get_commands(self): for key in sorted(backends): logger.debug("test_get_commands: key='%s'", key) for (option_id, __name) in backends[key].get_options(): for cmd in backends[key].get_commands(option_id): for result in cmd.execute(really_delete=False): if result != True: break common.validate_result(self, result) # make sure trash and tmp don't return the same results if 'nt' == os.name: return def get_files(option_id): ret = [] register_cleaners() for cmd in backends['system'].get_commands(option_id): result = cmd.execute(False).next() ret.append(result['path']) return ret trash_paths = get_files('trash') tmp_paths = get_files('tmp') for tmp_path in tmp_paths: self.assertNotIn(tmp_path, trash_paths)
def test_no_files_exist(self): """Verify only existing files are returned""" _exists = os.path.exists _iglob = glob.iglob _lexists = os.path.lexists _oswalk = os.walk glob.iglob = lambda path: [] os.path.exists = lambda path: False os.path.lexists = lambda path: False os.walk = lambda top, topdown = False: [] for key in sorted(backends): for (option_id, __name) in backends[key].get_options(): for cmd in backends[key].get_commands(option_id): for result in cmd.execute(really_delete=False): if result != True: break msg = "Expected no files to be deleted but got '%s'" % str( result) self.assertNotIsInstance(cmd, Command.Delete, msg) common.validate_result(self, result) glob.iglob = _iglob os.path.exists = _exists os.path.lexists = _lexists os.walk = _oswalk
def run_all(self, cleaner, really_delete): """Test all the cleaner options""" for (option_id, __name) in cleaner.get_options(): for cmd in cleaner.get_commands(option_id): for result in cmd.execute(really_delete): common.validate_result(self, result, really_delete)
def run_all(self, cleaner, really_delete): """Test all the cleaner options""" for (option_id, __name) in cleaner.get_options(): for cmd in cleaner.get_commands(option_id): for result in cmd.execute(really_delete): common.validate_result(self, result, really_delete)
def run_all(self, xmlcleaner, really_delete): """Helper function to execute all options in a cleaner""" for (option_id, __name) in xmlcleaner.cleaner.get_options(): for cmd in xmlcleaner.cleaner.get_commands(option_id): for result in cmd.execute(really_delete): common.validate_result(self, result, really_delete)
def run_all(self, xmlcleaner, really_delete): """Helper function to execute all options in a cleaner""" for (option_id, __name) in xmlcleaner.cleaner.get_options(): for cmd in xmlcleaner.cleaner.get_commands(option_id): for result in cmd.execute(really_delete): common.validate_result(self, result, really_delete)
def run_all(really_delete): for (option_id, __name) in xmlcleaner.cleaner.get_options(): for cmd in xmlcleaner.cleaner.get_commands(option_id): for result in cmd.execute(really_delete): common.validate_result(self, result, really_delete)