Skip to content

Commit

Permalink
Convert assert_s
Browse files Browse the repository at this point in the history
  • Loading branch information
tstenner committed Feb 5, 2017
1 parent 447a844 commit cf59f94
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 226 deletions.
43 changes: 19 additions & 24 deletions tests/TestAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def _test_action_str(self, action_str):
provider = actionplugin(action_node)
self.assertNotEqual(provider, None)
for cmd in provider.get_commands():
self.assert_(
isinstance(cmd, (Command.Delete, Command.Ini, Command.Json, Command.Function)))
self.assertIsInstance(cmd, (Command.Delete, Command.Ini, Command.Json, Command.Function))
if 'process' != command:
# process does not have a filename
self.assertLExists(filename)
Expand All @@ -131,8 +130,7 @@ def _test_action_str(self, action_str):
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)
self.assertTrue(dir_is_empty(filename), 'directory not empty after walk.all: %s' % filename)

def test_delete(self):
"""Unit test for class Delete"""
Expand Down Expand Up @@ -233,49 +231,48 @@ def test_regex(self):
# should match three files using no regexes
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" />'
results = _action_str_to_results(action_str)
self.assert_(3 == len(results))
self.assertEqual(len(results), 3)

# should match second file using positive regex
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" regex="^foo2$"/>'
results = _action_str_to_results(action_str)
self.assert_(1 == len(results))
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['path'], '/tmp/foo2')

# On Windows should be case insensitive
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" regex="^FOO2$"/>'
results = _action_str_to_results(action_str)
if 'nt' == os.name:
self.assert_(1 == len(results))
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['path'], '/tmp/foo2')
else:
self.assert_(0 == len(results))
self.assertEqual(len(results), 0)

# should match second file using negative regex
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" nregex="^(foo1|bar1)$"/>'
results = _action_str_to_results(action_str)
self.assert_(1 == len(results))
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['path'], '/tmp/foo2')

# should match second file using both regexes
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" regex="^f" nregex="1$"/>'
results = _action_str_to_results(action_str)
self.assert_(1 == len(results))
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['path'], '/tmp/foo2')

# should match nothing using positive regex
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" regex="^bar$"/>'
results = _action_str_to_results(action_str)
self.assert_(0 == len(results))
self.assertEqual(len(results), 0)

# should match nothing using negative regex
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" nregex="."/>'
results = _action_str_to_results(action_str)
self.assert_(0 == len(results))
self.assertEqual(len(results), 0)

# should give an error
action_str = u'<action command="delete" search="invalid" path="/tmp/foo*" regex="^bar$"/>'
self.assertRaises(
RuntimeError, lambda: _action_str_to_results(action_str))
self.assertRaises(RuntimeError, lambda: _action_str_to_results(action_str))

# clean up
glob.iglob = _iglob
Expand All @@ -291,18 +288,18 @@ def test_wholeregex(self):
# should match three files using no regexes
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" />'
results = _action_str_to_results(action_str)
self.assert_(3 == len(results))
self.assertEqual(len(results), 3)

# should match two files using wholeregex
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" wholeregex="^/tmp/foo.*$"/>'
results = _action_str_to_results(action_str)
self.assert_(2 == len(results))
self.assertEqual(len(results), 2)
self.assertEqual(results[0]['path'], '/tmp/foo1')

# should match third file using nwholeregex
action_str = u'<action command="delete" search="glob" path="/tmp/foo*" nwholeregex="^/tmp/foo.*$"/>'
results = _action_str_to_results(action_str)
self.assert_(1 == len(results))
self.assertEqual(len(results), 1)
self.assertEqual(results[0]['path'], '/tmp/bar1')

# clean up
Expand Down Expand Up @@ -362,19 +359,17 @@ def test_walk_all(self):

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 = u'<action command="delete" search="walk.files" path="%s" />' % path
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.assert_(not os.path.isdir(path), "%s is a directory" % path)
self.assertFalse(os.path.isdir(path), "%s is a directory" % path)
results += 1
self.assert_(results > 0)
self.assertGreater(results, 0)


def suite():
Expand Down
26 changes: 11 additions & 15 deletions tests/TestCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ def test_args_to_operations(self):
def test_cleaners_list(self):
"""Unit test for cleaners_list()"""
for cleaner in cleaners_list():
self.assert_(
isinstance(
cleaner,
str) or isinstance(
cleaner,
unicode))
self.assertIsString(cleaner)

@unittest.skipUnless('posix' == os.name, 'skipping on non-Unix')
def test_encoding(self):
Expand All @@ -90,7 +85,8 @@ def test_encoding(self):
(fd, filename) = tempfile.mkstemp(
prefix='bleachbit-test-cli-encoding-\xe4\xf6\xfc~', dir='/tmp')
os.close(fd)
self.assert_(os.path.exists(filename))
# not assertExists because it doesn't cope with invalid encodings
self.assertTrue(os.path.exists(filename))

