File: test_tools.py

package info (click to toggle)
dh-python 7.20251231
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,596 kB
  • sloc: python: 6,487; makefile: 605; perl: 251; sh: 36
file content (113 lines) | stat: -rw-r--r-- 4,114 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from pathlib import Path
from tempfile import TemporaryDirectory, NamedTemporaryFile
import os
import unittest

from dhpython.tools import fix_shebang, relpath, move_matching_files


class TestRelpath(unittest.TestCase):
    def test_common_parent_dir(self) -> None:
        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) -> None:
        r = relpath("/usr/share/python-foo/foo.py", "/usr/share")
        self.assertEqual(r, "python-foo/foo.py")

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


class TestMoveMatchingFiles(unittest.TestCase):
    def setUp(self) -> None:
        self.tmpdir = TemporaryDirectory()  # pylint: disable=consider-using-with
        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"):
            with open(self.tmppath(path), "wb"):
                # create a 0 byte file for the test
                pass

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

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

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

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


class TestFixShebang(unittest.TestCase):
    def setUp(self) -> None:
        self.tmpfile = Path(
            NamedTemporaryFile(
                prefix="dhptest_", suffix="_shebang.py", delete=False
            ).name
        )
        self.addCleanup(self.tmpfile.unlink)

    def write_shebang(self, shebang: str) -> None:
        self.tmpfile.write_text(shebang + "\nprint('This is Python')\n")

    def assert_shebang(self, shebang: str) -> None:
        contents = self.tmpfile.read_text().splitlines()
        self.assertEqual(len(contents), 2)
        self.assertEqual(contents[0], shebang)
        self.assertEqual(contents[1], "print('This is Python')")

    def test_perl(self) -> None:
        self.write_shebang("#!/usr/bin/perl")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#!/usr/bin/perl")

    def test_unversioned(self) -> None:
        self.write_shebang("#!/usr/bin/python")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#! /usr/bin/python3")

    def test_python2(self) -> None:
        self.write_shebang("#!/usr/bin/python2")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#!/usr/bin/python2")

    def test_python2_7(self) -> None:
        self.write_shebang("#!/usr/bin/python2.7")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#!/usr/bin/python2.7")

    def test_python3(self) -> None:
        self.write_shebang("#!/usr/bin/python3")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#! /usr/bin/python3")

    def test_python3_13(self) -> None:
        self.write_shebang("#!/usr/bin/python3.13")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#!/usr/bin/python3.13")

    def test_env_unversioned(self) -> None:
        self.write_shebang("#!/usr/bin/env python")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#! /usr/bin/python3")

    def test_env_python3(self) -> None:
        self.write_shebang("#!/usr/bin/env python3")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#! /usr/bin/python3")

    def test_env_python3_13(self) -> None:
        self.write_shebang("#!/usr/bin/env python3.13")
        fix_shebang(self.tmpfile)
        self.assert_shebang("#! /usr/bin/python3.13")

    def test_replacement(self) -> None:
        self.write_shebang("#!/usr/bin/env python")
        fix_shebang(self.tmpfile, "/usr/bin/foo")
        self.assert_shebang("#! /usr/bin/foo")