summaryrefslogtreecommitdiff
path: root/tests/test_find_top_level.py
blob: 17f83c4aa21c743f70dd67ce09dd4c619ccd99bb (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# gemato: Top-level Manifest finding tests
# vim:fileencoding=utf-8
# (c) 2017 Michał Górny
# Licensed under the terms of 2-clause BSD license

import gzip
import os
import os.path
import unittest

import gemato.find_top_level

from tests.testutil import TempDirTestCase


class TestCurrentDirectory(TempDirTestCase):
    """
    Test for finding top-level Manifest in a plain tree.
    """

    DIRS = ['suba', 'subb', 'subc', 'subc/sub']
    FILES = {
        'Manifest': u'',
        'subb/Manifest': u'',
        'subc/sub/Manifest': u'',
    }

    def test_find_top_level_manifest(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(self.dir),
                    self.dir),
                'Manifest')

    def test_find_top_level_manifest_from_empty_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'suba')),
                    self.dir),
                'Manifest')

    def test_find_top_level_manifest_from_manifest_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subb')),
                    self.dir),
                'Manifest')

    def test_find_top_level_manifest_from_deep_manifest_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subc', 'sub')),
                    self.dir),
                'Manifest')


class TestUnreadableManifest(TempDirTestCase):
    """
    Test whether the function fails correctly when it can not read
    a Manifest file.
    """

    FILES = {
        'Manifest': u'',
    }

    def setUp(self):
        super(TestUnreadableManifest, self).setUp()
        os.chmod(os.path.join(self.dir, 'Manifest'), 0)

    def test_find_top_level_manifest(self):
        self.assertRaises(IOError,
                gemato.find_top_level.find_top_level_manifest, self.dir)


class TestIgnoredSubdir(TempDirTestCase):
    """
    Test for ignoring irrelevant Manifest.
    """

    DIRS = ['sub', 'sub/sub', 'subb', 'subempty']
    FILES = {
        'Manifest': u'''
IGNORE sub
IGNORE subempty
''',
        'sub/Manifest': u'',
    }

    def test_find_top_level_manifest(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(self.dir),
                    self.dir),
                'Manifest')

    def test_find_top_level_manifest_from_ignored_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'sub')),
                    self.dir),
                'sub/Manifest')

    def test_find_top_level_manifest_from_sub_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'sub/sub')),
                    self.dir),
                'sub/Manifest')

    def test_find_top_level_manifest_from_non_ignored_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subb')),
                    self.dir),
                'Manifest')

    def test_find_top_level_manifest_from_ignored_empty_subdir(self):
        self.assertIsNone(
                gemato.find_top_level.find_top_level_manifest(
                    os.path.join(self.dir, 'subempty')))


class TestEmptyTree(TempDirTestCase):
    """
    Test for finding top-level Manifest in a tree without a Manifest
    """

    def test_find_top_level_manifest(self):
        self.assertIsNone(
                gemato.find_top_level.find_top_level_manifest(self.dir))


class TestRootDirectory(unittest.TestCase):
    """
    Test behavior when run on the system root directory.
    """

    def test_find_top_level_manifest(self):
        if os.path.exists('/Manifest'):
            raise unittest.SkipTest('/Manifest is present')
        self.assertIsNone(
                gemato.find_top_level.find_top_level_manifest('/'))


class TestCrossDevice(TempDirTestCase):
    """
    Test behavior when attempting to cross device boundary.
    """

    FILES = {
        'Manifest': u'',
    }

    def setUp(self):
        if not os.path.ismount('/proc'):
            raise unittest.SkipTest('/proc is not a mountpoint')
        super(TestCrossDevice, self).setUp()
        os.symlink('/proc', os.path.join(self.dir, 'test'))

    def test_find_top_level_manifest(self):
        self.assertIsNone(
                gemato.find_top_level.find_top_level_manifest(
                    os.path.join(self.dir, 'test')))


class TestCompressedManifest(TempDirTestCase):
    """
    Test for finding compressed Manifest in a plain tree.
    """

    DIRS = ['suba', 'subb', 'subc', 'subc/sub']
    FILES = {
        'subb/Manifest': u'',
    }

    def setUp(self):
        super(TestCompressedManifest, self).setUp()
        with gzip.GzipFile(os.path.join(self.dir, 'Manifest.gz'), 'wb'):
            pass
        with gzip.GzipFile(os.path.join(self.dir, 'subc/sub/Manifest.gz'), 'wb'):
            pass

    def test_find_top_level_manifest_no_allow_compressed(self):
        self.assertIsNone(
                gemato.find_top_level.find_top_level_manifest(self.dir))

    def test_find_top_level_manifest(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        self.dir, allow_compressed=True),
                    self.dir),
                'Manifest.gz')

    def test_find_top_level_manifest_from_empty_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'suba'),
                        allow_compressed=True),
                    self.dir),
                'Manifest.gz')

    def test_find_top_level_manifest_from_manifest_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subb'),
                        allow_compressed=True),
                    self.dir),
                'Manifest.gz')

    def test_find_top_level_manifest_from_deep_manifest_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subc', 'sub'),
                        allow_compressed=True),
                    self.dir),
                'Manifest.gz')


class TestCompressedManifestWithIgnore(TempDirTestCase):
    DIRS = ['suba', 'subb', 'subc', 'subc/sub']
    FILES = {
        'subb/Manifest': u'',
    }

    def setUp(self):
        super(TestCompressedManifestWithIgnore, self).setUp()
        with gzip.GzipFile(os.path.join(self.dir, 'Manifest.gz'), 'wb') as f:
            f.write(b'IGNORE suba\n')
            f.write(b'IGNORE subc\n')
        with gzip.GzipFile(os.path.join(self.dir, 'subc/sub/Manifest.gz'), 'wb'):
            pass

    def test_find_top_level_manifest(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        self.dir, allow_compressed=True),
                    self.dir),
                'Manifest.gz')

    def test_find_top_level_manifest_from_ignored_empty_subdir(self):
        self.assertIsNone(
                gemato.find_top_level.find_top_level_manifest(
                    os.path.join(self.dir, 'suba'),
                    allow_compressed=True))

    def test_find_top_level_manifest_from_manifest_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subb'),
                        allow_compressed=True),
                    self.dir),
                'Manifest.gz')

    def test_find_top_level_manifest_from_deep_ignored_subdir(self):
        self.assertEqual(
                os.path.relpath(
                    gemato.find_top_level.find_top_level_manifest(
                        os.path.join(self.dir, 'subc', 'sub'),
                        allow_compressed=True),
                    self.dir),
                'subc/sub/Manifest.gz')