env = copy.deepcopy(os.environ)
env['LANG'] = 'en_US' # not UTF-8
Expand All @@ -101,7 +97,7 @@ def test_encoding(self):
self._test_preview(args, stdout=True, env=env)

os.remove(filename)
self.assert_(not os.path.exists(filename))
self.assertNotExists(filename)

def test_invalid_locale(self):
"""Unit test for invalid locales"""
Expand Down Expand Up @@ -141,18 +137,17 @@ def test_delete(self):
deleted_paths = []

def dummy_delete(path, shred=False):
self.assert_(os.path.exists(path))
self.assertExists(path)
deleted_paths.append(os.path.normcase(path))
FileUtilities.delete = dummy_delete
FileUtilities.delete(filename)
self.assert_(os.path.exists(filename))
self.assertExists(filename)
operations = args_to_operations(['system.tmp'], False)
preview_or_clean(operations, True)
FileUtilities.delete = save_delete
self.assert_(filename in deleted_paths,
"%s not found deleted" % filename)
self.assertIn(filename, deleted_paths, "%s not found deleted" % filename)
os.remove(filename)
self.assert_(not os.path.exists(filename))
self.assertNotExists(filename)

def test_shred(self):
"""Unit test for --shred"""
Expand All @@ -165,7 +160,8 @@ def test_shred(self):
os.close(fd)
if '.' == dir_:
filename = os.path.basename(filename)
self.assert_(os.path.exists(filename))
self.assertExists(filename)
args = [sys.executable, '-m', 'bleachbit.CLI', '--shred', filename]
output = run_external(args, stdout=open(os.devnull, 'w'))
self.assert_(not os.path.exists(filename))
self.assertNotExists(filename)

40 changes: 19 additions & 21 deletions tests/TestCleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_add_action(self):
self.actions.append(
'<action command="delete" search="walk.all" path="/var/log/"/>')

self.assert_(len(self.actions) > 0)
self.assertGreater(len(self.actions), 0)

for action_str in self.actions:
cleaner = action_to_cleaner(action_str)
Expand All @@ -97,22 +97,21 @@ def test_add_action(self):
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)
self.assertLExists(pathname, "Does not exist: '%s'" % pathname)
count += 1
common.validate_result(self, result)
self.assert_(count > 0, "No files found for %s" % action_str)
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)
self.assert_(False, 'option2 should yield nothing')
raise AssertionError('option2 should yield nothing')
# should fail
self.assertRaises(RuntimeError, cleaner.get_commands('option3').next)

def test_auto_hide(self):
for key in sorted(backends):
self.assert_(isinstance(backends[key].auto_hide(), bool))
self.assertIsInstance(backends[key].auto_hide(), bool)

def test_create_simple_cleaner(self):
"""Unit test for method create_simple_cleaner"""
Expand All @@ -123,8 +122,8 @@ def test_create_simple_cleaner(self):
# 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))
self.assertExists(filename1)
self.assertExists(filename2)
cleaner = create_simple_cleaner([filename1, filename2, dirname])
for cmd in cleaner.get_commands('files'):
# preview
Expand All @@ -134,28 +133,27 @@ def test_create_simple_cleaner(self):
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))
self.assertNotExists(filename1)
self.assertNotExists(filename2)
self.assertNotExists(dirname)

def test_get_name(self):
for key in sorted(backends):
self.assert_(isinstance(backends[key].get_name(), (str, unicode)))
self.assertIsString(backends[key].get_name())

def test_get_description(self):
for key in sorted(backends):
self.assert_(isinstance(key, (str, unicode)))
self.assert_(isinstance(backends[key], Cleaner))
self.assertIsString(key)
self.assertIsInstance(backends[key], Cleaner)
desc = backends[key].get_description()
self.assert_(isinstance(desc, (str, unicode, type(None))),
"description for '%s' is '%s'" % (key, desc))
if desc is not None:
self.assertIsString(desc, "description for '%s' is '%s'" % (key, desc))

def test_get_options(self):
for key in sorted(backends):
for (test_id, name) in backends[key].get_options():
self.assert_(isinstance(test_id, (str, unicode)),
'%s.%s is not a string' % (key, test_id))
self.assert_(isinstance(name, (str, unicode)))
self.assertIsString(test_id, '%s.%s is not a string' % (key, test_id))
self.assertIsString(name)

def test_get_commands(self):
for key in sorted(backends):
Expand All @@ -180,7 +178,7 @@ def get_files(option_id):
trash_paths = get_files('trash')
tmp_paths = get_files('tmp')
for tmp_path in tmp_paths:
self.assert_(tmp_path not in trash_paths)
self.assertNotIn(tmp_path, trash_paths)

