class Sandbox(): """ A sandbox where executing commands under tests """ def __init__(self): self.path=Path("./sandbox") self.path.remove() self.path.mkdirs() self.trashdir = HomeTrashDirectory( Path('./sandbox/home/.local/share/Trash')) def create_file(self, path, content=None): """ Create a file in sandbox with content """ file=self.path.join(path) file.touch() if content!=None : file.write_file(content) return file def trash(self, path): """ Trash the file in the trash dir at sandbox/home/.local/share/Trash """ result = self.trashdir.trash(path) # sanity check assert not path.exists() return result
class TestTrashDirectory_persit_trash_info(TestCase) : def setUp(self): self.trashdirectory_base_dir = Path(os.path.realpath("./sandbox/testTrashDirectory")) self.trashdirectory_base_dir.remove() self.instance=TrashDirectory(self.trashdirectory_base_dir, Volume(Path("/"))) def test_persist_trash_info_first_time(self): trash_info=TrashInfo(Path("dummy-path"), datetime(2007,01,01)) basename=trash_info.path.basename content=trash_info.render() (trash_info_file, trash_info_id)=self.instance.persist_trash_info(basename,content) self.assertTrue(isinstance(trash_info_file, Path)) self.assertEquals('dummy-path', trash_info_id) self.assertEquals(self.trashdirectory_base_dir.join('info').join('dummy-path.trashinfo').path, trash_info_file) self.assertEquals("""[Trash Info] Path=dummy-path DeletionDate=2007-01-01T00:00:00 """, trash_info_file.read()) def test_persist_trash_info_first_100_times(self): self.test_persist_trash_info_first_time() for i in range(1,100) : trash_info=TrashInfo(Path("dummy-path"), datetime(2007,01,01)) basename=trash_info.path.basename content=trash_info.render() (trash_info_file, trash_info_id)=self.instance.persist_trash_info(basename,content) self.assertTrue(isinstance(trash_info_id, str)) self.assertEquals('dummy-path'+"_" + str(i), trash_info_id) self.assertEquals("""[Trash Info] Path=dummy-path DeletionDate=2007-01-01T00:00:00 """, trash_info_file.read()) def test_persist_trash_info_other_times(self): self.test_persist_trash_info_first_100_times() for i in range(101,200) : trash_info=TrashInfo(Path("dummy-path"), datetime(2007,01,01)) basename=trash_info.path.basename content=trash_info.render() (trash_info_file, trash_info_id)=self.instance.persist_trash_info(basename,content) self.assertTrue(isinstance(trash_info_id, str)) self.assertTrue(trash_info_id.startswith("dummy-path_")) self.assertEquals("""[Trash Info] Path=dummy-path DeletionDate=2007-01-01T00:00:00 """, trash_info_file.read())
def test_touch(self): instance=Path("sandbox/test-file") instance.remove() self.assertFalse(instance.exists()) instance.touch() self.assertTrue(instance.exists()) self.assertFalse(instance.isdir()) instance.remove() # clean up
def test_mkdir(self): Path("sandbox").mkdirs() instance=Path("sandbox/test-dir") instance.remove() self.assertFalse(instance.exists()) instance.mkdir() self.assertTrue(instance.exists()) self.assertTrue(instance.isdir()) instance.remove() # clean up
def test_list(self): instance=Path("sandbox/test-dir") instance.remove() instance.mkdirs() instance.join("file1").touch() instance.join("file2").touch() instance.join("file3").touch() result=instance.list() self.assertEquals("<type 'generator'>", str(type(result))) # is much easier test the content of a list than a generator result_as_list=list(result) self.assertEquals(3, len(result_as_list)) self.assertTrue(Path("sandbox/test-dir/file1") in result_as_list) self.assertTrue(Path("sandbox/test-dir/file1") in result_as_list) self.assertTrue(Path("sandbox/test-dir/file1") in result_as_list) # clean up instance.remove()
class RestoreTest(TestCase): def setUp(self): from common import create_cmdenv self.cmdenv = create_cmdenv() self.sandbox = Path("./sandbox").absolute() self.sandbox.remove() self.sandbox.mkdirs() self.trashdir = HomeTrashDirectory( Path('./sandbox/home/.local/share/Trash')) def create_file(self, path, content=None): """ Create a file in sandbox with content """ file=self.sandbox.join(path) file.touch() if content!=None : file.write_file(content) return file def trash(self, path): """ Trash the file in the trash dir at sandbox/home/.local/share/Trash """ result = self.trashdir.trash(path) # sanity check assert not path.exists() return result def test_version_option(self): """ $ trash-restore --version 0.2.1 """ raise SkipTest("trash-restore not yet ready") import re result = self.cmdenv.run('trash-restore','--version').assert_succeed() assert_equals("", result.err_data) expected = re.compile("trash-restore (\d)+\.(\d)+\.(\d)+") assert expected.match(result.out_data) is not None def test_restore_restores_trashed_file_absolute(self): from time import sleep """ $ trash-list 2009-01-12 12:00:00 /home/andrea/file 1977-01-12 12:00:00 /home/andrea/file $ trash-restore /home/andrea/file # restore the latest trashed one $ trash-list 1977-01-12 12:00:00 /home/andrea/file # the oldest remain in trashcan """ raise SkipTest("trash-restore not yet ready") # prepare foo_file = self.create_file('foo', "first") trashed_file1 = self.trash(foo_file) sleep(1) # to make sure that deletion dates differs foo_file = self.create_file('foo', "second") trashed_file2 = self.trash(foo_file) sleep(1) # to make sure that deletion dates differs foo_file = self.create_file('foo', "latest") trashed_file3 = self.trash(foo_file) assert_false(foo_file.exists()) print trashed_file1.deletion_date print trashed_file2.deletion_date print trashed_file3.deletion_date # execute self.cmd("trash-restore",foo_file.absolute()).assert_succeed() assert_true(foo_file.exists()) # File has been restored ? assert_equals("latest", foo_file.read()) # Is the latest deleted file? def test_restores_with_relative_name(self): """ $ trash-list 2009-01-12 12:00:00 /home/andrea/file $ cd /home/andrea $ trash-restore ./file """ raise SkipTest("trash-restore not yet ready") # prepare foo_file = self.create_file('file', "content") self.trash(foo_file) assert_false(foo_file.exists()) # execute self.cmdenv.run("trash-restore","./sandbox/file").assert_succeed() assert_true(foo_file.exists()) # File has been restored ? assert_equals("content", foo_file.read()) # Is the latest deleted file? def test_trashed_file_does_not_found(self): """ $ trash-restore non-existent trash-restore: cannot restore path `non-existent': " "Not found in any trash directory. """ raise SkipTest() # execute result = self.cmd(self.restore_cmd, 'non-existent').assert_fail() # test assert_equals(result.exit_code, 1) assert_equals(result.out_data, "") assert_equals(result.err_data, "trash-restore: cannot restore path `non-existent': " "Not found in any trash directory.") def test_overwrite_attempt(self): """ $ touch file $ trash-restore file trash-restore: cannot overwrite `file': File exists. """ raise SkipTest() # prepare self.create_file('existing-file') self.trash('existing-file') self.create_file('existing-file') # execute result = self.cmdenv.run('trash-restore', 'existing-file').assert_fail() # test assert_equals(result.exit_code, 1) assert_equals(result.out_data, "") assert_equals(result.err_data, "trash-restore: cannot overwrite`existing-file': " "File exists.") def test_overwrite_attempt_with_force_option(self): """ $ touch file $ trash-restore --force file #succeed """ raise SkipTest() # prepare self.create_file('existing-file') self.trash('existing-file') self.create_file('existing-file') # execute result = self.cmdenv.run('trash-restore', '--force', 'existing-file').assert_succeed # test assert_equals(result.exit_code, 0) assert_equals(result.out_data, "") assert_equals(result.err_data, "") def test_help_option(self): """ $ trash-restore --help Usage: trash-restore ... ... """ raise SkipTest() result = self.cmdenv.run('trash-restore', '--help').assert_succeed() assert_equals(result.exit_code, 0) assert_equals(result.out_data, """Usage: trash-restore TRASHED-FILE [NEW-LOCATION] Restore the TRASHED-FILE to its original location or to NEW-LOCATION. Options: --version show program's version number and exit -h, --help show this help message and exit -f, --force force overwrite -v, --verbose explain what is being done """) assert_equals(result.err_data, "") def test_issue_19(self): # bug: http://code.google.com/p/trash-cli/issues/detail?id=19 # also reported in: # https://bugs.launchpad.net/ubuntu/+source/trash-cli/+bug/310088 self.sandbox.join('dir').mkdir() self.sandbox.join("dir/file").touch() self.trash(Path('sandbox/dir/file')) self.sandbox.join('dir').remove() result=self.cmdenv.cmd('restore-trash').run("0") result.assert_result( exit_code=0, error="" ) assert_true(self.sandbox.child("dir").child("file").exists()) def test_overwrite(self): # Trash a file, then create a file of the same name in its place. # Restore the file, and ensure that it asks before overwriting. self.sandbox.join('testfile').touch() self.trash(Path('sandbox/testfile')) self.sandbox.join('testfile').touch() result = self.cmdenv.cmd('restore-trash').run('0') result.assert_result( exit_code=1, error='Refusing to overwrite existing file "testfile".\n', )