Skip to content

Commit

Permalink
temp file handling part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tstenner committed Feb 5, 2017
1 parent 7c6c6b9 commit f6d77e8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 56 deletions.
20 changes: 6 additions & 14 deletions tests/TestAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ def test_delete(self):
for path in paths:
for mode in ('delete', 'truncate', 'delete_forward'):
expanded = expanduser(expandvars(path))
(fd, filename) = tempfile.mkstemp(
dir=expanded, prefix='bleachbit-action-delete')
os.close(fd)
filename = self.mkstemp(dir=expanded, prefix='bleachbit-action-delete')
command = mode
if 'delete_forward' == mode:
# forward slash needs to be normalized on Windows
Expand All @@ -166,19 +164,16 @@ def test_delete(self):

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)
pathname = self.write_file(test)
action_str = u'<action command="delete" search="file" path="%s" />' % pathname
self._test_action_str(action_str)
self.assertNotExists(pathname)
os.rmdir(dirname)

def test_ini(self):
"""Unit test for class Ini"""
Expand Down Expand Up @@ -207,10 +202,7 @@ def execute_json(path, address):

def test_process(self):
"""Unit test for process action"""
if 'nt' == os.name:
cmd = 'cmd.exe /c dir'
if 'posix' == os.name:
cmd = 'dir'
cmds = {'nt': 'cmd.exe /c dir', 'posix': 'dir'}
tests = [u'<action command="process" cmd="%s" />',
u'<action command="process" wait="false" cmd="%s" />',
u'<action command="process" wait="f" cmd="%s" />',
Expand All @@ -219,7 +211,7 @@ def test_process(self):
]

for test in tests:
self._test_action_str(test % cmd)
self._test_action_str(test % cmds[os.name])

def test_regex(self):
"""Unit test for regex option"""
Expand Down Expand Up @@ -308,7 +300,7 @@ def test_wholeregex(self):

def test_type(self):
"""Unit test for type attribute"""
dirname = tempfile.mkdtemp(prefix='bleachbit-action-type')
dirname = self.mkdtemp(prefix='bleachbit-action-type')
filename = os.path.join(dirname, 'file')

# this should not delete anything
Expand Down Expand Up @@ -340,7 +332,7 @@ def test_type(self):

def test_walk_all(self):
"""Unit test for walk.all"""
dirname = tempfile.mkdtemp(prefix='bleachbit-walk-all')
dirname = self.mkdtemp(prefix='bleachbit-walk-all')

# this sub-directory should be deleted
subdir = os.path.join(dirname, 'sub')
Expand Down
48 changes: 19 additions & 29 deletions tests/TestCleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
from tests import common

import logging
import tempfile
import unittest
from xml.dom.minidom import parseString

logger = logging.getLogger('bleachbit')


def action_to_cleaner(action_str):
"""Given an action XML fragment, return a cleaner"""
return actions_to_cleaner([action_str])
Expand Down Expand Up @@ -69,25 +68,20 @@ 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\\"/>')
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.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.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:
Expand Down Expand Up @@ -115,27 +109,23 @@ def test_auto_hide(self):

def test_create_simple_cleaner(self):
"""Unit test for method create_simple_cleaner"""
dirname = tempfile.mkdtemp(
prefix='bleachbit-test-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)
self.assertExists(filename1)
self.assertExists(filename2)
cleaner = create_simple_cleaner([filename1, filename2, dirname])
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
for result in cmd.execute(True):
pass
list(cmd.execute(True))

self.assertNotExists(filename1)
self.assertNotExists(filename2)
self.assertNotExists(dirname)
for target in targets:
self.assertNotExists(target)

def test_get_name(self):
for key in sorted(backends):
Expand Down
15 changes: 3 additions & 12 deletions tests/TestCleanerML.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@
"""
Test cases for module CleanerML
"""


from __future__ import absolute_import, print_function

from tests import common
from bleachbit.CleanerML import *

import unittest


class CleanerMLTestCase(common.BleachbitTestCase):

"""Test cases for CleanerML"""

def test_CleanerML(self):
Expand Down Expand Up @@ -82,14 +77,10 @@ def test_load_cleaners(self):
load_cleaners()

# should catch exception with invalid XML
import tempfile
pcd = bleachbit.personal_cleaners_dir
bleachbit.personal_cleaners_dir = tempfile.mkdtemp(
prefix='bleachbit-cleanerml-load')
fn_xml = os.path.join(bleachbit.personal_cleaners_dir, 'invalid.xml')
f = open(fn_xml, 'w')
f.write('<xml><broken>')
f.close()
bleachbit.personal_cleaners_dir = self.mkdtemp(prefix='bleachbit-cleanerml-load')
self.write_file(os.path.join(bleachbit.personal_cleaners_dir, 'invalid.xml'),
contents='<xml><broken>')
load_cleaners()
import shutil
shutil.rmtree(bleachbit.personal_cleaners_dir)
Expand Down
11 changes: 10 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ def write_file(self, filename, contents=''):
assert (os.path.exists(filename))
return filename

def mkstemp(self, dir=None, **kwargs):
if dir is None:
dir = self.tempdir
(fd, filename) = tempfile.mkstemp(dir = dir, **kwargs)
os.close(fd)
return filename

def mkdtemp(self, **kwargs):
return tempfile.mkdtemp(dir=self.tempdir, **kwargs)


def getTestPath(path):
if 'nt' == os.name:
Expand All @@ -120,7 +130,6 @@ def touch_file(filename):
"""Create an empty file"""
with open(filename, "w") as f:
pass
import os.path
assert(os.path.exists(filename))


Expand Down

0 comments on commit f6d77e8

Please sign in to comment.