1
0
mirror of https://github.com/arsenetar/dupeguru.git synced 2024-11-18 04:59:03 +00:00
dupeguru/base/py/tests/app_test.py
hsoft d1f460b091 Moving the 'py' folder into 'base'.
--HG--
rename : py/__init__.py => base/py/__init__.py
rename : py/app.py => base/py/app.py
rename : py/app_cocoa.py => base/py/app_cocoa.py
rename : py/app_me_cocoa.py => base/py/app_me_cocoa.py
rename : py/app_pe_cocoa.py => base/py/app_pe_cocoa.py
rename : py/app_se_cocoa.py => base/py/app_se_cocoa.py
rename : py/data.py => base/py/data.py
rename : py/data_me.py => base/py/data_me.py
rename : py/data_pe.py => base/py/data_pe.py
rename : py/directories.py => base/py/directories.py
rename : py/engine.py => base/py/engine.py
rename : py/export.py => base/py/export.py
rename : py/gen.py => base/py/gen.py
rename : py/ignore.py => base/py/ignore.py
rename : py/modules/block/block.pyx => base/py/modules/block/block.pyx
rename : py/modules/block/setup.py => base/py/modules/block/setup.py
rename : py/modules/cache/cache.pyx => base/py/modules/cache/cache.pyx
rename : py/modules/cache/setup.py => base/py/modules/cache/setup.py
rename : py/picture/__init__.py => base/py/picture/__init__.py
rename : py/picture/block.py => base/py/picture/block.py
rename : py/picture/cache.py => base/py/picture/cache.py
rename : py/picture/matchbase.py => base/py/picture/matchbase.py
rename : py/results.py => base/py/results.py
rename : py/scanner.py => base/py/scanner.py
rename : py/tests/__init__.py => base/py/tests/__init__.py
rename : py/tests/app_cocoa_test.py => base/py/tests/app_cocoa_test.py
rename : py/tests/app_test.py => base/py/tests/app_test.py
rename : py/tests/block_test.py => base/py/tests/block_test.py
rename : py/tests/cache_test.py => base/py/tests/cache_test.py
rename : py/tests/directories_test.py => base/py/tests/directories_test.py
rename : py/tests/engine_test.py => base/py/tests/engine_test.py
rename : py/tests/export_test.py => base/py/tests/export_test.py
rename : py/tests/ignore_test.py => base/py/tests/ignore_test.py
rename : py/tests/results_test.py => base/py/tests/results_test.py
rename : py/tests/scanner_test.py => base/py/tests/scanner_test.py
extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%4074
2009-06-18 18:55:00 +00:00

130 lines
5.4 KiB
Python

