示例#1
0
 def action_test_helper(self, command, special_expected, errors_expected,
                        bytes_expected_posix, count_deleted_posix,
                        bytes_expected_nt, count_deleted_nt):
     ui = CLI.CliCallback()
     (fd, filename) = tempfile.mkstemp(prefix='bleachbit-test-worker')
     os.write(fd, '123')
     os.close(fd)
     self.assert_(os.path.exists(filename))
     astr = '<action command="%s" path="%s"/>' % (command, filename)
     cleaner = TestCleaner.action_to_cleaner(astr)
     backends['test'] = cleaner
     operations = {'test': ['option1']}
     worker = Worker(ui, True, operations)
     run = worker.run()
     while run.next():
         pass
     self.assert_(not os.path.exists(filename),
                  "Path still exists '%s'" % filename)
     self.assertEqual(worker.total_special, special_expected,
                      'For command %s expecting %s special operations but observed %d'
                      % (command, special_expected, worker.total_special))
     self.assertEqual(worker.total_errors, errors_expected,
                      'For command %s expecting %d errors but observed %d'
                      % (command, errors_expected, worker.total_errors))
     if 'posix' == os.name:
         self.assertEqual(worker.total_bytes, bytes_expected_posix)
         self.assertEqual(worker.total_deleted, count_deleted_posix)
     elif 'nt' == os.name:
         self.assertEqual(worker.total_bytes, bytes_expected_nt)
         self.assertEqual(worker.total_deleted, count_deleted_nt)
示例#2
0
    def test_multiple_options(self):
        """Test one cleaner with two options"""
        ui = CLI.CliCallback()
        (fd, filename1) = tempfile.mkstemp(prefix='bleachbit-test-worker')
        os.close(fd)
        self.assert_(os.path.exists(filename1))
        (fd, filename2) = tempfile.mkstemp(prefix='bleachbit-test-worker')
        os.close(fd)
        self.assert_(os.path.exists(filename2))

        astr1 = '<action command="delete" search="file" path="%s"/>' % filename1
        astr2 = '<action command="delete" search="file" path="%s"/>' % filename2
        cleaner = TestCleaner.actions_to_cleaner([astr1, astr2])
        backends['test'] = cleaner
        operations = {'test': ['option1', 'option2']}
        worker = Worker(ui, True, operations)
        run = worker.run()
        while run.next():
            pass
        self.assert_(not os.path.exists(filename1),
                     "Path still exists '%s'" % filename1)
        self.assert_(not os.path.exists(filename2),
                     "Path still exists '%s'" % filename2)
        self.assertEqual(worker.total_special, 0)
        self.assertEqual(worker.total_errors, 0)
        self.assertEqual(worker.total_deleted, 2)
示例#3
0
 def test_TestActionProvider(self):
     """Test Worker using Action.TestActionProvider"""
     ui = CLI.CliCallback()
     (fd, filename) = tempfile.mkstemp('bleachbit-test')
     os.write(fd, '123')
     os.close(fd)
     self.assert_(os.path.exists(filename))
     astr = '<action command="test" path="%s"/>' % filename
     cleaner = TestCleaner.action_to_cleaner(astr)
     backends['test'] = cleaner
     operations = {'test': ['option1']}
     worker = Worker(ui, True, operations)
     run = worker.run()
     while run.next():
         pass
     self.assert_(not os.path.exists(filename),
                  "Path still exists '%s'" % filename)
     self.assertEqual(worker.total_special, 3)
     self.assertEqual(worker.total_errors, 2)
     if 'posix' == os.name:
         self.assertEqual(worker.total_bytes, 4096 + 10 + 10)
         self.assertEqual(worker.total_deleted, 3)
     elif 'nt' == os.name:
         self.assertEqual(worker.total_bytes, 3 + 3 + 10 + 10)
         self.assertEqual(worker.total_deleted, 4)
示例#4
0
    def test_deep_scan(self):
        """Test for deep scan"""

        # load cleaners from XML
        import bleachbit.CleanerML
        bleachbit.CleanerML.load_cleaners()

        # DeepScan itself is tested elsewhere, so replace it here
        import bleachbit.DeepScan
        SaveDeepScan = bleachbit.DeepScan.DeepScan
        self.scanned = 0
        parent = self

        class MyDeepScan:
            def add_search(self, dirname, regex):
                parent.assertEqual(dirname, expanduser('~'))
                parent.assertIn(regex, ['^Thumbs\\.db$', '^Thumbs\\.db:encryptable$'])

            def scan(self):
                parent.scanned+=1
                yield True

        bleachbit.DeepScan.DeepScan = MyDeepScan

        # test
        operations = {'deepscan': ['thumbs_db']}
        ui = CLI.CliCallback()
        worker = Worker(ui, False, operations).run()
        while worker.next():
            pass
        self.assertEqual(1, self.scanned)

        # clean up
        bleachbit.DeepScan.DeepScan = SaveDeepScan
