summaryrefslogtreecommitdiffstats
path: root/tests/test_deps.py
blob: 860856caae44938f7b7d014898abde0543ecdb6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
#
# Copyright (C) 2010 Julian Andres Klode <jak@debian.org>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.
"""Unit tests for verifying the correctness of check_dep, etc in apt_pkg."""
import itertools
import unittest

import apt_pkg
import testcommon

import apt.package


class TestDependencies(testcommon.TestCase):
    def testCheckDep(self):
        """dependencies: Test apt_pkg.CheckDep() for '<' and '>'

        The CheckDep function should treat '<' as '<=' and '>' as '>=', for
        compatibility reasons."""
        if not hasattr(apt_pkg, "CheckDep"):
            return
        self.assertFalse(apt_pkg.CheckDep("1", "<", "0"))
        self.assertTrue(apt_pkg.CheckDep("1", "<", "1"))
        self.assertTrue(apt_pkg.CheckDep("1", "<", "2"))

        self.assertFalse(apt_pkg.CheckDep("0", ">", "1"))
        self.assertTrue(apt_pkg.CheckDep("1", ">", "1"))
        self.assertTrue(apt_pkg.CheckDep("2", ">", "1"))

    def test_check_dep(self):
        "dependencies: Test apt_pkg.check_dep()"
        self.assertFalse(apt_pkg.check_dep("1", "<<", "0"))
        self.assertFalse(apt_pkg.check_dep("1", "<<", "1"))
        self.assertTrue(apt_pkg.check_dep("1", "<<", "2"))

        self.assertFalse(apt_pkg.check_dep("1", "<", "0"))
        self.assertFalse(apt_pkg.check_dep("1", "<", "1"))
        self.assertTrue(apt_pkg.check_dep("1", "<", "2"))

        self.assertFalse(apt_pkg.check_dep("1", "<=", "0"))
        self.assertTrue(apt_pkg.check_dep("1", "<=", "1"))
        self.assertTrue(apt_pkg.check_dep("1", "<=", "2"))

        self.assertFalse(apt_pkg.check_dep("0", "=", "1"))
        self.assertTrue(apt_pkg.check_dep("1", "=", "1"))
        self.assertFalse(apt_pkg.check_dep("2", "=", "1"))

        self.assertFalse(apt_pkg.check_dep("0", ">=", "1"))
        self.assertTrue(apt_pkg.check_dep("1", ">=", "1"))
        self.assertTrue(apt_pkg.check_dep("2", ">=", "1"))

        self.assertFalse(apt_pkg.check_dep("0", ">", "1"))
        self.assertFalse(apt_pkg.check_dep("1", ">", "1"))
        self.assertTrue(apt_pkg.check_dep("2", ">", "1"))

        self.assertFalse(apt_pkg.check_dep("0", ">>", "1"))
        self.assertFalse(apt_pkg.check_dep("1", ">>", "1"))
        self.assertTrue(apt_pkg.check_dep("2", ">>", "1"))

    def test_parse_depends_multiarch(self):
        # strip multiarch
        deps = apt_pkg.parse_depends("po4a:native", True)
        self.assertEqual(deps[0][0][0], "po4a")
        # do not strip multiarch
        deps = apt_pkg.parse_depends("po4a:native", False)
        self.assertEqual(deps[0][0][0], "po4a:native")

    def test_parse_depends(self):
        """dependencies: Test apt_pkg.parse_depends()"""
        deps = apt_pkg.parse_depends("p1a (<< 1a) | p1b (>> 1b)")
        self.assertTrue(isinstance(deps, list))
        self.assertEqual(len(deps), 1)
        self.assertTrue(isinstance(deps[0], list))
        self.assertEqual(len(deps[0]), 2)
        self.assertEqual(len(deps[0][0]), 3)
        self.assertEqual(len(deps[0][1]), 3)
        self.assertEqual(deps[0][0][0], "p1a")
        self.assertEqual(deps[0][0][1], "1a")
        self.assertEqual(deps[0][0][2], "<")
        self.assertEqual(deps[0][1][0], "p1b")
        self.assertEqual(deps[0][1][1], "1b")
        self.assertEqual(deps[0][1][2], ">")

        # Check that the type of comparison is parsed correctly.
        self.assertEqual("<", apt_pkg.parse_depends("p1 (<< 1)")[0][0][2])
        self.assertEqual("<=", apt_pkg.parse_depends("p1 (< 1)")[0][0][2])
        self.assertEqual("<=", apt_pkg.parse_depends("p1 (<= 1)")[0][0][2])
        self.assertEqual("=", apt_pkg.parse_depends("p1 (= 1)")[0][0][2])
        self.assertEqual(">=", apt_pkg.parse_depends("p1 (>= 1)")[0][0][2])
        self.assertEqual(">=", apt_pkg.parse_depends("p1 (> 1)")[0][0][2])
        self.assertEqual(">", apt_pkg.parse_depends("p1 (>> 1)")[0][0][2])

    def test_parse_src_depends(self):
        """dependencies: Test apt_pkg.parse_src_depends()."""
        # Check that architecture exclusion works
        # depends_this: Current architecture is included
        # depends_this_too: Another architecture is excluded
        # depends_other: The current architecture is excluded
        # depends_other: Another architecture is requested.
        architecture = apt_pkg.config["APT::Architecture"]
        depends_this = apt_pkg.parse_src_depends("p [%s]" % architecture)
        depends_this_too = apt_pkg.parse_src_depends("p [!not-existing-arch]")
        depends_other = apt_pkg.parse_src_depends("p [!%s]" % architecture)
        depends_other_too = apt_pkg.parse_src_depends("p [not-existing-arch]")

        self.assertEqual(len(depends_this), len(depends_this_too), 1)
        self.assertEqual(len(depends_other), len(depends_other_too), 0)

    def test_dstr(self):
        """Test apt.package.BaseDependency.__dstr"""
        dstr = apt.package.BaseDependency._BaseDependency__dstr
        equal = {"<": {"<<", "<"}, "=": {"==", "="}, ">": {">>", ">"}}
        operators = ["<<", "<", "<=", "!=", "=", "==", ">=", ">", ">>"]

        for a, b in itertools.product(equal.keys(), operators):
            if b in equal[a]:
                self.assertEqual(dstr(a), b)
                self.assertEqual(b, dstr(a))
            else:
                self.assertNotEqual(dstr(a), b)
                self.assertNotEqual(b, dstr(a))

    def testParseDepends(self):
        """dependencies: Test apt_pkg.ParseDepends()."""
        if not hasattr(apt_pkg, "ParseDepends"):
            return
        # Check that the type of comparison is parsed correctly.
        self.assertEqual("<<", apt_pkg.ParseDepends("p1 (<< 1)")[0][0][2])
        self.assertEqual("<=", apt_pkg.ParseDepends("p1 (< 1)")[0][0][2])
        self.assertEqual("<=", apt_pkg.ParseDepends("p1 (<= 1)")[0][0][2])
        self.assertEqual("=", apt_pkg.ParseDepends("p1 (= 1)")[0][0][2])
        self.assertEqual(">=", apt_pkg.ParseDepends("p1 (>= 1)")[0][0][2])
        self.assertEqual(">=", apt_pkg.ParseDepends("p1 (> 1)")[0][0][2])
        self.assertEqual(">>", apt_pkg.ParseDepends("p1 (>> 1)")[0][0][2])


if __name__ == "__main__":
    unittest.main()