Skip to content

Commit

Permalink
squash! temp file handling part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tstenner committed Feb 6, 2017
1 parent f6d77e8 commit 77c3f78
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 198 deletions.
14 changes: 4 additions & 10 deletions tests/TestCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@


class CLITestCase(common.BleachbitTestCase):

"""Test case for module CLI"""

def setUp(self):
Expand Down Expand Up @@ -82,16 +81,13 @@ def test_cleaners_list(self):
def test_encoding(self):
"""Unit test for encoding"""

(fd, filename) = tempfile.mkstemp(
prefix='bleachbit-test-cli-encoding-\xe4\xf6\xfc~', dir='/tmp')
os.close(fd)
filename = self.write_file('/tmp/bleachbit-test-cli-encoding-\xe4\xf6\xfc~')
# 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
module = 'bleachbit.CLI'
args = [sys.executable, '-m', module, '-p', 'system.tmp']
args = [sys.executable, '-m', 'bleachbit.CLI', '-p', 'system.tmp']
# If Python pipes stdout to file or devnull, the test may give
# a false negative. It must print stdout to terminal.
self._test_preview(args, stdout=True, env=env)
Expand All @@ -104,8 +100,7 @@ def test_invalid_locale(self):
lang = os.environ['LANG']
os.environ['LANG'] = 'blahfoo'
# tests are run from the parent directory
module = 'bleachbit.CLI'
args = [sys.executable, '-m', module, '--version']
args = [sys.executable, '-m', 'bleachbit.CLI', '--version']
output = run_external(args)
self.assertNotEqual(output[1].find('Copyright'), -1, str(output))
os.environ['LANG'] = lang
Expand All @@ -127,8 +122,7 @@ def test_preview(self):

def test_delete(self):
"""Unit test for --delete option"""
(fd, filename) = tempfile.mkstemp(prefix='bleachbit-test-cli-delete')
os.close(fd)
filename = self.mkstemp(prefix='bleachbit-test-cli-delete')
if 'nt' == os.name:
import win32api
filename = os.path.normcase(filename)
Expand Down
13 changes: 2 additions & 11 deletions tests/TestCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,18 @@
"""
Test case for Command
"""


from __future__ import absolute_import, print_function

from tests import common
from bleachbit.Command import *

import tempfile


class CommandTestCase(common.BleachbitTestCase):

"""Test case for Command"""

def test_Delete(self, cls=Delete):
"""Unit test for Delete"""
(fd, path) = tempfile.mkstemp(prefix='bleachbit-test-command')
os.write(fd, "foo")
os.close(fd)
path = self.write_file('test_Delete', b'foo')
cmd = cls(path)
self.assertExists(path)

Expand All @@ -58,9 +51,7 @@ def test_Delete(self, cls=Delete):

def test_Function(self):
"""Unit test for Function"""
(fd, path) = tempfile.mkstemp(prefix='bleachbit-test-command')
os.write(fd, "foo")
os.close(fd)
path = self.write_file('test_Function', b'foo')
cmd = Function(path, FileUtilities.delete, 'bar')
self.assertExists(path)
self.assertGreater(os.path.getsize(path), 0)
Expand Down
31 changes: 13 additions & 18 deletions tests/TestCommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


class CommonTestCase(common.BleachbitTestCase):

"""Test case for Common."""

def test_expandvars(self):
Expand All @@ -43,36 +42,32 @@ def test_environment(self):
"""Test for important environment variables"""
# useful for researching
# grep -Poh "([\\$%]\w+)" cleaners/*xml | cut -b2- | sort | uniq -i
if 'posix' == os.name:
envs = ('XDG_DATA_HOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME',
'HOME')
else:
envs = ('AppData', 'CommonAppData', 'Documents', 'ProgramFiles',
'UserProfile', 'WinDir')
for env in envs:
envs = {'posix': ['XDG_DATA_HOME', 'XDG_CONFIG_HOME', 'XDG_CACHE_HOME', 'HOME'],
'nt': ['AppData', 'CommonAppData', 'Documents', 'ProgramFiles', 'UserProfile', 'WinDir']}
for env in envs[os.name]:
e = os.getenv(env)
self.assertIsNotNone(e)
self.assertTrue(len(e) > 4)
self.assertGreater(len(e), 4)

