1 |
commit: 0891a5f671527f99fa1e9ca56ea96e5566abd52f |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Mon Aug 29 06:50:16 2011 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Aug 29 06:50:16 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=0891a5f6 |
7 |
|
8 |
emerge: add simple unit tests |
9 |
|
10 |
These tests are similar to the repoman tests, using a |
11 |
__PORTAGE_TEST_EPREFIX environment variable to make emerge confine |
12 |
itself to a testing prefix so that things like install and uninstall |
13 |
operations can be performed. |
14 |
|
15 |
--- |
16 |
pym/portage/__init__.py | 6 +- |
17 |
pym/portage/data.py | 3 + |
18 |
pym/portage/tests/emerge/__init__.py | 2 + |
19 |
pym/portage/tests/emerge/test_simple.py | 139 +++++++++++++++++++++++++++++++ |
20 |
4 files changed, 148 insertions(+), 2 deletions(-) |
21 |
|
22 |
diff --git a/pym/portage/__init__.py b/pym/portage/__init__.py |
23 |
index 78d9b54..72cdf2d 100644 |
24 |
--- a/pym/portage/__init__.py |
25 |
+++ b/pym/portage/__init__.py |
26 |
@@ -472,8 +472,9 @@ def create_trees(config_root=None, target_root=None, trees=None): |
27 |
portdbapi.portdbapi_instances.remove(portdb) |
28 |
del trees[myroot]["porttree"], myroot, portdb |
29 |
|
30 |
+ eprefix = os.environ.get("__PORTAGE_TEST_EPREFIX") |
31 |
settings = config(config_root=config_root, target_root=target_root, |
32 |
- config_incrementals=portage.const.INCREMENTALS) |
33 |
+ config_incrementals=portage.const.INCREMENTALS, _eprefix=eprefix) |
34 |
settings.lock() |
35 |
|
36 |
myroots = [(settings["ROOT"], settings)] |
37 |
@@ -489,7 +490,8 @@ def create_trees(config_root=None, target_root=None, trees=None): |
38 |
v = settings.get(k) |
39 |
if v is not None: |
40 |
clean_env[k] = v |
41 |
- settings = config(config_root=None, target_root="/", env=clean_env) |
42 |
+ settings = config(config_root=None, target_root="/", |
43 |
+ env=clean_env, _eprefix=eprefix) |
44 |
settings.lock() |
45 |
myroots.append((settings["ROOT"], settings)) |
46 |
|
47 |
|
48 |
diff --git a/pym/portage/data.py b/pym/portage/data.py |
49 |
index c38fa17..c496c0b 100644 |
50 |
--- a/pym/portage/data.py |
51 |
+++ b/pym/portage/data.py |
52 |
@@ -65,6 +65,9 @@ wheelgid=0 |
53 |
|
54 |
if uid==0: |
55 |
secpass=2 |
56 |
+elif "__PORTAGE_TEST_EPREFIX" in os.environ: |
57 |
+ secpass = 2 |
58 |
+ |
59 |
try: |
60 |
wheelgid=grp.getgrnam("wheel")[2] |
61 |
except KeyError: |
62 |
|
63 |
diff --git a/pym/portage/tests/emerge/__init__.py b/pym/portage/tests/emerge/__init__.py |
64 |
new file mode 100644 |
65 |
index 0000000..532918b |
66 |
--- /dev/null |
67 |
+++ b/pym/portage/tests/emerge/__init__.py |
68 |
@@ -0,0 +1,2 @@ |
69 |
+# Copyright 2011 Gentoo Foundation |
70 |
+# Distributed under the terms of the GNU General Public License v2 |
71 |
|
72 |
diff --git a/pym/portage/tests/emerge/__test__ b/pym/portage/tests/emerge/__test__ |
73 |
new file mode 100644 |
74 |
index 0000000..e69de29 |
75 |
|
76 |
diff --git a/pym/portage/tests/emerge/test_simple.py b/pym/portage/tests/emerge/test_simple.py |
77 |
new file mode 100644 |
78 |
index 0000000..a13d0e6 |
79 |
--- /dev/null |
80 |
+++ b/pym/portage/tests/emerge/test_simple.py |
81 |
@@ -0,0 +1,139 @@ |
82 |
+# Copyright 2011 Gentoo Foundation |
83 |
+# Distributed under the terms of the GNU General Public License v2 |
84 |
+ |
85 |
+import subprocess |
86 |
+import sys |
87 |
+ |
88 |
+import portage |
89 |
+from portage import os |
90 |
+from portage import _unicode_decode |
91 |
+from portage.const import PORTAGE_BIN_PATH, PORTAGE_PYM_PATH |
92 |
+from portage.process import find_binary |
93 |
+from portage.tests import TestCase |
94 |
+from portage.tests.resolver.ResolverPlayground import ResolverPlayground |
95 |
+from portage.util import ensure_dirs |
96 |
+ |
97 |
+class SimpleEmergeTestCase(TestCase): |
98 |
+ |
99 |
+ def testSimple(self): |
100 |
+ |
101 |
+ ebuilds = { |
102 |
+ "dev-libs/A-1": { |
103 |
+ "EAPI" : "4", |
104 |
+ "IUSE" : "+flag", |
105 |
+ "KEYWORDS": "x86", |
106 |
+ "LICENSE": "GPL-2", |
107 |
+ "RDEPEND": "flag? ( dev-libs/B[flag] )", |
108 |
+ }, |
109 |
+ "dev-libs/B-1": { |
110 |
+ "EAPI" : "4", |
111 |
+ "IUSE" : "+flag", |
112 |
+ "KEYWORDS": "x86", |
113 |
+ "LICENSE": "GPL-2", |
114 |
+ }, |
115 |
+ } |
116 |
+ |
117 |
+ installed = { |
118 |
+ "dev-libs/A-1": { |
119 |
+ "EAPI" : "4", |
120 |
+ "IUSE" : "+flag", |
121 |
+ "KEYWORDS": "x86", |
122 |
+ "LICENSE": "GPL-2", |
123 |
+ "RDEPEND": "flag? ( dev-libs/B[flag] )", |
124 |
+ }, |
125 |
+ "dev-libs/B-1": { |
126 |
+ "EAPI" : "4", |
127 |
+ "IUSE" : "+flag", |
128 |
+ "KEYWORDS": "x86", |
129 |
+ "LICENSE": "GPL-2", |
130 |
+ }, |
131 |
+ } |
132 |
+ |
133 |
+ default_args = ("--package-moves=n",) |
134 |
+ test_args = ( |
135 |
+ ("--version",), |
136 |
+ ("--info",), |
137 |
+ ("--info", "--verbose"), |
138 |
+ ("--pretend", "dev-libs/A"), |
139 |
+ ("--pretend", "--tree", "--complete-graph", "dev-libs/A"), |
140 |
+ ("-p", "dev-libs/B"), |
141 |
+ ("--oneshot", "dev-libs/A",), |
142 |
+ ("--noreplace", "dev-libs/A",), |
143 |
+ ("--pretend", "--depclean", "--verbose", "dev-libs/B"), |
144 |
+ ("--pretend", "--depclean",), |
145 |
+ ("--depclean",), |
146 |
+ ("--unmerge", "--quiet", "dev-libs/A"), |
147 |
+ ("-C", "--quiet", "dev-libs/B"), |
148 |
+ ) |
149 |
+ |
150 |
+ playground = ResolverPlayground(ebuilds=ebuilds, installed=installed) |
151 |
+ settings = playground.settings |
152 |
+ eprefix = settings["EPREFIX"] |
153 |
+ distdir = os.path.join(eprefix, "distdir") |
154 |
+ fake_bin = os.path.join(eprefix, "bin") |
155 |
+ portage_tmpdir = os.path.join(eprefix, "var", "tmp", "portage") |
156 |
+ profile_path = settings.profile_path |
157 |
+ var_cache_edb = os.path.join(eprefix, "var", "cache", "edb") |
158 |
+ env = os.environ.copy() |
159 |
+ |
160 |
+ path = env.get("PATH") |
161 |
+ if path is not None and not path.strip(): |
162 |
+ path = None |
163 |
+ if path is None: |
164 |
+ path = "" |
165 |
+ else: |
166 |
+ path = ":" + path |
167 |
+ path = fake_bin + path |
168 |
+ |
169 |
+ pythonpath = env.get("PYTHONPATH") |
170 |
+ if pythonpath is not None and not pythonpath.strip(): |
171 |
+ pythonpath = None |
172 |
+ if pythonpath is not None and \ |
173 |
+ pythonpath.startswith(PORTAGE_PYM_PATH + ":"): |
174 |
+ pass |
175 |
+ else: |
176 |
+ if pythonpath is None: |
177 |
+ pythonpath = "" |
178 |
+ else: |
179 |
+ pythonpath = ":" + pythonpath |
180 |
+ pythonpath = PORTAGE_PYM_PATH + pythonpath |
181 |
+ |
182 |
+ env['PYTHONPATH'] = pythonpath |
183 |
+ env.update({ |
184 |
+ "__PORTAGE_TEST_EPREFIX" : eprefix, |
185 |
+ "DISTDIR" : distdir, |
186 |
+ "INFODIR" : "", |
187 |
+ "INFOPATH" : "", |
188 |
+ "PATH" : path, |
189 |
+ "PORTAGE_TMPDIR" : portage_tmpdir, |
190 |
+ }) |
191 |
+ dirs = [distdir, fake_bin, portage_tmpdir, var_cache_edb] |
192 |
+ true_symlinks = ["chown", "chgrp"] |
193 |
+ true_binary = find_binary("true") |
194 |
+ self.assertEqual(true_binary is None, False, |
195 |
+ "true command not found") |
196 |
+ try: |
197 |
+ for d in dirs: |
198 |
+ ensure_dirs(d) |
199 |
+ for x in true_symlinks: |
200 |
+ os.symlink(true_binary, os.path.join(fake_bin, x)) |
201 |
+ with open(os.path.join(var_cache_edb, "counter"), 'wb') as f: |
202 |
+ f.write(b"100") |
203 |
+ # non-empty system set keeps --depclean quiet |
204 |
+ with open(os.path.join(profile_path, "packages"), 'w') as f: |
205 |
+ f.write("*dev-libs/token-system-pkg") |
206 |
+ for args in test_args: |
207 |
+ proc = subprocess.Popen([portage._python_interpreter, "-Wd", |
208 |
+ os.path.join(PORTAGE_BIN_PATH, "emerge")] + \ |
209 |
+ list(default_args) + list(args), |
210 |
+ env=env, stdout=subprocess.PIPE) |
211 |
+ output = proc.stdout.readlines() |
212 |
+ proc.wait() |
213 |
+ proc.stdout.close() |
214 |
+ if proc.returncode != os.EX_OK: |
215 |
+ for line in output: |
216 |
+ sys.stderr.write(_unicode_decode(line)) |
217 |
+ |
218 |
+ self.assertEqual(os.EX_OK, proc.returncode, "emerge failed") |
219 |
+ finally: |
220 |
+ playground.cleanup() |