summaryrefslogtreecommitdiffstats
path: root/tests/test_tools.py
blob: 55097e8cd094534aa0a7e8ed3c7d509f3f35cf35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from tempfile import TemporaryDirectory
import os
import unittest

from dhpython.tools import relpath, move_matching_files


class TestRelpath(unittest.TestCase):
    def test_common_parent_dir(self):
        r = relpath('/usr/share/python-foo/foo.py', '/usr/bin/foo')
        self.assertEqual(r, '../share/python-foo/foo.py')

    def test_strips_common_prefix(self):
        r = relpath('/usr/share/python-foo/foo.py', '/usr/share')
        self.assertEqual(r, 'python-foo/foo.py')

    def test_trailing_slash_ignored(self):
        r = relpath('/usr/share/python-foo/foo.py', '/usr/share/')
        self.assertEqual(r, 'python-foo/foo.py')


class TestMoveMatchingFiles(unittest.TestCase):
    def setUp(self):
        self.tmpdir = TemporaryDirectory()
        self.addCleanup(self.tmpdir.cleanup)
        os.makedirs(self.tmppath('foo/bar/a/b/c/spam'))
        for path in ('foo/bar/a/b/c/spam/file.so',
                     'foo/bar/a/b/c/spam/file.py'):
            open(self.tmppath(path), 'w').close()

        move_matching_files(self.tmppath('foo/bar/'),
                            self.tmppath('foo/baz/'),
                            'spam/.*\.so$')

    def tmppath(self, *path):
        return os.path.join(self.tmpdir.name, *path)

    def test_moved_matching_file(self):
        self.assertTrue(os.path.exists(
            self.tmppath('foo/baz/a/b/c/spam/file.so')))

    def test_left_non_matching_file(self):
        self.assertTrue(os.path.exists(
            self.tmppath('foo/bar/a/b/c/spam/file.py')))