summaryrefslogtreecommitdiffstats
path: root/Lib/distutils/tests/test_filelist.py
blob: d98325ae548a23a9cc032a131b89ccdf6e8e54b4 (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
"""Tests for distutils.filelist."""
from os.path import join
import unittest
from test.support import captured_stdout

from distutils.filelist import glob_to_re, FileList
from distutils import debug

MANIFEST_IN = """\
include ok
include xo
exclude xo
include foo.tmp
global-include *.x
global-include *.txt
global-exclude *.tmp
recursive-include f *.oo
recursive-exclude global *.x
graft dir
prune dir3
"""

class FileListTestCase(unittest.TestCase):

    def test_glob_to_re(self):
        # simple cases
        self.assertEquals(glob_to_re('foo*'), 'foo[^/]*\\Z(?ms)')
        self.assertEquals(glob_to_re('foo?'), 'foo[^/]\\Z(?ms)')
        self.assertEquals(glob_to_re('foo??'), 'foo[^/][^/]\\Z(?ms)')

        # special cases
        self.assertEquals(glob_to_re(r'foo\\*'), r'foo\\\\[^/]*\Z(?ms)')
        self.assertEquals(glob_to_re(r'foo\\\*'), r'foo\\\\\\[^/]*\Z(?ms)')
        self.assertEquals(glob_to_re('foo????'), r'foo[^/][^/][^/][^/]\Z(?ms)')
        self.assertEquals(glob_to_re(r'foo\\??'), r'foo\\\\[^/][^/]\Z(?ms)')

    def test_process_template_line(self):
        # testing  all MANIFEST.in template patterns
        file_list = FileList()

        # simulated file list
        file_list.allfiles = ['foo.tmp', 'ok', 'xo', 'four.txt',
                              join('global', 'one.txt'),
                              join('global', 'two.txt'),
                              join('global', 'files.x'),
                              join('global', 'here.tmp'),
                              join('f', 'o', 'f.oo'),
                              join('dir', 'graft-one'),
                              join('dir', 'dir2', 'graft2'),
                              join('dir3', 'ok'),
                              join('dir3', 'sub', 'ok.txt')
                              ]

        for line in MANIFEST_IN.split('\n'):
            if line.strip() == '':
                continue
            file_list.process_template_line(line)

        wanted = ['ok', 'four.txt', join('global', 'one.txt'),
                  join('global', 'two.txt'), join('f', 'o', 'f.oo'),
                  join('dir', 'graft-one'), join('dir', 'dir2', 'graft2')]

        self.assertEquals(file_list.files, wanted)

    def test_debug_print(self):
        file_list = FileList()
        with captured_stdout() as stdout:
            file_list.debug_print('xxx')
        stdout.seek(0)
        self.assertEquals(stdout.read(), '')

        debug.DEBUG = True
        try:
            with captured_stdout() as stdout:
                file_list.debug_print('xxx')
            stdout.seek(0)
            self.assertEquals(stdout.read(), 'xxx\n')
        finally:
            debug.DEBUG = False

def test_suite():
    return unittest.makeSuite(FileListTestCase)

if __name__ == "__main__":
    unittest.main(defaultTest="test_suite")