blob: ade191c57a0e9bb9bdffd882d25b459ec200bd52 [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Tests for ``packaging.command.install_distinfo``. """
2
3import os
4import csv
5import hashlib
6import sys
7
8from packaging.command.install_distinfo import install_distinfo
9from packaging.command.cmd import Command
10from packaging.metadata import Metadata
11from packaging.tests import unittest, support
12
13
14class DummyInstallCmd(Command):
15
16 def __init__(self, dist=None):
17 self.outputs = []
18 self.distribution = dist
19
20 def __getattr__(self, name):
21 return None
22
23 def ensure_finalized(self):
24 pass
25
26 def get_outputs(self):
27 return (self.outputs +
28 self.get_finalized_command('install_distinfo').get_outputs())
29
30
31class InstallDistinfoTestCase(support.TempdirManager,
32 support.LoggingCatcher,
33 unittest.TestCase):
34
35 checkLists = lambda self, x, y: self.assertListEqual(sorted(x), sorted(y))
36
37 def test_empty_install(self):
38 pkg_dir, dist = self.create_dist(name='foo',
39 version='1.0')
40 install_dir = self.mkdtemp()
41
42 install = DummyInstallCmd(dist)
43 dist.command_obj['install_dist'] = install
44
45 cmd = install_distinfo(dist)
46 dist.command_obj['install_distinfo'] = cmd
47
48 cmd.initialize_options()
49 cmd.distinfo_dir = install_dir
50 cmd.ensure_finalized()
51 cmd.run()
52
53 self.checkLists(os.listdir(install_dir), ['foo-1.0.dist-info'])
54
55 dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
56 self.checkLists(os.listdir(dist_info),
57 ['METADATA', 'RECORD', 'REQUESTED', 'INSTALLER'])
58 with open(os.path.join(dist_info, 'INSTALLER')) as fp:
59 self.assertEqual(fp.read(), 'distutils')
60 with open(os.path.join(dist_info, 'REQUESTED')) as fp:
61 self.assertEqual(fp.read(), '')
62 meta_path = os.path.join(dist_info, 'METADATA')
63 self.assertTrue(Metadata(path=meta_path).check())
64
65 def test_installer(self):
66 pkg_dir, dist = self.create_dist(name='foo',
67 version='1.0')
68 install_dir = self.mkdtemp()
69
70 install = DummyInstallCmd(dist)
71 dist.command_obj['install_dist'] = install
72
73 cmd = install_distinfo(dist)
74 dist.command_obj['install_distinfo'] = cmd
75
76 cmd.initialize_options()
77 cmd.distinfo_dir = install_dir
78 cmd.installer = 'bacon-python'
79 cmd.ensure_finalized()
80 cmd.run()
81
82 dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
83 with open(os.path.join(dist_info, 'INSTALLER')) as fp:
84 self.assertEqual(fp.read(), 'bacon-python')
85
86 def test_requested(self):
87 pkg_dir, dist = self.create_dist(name='foo',
88 version='1.0')
89 install_dir = self.mkdtemp()
90
91 install = DummyInstallCmd(dist)
92 dist.command_obj['install_dist'] = install
93
94 cmd = install_distinfo(dist)
95 dist.command_obj['install_distinfo'] = cmd
96
97 cmd.initialize_options()
98 cmd.distinfo_dir = install_dir
99 cmd.requested = False
100 cmd.ensure_finalized()
101 cmd.run()
102
103 dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
104 self.checkLists(os.listdir(dist_info),
105 ['METADATA', 'RECORD', 'INSTALLER'])
106
107 def test_no_record(self):
108 pkg_dir, dist = self.create_dist(name='foo',
109 version='1.0')
110 install_dir = self.mkdtemp()
111
112 install = DummyInstallCmd(dist)
113 dist.command_obj['install_dist'] = install
114
115 cmd = install_distinfo(dist)
116 dist.command_obj['install_distinfo'] = cmd
117
118 cmd.initialize_options()
119 cmd.distinfo_dir = install_dir
120 cmd.no_record = True
121 cmd.ensure_finalized()
122 cmd.run()
123
124 dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
125 self.checkLists(os.listdir(dist_info),
126 ['METADATA', 'REQUESTED', 'INSTALLER'])
127
128 def test_record(self):
129 pkg_dir, dist = self.create_dist(name='foo',
130 version='1.0')
131 install_dir = self.mkdtemp()
132
133 install = DummyInstallCmd(dist)
134 dist.command_obj['install_dist'] = install
135
136 fake_dists = os.path.join(os.path.dirname(__file__), 'fake_dists')
137 fake_dists = os.path.realpath(fake_dists)
138
139 # for testing, we simply add all files from _backport's fake_dists
140 dirs = []
141 for dir in os.listdir(fake_dists):
142 full_path = os.path.join(fake_dists, dir)
143 if (not dir.endswith('.egg') or dir.endswith('.egg-info') or
144 dir.endswith('.dist-info')) and os.path.isdir(full_path):
145 dirs.append(full_path)
146
147 for dir in dirs:
148 for path, subdirs, files in os.walk(dir):
149 install.outputs += [os.path.join(path, f) for f in files]
150 install.outputs += [os.path.join('path', f + 'c')
151 for f in files if f.endswith('.py')]
152
153 cmd = install_distinfo(dist)
154 dist.command_obj['install_distinfo'] = cmd
155
156 cmd.initialize_options()
157 cmd.distinfo_dir = install_dir
158 cmd.ensure_finalized()
159 cmd.run()
160
161 dist_info = os.path.join(install_dir, 'foo-1.0.dist-info')
162
163 expected = []
164 for f in install.get_outputs():
Éric Araujof53cd892011-06-10 03:53:49 +0200165 if (f.endswith(('.pyc', '.pyo')) or f == os.path.join(
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200166 install_dir, 'foo-1.0.dist-info', 'RECORD')):
167 expected.append([f, '', ''])
168 else:
169 size = os.path.getsize(f)
170 md5 = hashlib.md5()
Victor Stinner35de5ac2011-05-19 15:09:57 +0200171 with open(f, 'rb') as fp:
172 md5.update(fp.read())
Tarek Ziade1231a4e2011-05-19 13:07:25 +0200173 hash = md5.hexdigest()
174 expected.append([f, hash, str(size)])
175
176 parsed = []
177 with open(os.path.join(dist_info, 'RECORD'), 'r') as f:
178 reader = csv.reader(f, delimiter=',',
179 lineterminator=os.linesep,
180 quotechar='"')
181 parsed = list(reader)
182
183 self.maxDiff = None
184 self.checkLists(parsed, expected)
185
186
187def test_suite():
188 return unittest.makeSuite(InstallDistinfoTestCase)
189
190
191if __name__ == "__main__":
192 unittest.main(defaultTest="test_suite")