Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: recipe_engine/third_party/setuptools/tests/test_find_packages.py

Issue 1344583003: Recipe package system. (Closed) Base URL: git@github.com:luci/recipes-py.git@master
Patch Set: Recompiled proto Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 """Tests for setuptools.find_packages()."""
2 import os
3 import sys
4 import shutil
5 import tempfile
6 import unittest
7 import platform
8
9 import setuptools
10 from setuptools import find_packages
11 from setuptools.tests.py26compat import skipIf
12
13 find_420_packages = setuptools.PEP420PackageFinder.find
14
15 # modeled after CPython's test.support.can_symlink
16 def can_symlink():
17 TESTFN = tempfile.mktemp()
18 symlink_path = TESTFN + "can_symlink"
19 try:
20 os.symlink(TESTFN, symlink_path)
21 can = True
22 except (OSError, NotImplementedError, AttributeError):
23 can = False
24 else:
25 os.remove(symlink_path)
26 globals().update(can_symlink=lambda: can)
27 return can
28
29 def has_symlink():
30 bad_symlink = (
31 # Windows symlink directory detection is broken on Python 3.2
32 platform.system() == 'Windows' and sys.version_info[:2] == (3,2)
33 )
34 return can_symlink() and not bad_symlink
35
36 class TestFindPackages(unittest.TestCase):
37
38 def setUp(self):
39 self.dist_dir = tempfile.mkdtemp()
40 self._make_pkg_structure()
41
42 def tearDown(self):
43 shutil.rmtree(self.dist_dir)
44
45 def _make_pkg_structure(self):
46 """Make basic package structure.
47
48 dist/
49 docs/
50 conf.py
51 pkg/
52 __pycache__/
53 nspkg/
54 mod.py
55 subpkg/
56 assets/
57 asset
58 __init__.py
59 setup.py
60
61 """
62 self.docs_dir = self._mkdir('docs', self.dist_dir)
63 self._touch('conf.py', self.docs_dir)
64 self.pkg_dir = self._mkdir('pkg', self.dist_dir)
65 self._mkdir('__pycache__', self.pkg_dir)
66 self.ns_pkg_dir = self._mkdir('nspkg', self.pkg_dir)
67 self._touch('mod.py', self.ns_pkg_dir)
68 self.sub_pkg_dir = self._mkdir('subpkg', self.pkg_dir)
69 self.asset_dir = self._mkdir('assets', self.sub_pkg_dir)
70 self._touch('asset', self.asset_dir)
71 self._touch('__init__.py', self.sub_pkg_dir)
72 self._touch('setup.py', self.dist_dir)
73
74 def _mkdir(self, path, parent_dir=None):
75 if parent_dir:
76 path = os.path.join(parent_dir, path)
77 os.mkdir(path)
78 return path
79
80 def _touch(self, path, dir_=None):
81 if dir_:
82 path = os.path.join(dir_, path)
83 fp = open(path, 'w')
84 fp.close()
85 return path
86
87 def test_regular_package(self):
88 self._touch('__init__.py', self.pkg_dir)
89 packages = find_packages(self.dist_dir)
90 self.assertEqual(packages, ['pkg', 'pkg.subpkg'])
91
92 def test_exclude(self):
93 self._touch('__init__.py', self.pkg_dir)
94 packages = find_packages(self.dist_dir, exclude=('pkg.*',))
95 assert packages == ['pkg']
96
97 def test_include_excludes_other(self):
98 """
99 If include is specified, other packages should be excluded.
100 """
101 self._touch('__init__.py', self.pkg_dir)
102 alt_dir = self._mkdir('other_pkg', self.dist_dir)
103 self._touch('__init__.py', alt_dir)
104 packages = find_packages(self.dist_dir, include=['other_pkg'])
105 self.assertEqual(packages, ['other_pkg'])
106
107 def test_dir_with_dot_is_skipped(self):
108 shutil.rmtree(os.path.join(self.dist_dir, 'pkg/subpkg/assets'))
109 data_dir = self._mkdir('some.data', self.pkg_dir)
110 self._touch('__init__.py', data_dir)
111 self._touch('file.dat', data_dir)
112 packages = find_packages(self.dist_dir)
113 self.assertTrue('pkg.some.data' not in packages)
114
115 def test_dir_with_packages_in_subdir_is_excluded(self):
116 """
117 Ensure that a package in a non-package such as build/pkg/__init__.py
118 is excluded.
119 """
120 build_dir = self._mkdir('build', self.dist_dir)
121 build_pkg_dir = self._mkdir('pkg', build_dir)
122 self._touch('__init__.py', build_pkg_dir)
123 packages = find_packages(self.dist_dir)
124 self.assertTrue('build.pkg' not in packages)
125
126 @skipIf(not has_symlink(), 'Symlink support required')
127 def test_symlinked_packages_are_included(self):
128 """
129 A symbolically-linked directory should be treated like any other
130 directory when matched as a package.
131
132 Create a link from lpkg -> pkg.
133 """
134 self._touch('__init__.py', self.pkg_dir)
135 linked_pkg = os.path.join(self.dist_dir, 'lpkg')
136 os.symlink('pkg', linked_pkg)
137 assert os.path.isdir(linked_pkg)
138 packages = find_packages(self.dist_dir)
139 self.assertTrue('lpkg' in packages)
140
141 def _assert_packages(self, actual, expected):
142 self.assertEqual(set(actual), set(expected))
143
144 def test_pep420_ns_package(self):
145 packages = find_420_packages(
146 self.dist_dir, include=['pkg*'], exclude=['pkg.subpkg.assets'])
147 self._assert_packages(packages, ['pkg', 'pkg.nspkg', 'pkg.subpkg'])
148
149 def test_pep420_ns_package_no_includes(self):
150 packages = find_420_packages(
151 self.dist_dir, exclude=['pkg.subpkg.assets'])
152 self._assert_packages(packages, ['docs', 'pkg', 'pkg.nspkg', 'pkg.subpkg '])
153
154 def test_pep420_ns_package_no_includes_or_excludes(self):
155 packages = find_420_packages(self.dist_dir)
156 expected = [
157 'docs', 'pkg', 'pkg.nspkg', 'pkg.subpkg', 'pkg.subpkg.assets']
158 self._assert_packages(packages, expected)
159
160 def test_regular_package_with_nested_pep420_ns_packages(self):
161 self._touch('__init__.py', self.pkg_dir)
162 packages = find_420_packages(
163 self.dist_dir, exclude=['docs', 'pkg.subpkg.assets'])
164 self._assert_packages(packages, ['pkg', 'pkg.nspkg', 'pkg.subpkg'])
165
166 def test_pep420_ns_package_no_non_package_dirs(self):
167 shutil.rmtree(self.docs_dir)
168 shutil.rmtree(os.path.join(self.dist_dir, 'pkg/subpkg/assets'))
169 packages = find_420_packages(self.dist_dir)
170 self._assert_packages(packages, ['pkg', 'pkg.nspkg', 'pkg.subpkg'])
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698