示例#5
0
    def test_delete(self):
        """Delete files in a test environment"""

        # make some files
        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 = self.write_file(os.path.join(subdir, '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"/>' % self.tempdir
        from tests import TestCleaner
        cleaner = TestCleaner.action_to_cleaner(astr)
        from bleachbit.Worker import backends, Worker
        backends['test'] = cleaner
        operations = {'test': ['option1']}
        from bleachbit import CLI
        ui = CLI.CliCallback()
        worker = Worker(ui, True, operations)
        list(worker.run())

        # validate results
        self.assertFalse(os.path.exists(f_del1))
        self.assertExists(f_keep)
        self.assertFalse(os.path.exists(f_del2))
示例#6
0
 def run_deep_scan(regex):
     astr = '<action command="{}" search="deep" cache="false" path="{}" {}/>'.format(
         command, self.tempdir, regex)
     from tests import TestCleaner
     cleaner = TestCleaner.action_to_cleaner(astr)
     from bleachbit.Worker import backends, Worker
     backends['test'] = cleaner
     operations = {'test': ['option1']}
     from bleachbit import CLI
     ui = CLI.CliCallback()
     worker = Worker(ui, True, operations)
     list(worker.run())
     del backends['test']
示例#7
0
    def test_delete(self):
        """Delete files in a test environment"""

        # make some files
        base = os.path.expanduser('~/bleachbit-deepscan-test')
        if os.path.exists(base):
            shutil.rmtree(base)
        os.mkdir(base)
        f_del1 = os.path.join(base, 'foo.txt.bbtestbak')
        file(f_del1, 'w').write('')
        f_keep = os.path.join(base, 'foo.txt')
        file(f_keep, 'w').write('')
        subdir = os.path.join(base, 'sub')
        os.mkdir(subdir)
        f_del2 = os.path.join(base, 'sub/bar.ini.bbtestbak')
        file(f_del2, 'w').write('')

        # sanity check
        self.assert_(os.path.exists(f_del1))
        self.assert_(os.path.exists(f_keep))
        self.assert_(os.path.exists(f_del2))

        # run deep scan
        astr = '<action command="delete" search="deep" regex="\.bbtestbak$" cache="false"/>'
        import TestCleaner
        cleaner = TestCleaner.action_to_cleaner(astr)
        from bleachbit.Worker import backends, Worker
        backends['test'] = cleaner
        operations = {'test': ['option1']}
        from bleachbit import CLI
        ui = CLI.CliCallback()
        worker = Worker(ui, True, operations)
        run = worker.run()
        while run.next():
            pass

        # validate results
        self.assert_(not os.path.exists(f_del1))
        self.assert_(os.path.exists(f_keep))
        self.assert_(not os.path.exists(f_del2))

        # clean up
        shutil.rmtree(base)
示例#8
0
    def test_multiple_options(self):
        """Test one cleaner with two options"""
        ui = CLI.CliCallback()
        filename1 = self.mkstemp(prefix='bleachbit-test-worker')
        filename2 = self.mkstemp(prefix='bleachbit-test-worker')

        astr1 = '<action command="delete" search="file" path="%s"/>' % filename1
        astr2 = '<action command="delete" search="file" path="%s"/>' % filename2
        cleaner = TestCleaner.actions_to_cleaner([astr1, astr2])
        backends['test'] = cleaner
        operations = {'test': ['option1', 'option2']}
        worker = Worker(ui, True, operations)
        run = worker.run()
        while next(run):
            pass
        del backends['test']
        self.assertNotExists(filename1)
        self.assertNotExists(filename2)
        self.assertEqual(worker.total_special, 0)
        self.assertEqual(worker.total_errors, 0)
        self.assertEqual(worker.total_deleted, 2)
示例#9
0
    def test_deep_scan(self):
        """Test for deep scan"""

        # load cleaners from XML
        import bleachbit.CleanerML
        list(bleachbit.CleanerML.load_cleaners())

        # DeepScan itself is tested elsewhere, so replace it here
        import bleachbit.DeepScan
        SaveDeepScan = bleachbit.DeepScan.DeepScan
        self.scanned = 0
        parent = self

        class MyDeepScan:
            def __init__(self, searches):
                for (path, searches) in searches.items():
                    parent.assertEqual(path, os.path.expanduser('~'))
                    for s in searches:
                        parent.assertIn(
                            s.regex,
                            ['^Thumbs\\.db$', '^Thumbs\\.db:encryptable$'])

            def scan(self):
                parent.scanned += 1
                yield True

        bleachbit.DeepScan.DeepScan = MyDeepScan

        # test
        operations = {'deepscan': ['thumbs_db']}
        ui = CLI.CliCallback()
        worker = Worker(ui, False, operations).run()
        while next(worker):
            pass
        self.assertEqual(1, self.scanned)

        # clean up
        bleachbit.DeepScan.DeepScan = SaveDeepScan