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.assert_( isinstance(cmd, (Command.Delete, Command.Ini, Command.Json))) 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 ('ini', 'json'): self.assertLExists(filename) else: raise RuntimeError("Unknown command '%s'" % command) if 'walk.all' == search: self.assert_(dir_is_empty(filename), 'directory not empty after walk.all: %s' % filename)
def test_get_commands(self): for key in sorted(backends): print "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.assert_(tmp_path not in trash_paths)
def test_add_action(self): """Unit test for Cleaner.add_action()""" self.actions = [] if 'nt' == os.name: self.actions.append( '<action command="delete" search="file" path="$WINDIR\\notepad.exe"/>' ) self.actions.append( '<action command="delete" search="glob" path="$WINDIR\\system32\\*.dll"/>' ) self.actions.append( '<action command="delete" search="walk.files" path="$WINDIR\\system\\"/>' ) self.actions.append( '<action command="delete" search="walk.all" path="$WINDIR\\system32\\"/>' ) elif 'posix' == os.name: self.actions.append( '<action command="delete" search="file" path="%s"/>' % __file__) self.actions.append( '<action command="delete" search="glob" path="/usr/sbin/*sh"/>' ) self.actions.append( '<action command="delete" search="walk.files" path="/usr/sbin/"/>' ) self.actions.append( '<action command="delete" search="walk.all" path="/var/log/"/>' ) self.assert_(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.assert_(os.path.lexists(pathname), "Does not exist: '%s'" % pathname) count += 1 common.validate_result(self, result) self.assert_(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 self.assert_(False, 'option2 should yield nothing') # should fail self.assertRaises(RuntimeError, cleaner.get_commands('option3').next)
def test_walk_files(self): """Unit test for walk.files""" if 'posix' == os.name: path = '/var' elif 'nt' == os.name: path = '$WINDIR\\system32' action_str = '<action command="delete" search="walk.files" path="%s" />' % path 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.assert_(not os.path.isdir(path), "%s is a directory" % path) results += 1 self.assert_(results > 0)
def test_add_action(self): """Unit test for Cleaner.add_action()""" self.actions = [] if 'nt' == os.name: self.actions.append( '<action command="delete" search="file" path="$WINDIR\\explorer.exe"/>') self.actions.append( '<action command="delete" search="glob" path="$WINDIR\\system32\\*.dll"/>') self.actions.append( '<action command="delete" search="walk.files" path="$WINDIR\\system32\\"/>') self.actions.append( '<action command="delete" search="walk.all" path="$WINDIR\\system32\\"/>') elif 'posix' == os.name: self.actions.append( '<action command="delete" search="file" path="%s"/>' % __file__) self.actions.append( '<action command="delete" search="glob" path="/bin/*sh"/>') self.actions.append( '<action command="delete" search="walk.files" path="/bin/"/>') self.actions.append( '<action command="delete" search="walk.all" path="/var/log/"/>') self.assert_(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.assert_(os.path.lexists(pathname), "Does not exist: '%s'" % pathname) count += 1 common.validate_result(self, result) self.assert_(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 self.assert_(False, 'option2 should yield nothing') # should fail self.assertRaises(RuntimeError, cleaner.get_commands('option3').next)
def test_create_simple_cleaner(self): """Unit test for method create_simple_cleaner""" (fd, filename1) = tempfile.mkstemp(prefix='bleachbit-test-cleaner') os.close(fd) (fd, filename2) = tempfile.mkstemp(prefix='bleachbit-test-cleaner') os.close(fd) self.assert_(os.path.exists(filename1)) self.assert_(os.path.exists(filename2)) cleaner = create_simple_cleaner([filename1, filename2]) 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))
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 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.assert_(not isinstance(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): print "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.assert_(tmp_path not in 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.assert_(not isinstance(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(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)