ยปCore Development>Code coverage>Lib/packaging/tests/test_database.py

Python code coverage for Lib/packaging/tests/test_database.py

#countcontent
1n/aimport os
2n/aimport io
3n/aimport csv
4n/aimport sys
5n/aimport shutil
6n/aimport tempfile
7n/afrom hashlib import md5
8n/afrom textwrap import dedent
9n/a
10n/afrom packaging.tests.test_util import GlobTestCaseBase
11n/afrom packaging.tests.support import requires_zlib
12n/a
13n/aimport packaging.database
14n/afrom packaging.config import get_resources_dests
15n/afrom packaging.errors import PackagingError
16n/afrom packaging.metadata import Metadata
17n/afrom packaging.tests import unittest, support
18n/afrom packaging.database import (
19n/a Distribution, EggInfoDistribution, get_distribution, get_distributions,
20n/a provides_distribution, obsoletes_distribution, get_file_users,
21n/a enable_cache, disable_cache, distinfo_dirname, _yield_distributions,
22n/a get_file, get_file_path)
23n/a
24n/a# TODO Add a test for getting a distribution provided by another distribution
25n/a# TODO Add a test for absolute pathed RECORD items (e.g. /etc/myapp/config.ini)
26n/a# TODO Add tests from the former pep376 project (zipped site-packages, etc.)
27n/a
28n/a
29n/adef get_hexdigest(filename):
30n/a with open(filename, 'rb') as file:
31n/a checksum = md5(file.read())
32n/a return checksum.hexdigest()
33n/a
34n/a
35n/adef record_pieces(path):
36n/a path = os.path.join(*path)
37n/a digest = get_hexdigest(path)
38n/a size = os.path.getsize(path)
39n/a return path, digest, size
40n/a
41n/a
42n/aclass FakeDistsMixin:
43n/a
44n/a def setUp(self):
45n/a super(FakeDistsMixin, self).setUp()
46n/a self.addCleanup(enable_cache)
47n/a disable_cache()
48n/a
49n/a # make a copy that we can write into for our fake installed
50n/a # distributions
51n/a tmpdir = tempfile.mkdtemp()
52n/a self.addCleanup(shutil.rmtree, tmpdir)
53n/a self.fake_dists_path = os.path.realpath(
54n/a os.path.join(tmpdir, 'fake_dists'))
55n/a fake_dists_src = os.path.abspath(
56n/a os.path.join(os.path.dirname(__file__), 'fake_dists'))
57n/a shutil.copytree(fake_dists_src, self.fake_dists_path)
58n/a # XXX ugly workaround: revert copystat calls done by shutil behind our
59n/a # back (to avoid getting a read-only copy of a read-only file). we
60n/a # could pass a custom copy_function to change the mode of files, but
61n/a # shutil gives no control over the mode of directories :(
62n/a # see http://bugs.python.org/issue1666318
63n/a for root, dirs, files in os.walk(self.fake_dists_path):
64n/a os.chmod(root, 0o755)
65n/a for f in files:
66n/a os.chmod(os.path.join(root, f), 0o644)
67n/a for d in dirs:
68n/a os.chmod(os.path.join(root, d), 0o755)
69n/a
70n/a
71n/aclass CommonDistributionTests(FakeDistsMixin):
72n/a """Mixin used to test the interface common to both Distribution classes.
73n/a
74n/a Derived classes define cls, sample_dist, dirs and records. These
75n/a attributes are used in test methods. See source code for details.
76n/a """
77n/a
78n/a def test_instantiation(self):
79n/a # check that useful attributes are here
80n/a name, version, distdir = self.sample_dist
81n/a here = os.path.abspath(os.path.dirname(__file__))
82n/a dist_path = os.path.join(here, 'fake_dists', distdir)
83n/a
84n/a dist = self.dist = self.cls(dist_path)
85n/a self.assertEqual(dist.path, dist_path)
86n/a self.assertEqual(dist.name, name)
87n/a self.assertEqual(dist.metadata['Name'], name)
88n/a self.assertIsInstance(dist.metadata, Metadata)
89n/a self.assertEqual(dist.version, version)
90n/a self.assertEqual(dist.metadata['Version'], version)
91n/a
92n/a @requires_zlib
93n/a def test_repr(self):
94n/a dist = self.cls(self.dirs[0])
95n/a # just check that the class name is in the repr
96n/a self.assertIn(self.cls.__name__, repr(dist))
97n/a
98n/a @requires_zlib
99n/a def test_comparison(self):
100n/a # tests for __eq__ and __hash__
101n/a dist = self.cls(self.dirs[0])
102n/a dist2 = self.cls(self.dirs[0])
103n/a dist3 = self.cls(self.dirs[1])
104n/a self.assertIn(dist, {dist: True})
105n/a self.assertEqual(dist, dist)
106n/a
107n/a self.assertIsNot(dist, dist2)
108n/a self.assertEqual(dist, dist2)
109n/a self.assertNotEqual(dist, dist3)
110n/a self.assertNotEqual(dist, ())
111n/a
112n/a def test_list_installed_files(self):
113n/a for dir_ in self.dirs:
114n/a dist = self.cls(dir_)
115n/a for path, md5_, size in dist.list_installed_files():
116n/a record_data = self.records[dist.path]
117n/a self.assertIn(path, record_data)
118n/a self.assertEqual(md5_, record_data[path][0])
119n/a self.assertEqual(size, record_data[path][1])
120n/a
121n/a
122n/aclass TestDistribution(CommonDistributionTests, unittest.TestCase):
123n/a
124n/a cls = Distribution
125n/a sample_dist = 'choxie', '2.0.0.9', 'choxie-2.0.0.9.dist-info'
126n/a
127n/a def setUp(self):
128n/a super(TestDistribution, self).setUp()
129n/a self.dirs = [os.path.join(self.fake_dists_path, f)
130n/a for f in os.listdir(self.fake_dists_path)
131n/a if f.endswith('.dist-info')]
132n/a
133n/a self.records = {}
134n/a for distinfo_dir in self.dirs:
135n/a
136n/a record_file = os.path.join(distinfo_dir, 'RECORD')
137n/a with open(record_file, 'w') as file:
138n/a record_writer = csv.writer(
139n/a file, delimiter=',', quoting=csv.QUOTE_NONE,
140n/a lineterminator='\n')
141n/a
142n/a dist_location = distinfo_dir.replace('.dist-info', '')
143n/a
144n/a for path, dirs, files in os.walk(dist_location):
145n/a for f in files:
146n/a record_writer.writerow(record_pieces((path, f)))
147n/a for file in ('INSTALLER', 'METADATA', 'REQUESTED'):
148n/a record_writer.writerow(record_pieces((distinfo_dir, file)))
149n/a record_writer.writerow([record_file])
150n/a
151n/a with open(record_file) as file:
152n/a record_reader = csv.reader(file, lineterminator='\n')
153n/a record_data = {}
154n/a for row in record_reader:
155n/a if row == []:
156n/a continue
157n/a path, md5_, size = (row[:] +
158n/a [None for i in range(len(row), 3)])
159n/a record_data[path] = md5_, size
160n/a self.records[distinfo_dir] = record_data
161n/a
162n/a def test_instantiation(self):
163n/a super(TestDistribution, self).test_instantiation()
164n/a self.assertIsInstance(self.dist.requested, bool)
165n/a
166n/a def test_uses(self):
167n/a # Test to determine if a distribution uses a specified file.
168n/a # Criteria to test against
169n/a distinfo_name = 'grammar-1.0a4'
170n/a distinfo_dir = os.path.join(self.fake_dists_path,
171n/a distinfo_name + '.dist-info')
172n/a true_path = [self.fake_dists_path, distinfo_name,
173n/a 'grammar', 'utils.py']
174n/a true_path = os.path.join(*true_path)
175n/a false_path = [self.fake_dists_path, 'towel_stuff-0.1', 'towel_stuff',
176n/a '__init__.py']
177n/a false_path = os.path.join(*false_path)
178n/a
179n/a # Test if the distribution uses the file in question
180n/a dist = Distribution(distinfo_dir)
181n/a self.assertTrue(dist.uses(true_path), 'dist %r is supposed to use %r' %
182n/a (dist, true_path))
183n/a self.assertFalse(dist.uses(false_path), 'dist %r is not supposed to '
184n/a 'use %r' % (dist, true_path))
185n/a
186n/a def test_get_distinfo_file(self):
187n/a # Test the retrieval of dist-info file objects.
188n/a distinfo_name = 'choxie-2.0.0.9'
189n/a other_distinfo_name = 'grammar-1.0a4'
190n/a distinfo_dir = os.path.join(self.fake_dists_path,
191n/a distinfo_name + '.dist-info')
192n/a dist = Distribution(distinfo_dir)
193n/a # Test for known good file matches
194n/a distinfo_files = [
195n/a # Relative paths
196n/a 'INSTALLER', 'METADATA',
197n/a # Absolute paths
198n/a os.path.join(distinfo_dir, 'RECORD'),
199n/a os.path.join(distinfo_dir, 'REQUESTED'),
200n/a ]
201n/a
202n/a for distfile in distinfo_files:
203n/a with dist.get_distinfo_file(distfile) as value:
204n/a self.assertIsInstance(value, io.TextIOWrapper)
205n/a # Is it the correct file?
206n/a self.assertEqual(value.name,
207n/a os.path.join(distinfo_dir, distfile))
208n/a
209n/a # Test an absolute path that is part of another distributions dist-info
210n/a other_distinfo_file = os.path.join(
211n/a self.fake_dists_path, other_distinfo_name + '.dist-info',
212n/a 'REQUESTED')
213n/a self.assertRaises(PackagingError, dist.get_distinfo_file,
214n/a other_distinfo_file)
215n/a # Test for a file that should not exist
216n/a self.assertRaises(PackagingError, dist.get_distinfo_file,
217n/a 'MAGICFILE')
218n/a
219n/a def test_list_distinfo_files(self):
220n/a distinfo_name = 'towel_stuff-0.1'
221n/a distinfo_dir = os.path.join(self.fake_dists_path,
222n/a distinfo_name + '.dist-info')
223n/a dist = Distribution(distinfo_dir)
224n/a # Test for the iteration of the raw path
225n/a distinfo_files = [os.path.join(distinfo_dir, filename) for filename in
226n/a os.listdir(distinfo_dir)]
227n/a found = dist.list_distinfo_files()
228n/a self.assertEqual(sorted(found), sorted(distinfo_files))
229n/a # Test for the iteration of local absolute paths
230n/a distinfo_files = [os.path.join(sys.prefix, distinfo_dir, path) for
231n/a path in distinfo_files]
232n/a found = sorted(dist.list_distinfo_files(local=True))
233n/a if os.sep != '/':
234n/a self.assertNotIn('/', found[0])
235n/a self.assertIn(os.sep, found[0])
236n/a self.assertEqual(found, sorted(distinfo_files))
237n/a
238n/a def test_get_resources_path(self):
239n/a distinfo_name = 'babar-0.1'
240n/a distinfo_dir = os.path.join(self.fake_dists_path,
241n/a distinfo_name + '.dist-info')
242n/a dist = Distribution(distinfo_dir)
243n/a resource_path = dist.get_resource_path('babar.png')
244n/a self.assertEqual(resource_path, 'babar.png')
245n/a self.assertRaises(KeyError, dist.get_resource_path, 'notexist')
246n/a
247n/a
248n/aclass TestEggInfoDistribution(CommonDistributionTests,
249n/a support.LoggingCatcher,
250n/a unittest.TestCase):
251n/a
252n/a cls = EggInfoDistribution
253n/a sample_dist = 'bacon', '0.1', 'bacon-0.1.egg-info'
254n/a
255n/a def setUp(self):
256n/a super(TestEggInfoDistribution, self).setUp()
257n/a
258n/a self.dirs = [os.path.join(self.fake_dists_path, f)
259n/a for f in os.listdir(self.fake_dists_path)
260n/a if f.endswith('.egg') or f.endswith('.egg-info')]
261n/a
262n/a self.records = {}
263n/a
264n/a @unittest.skip('not implemented yet')
265n/a def test_list_installed_files(self):
266n/a # EggInfoDistribution defines list_installed_files but there is no
267n/a # test for it yet; someone with setuptools expertise needs to add a
268n/a # file with the list of installed files for one of the egg fake dists
269n/a # and write the support code to populate self.records (and then delete
270n/a # this method)
271n/a pass
272n/a
273n/a
274n/aclass TestDatabase(support.LoggingCatcher,
275n/a FakeDistsMixin,
276n/a unittest.TestCase):
277n/a
278n/a def setUp(self):
279n/a super(TestDatabase, self).setUp()
280n/a sys.path.insert(0, self.fake_dists_path)
281n/a self.addCleanup(sys.path.remove, self.fake_dists_path)
282n/a
283n/a def test_caches(self):
284n/a # sanity check for internal caches
285n/a for name in ('_cache_name', '_cache_name_egg',
286n/a '_cache_path', '_cache_path_egg'):
287n/a self.assertEqual(getattr(packaging.database, name), {})
288n/a
289n/a def test_distinfo_dirname(self):
290n/a # Given a name and a version, we expect the distinfo_dirname function
291n/a # to return a standard distribution information directory name.
292n/a
293n/a items = [
294n/a # (name, version, standard_dirname)
295n/a # Test for a very simple single word name and decimal version
296n/a # number
297n/a ('docutils', '0.5', 'docutils-0.5.dist-info'),
298n/a # Test for another except this time with a '-' in the name, which
299n/a # needs to be transformed during the name lookup
300n/a ('python-ldap', '2.5', 'python_ldap-2.5.dist-info'),
301n/a # Test for both '-' in the name and a funky version number
302n/a ('python-ldap', '2.5 a---5', 'python_ldap-2.5 a---5.dist-info'),
303n/a ]
304n/a
305n/a # Loop through the items to validate the results
306n/a for name, version, standard_dirname in items:
307n/a dirname = distinfo_dirname(name, version)
308n/a self.assertEqual(dirname, standard_dirname)
309n/a
310n/a @requires_zlib
311n/a def test_get_distributions(self):
312n/a # Lookup all distributions found in the ``sys.path``.
313n/a # This test could potentially pick up other installed distributions
314n/a fake_dists = [('grammar', '1.0a4'), ('choxie', '2.0.0.9'),
315n/a ('towel-stuff', '0.1'), ('babar', '0.1')]
316n/a found_dists = []
317n/a
318n/a # Verify the fake dists have been found.
319n/a dists = [dist for dist in get_distributions()]
320n/a for dist in dists:
321n/a self.assertIsInstance(dist, Distribution)
322n/a if (dist.name in dict(fake_dists) and
323n/a dist.path.startswith(self.fake_dists_path)):
324n/a found_dists.append((dist.name, dist.version))
325n/a else:
326n/a # check that it doesn't find anything more than this
327n/a self.assertFalse(dist.path.startswith(self.fake_dists_path))
328n/a # otherwise we don't care what other distributions are found
329n/a
330n/a # Finally, test that we found all that we were looking for
331n/a self.assertEqual(sorted(found_dists), sorted(fake_dists))
332n/a
333n/a # Now, test if the egg-info distributions are found correctly as well
334n/a fake_dists += [('bacon', '0.1'), ('cheese', '2.0.2'),
335n/a ('coconuts-aster', '10.3'),
336n/a ('banana', '0.4'), ('strawberry', '0.6'),
337n/a ('truffles', '5.0'), ('nut', 'funkyversion')]
338n/a found_dists = []
339n/a
340n/a dists = [dist for dist in get_distributions(use_egg_info=True)]
341n/a for dist in dists:
342n/a self.assertIsInstance(dist, (Distribution, EggInfoDistribution))
343n/a if (dist.name in dict(fake_dists) and
344n/a dist.path.startswith(self.fake_dists_path)):
345n/a found_dists.append((dist.name, dist.version))
346n/a else:
347n/a self.assertFalse(dist.path.startswith(self.fake_dists_path))
348n/a
349n/a self.assertEqual(sorted(fake_dists), sorted(found_dists))
350n/a
351n/a @requires_zlib
352n/a def test_get_distribution(self):
353n/a # Test for looking up a distribution by name.
354n/a # Test the lookup of the towel-stuff distribution
355n/a name = 'towel-stuff' # Note: This is different from the directory name
356n/a
357n/a # Lookup the distribution
358n/a dist = get_distribution(name)
359n/a self.assertIsInstance(dist, Distribution)
360n/a self.assertEqual(dist.name, name)
361n/a
362n/a # Verify that an unknown distribution returns None
363n/a self.assertIsNone(get_distribution('bogus'))
364n/a
365n/a # Verify partial name matching doesn't work
366n/a self.assertIsNone(get_distribution('towel'))
367n/a
368n/a # Verify that it does not find egg-info distributions, when not
369n/a # instructed to
370n/a self.assertIsNone(get_distribution('bacon'))
371n/a self.assertIsNone(get_distribution('cheese'))
372n/a self.assertIsNone(get_distribution('strawberry'))
373n/a self.assertIsNone(get_distribution('banana'))
374n/a
375n/a # Now check that it works well in both situations, when egg-info
376n/a # is a file and directory respectively.
377n/a dist = get_distribution('cheese', use_egg_info=True)
378n/a self.assertIsInstance(dist, EggInfoDistribution)
379n/a self.assertEqual(dist.name, 'cheese')
380n/a
381n/a dist = get_distribution('bacon', use_egg_info=True)
382n/a self.assertIsInstance(dist, EggInfoDistribution)
383n/a self.assertEqual(dist.name, 'bacon')
384n/a
385n/a dist = get_distribution('banana', use_egg_info=True)
386n/a self.assertIsInstance(dist, EggInfoDistribution)
387n/a self.assertEqual(dist.name, 'banana')
388n/a
389n/a dist = get_distribution('strawberry', use_egg_info=True)
390n/a self.assertIsInstance(dist, EggInfoDistribution)
391n/a self.assertEqual(dist.name, 'strawberry')
392n/a
393n/a def test_get_file_users(self):
394n/a # Test the iteration of distributions that use a file.
395n/a name = 'towel_stuff-0.1'
396n/a path = os.path.join(self.fake_dists_path, name,
397n/a 'towel_stuff', '__init__.py')
398n/a for dist in get_file_users(path):
399n/a self.assertIsInstance(dist, Distribution)
400n/a self.assertEqual(dist.name, name)
401n/a
402n/a @requires_zlib
403n/a def test_provides(self):
404n/a # Test for looking up distributions by what they provide
405n/a checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y))
406n/a
407n/a l = [dist.name for dist in provides_distribution('truffles')]
408n/a checkLists(l, ['choxie', 'towel-stuff'])
409n/a
410n/a l = [dist.name for dist in provides_distribution('truffles', '1.0')]
411n/a checkLists(l, ['choxie'])
412n/a
413n/a l = [dist.name for dist in provides_distribution('truffles', '1.0',
414n/a use_egg_info=True)]
415n/a checkLists(l, ['choxie', 'cheese'])
416n/a
417n/a l = [dist.name for dist in provides_distribution('truffles', '1.1.2')]
418n/a checkLists(l, ['towel-stuff'])
419n/a
420n/a l = [dist.name for dist in provides_distribution('truffles', '1.1')]
421n/a checkLists(l, ['towel-stuff'])
422n/a
423n/a l = [dist.name for dist in provides_distribution('truffles',
424n/a '!=1.1,<=2.0')]
425n/a checkLists(l, ['choxie'])
426n/a
427n/a l = [dist.name for dist in provides_distribution('truffles',
428n/a '!=1.1,<=2.0',
429n/a use_egg_info=True)]
430n/a checkLists(l, ['choxie', 'bacon', 'cheese'])
431n/a
432n/a l = [dist.name for dist in provides_distribution('truffles', '>1.0')]
433n/a checkLists(l, ['towel-stuff'])
434n/a
435n/a l = [dist.name for dist in provides_distribution('truffles', '>1.5')]
436n/a checkLists(l, [])
437n/a
438n/a l = [dist.name for dist in provides_distribution('truffles', '>1.5',
439n/a use_egg_info=True)]
440n/a checkLists(l, ['bacon'])
441n/a
442n/a l = [dist.name for dist in provides_distribution('truffles', '>=1.0')]
443n/a checkLists(l, ['choxie', 'towel-stuff'])
444n/a
445n/a l = [dist.name for dist in provides_distribution('strawberry', '0.6',
446n/a use_egg_info=True)]
447n/a checkLists(l, ['coconuts-aster'])
448n/a
449n/a l = [dist.name for dist in provides_distribution('strawberry', '>=0.5',
450n/a use_egg_info=True)]
451n/a checkLists(l, ['coconuts-aster'])
452n/a
453n/a l = [dist.name for dist in provides_distribution('strawberry', '>0.6',
454n/a use_egg_info=True)]
455n/a checkLists(l, [])
456n/a
457n/a l = [dist.name for dist in provides_distribution('banana', '0.4',
458n/a use_egg_info=True)]
459n/a checkLists(l, ['coconuts-aster'])
460n/a
461n/a l = [dist.name for dist in provides_distribution('banana', '>=0.3',
462n/a use_egg_info=True)]
463n/a checkLists(l, ['coconuts-aster'])
464n/a
465n/a l = [dist.name for dist in provides_distribution('banana', '!=0.4',
466n/a use_egg_info=True)]
467n/a checkLists(l, [])
468n/a
469n/a @requires_zlib
470n/a def test_obsoletes(self):
471n/a # Test looking for distributions based on what they obsolete
472n/a checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y))
473n/a
474n/a l = [dist.name for dist in obsoletes_distribution('truffles', '1.0')]
475n/a checkLists(l, [])
476n/a
477n/a l = [dist.name for dist in obsoletes_distribution('truffles', '1.0',
478n/a use_egg_info=True)]
479n/a checkLists(l, ['cheese', 'bacon'])
480n/a
481n/a l = [dist.name for dist in obsoletes_distribution('truffles', '0.8')]
482n/a checkLists(l, ['choxie'])
483n/a
484n/a l = [dist.name for dist in obsoletes_distribution('truffles', '0.8',
485n/a use_egg_info=True)]
486n/a checkLists(l, ['choxie', 'cheese'])
487n/a
488n/a l = [dist.name for dist in obsoletes_distribution('truffles', '0.9.6')]
489n/a checkLists(l, ['choxie', 'towel-stuff'])
490n/a
491n/a l = [dist.name for dist in obsoletes_distribution('truffles',
492n/a '0.5.2.3')]
493n/a checkLists(l, ['choxie', 'towel-stuff'])
494n/a
495n/a l = [dist.name for dist in obsoletes_distribution('truffles', '0.2')]
496n/a checkLists(l, ['towel-stuff'])
497n/a
498n/a @requires_zlib
499n/a def test_yield_distribution(self):
500n/a # tests the internal function _yield_distributions
501n/a checkLists = lambda x, y: self.assertEqual(sorted(x), sorted(y))
502n/a
503n/a eggs = [('bacon', '0.1'), ('banana', '0.4'), ('strawberry', '0.6'),
504n/a ('truffles', '5.0'), ('cheese', '2.0.2'),
505n/a ('coconuts-aster', '10.3'), ('nut', 'funkyversion')]
506n/a dists = [('choxie', '2.0.0.9'), ('grammar', '1.0a4'),
507n/a ('towel-stuff', '0.1'), ('babar', '0.1')]
508n/a
509n/a checkLists([], _yield_distributions(False, False, sys.path))
510n/a
511n/a found = [(dist.name, dist.version)
512n/a for dist in _yield_distributions(False, True, sys.path)
513n/a if dist.path.startswith(self.fake_dists_path)]
514n/a checkLists(eggs, found)
515n/a
516n/a found = [(dist.name, dist.version)
517n/a for dist in _yield_distributions(True, False, sys.path)
518n/a if dist.path.startswith(self.fake_dists_path)]
519n/a checkLists(dists, found)
520n/a
521n/a found = [(dist.name, dist.version)
522n/a for dist in _yield_distributions(True, True, sys.path)
523n/a if dist.path.startswith(self.fake_dists_path)]
524n/a checkLists(dists + eggs, found)
525n/a
526n/a
527n/aclass DataFilesTestCase(GlobTestCaseBase):
528n/a
529n/a def assertRulesMatch(self, rules, spec):
530n/a tempdir = self.build_files_tree(spec)
531n/a expected = self.clean_tree(spec)
532n/a result = get_resources_dests(tempdir, rules)
533n/a self.assertEqual(expected, result)
534n/a
535n/a def clean_tree(self, spec):
536n/a files = {}
537n/a for path, value in spec.items():
538n/a if value is not None:
539n/a files[path] = value
540n/a return files
541n/a
542n/a def test_simple_glob(self):
543n/a rules = [('', '*.tpl', '{data}')]
544n/a spec = {'coucou.tpl': '{data}/coucou.tpl',
545n/a 'Donotwant': None}
546n/a self.assertRulesMatch(rules, spec)
547n/a
548n/a def test_multiple_match(self):
549n/a rules = [('scripts', '*.bin', '{appdata}'),
550n/a ('scripts', '*', '{appscript}')]
551n/a spec = {'scripts/script.bin': '{appscript}/script.bin',
552n/a 'Babarlikestrawberry': None}
553n/a self.assertRulesMatch(rules, spec)
554n/a
555n/a def test_set_match(self):
556n/a rules = [('scripts', '*.{bin,sh}', '{appscript}')]
557n/a spec = {'scripts/script.bin': '{appscript}/script.bin',
558n/a 'scripts/babar.sh': '{appscript}/babar.sh',
559n/a 'Babarlikestrawberry': None}
560n/a self.assertRulesMatch(rules, spec)
561n/a
562n/a def test_set_match_multiple(self):
563n/a rules = [('scripts', 'script{s,}.{bin,sh}', '{appscript}')]
564n/a spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
565n/a 'scripts/script.sh': '{appscript}/script.sh',
566n/a 'Babarlikestrawberry': None}
567n/a self.assertRulesMatch(rules, spec)
568n/a
569n/a def test_set_match_exclude(self):
570n/a rules = [('scripts', '*', '{appscript}'),
571n/a ('', os.path.join('**', '*.sh'), None)]
572n/a spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
573n/a 'scripts/script.sh': None,
574n/a 'Babarlikestrawberry': None}
575n/a self.assertRulesMatch(rules, spec)
576n/a
577n/a def test_glob_in_base(self):
578n/a rules = [('scrip*', '*.bin', '{appscript}')]
579n/a spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
580n/a 'scripouille/babar.bin': '{appscript}/babar.bin',
581n/a 'scriptortu/lotus.bin': '{appscript}/lotus.bin',
582n/a 'Babarlikestrawberry': None}
583n/a self.assertRulesMatch(rules, spec)
584n/a
585n/a def test_recursive_glob(self):
586n/a rules = [('', os.path.join('**', '*.bin'), '{binary}')]
587n/a spec = {'binary0.bin': '{binary}/binary0.bin',
588n/a 'scripts/binary1.bin': '{binary}/scripts/binary1.bin',
589n/a 'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin',
590n/a 'you/kill/pandabear.guy': None}
591n/a self.assertRulesMatch(rules, spec)
592n/a
593n/a def test_final_exemple_glob(self):
594n/a rules = [
595n/a ('mailman/database/schemas/', '*', '{appdata}/schemas'),
596n/a ('', os.path.join('**', '*.tpl'), '{appdata}/templates'),
597n/a ('', os.path.join('developer-docs', '**', '*.txt'), '{doc}'),
598n/a ('', 'README', '{doc}'),
599n/a ('mailman/etc/', '*', '{config}'),
600n/a ('mailman/foo/', os.path.join('**', 'bar', '*.cfg'),
601n/a '{config}/baz'),
602n/a ('mailman/foo/', os.path.join('**', '*.cfg'), '{config}/hmm'),
603n/a ('', 'some-new-semantic.sns', '{funky-crazy-category}'),
604n/a ]
605n/a spec = {
606n/a 'README': '{doc}/README',
607n/a 'some.tpl': '{appdata}/templates/some.tpl',
608n/a 'some-new-semantic.sns':
609n/a '{funky-crazy-category}/some-new-semantic.sns',
610n/a 'mailman/database/mailman.db': None,
611n/a 'mailman/database/schemas/blah.schema':
612n/a '{appdata}/schemas/blah.schema',
613n/a 'mailman/etc/my.cnf': '{config}/my.cnf',
614n/a 'mailman/foo/some/path/bar/my.cfg':
615n/a '{config}/hmm/some/path/bar/my.cfg',
616n/a 'mailman/foo/some/path/other.cfg':
617n/a '{config}/hmm/some/path/other.cfg',
618n/a 'developer-docs/index.txt': '{doc}/developer-docs/index.txt',
619n/a 'developer-docs/api/toc.txt': '{doc}/developer-docs/api/toc.txt',
620n/a }
621n/a self.maxDiff = None
622n/a self.assertRulesMatch(rules, spec)
623n/a
624n/a def test_get_file(self):
625n/a # Create a fake dist
626n/a temp_site_packages = tempfile.mkdtemp()
627n/a self.addCleanup(shutil.rmtree, temp_site_packages)
628n/a
629n/a dist_name = 'test'
630n/a dist_info = os.path.join(temp_site_packages, 'test-0.1.dist-info')
631n/a os.mkdir(dist_info)
632n/a
633n/a metadata_path = os.path.join(dist_info, 'METADATA')
634n/a resources_path = os.path.join(dist_info, 'RESOURCES')
635n/a
636n/a with open(metadata_path, 'w') as fp:
637n/a fp.write(dedent("""\
638n/a Metadata-Version: 1.2
639n/a Name: test
640n/a Version: 0.1
641n/a Summary: test
642n/a Author: me
643n/a """))
644n/a
645n/a test_path = 'test.cfg'
646n/a
647n/a fd, test_resource_path = tempfile.mkstemp()
648n/a os.close(fd)
649n/a self.addCleanup(os.remove, test_resource_path)
650n/a
651n/a with open(test_resource_path, 'w') as fp:
652n/a fp.write('Config')
653n/a
654n/a with open(resources_path, 'w') as fp:
655n/a fp.write('%s,%s' % (test_path, test_resource_path))
656n/a
657n/a # Add fake site-packages to sys.path to retrieve fake dist
658n/a self.addCleanup(sys.path.remove, temp_site_packages)
659n/a sys.path.insert(0, temp_site_packages)
660n/a
661n/a # Force packaging.database to rescan the sys.path
662n/a self.addCleanup(enable_cache)
663n/a disable_cache()
664n/a
665n/a # Try to retrieve resources paths and files
666n/a self.assertEqual(get_file_path(dist_name, test_path),
667n/a test_resource_path)
668n/a self.assertRaises(KeyError, get_file_path, dist_name, 'i-dont-exist')
669n/a
670n/a with get_file(dist_name, test_path) as fp:
671n/a self.assertEqual(fp.read(), 'Config')
672n/a self.assertRaises(KeyError, get_file, dist_name, 'i-dont-exist')
673n/a
674n/a
675n/adef test_suite():
676n/a suite = unittest.TestSuite()
677n/a load = unittest.defaultTestLoader.loadTestsFromTestCase
678n/a suite.addTest(load(TestDistribution))
679n/a suite.addTest(load(TestEggInfoDistribution))
680n/a suite.addTest(load(TestDatabase))
681n/a suite.addTest(load(DataFilesTestCase))
682n/a return suite
683n/a
684n/a
685n/aif __name__ == "__main__":
686n/a unittest.main(defaultTest='test_suite')