aboutsummaryrefslogtreecommitdiffstats
path: root/setuptools/tests/test_egg_info.py
blob: e80684205f7f53155c41042f87aa812648ef53d8 (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

import distutils.core
import os
import sys
import tempfile
import shutil
import stat
import unittest

import pkg_resources
import warnings
from setuptools.command import egg_info
from setuptools import svn_utils
from setuptools.tests import environment, test_svn
from setuptools.tests.py26compat import skipIf

ENTRIES_V10 = pkg_resources.resource_string(__name__, 'entries-v10')
"An entries file generated with svn 1.6.17 against the legacy Setuptools repo"


class TestEggInfo(unittest.TestCase):

    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        os.mkdir(os.path.join(self.test_dir, '.svn'))

        self.old_cwd = os.getcwd()
        os.chdir(self.test_dir)

    def tearDown(self):
        os.chdir(self.old_cwd)
        shutil.rmtree(self.test_dir)

    def _write_entries(self, entries):
        fn = os.path.join(self.test_dir, '.svn', 'entries')
        entries_f = open(fn, 'wb')
        entries_f.write(entries)
        entries_f.close()

    def _create_project(self):
        with open('setup.py', 'w') as f:
            f.write('from setuptools import setup\n')
            f.write('\n')
            f.write('setup(\n')
            f.write("    name='foo',\n")
            f.write("    py_modules=['hello'],\n")
            f.write("    entry_points={'console_scripts': ['hi = hello.run']},\n")
            f.write('    zip_safe=False,\n')
            f.write('    )\n')
        with open('hello.py', 'w') as f:
            f.write('def run():\n')
            f.write("    print('hello')\n")

    @skipIf(not test_svn._svn_check, "No SVN to text, in the first place")
    def test_version_10_format(self):
        """
        """
        #keeping this set for 1.6 is a good check on the get_svn_revision
        #to ensure I return using svnversion what would had been returned
        version_str = svn_utils.SvnInfo.get_svn_version()
        version = [int(x) for x in version_str.split('.')[:2]]
        if version != [1, 6]:
            if hasattr(self, 'skipTest'):
                self.skipTest('')
            else:
                sys.stderr.write('\n   Skipping due to SVN Version\n')
                return

        self._write_entries(ENTRIES_V10)
        rev = egg_info.egg_info.get_svn_revision()
        self.assertEqual(rev, '89000')

    def test_version_10_format_legacy_parser(self):
        """
        """
        path_variable = None
        for env in os.environ:
            if env.lower() == 'path':
                path_variable = env

        if path_variable:
            old_path = os.environ[path_variable]
            os.environ[path_variable] = ''
        #catch_warnings not available until py26
        warning_filters = warnings.filters
        warnings.filters = warning_filters[:]
        try:
            warnings.simplefilter("ignore", DeprecationWarning)
            self._write_entries(ENTRIES_V10)
            rev = egg_info.egg_info.get_svn_revision()
        finally:
            #restore the warning filters
            warnings.filters = warning_filters
            #restore the os path
            if path_variable:
                os.environ[path_variable] = old_path

        self.assertEqual(rev, '89000')

    def test_egg_base_installed_egg_info(self):
        self._create_project()
        temp_dir = tempfile.mkdtemp(prefix='setuptools-test.')
        os.chmod(temp_dir, stat.S_IRWXU)
        try:
            paths = {}
            for dirname in ['home', 'lib', 'scripts', 'data', 'egg-base']:
                paths[dirname] = os.path.join(temp_dir, dirname)
                os.mkdir(paths[dirname])
            config = os.path.join(paths['home'], '.pydistutils.cfg')
            with open(config, 'w') as f:
                f.write('[egg_info]\n')
                f.write('egg-base = %s\n' % paths['egg-base'])
            environ = os.environ.copy()
            environ['HOME'] = paths['home']
            code, data = environment.run_setup_py(
                cmd=[
                    'install', '--home', paths['home'],
                    '--install-lib', paths['lib'],
                    '--install-scripts', paths['scripts'],
                    '--install-data', paths['data']],
                pypath=os.pathsep.join([paths['lib'], self.old_cwd]),
                data_stream=1,
                env=environ)
            if code:
                raise AssertionError(data)
            egg_info = None
            for dirpath, dirnames, filenames in os.walk(paths['lib']):
                if os.path.basename(dirpath) == 'EGG-INFO':
                    egg_info = sorted(filenames)
            self.assertEqual(
                egg_info,
                ['PKG-INFO',
                 'SOURCES.txt',
                 'dependency_links.txt',
                 'entry_points.txt',
                 'not-zip-safe',
                 'top_level.txt'])
        finally:
            shutil.rmtree(temp_dir)


DUMMY_SOURCE_TXT = """CHANGES.txt
CONTRIBUTORS.txt
HISTORY.txt
LICENSE
MANIFEST.in
README.txt
setup.py
dummy/__init__.py
dummy/test.txt
dummy.egg-info/PKG-INFO
dummy.egg-info/SOURCES.txt
dummy.egg-info/dependency_links.txt
dummy.egg-info/top_level.txt"""


class TestSvnDummy(environment.ZippedEnvironment):

    def setUp(self):
        version = svn_utils.SvnInfo.get_svn_version()
        if not version:  # None or Empty
            return None

        self.base_version = tuple([int(x) for x in version.split('.')][:2])

        if not self.base_version:
            raise ValueError('No SVN tools installed')
        elif self.base_version < (1, 3):
            raise ValueError('Insufficient SVN Version %s' % version)
        elif self.base_version >= (1, 9):
            #trying the latest version
            self.base_version = (1, 8)

        self.dataname = "dummy%i%i" % self.base_version
        self.datafile = os.path.join('setuptools', 'tests',
                                     'svn_data', self.dataname + ".zip")
        super(TestSvnDummy, self).setUp()

    @skipIf(not test_svn._svn_check, "No SVN to text, in the first place")
    def test_sources(self):
        code, data = environment.run_setup_py(["sdist"],
                                              pypath=self.old_cwd,
                                              data_stream=1)
        if code:
            raise AssertionError(data)

        sources = os.path.join('dummy.egg-info', 'SOURCES.txt')
        infile = open(sources, 'r')
        try:
            read_contents = infile.read()
        finally:
            infile.close()
            del infile

        self.assertEqual(DUMMY_SOURCE_TXT, read_contents)

        return data

    @skipIf(not test_svn._svn_check, "No SVN to text, in the first place")
    def test_svn_tags(self):
        code, data = environment.run_setup_py(["egg_info",
                                               "--tag-svn-revision"],
                                              pypath=self.old_cwd,
                                              data_stream=1)
        if code:
            raise AssertionError(data)

        pkginfo = os.path.join('dummy.egg-info', 'PKG-INFO')
        infile = open(pkginfo, 'r')
        try:
            read_contents = infile.readlines()
        finally:
            infile.close()
            del infile

        self.assertTrue("Version: 0.1.1.post1\n" in read_contents)

    @skipIf(not test_svn._svn_check, "No SVN to text, in the first place")
    def test_no_tags(self):
        code, data = environment.run_setup_py(["egg_info"],
                                              pypath=self.old_cwd,
                                              data_stream=1)
        if code:
            raise AssertionError(data)

        pkginfo = os.path.join('dummy.egg-info', 'PKG-INFO')
        infile = open(pkginfo, 'r')
        try:
            read_contents = infile.readlines()
        finally:
            infile.close()
            del infile

        self.assertTrue("Version: 0.1.1\n" in read_contents)


class TestSvnDummyLegacy(environment.ZippedEnvironment):

    def setUp(self):
        self.base_version = (1, 6)
        self.dataname = "dummy%i%i" % self.base_version
        self.datafile = os.path.join('setuptools', 'tests',
                                     'svn_data', self.dataname + ".zip")
        super(TestSvnDummyLegacy, self).setUp()

    def test_sources(self):
        code, data = environment.run_setup_py(["sdist"],
                                              pypath=self.old_cwd,
                                              path="",
                                              data_stream=1)
        if code:
            raise AssertionError(data)

        sources = os.path.join('dummy.egg-info', 'SOURCES.txt')
        infile = open(sources, 'r')
        try:
            read_contents = infile.read()
        finally:
            infile.close()
            del infile

        self.assertEqual(DUMMY_SOURCE_TXT, read_contents)

        return data


def test_suite():
    return unittest.defaultTestLoader.loadTestsFromName(__name__)