Use EROOT instead of ROOT for keys everywhere.
[portage.git] / pym / portage / tests / repoman / test_simple.py
1 # Copyright 2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 import shutil
5 import subprocess
6 import sys
7 import time
8
9 import portage
10 from portage import os
11 from portage import _unicode_decode
12 from portage.const import PORTAGE_BASE_PATH, PORTAGE_BIN_PATH, PORTAGE_PYM_PATH
13 from portage.process import find_binary
14 from portage.tests import TestCase
15 from portage.tests.resolver.ResolverPlayground import ResolverPlayground
16 from portage.util import ensure_dirs
17 from repoman.utilities import _update_copyright_year
18
19 class SimpleRepomanTestCase(TestCase):
20
21         def testCopyrightUpdate(self):
22                 test_cases = (
23                         (
24                                 '2011',
25                                 '# Copyright 1999-2008 Gentoo Foundation; Distributed under the GPL v2',
26                                 '# Copyright 1999-2011 Gentoo Foundation; Distributed under the GPL v2',
27                         ),
28                         (
29                                 '2011',
30                                 '# Copyright 1999 Gentoo Foundation; Distributed under the GPL v2',
31                                 '# Copyright 1999-2011 Gentoo Foundation; Distributed under the GPL v2',
32                         ),
33                         (
34                                 '1999',
35                                 '# Copyright 1999 Gentoo Foundation; Distributed under the GPL v2',
36                                 '# Copyright 1999 Gentoo Foundation; Distributed under the GPL v2',
37                         ),
38                 )
39
40                 for year, before, after in test_cases:
41                         self.assertEqual(_update_copyright_year(year, before), after)
42
43         def _must_skip(self):
44                 xmllint = find_binary("xmllint")
45                 if not xmllint:
46                         return "xmllint not found"
47
48                 try:
49                         __import__("xml.etree.ElementTree")
50                         __import__("xml.parsers.expat").parsers.expat.ExpatError
51                 except (AttributeError, ImportError):
52                         return "python is missing xml support"
53
54         def testSimple(self):
55                 debug = False
56
57                 skip_reason = self._must_skip()
58                 if skip_reason:
59                         self.portage_skip = skip_reason
60                         self.assertFalse(True, skip_reason)
61                         return
62
63                 copyright_header = """# Copyright 1999-%s Gentoo Foundation
64 # Distributed under the terms of the GNU General Public License v2
65 # $Header: $
66 """ % time.gmtime().tm_year
67
68                 repo_configs = {
69                         "test_repo": {
70                                 "layout.conf":
71                                         (
72                                                 "update-changelog = true",
73                                         ),
74                         }
75                 }
76
77                 profiles = (
78                         ("x86", "default/linux/x86/test_profile", "stable"),
79                 )
80
81                 ebuilds = {
82                         "dev-libs/A-1": {
83                                 "COPYRIGHT_HEADER" : copyright_header,
84                                 "DESCRIPTION" : "Desc goes here",
85                                 "EAPI" : "4",
86                                 "HOMEPAGE" : "http://example.com",
87                                 "IUSE" : "flag",
88                                 "KEYWORDS": "~x86",
89                                 "LICENSE": "GPL-2",
90                                 "RDEPEND": "flag? ( dev-libs/B[flag] )",
91                         },
92                         "dev-libs/B-1": {
93                                 "COPYRIGHT_HEADER" : copyright_header,
94                                 "DESCRIPTION" : "Desc goes here",
95                                 "EAPI" : "4",
96                                 "HOMEPAGE" : "http://example.com",
97                                 "IUSE" : "flag",
98                                 "KEYWORDS": "~x86",
99                                 "LICENSE": "GPL-2",
100                         },
101                 }
102                 licenses = ["GPL-2"]
103                 arch_list = ["x86"]
104                 metadata_dtd = os.path.join(PORTAGE_BASE_PATH, "cnf/metadata.dtd")
105                 metadata_xml_files = (
106                         (
107                                 "dev-libs/A",
108                                 {
109                                         "herd" : "base-system",
110                                         "flags" : "<flag name='flag'>Description of how USE='flag' affects this package</flag>",
111                                 },
112                         ),
113                         (
114                                 "dev-libs/B",
115                                 {
116                                         "herd" : "no-herd",
117                                         "flags" : "<flag name='flag'>Description of how USE='flag' affects this package</flag>",
118                                 },
119                         ),
120                 )
121
122                 use_desc = (
123                         ("flag", "Description of how USE='flag' affects packages"),
124                 )
125
126                 playground = ResolverPlayground(ebuilds=ebuilds,
127                         repo_configs=repo_configs, debug=debug)
128                 settings = playground.settings
129                 eprefix = settings["EPREFIX"]
130                 eroot = settings["EROOT"]
131                 portdb = playground.trees[playground.eroot]["porttree"].dbapi
132                 homedir = os.path.join(eroot, "home")
133                 distdir = os.path.join(eprefix, "distdir")
134                 portdir = settings["PORTDIR"]
135                 profiles_dir = os.path.join(portdir, "profiles")
136                 license_dir = os.path.join(portdir, "licenses")
137
138                 repoman_cmd = (portage._python_interpreter, "-Wd",
139                         os.path.join(PORTAGE_BIN_PATH, "repoman"))
140
141                 git_binary = find_binary("git")
142                 git_cmd = (git_binary,)
143
144                 cp_binary = find_binary("cp")
145                 self.assertEqual(cp_binary is None, False,
146                         "cp command not found")
147                 cp_cmd = (cp_binary,)
148
149                 test_ebuild = portdb.findname("dev-libs/A-1")
150                 self.assertFalse(test_ebuild is None)
151
152                 committer_name = "Gentoo Dev"
153                 committer_email = "gentoo-dev@gentoo.org"
154                 
155                 git_test = (
156                         ("", repoman_cmd + ("manifest",)),
157                         ("", git_cmd + ("config", "--global", "user.name", committer_name,)),
158                         ("", git_cmd + ("config", "--global", "user.email", committer_email,)),
159                         ("", git_cmd + ("init-db",)),
160                         ("", git_cmd + ("add", ".")),
161                         ("", git_cmd + ("commit", "-a", "-m", "add whole repo")),
162                         ("", cp_cmd + (test_ebuild, test_ebuild[:-8] + "2.ebuild")),
163                         ("", git_cmd + ("add", test_ebuild[:-8] + "2.ebuild")),
164                         ("", repoman_cmd + ("commit", "-m", "bump to version 2")),
165                         ("", cp_cmd + (test_ebuild, test_ebuild[:-8] + "3.ebuild")),
166                         ("", git_cmd + ("add", test_ebuild[:-8] + "3.ebuild")),
167                         ("dev-libs", repoman_cmd + ("commit", "-m", "bump to version 3")),
168                         ("", cp_cmd + (test_ebuild, test_ebuild[:-8] + "4.ebuild")),
169                         ("", git_cmd + ("add", test_ebuild[:-8] + "4.ebuild")),
170                         ("dev-libs/A", repoman_cmd + ("commit", "-m", "bump to version 4")),
171                 )
172
173                 pythonpath =  os.environ.get("PYTHONPATH")
174                 if pythonpath is not None and not pythonpath.strip():
175                         pythonpath = None
176                 if pythonpath is not None and \
177                         pythonpath.split(":")[0] == PORTAGE_PYM_PATH:
178                         pass
179                 else:
180                         if pythonpath is None:
181                                 pythonpath = ""
182                         else:
183                                 pythonpath = ":" + pythonpath
184                         pythonpath = PORTAGE_PYM_PATH + pythonpath
185
186                 env = {
187                         "__REPOMAN_TEST_EPREFIX" : eprefix,
188                         "DISTDIR" : distdir,
189                         "GENTOO_COMMITTER_NAME" : committer_name,
190                         "GENTOO_COMMITTER_EMAIL" : committer_email,
191                         "HOME" : homedir,
192                         "PATH" : os.environ["PATH"],
193                         "PORTAGE_GRPNAME" : os.environ["PORTAGE_GRPNAME"],
194                         "PORTAGE_USERNAME" : os.environ["PORTAGE_USERNAME"],
195                         "PORTDIR" : portdir,
196                         "PYTHONPATH" : pythonpath,
197                 }
198
199                 dirs = [homedir, license_dir, profiles_dir, distdir]
200                 try:
201                         for d in dirs:
202                                 ensure_dirs(d)
203                         with open(os.path.join(portdir, "skel.ChangeLog"), 'w') as f:
204                                 f.write(copyright_header)
205                         with open(os.path.join(profiles_dir, "profiles.desc"), 'w') as f:
206                                 for x in profiles:
207                                         f.write("%s %s %s\n" % x)
208                         for x in licenses:
209                                 open(os.path.join(license_dir, x), 'wb').close()
210                         with open(os.path.join(profiles_dir, "arch.list"), 'w') as f:
211                                 for x in arch_list:
212                                         f.write("%s\n" % x)
213                         with open(os.path.join(profiles_dir, "use.desc"), 'w') as f:
214                                 for k, v in use_desc:
215                                         f.write("%s - %s\n" % (k, v))
216                         for cp, xml_data in metadata_xml_files:
217                                 with open(os.path.join(portdir, cp, "metadata.xml"), 'w') as f:
218                                         f.write(playground.metadata_xml_template % xml_data)
219                         # Use a symlink to portdir, in order to trigger bugs
220                         # involving canonical vs. non-canonical paths.
221                         portdir_symlink = os.path.join(eroot, "portdir_symlink")
222                         os.symlink(portdir, portdir_symlink)
223                         # repoman checks metadata.dtd for recent CTIME, so copy the file in
224                         # order to ensure that the CTIME is current
225                         shutil.copyfile(metadata_dtd, os.path.join(distdir, "metadata.dtd"))
226
227                         if debug:
228                                 # The subprocess inherits both stdout and stderr, for
229                                 # debugging purposes.
230                                 stdout = None
231                         else:
232                                 # The subprocess inherits stderr so that any warnings
233                                 # triggered by python -Wd will be visible.
234                                 stdout = subprocess.PIPE
235
236                         for cwd in ("", "dev-libs", "dev-libs/A", "dev-libs/B"):
237                                 abs_cwd = os.path.join(portdir_symlink, cwd)
238                                 proc = subprocess.Popen([portage._python_interpreter, "-Wd",
239                                         os.path.join(PORTAGE_BIN_PATH, "repoman"), "full"],
240                                         cwd=abs_cwd, env=env, stdout=stdout)
241
242                                 if debug:
243                                         proc.wait()
244                                 else:
245                                         output = proc.stdout.readlines()
246                                         proc.wait()
247                                         proc.stdout.close()
248                                         if proc.returncode != os.EX_OK:
249                                                 for line in output:
250                                                         sys.stderr.write(_unicode_decode(line))
251
252                                 self.assertEqual(os.EX_OK, proc.returncode,
253                                         "repoman failed in %s" % (cwd,))
254
255                         if git_binary is not None:
256                                 for cwd, cmd in git_test:
257                                         abs_cwd = os.path.join(portdir_symlink, cwd)
258                                         proc = subprocess.Popen(cmd,
259                                                 cwd=abs_cwd, env=env, stdout=stdout)
260
261                                         if debug:
262                                                 proc.wait()
263                                         else:
264                                                 output = proc.stdout.readlines()
265                                                 proc.wait()
266                                                 proc.stdout.close()
267                                                 if proc.returncode != os.EX_OK:
268                                                         for line in output:
269                                                                 sys.stderr.write(_unicode_decode(line))
270
271                                         self.assertEqual(os.EX_OK, proc.returncode,
272                                                 "%s failed in %s" % (cmd, cwd,))
273                 finally:
274                         playground.cleanup()