Пример #1
0
    def test_list(self, dict_mock):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='abc')
        self.fs.CreateFile('/test/file3', contents='abc')

        core.update_index(self.index, ['/test'])
        results = core.list_duplicates(self.index)
        self.assertSetEqual(set(results), {'/test/file2', '/test/file3'})
Пример #2
0
    def test_list_duplicates_with_originals_pairs(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='abc')

        core.update_index(self.index, ['/test'])
        results = core.list_duplicates_with_originals(self.index)
        self.assertEqual(1, len(results))
        self.assertSetEqual(set(results[0]), {'/test/file1', '/test/file2'})
Пример #3
0
    def test_list_no_duplicates_with_originals(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file3', contents='defg')
        self.fs.CreateFile('/test/file4', contents='hijk')

        core.update_index(self.index, ['/test'])
        results = core.list_duplicates_with_originals(self.index)
        self.assertEqual(0, len(results))
Пример #4
0
    def test_update_with_dirs_handles_disappearing_files(self):
        self.fs.CreateFile('/test/blahfile', contents='abc')

        with patch('dupi.index.os.path',
                   side_effect=self._delete_file_before_stat):
            core.update_index(self.index, ['/test'])

        # Nothing added .. the file disappeared
        self.assertEqual(len(self.index.all()), 0)
Пример #5
0
    def test_update_index_deletes_removed_files(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='def')

        core.update_index(self.index, ['/test'])

        self.fs.RemoveObject('/test/file2')

        core.update_index(self.index, ['/test'])

        self.assertEqual(1, len(self.index.all()))
Пример #6
0
    def test_update_empty_handles_disappearing_files(self):
        self.fs.CreateFile('/test/blahfile', contents='abc')

        core.update_index(self.index, ['/test'])
        self.assertEqual(len(self.index.all()), 1)

        with patch('dupi.index.os.stat',
                   side_effect=self._delete_file_before_stat):
            core.update_index(self.index, [])

        self.assertEqual(len(self.index.all()), 0)
Пример #7
0
    def test_update_index_without_change(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='def')
        core.update_index(self.index, ['/test'])

        orig = dict()
        for i in self.index.all():
            orig[i['fullpath']] = i

        core.update_index(self.index, ['/test'])

        for i in self.index.all():
            self.assertEqual(i, orig[i['fullpath']])
Пример #8
0
    def test_list_with_originals(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='abc')
        self.fs.CreateFile('/test/file3', contents='defg')
        self.fs.CreateFile('/test/file4', contents='hijk')
        self.fs.CreateFile('/test/afile', contents='abc')

        core.update_index(self.index, ['/test'])
        results = core.list_duplicates_with_originals(self.index)
        self.assertEqual(1, len(results))
        orig, *dupes = results[0]
        self.assertEqual('/test/afile', orig)
        self.assertSetEqual(set(dupes), {'/test/file1', '/test/file2'})
Пример #9
0
    def test_update_index_deletes_and_updates_files(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='def')

        core.update_index(self.index, ['/test'])
        sha = self.index.get('/test/file1')['sha256']

        with open('/test/file1', "w") as f:
            f.write('ghi')
        self.fs.RemoveObject('/test/file2')

        core.update_index(self.index)

        self.assertEqual(1, len(self.index.all()))
        self.assertNotEqual(sha, self.index.get('/test/file1')['sha256'])
Пример #10
0
    def test_report_stats(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='abc')
        self.fs.CreateFile('/test/file3', contents='defg')
        self.fs.CreateFile('/test/file4', contents='hijk')
        self.fs.CreateFile('/test/afile', contents='abc')

        _old_stdout = sys.stdout
        stdout_cap = io.StringIO()
        sys.stdout = stdout_cap

        index = Index(conf.index_file)
        core.update_index(index, ['/test'])
        dispatch(index, 'stats')

        sys.stdout = _old_stdout

        self.assertRegex(stdout_cap.getvalue(),
                         re.compile('file records', re.M))
Пример #11
0
    def test_report_command(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='abc')
        self.fs.CreateFile('/test/file3', contents='defg')
        self.fs.CreateFile('/test/file4', contents='hijk')
        self.fs.CreateFile('/test/afile', contents='abc')

        _old_stdout = sys.stdout
        stdout_cap = io.StringIO()
        sys.stdout = stdout_cap

        index = Index(conf.index_file)
        core.update_index(index, ['/test'])
        dispatch(index, 'report')

        sys.stdout = _old_stdout

        # Just check that three lines got written..
        self.assertEqual(3, len(stdout_cap.getvalue().strip().split("\n")))
Пример #12
0
    def test_update_index_on_content_change(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        self.fs.CreateFile('/test/file2', contents='def')

        core.update_index(self.index, ['/test'])

        shas = dict()
        for i in self.index.all():
            shas[i['fullpath']] = i['sha256']

        with open('/test/file2', "w") as f:
            f.write('ghi')

        core.update_index(self.index, ['/test'])

        file1_sha = self.index.get('/test/file1')['sha256']
        file2_sha = self.index.get('/test/file2')['sha256']

        self.assertEqual(shas['/test/file1'], file1_sha)
        self.assertNotEqual(shas['/test/file2'], file2_sha)
Пример #13
0
    def test_update_index_with_single_file(self):
        self.fs.CreateFile('/test/file1', contents='abc')
        core.update_index(self.index, ['/test'])

        self.assertEqual(len(self.index.all()), 1)
Пример #14
0
def update(index, **kwargs):
    if 'dirs'in kwargs:
        core.update_index(index, kwargs['dirs'])
    else:
        core.purge_removed_files(index)
Пример #15
0
 def test_list_empty_with_originals(self):
     core.update_index(self.index, ['/test'])
     results = core.list_duplicates_with_originals(self.index)
     self.assertEqual(0, len(results))
Пример #16
0
 def test_list_empty(self):
     core.update_index(self.index, ['/test'])
     results = core.list_duplicates(self.index)
     self.assertEqual(0, len(list(results)))