File: test_fs.py

package info (click to toggle)
dh-python 6.20250414
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,560 kB
  • sloc: python: 5,209; makefile: 575; perl: 249; sh: 35
file content (127 lines) | stat: -rw-r--r-- 4,555 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from tempfile import TemporaryDirectory
from pathlib import Path
from unittest import TestCase

from dhpython.interpreter import Interpreter
from dhpython.fs import merge_WHEEL, share_files

from .common import FakeOptions


class MergeWheelTestCase(TestCase):
    files = {}
    def setUp(self):
        self.tempdir = TemporaryDirectory()  # pylint: disable=consider-using-with
        self.addCleanup(self.tempdir.cleanup)
        temp_path = Path(self.tempdir.name)
        for fn, contents in self.files.items():
            path = temp_path / fn
            setattr(self, path.name, path)
            path.parent.mkdir(parents=True, exist_ok=True)
            with path.open('w') as f:
                f.write('\n'.join(contents))
                f.write('\n')

    def assertFileContents(self, path, contents):
        """Assert that the contents of path is contents

        Contents may be specified as a list of strings, one per line, without
        line-breaks.
        """
        if isinstance(contents, (list, tuple)):
            contents = '\n'.join(contents) + '\n'
        with path.open('r') as f:
            self.assertMultiLineEqual(contents, f.read())


class MergeTagsTest(MergeWheelTestCase):
    files = {
        'a': ('foo', 'Tag: A'),
        'b': ('foo', 'Tag: B'),
    }

    def test_merge_wheel(self):
        merge_WHEEL(self.a, self.b)
        self.assertFileContents(self.b, ('foo', 'Tag: B', 'Tag: A'))


class ShareFilesTestCase(MergeWheelTestCase):
    impl = 'cpython3'
    options = {}

    def setUp(self):
        super().setUp()
        self.destdir = TemporaryDirectory()  # pylint: disable=consider-using-with
        self.addCleanup(self.destdir.cleanup)
        self.interpreter = Interpreter(self.impl)

    def share_files(self, options=None):
        if options is None:
            options = self.options
        share_files(self.tempdir.name, self.destdir.name, self.interpreter,
                    FakeOptions(**options))

    def destPath(self, name):
        return Path(self.destdir.name) / name


class HatchlingLicenseTest(ShareFilesTestCase):
    files = {
        'foo.dist-info/license_files/LICENSE.txt': ('foo'),
        'foo.dist-info/licenses/COPYING': ('foo'),
        'foo.dist-info/RECORD': (
            'foo.dist-info/license_files/LICENSE.txt,sha256=2c26b46b68ffc68ff99'
            'b453c1d30413413422d706483bfa0f98a5e886266e7ae,4',
            'foo.dist-info/licenses/COPYING,sha256=2c26b46b68ffc68ff99b453c1d30'
            '413413422d706483bfa0f98a5e886266e7ae,4',
            'foo.dist-info/WHEEL,sha256=447fb61fa39a067229e1cce8fc0953bfced53ea'
            'c85d1844f5940f51c1fcba725,6'),
        'foo.dist-info/WHEEL': ('foo'),
    }

    def test_removes_license_files(self):
        self.share_files()
        self.assertFalse(
            self.destPath('foo.dist-info/license_files/LICENSE.txt').exists())
        self.assertFalse(
            self.destPath('foo.dist-info/licenses/COPYING').exists())


class FlitLicenseTest(ShareFilesTestCase):
    files = {
        'foo.dist-info/COPYING': ('foo'),
        'foo.dist-info/COPYING.LESSER': ('foo'),
        'foo.dist-info/RECORD': (
            'foo.dist-info/COPYING,sha256=2c26b46b68ffc68ff99b453c1d30413413422'
            'd706483bfa0f98a5e886266e7ae,4',
            'foo.dist-info/COPYING.LESSER,sha256=2c26b46b68ffc68ff99b453c1d3041'
            '3413422d706483bfa0f98a5e886266e7ae,4',
            'foo.dist-info/WHEEL,sha256=447fb61fa39a067229e1cce8fc0953bfced53ea'
            'c85d1844f5940f51c1fcba725,6'),
        'foo.dist-info/WHEEL': ('foo'),
    }

    def test_removes_license_files(self):
        self.share_files()
        self.assertFalse(self.destPath('foo.dist-info/COPYING.LESSER').exists())
        self.assertFalse(self.destPath('foo.dist-info/COPYING').exists())


class CExtensionsRenameTest(ShareFilesTestCase):
    def setUp(self):
        ver = Interpreter(self.impl).default_version
        self.files = {
            f'usr/lib/python{ver}/foo.so': ('binary-data'),
        }
        self.ext_filename = f'usr/lib/python{ver}/foo.so'
        super().setUp()

    def test_python_extensions(self):
        self.share_files()
        self.assertFalse(self.destPath(self.ext_filename).exists())
        expected_name = self.interpreter.check_extname(self.ext_filename)
        self.assertTrue(self.destPath(expected_name).exists())

    def test_no_ext_rename(self):
        self.share_files(options={"no_ext_rename": True})
        self.assertTrue(self.destPath(self.ext_filename).exists())