# Unit Name: dupeguru.tests.app_test
# Created By: Virgil Dupras
# Created On: 2007-06-23
# $Id$
# Copyright 2009 Hardcoded Software (http://www.hardcoded.net)
import os
from hsutil.testcase import TestCase
from hsutil import io
from hsutil.path import Path
from hsutil.decorators import log_calls
import hsfs as fs
import hsfs.phys
import hsutil.files
from hsutil.job import nulljob
from .. import data, app
from ..app import DupeGuru as DupeGuruBase
class DupeGuru(DupeGuruBase):
def __init__(self):
DupeGuruBase.__init__(self, data, '/tmp', appid=4)
def _start_job(self, jobid, func):
func(nulljob)
class TCDupeGuru(TestCase):
cls_tested_module = app
def test_apply_filter_calls_results_apply_filter(self):
app = DupeGuru()
self.mock(app.results, 'apply_filter', log_calls(app.results.apply_filter))
app.apply_filter('foo')
self.assertEqual(2, len(app.results.apply_filter.calls))
call = app.results.apply_filter.calls[0]
self.assert_(call['filter_str'] is None)
call = app.results.apply_filter.calls[1]
self.assertEqual('foo', call['filter_str'])
def test_apply_filter_escapes_regexp(self):
app = DupeGuru()
self.mock(app.results, 'apply_filter', log_calls(app.results.apply_filter))
app.apply_filter('()[]\\.|+?^abc')
call = app.results.apply_filter.calls[1]
self.assertEqual('\\(\\)\\[\\]\\\\\\.\\|\\+\\?\\^abc', call['filter_str'])
app.apply_filter('(*)') # In "simple mode", we want the * to behave as a wilcard
call = app.results.apply_filter.calls[3]
self.assertEqual('\(.*\)', call['filter_str'])
app.options['escape_filter_regexp'] = False
app.apply_filter('(abc)')
call = app.results.apply_filter.calls[5]
self.assertEqual('(abc)', call['filter_str'])
def test_copy_or_move(self):
# The goal here is just to have a test for a previous blowup I had. I know my test coverage
# for this unit is pathetic. What's done is done. My approach now is to add tests for
# every change I want to make. The blowup was caused by a missing import.
dupe_parent = fs.Directory(None, 'foo')
dupe = fs.File(dupe_parent, 'bar')
dupe.copy = log_calls(lambda dest, newname: None)
self.mock(hsutil.files, 'copy', log_calls(lambda source_path, dest_path: None))
self.mock(os, 'makedirs', lambda path: None) # We don't want the test to create that fake directory
self.mock(fs.phys, 'Directory', fs.Directory) # We don't want an error because makedirs didn't work
app = DupeGuru()
app.copy_or_move(dupe, True, 'some_destination', 0)
self.assertEqual(1, len(hsutil.files.copy.calls))
call = hsutil.files.copy.calls[0]
self.assertEqual('some_destination', call['dest_path'])
self.assertEqual(dupe.path, call['source_path'])
def test_copy_or_move_clean_empty_dirs(self):
tmppath = Path(self.tmpdir())
sourcepath = tmppath + 'source'
io.mkdir(sourcepath)
io.open(sourcepath + 'myfile', 'w')
tmpdir = hsfs.phys.Directory(None, unicode(tmppath))
myfile = tmpdir['source']['myfile']
app = DupeGuru()
self.mock(app, 'clean_empty_dirs', log_calls(lambda path: None))
app.copy_or_move(myfile, False, tmppath + 'dest', 0)
calls = app.clean_empty_dirs.calls
self.assertEqual(1, len(calls))
self.assertEqual(sourcepath, calls[0]['path'])
def test_Scan_with_objects_evaluating_to_false(self):
# At some point, any() was used in a wrong way that made Scan() wrongly return 1
app = DupeGuru()
f1, f2 = [fs.File(None, 'foo') for i in range(2)]
f1.is_ref, f2.is_ref = (False, False)
assert not (bool(f1) and bool(f2))
app.directories.get_files = lambda: [f1, f2]
app.directories._dirs.append('this is just so Scan() doesnt return 3')
app.start_scanning() # no exception
class TCDupeGuru_clean_empty_dirs(TestCase):
cls_tested_module = app
def setUp(self):
self.mock(hsutil.files, 'delete_if_empty', log_calls(lambda path, files_to_delete=[]: None))
self.app = DupeGuru()
def test_option_off(self):
self.app.clean_empty_dirs(Path('/foo/bar'))
self.assertEqual(0, len(hsutil.files.delete_if_empty.calls))
def test_option_on(self):
self.app.options['clean_empty_dirs'] = True
self.app.clean_empty_dirs(Path('/foo/bar'))
calls = hsutil.files.delete_if_empty.calls
self.assertEqual(1, len(calls))
self.assertEqual(Path('/foo/bar'), calls[0]['path'])
self.assertEqual(['.DS_Store'], calls[0]['files_to_delete'])
def test_recurse_up(self):
# delete_if_empty must be recursively called up in the path until it returns False
@log_calls
def mock_delete_if_empty(path, files_to_delete=[]):
return len(path) > 1
self.mock(hsutil.files, 'delete_if_empty', mock_delete_if_empty)
self.app.options['clean_empty_dirs'] = True
self.app.clean_empty_dirs(Path('not-empty/empty/empty'))
calls = hsutil.files.delete_if_empty.calls
self.assertEqual(3, len(calls))
self.assertEqual(Path('not-empty/empty/empty'), calls[0]['path'])
self.assertEqual(Path('not-empty/empty'), calls[1]['path'])
self.assertEqual(Path('not-empty'), calls[2]['path'])