Skip to content

Commit 3314f39

Browse files
waebbllinxon
authored andcommitted
New Atom implementation
Now it requires gentoopm from app-portage/gentoopm package, see https://github.com/mgorny/gentoopm/ Add tests Closes: nicolasbock#99 Closes: nicolasbock#96
1 parent 9fcd99e commit 3314f39

File tree

3 files changed

+81
-23
lines changed

3 files changed

+81
-23
lines changed

ebuildtester/atom.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import gentoopm
2+
3+
14
class AtomException(Exception):
25
pass
36

@@ -10,36 +13,51 @@ def __init__(self, atom):
1013
self.package = None
1114
self.version = None
1215

13-
# We don't store the optional '='.
14-
temp = atom.split("=")
15-
self.atom = temp[-1]
16-
1716
try:
18-
self.category, self.package = self.atom.split("/")
19-
except ValueError:
20-
raise AtomException(
21-
"ATOM has to be of the form [=]SECTION/PACKAGE[-VERSION]")
17+
pm = gentoopm.get_package_manager()
2218

23-
# Split off version.
24-
try:
25-
temp = self.package.index("-")
26-
if temp > -1:
27-
self.version = self.package[temp + 1:]
28-
self.package = self.package[:temp]
19+
self.category = pm.Atom(atom).key.category
20+
self.package = pm.Atom(atom).key.package
21+
self.version = pm.Atom(atom).version
22+
23+
if self.category is None or self.package is None:
24+
raise ValueError
2925
except ValueError:
30-
pass
26+
raise AtomException("ATOM has to be of the form [=]SECTION/PACKAGE[-VERSION]")
27+
28+
def atomCategory(self):
29+
"""Returns the package category without name"""
30+
return self.category
31+
32+
def atomName(self):
33+
"""Returns the package name without category"""
34+
return self.package
35+
36+
def atomVersion(self):
37+
"""Returns the package version"""
38+
return self.version
39+
40+
def atomCatName(self):
41+
"""Returns the package category and name without version"""
42+
return "/".join([self.category, self.package])
43+
44+
def atomString(self):
45+
"""Returns a portage compatible string representation"""
46+
if self.version is None:
47+
return self.atomCatName()
48+
49+
return ("=" + "/".join([self.category,
50+
"-".join([self.package,
51+
self.version])]))
3152

3253
def __str__(self):
33-
if self.version is not None:
34-
prefix = "="
35-
suffix = "-" + self.version
36-
else:
37-
prefix = ""
38-
suffix = ""
39-
return prefix + self.category + "/" + self.package + suffix
54+
return self.atomString()
4055

4156
def __eq__(self, other):
42-
result = (self.atom == other.atom)
57+
result = (self.category == other.category
58+
and self.package == other.package
59+
and self.version == other.version)
60+
4361
return result
4462

4563
def __repr__(self):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bandit
2+
git+https://github.com/mgorny/[email protected]#egg=gentoopm
23
pycodestyle
34
pytest
45
sphinx

tests/test_atom.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,42 @@ def test_str(self):
2828
atom_1 = Atom("=CATEGORY/PACKAGE-1.0.0-r1")
2929
atom_2 = Atom(str(atom_1))
3030
self.assertEqual(atom_1, atom_2)
31+
32+
def test_atomName(self):
33+
self.assertEqual(Atom("CATEGORY/PACKAGE").atomName(), "PACKAGE")
34+
self.assertEqual(Atom("=CATEGORY/PACKAGE-1.0.0").atomName(), "PACKAGE")
35+
self.assertEqual(Atom("CATEGORY/PACKAGE-1.0.0").atomName(), "PACKAGE")
36+
self.assertEqual(Atom("=CATEGORY/PACKAGE-DEP-1.0b-r1").atomName(),
37+
"PACKAGE-DEP")
38+
39+
def test_atomCategory(self):
40+
self.assertEqual(Atom("CATEGORY/PACKAGE").atomCategory(), "CATEGORY")
41+
self.assertEqual(Atom("=CATEGORY/PACKAGE-1.0.0").atomCategory(),
42+
"CATEGORY")
43+
44+
def test_atomVersion(self):
45+
self.assertEqual(Atom("=CATEGORY/PACKAGE-1").atomVersion(), "1")
46+
self.assertEqual(Atom("=CATEGORY/PACKAGE-1.0").atomVersion(), "1.0")
47+
self.assertEqual(Atom("=CATEGORY/PACKAGE-1.0-r1").atomVersion(),
48+
"1.0-r1")
49+
self.assertEqual(Atom("=CATEGORY/PACKAGE-DEP-1.0b-r1").atomVersion(),
50+
"1.0b-r1")
51+
52+
def test_atomCatName(self):
53+
self.assertEqual(Atom("CATEGORY/PACKAGE").atomCatName(),
54+
"CATEGORY/PACKAGE")
55+
self.assertEqual(Atom("=CATEGORY/PACKAGE-1.0").atomCatName(),
56+
"CATEGORY/PACKAGE")
57+
58+
def test_atomString(self):
59+
atom1 = Atom("=CATEGORY/PACKAGE-1.0.0")
60+
atom2 = Atom(atom1.atomString())
61+
self.assertEqual(atom1, atom2)
62+
63+
def test_atom(self):
64+
atom1 = Atom("=CATEGORY/PACKAGE-DEP-1.0b-r1")
65+
self.assertEqual(atom1.atomCategory(), "CATEGORY")
66+
self.assertEqual(atom1.atomName(), "PACKAGE-DEP")
67+
self.assertEqual(atom1.atomCatName(), "CATEGORY/PACKAGE-DEP")
68+
self.assertEqual(atom1.atomVersion(), "1.0b-r1")
69+
self.assertEqual(atom1.atomString(), "=CATEGORY/PACKAGE-DEP-1.0b-r1")

0 commit comments

Comments
 (0)