def test_no_files_exist(self):
"""Verify only existing files are returned"""
Expand All @@ -200,7 +198,7 @@ def test_no_files_exist(self):
break
msg = "Expected no files to be deleted but got '%s'" % str(
result)
self.assert_(not isinstance(cmd, Command.Delete), msg)
self.assertNotIsInstance(cmd, Command.Delete, msg)
common.validate_result(self, result)
glob.iglob = _iglob
os.path.exists = _exists
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCleanerML.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_CleanerML(self):
os.chdir('tests')
xmlcleaner = CleanerML("../doc/example_cleaner.xml")

self.assert_(isinstance(xmlcleaner, CleanerML))
self.assert_(isinstance(xmlcleaner.cleaner, Cleaner.Cleaner))
self.assertIsInstance(xmlcleaner, CleanerML)
self.assertIsInstance(xmlcleaner.cleaner, Cleaner.Cleaner)

def run_all(really_delete):
for (option_id, __name) in xmlcleaner.cleaner.get_options():
Expand Down Expand Up @@ -97,4 +97,4 @@ def test_load_cleaners(self):

def test_pot_fragment(self):
"""Unit test for pot_fragment()"""
self.assert_(isinstance(pot_fragment("Foo", 'bar.xml'), str))
self.assertIsString(pot_fragment("Foo", 'bar.xml'))
22 changes: 11 additions & 11 deletions tests/TestCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,40 @@ def test_Delete(self, cls=Delete):
os.write(fd, "foo")
os.close(fd)
cmd = cls(path)
self.assert_(os.path.exists(path))
self.assertExists(path)

# preview
ret = cmd.execute(really_delete=False).next()
s = str(cmd)
self.assert_(ret['size'] > 0)
self.assertGreater(ret['size'], 0)
self.assertEqual(ret['path'], path)
self.assert_(os.path.exists(path))
self.assertExists(path)

# delete
ret = cmd.execute(really_delete=True).next()
self.assert_(ret['size'] > 0)
self.assertGreater(ret['size'], 0)
self.assertEqual(ret['path'], path)
self.assert_(not os.path.exists(path))
self.assertNotExists(path)

def test_Function(self):
"""Unit test for Function"""
(fd, path) = tempfile.mkstemp(prefix='bleachbit-test-command')
os.write(fd, "foo")
os.close(fd)
cmd = Function(path, FileUtilities.delete, 'bar')
self.assert_(os.path.exists(path))
self.assert_(os.path.getsize(path) > 0)
self.assertExists(path)
self.assertGreater(os.path.getsize(path), 0)

# preview
ret = cmd.execute(False).next()
self.assert_(os.path.exists(path))
self.assert_(os.path.getsize(path) > 0)
self.assertExists(path)
self.assertGreater(os.path.getsize(path), 0)

# delete
ret = cmd.execute(True).next()
self.assert_(ret['size'] > 0, 'Size is %d' % ret['size'])
self.assertGreater(ret['size'], 0)
self.assertEqual(ret['path'], path)
self.assert_(not os.path.exists(path))
self.assertNotExists(path)

def test_Shred(self):
"""Unit test for Shred"""
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CommonTestCase(common.BleachbitTestCase):
def test_expandvars(self):
"""Unit test for expandvars."""
var = bleachbit.expandvars('$HOME')
self.assertIsInstance(var, unicode)
self.assertIsUnicodeString(var)

def test_environment(self):
"""Test for important environment variables"""
Expand All @@ -58,10 +58,10 @@ def test_expanduser(self):
"""Unit test for expanduser."""
# Return Unicode when given str.
var = bleachbit.expanduser('~')
self.assertIsInstance(var, unicode)
self.assertIsUnicodeString(var)
# Return Unicode when given Unicode.
var = bleachbit.expanduser(u'~')
self.assertIsInstance(var, unicode)
self.assertIsUnicodeString(var)
# Blank input should give blank output.
self.assertEqual(bleachbit.expanduser(''), u'')
# An absolute path should not be altered.
Expand All @@ -72,7 +72,7 @@ def test_expanduser(self):
self.assertExists(abs_dir)
self.assertEqual(bleachbit.expanduser(abs_dir), abs_dir)
# Path with tilde should be expanded
self.assertTrue(os.path.normpath(bleachbit.expanduser('~')), os.path.normpath(os.path.expanduser('~')))
self.assertEqual(os.path.normpath(bleachbit.expanduser('~')), os.path.normpath(os.path.expanduser('~')))
# A relative path (without a reference to the home directory)
# should not be expanded.
self.assertEqual(bleachbit.expanduser('common'), 'common')
Loading

0 comments on commit cf59f94

Please sign in to comment.