def test_expanduser(self):
"""Unit test for expanduser."""
# Return Unicode when given str.
var = bleachbit.expanduser('~')
self.assertIsUnicodeString(var)
self.assertIsUnicodeString(bleachbit.expanduser('~'))

# Return Unicode when given Unicode.
var = bleachbit.expanduser(u'~')
self.assertIsUnicodeString(var)
self.assertIsUnicodeString(bleachbit.expanduser(u'~'))

# Blank input should give blank output.
self.assertEqual(bleachbit.expanduser(''), u'')

# An absolute path should not be altered.
if 'posix' == os.name:
abs_dir = os.path.expandvars('$HOME')
if 'nt' == os.name:
abs_dir = os.path.expandvars('%USERPROFILE%')
abs_dirs = {'posix': '$HOME', 'nt': '%USERPROFILE%'}
abs_dir = os.path.expandvars(abs_dirs[os.name])
self.assertExists(abs_dir)
self.assertEqual(bleachbit.expanduser(abs_dir), abs_dir)
# Path with tilde should be expanded
self.assertEqual(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')
38 changes: 11 additions & 27 deletions tests/TestDeepScan.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,26 @@


class DeepScanTestCase(common.BleachbitTestCase):

"""Test Case for module DeepScan"""

def _test_encoding(self, fn):
"""Test encoding"""

tempd = tempfile.mkdtemp(prefix='bleachbit-test-deepscan')
self.assertExists(tempd)

fullpath = os.path.join(tempd, fn)
common.touch_file(fullpath)
fullpath = self.write_file(fn)

ds = DeepScan()
ds.add_search(tempd, '^%s$' % fn)
ds.add_search(self.tempdir, '^%s$' % fn)
found = False
for ret in ds.scan():
if True == ret:
continue
self.assert_(ret == fullpath)
print(ret)
self.assertEqual(ret, fullpath)
found = True
self.assert_(found, "Did not find '%s'" % fullpath)
self.assertTrue(found, "Did not find '%s'" % fullpath)

os.unlink(fullpath)
self.assertNotExists(fullpath)
os.rmdir(tempd)
self.assertNotExists(tempd)

def test_encoding(self):
"""Test encoding"""
Expand All @@ -87,23 +81,19 @@ def test_delete(self):
"""Delete files in a test environment"""

# make some files
base = tempfile.mkdtemp(prefix='bleachbit-deepscan-test')
f_del1 = os.path.join(base, 'foo.txt.bbtestbak')
open(f_del1, 'w').close()
f_keep = os.path.join(base, 'foo.txt')
open(f_keep, 'w').close()
subdir = os.path.join(base, 'sub')
f_del1 = self.write_file('foo.txt.bbtestbak')
f_keep = self.write_file('foo.txt')
subdir = os.path.join(self.tempdir, 'sub')
os.mkdir(subdir)
f_del2 = os.path.join(base, 'sub/bar.ini.bbtestbak')
open(f_del2, 'w').close()
f_del2 = self.write_file('sub/bar.ini.bbtestbak')

# sanity check
self.assertExists(f_del1)
self.assertExists(f_keep)
self.assertExists(f_del2)

# run deep scan
astr = '<action command="delete" search="deep" regex="\.bbtestbak$" cache="false" path="%s"/>' % base
astr = '<action command="delete" search="deep" regex="\.bbtestbak$" cache="false" path="%s"/>' % self.tempdir
from tests import TestCleaner
cleaner = TestCleaner.action_to_cleaner(astr)
from bleachbit.Worker import backends, Worker
Expand All @@ -112,19 +102,13 @@ def test_delete(self):
from bleachbit import CLI
ui = CLI.CliCallback()
worker = Worker(ui, True, operations)
run = worker.run()
while run.next():
pass
list(worker.run())

# validate results

self.assertFalse(os.path.exists(f_del1))
self.assertExists(f_keep)
self.assertFalse(os.path.exists(f_del2))

# clean up
shutil.rmtree(base)

def test_normalized_walk_darwin(self):
import mock

Expand Down
Loading

0 comments on commit 77c3f78

Please sign in to comment.