aboutsummaryrefslogtreecommitdiff
path: root/python/environment_TEST.py
blob: 98f7542fe6ac43ea5912be0921a329b83392d882 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
# vim: set fileencoding=utf-8 sw=4 sts=4 et :

#
# Copyright (c) 2007 Piotr JaroszyƄski
#
# This file is part of the Paludis package manager. Paludis is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
# Public License version 2, as published by the Free Software Foundation.
#
# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA
#

import os

ph = os.path.join(os.getcwd(), "environment_TEST_dir/home")
repo = os.path.join(os.getcwd(), "environment_TEST_dir/testrepo")
slaverepo = os.path.join(os.getcwd(), "environment_TEST_dir/slaverepo")
os.environ["PALUDIS_HOME"] = ph

from paludis import *
from additional_tests import *

import unittest

Log.instance.log_level = LogLevel.WARNING

class TestCase_01_Environments(unittest.TestCase):
    def setUp(self):
        self.e = EnvironmentMaker.instance.make_from_spec("")
        self.nce = NoConfigEnvironment(repo)

    def test_01_create(self):
        NoConfigEnvironment(repo)
        NoConfigEnvironment(repo, "/var/empty")
        NoConfigEnvironment(repo, "/var/empty", "/var/empty")
        NoConfigEnvironment(repo, master_repository_dir="/var/empty")
        NoConfigEnvironment(repo, write_cache_dir="/var/empty")

    def test_02_create_error(self):
        self.assertRaises(Exception, Environment)
        self.assertRaises(Exception, NoConfigEnvironment)

    def test_03_subclass(self):
        self.assert_(isinstance(NoConfigEnvironment(repo), Environment))

    def test_04_query_use(self):
        pid = iter(self.e.package_database.query(Query.Matches(
            parse_user_package_dep_spec("=foo/bar-1.0", [])),
            QueryOrder.REQUIRE_EXACTLY_ONE)).next()

        self.assert_(self.e.query_use("enabled", pid))
        self.assert_(not self.e.query_use("not_enabled", pid))
        self.assert_(self.e.query_use("sometimes_enabled", pid))

        self.assert_(not self.nce.query_use("foo", pid))

    def test_06_package_database(self):
        self.assert_(isinstance(self.e.package_database, PackageDatabase))
        self.assert_(isinstance(self.nce.package_database, PackageDatabase))

    def test_07_sets(self):
        self.assert_(isinstance(self.e.set("everything"), AllDepSpec))
        self.assert_(isinstance(self.nce.set("everything"), AllDepSpec))

        self.assert_(isinstance(self.e.set_names, SetNameIterable))
        self.assert_(isinstance(self.nce.set_names, SetNameIterable))

    def test_08_repositories(self):
        nce2 = NoConfigEnvironment(repo, master_repository_dir=slaverepo)

        self.assert_(isinstance(self.nce.main_repository, Repository))
        self.assertEquals(self.nce.master_repository, None)
        self.assert_(isinstance(nce2.main_repository, Repository))
        self.assert_(isinstance(nce2.master_repository, Repository))

    def test_09_root(self):
        self.assert_(isinstance(self.e.root, str))
        self.assert_(isinstance(self.nce.root, str))

    def test_10_default_destinations(self):
        self.assert_(isinstance(self.e.default_destinations, DestinationsIterable))
        self.assert_(isinstance(self.nce.default_destinations, DestinationsIterable))

    def test_11_set_accept_unstable(self):
        self.nce.accept_unstable = True
        self.assertRaises(AttributeError, lambda: self.nce.accept_unstable)

    def test_12_config_dir(self):
        self.assert_(isinstance(self.e.config_dir, str))

class TestCase_02_AdaptedEnvironment(unittest.TestCase):
    def test_01_create(self):
        env = AdaptedEnvironment(EnvironmentMaker.instance.make_from_spec(""))

    def test_02_adapt_use(self):
        env = AdaptedEnvironment(EnvironmentMaker.instance.make_from_spec(""))
        pid = iter(env.package_database.query(Query.Matches(
            parse_user_package_dep_spec("=foo/bar-1.0", [])),
            QueryOrder.REQUIRE_EXACTLY_ONE)).next()
        pds = parse_user_package_dep_spec("foo/bar", [])

        self.assert_(env.query_use("enabled", pid))
        self.assert_(not env.query_use("not_enabled", pid))
        self.assert_(env.query_use("sometimes_enabled", pid))

        env.adapt_use(pds, "enabled", UseFlagState.DISABLED)
        self.assert_(not env.query_use("enabled", pid))

        env.adapt_use(pds, "not_enabled", UseFlagState.ENABLED)
        self.assert_(env.query_use("not_enabled", pid))

        env.adapt_use(pds, "sometimes_enabled", UseFlagState.ENABLED)
        self.assert_(env.query_use("sometimes_enabled", pid))

    def test_03_clear_adaptions(self):
        env = AdaptedEnvironment(EnvironmentMaker.instance.make_from_spec(""))
        pid = iter(env.package_database.query(Query.Matches(parse_user_package_dep_spec("=foo/bar-1.0", [])),
            QueryOrder.REQUIRE_EXACTLY_ONE)).next()
        pds = parse_user_package_dep_spec("foo/bar", [])

        self.assert_(env.query_use("enabled", pid))

        env.adapt_use(pds, "enabled", UseFlagState.DISABLED)
        self.assert_(not env.query_use("enabled", pid))

        env.clear_adaptions()
        self.assert_(env.query_use("enabled", pid))

class TestCase_03_TestEnvironment(unittest.TestCase):
    def test_01_create(self):
        env = TestEnvironment()

class TestCase_04_Environment_subclassingd(unittest.TestCase):
    class SubEnv(EnvironmentImplementation):
        def __init__(self):
            EnvironmentImplementation.__init__(self)

        def query_use(self, use, pid):
            return EnvironmentImplementation.query_use(self, use, pid)

        def known_use_expand_names(self, use, pid):
            return [UseFlagName("a"), "u"]

        def accept_license(self, l, pid):
            return False

        def accept_keywords(self, kns, pid):
            return False

        def mask_for_breakage(self, pid):
            return UserMask()

        def mask_for_user(self, pid):
            return UserMask()

        def unmasked_by_user(self, pid):
            return False

        def bashrc_files(self):
            return ["/path"]

        def syncers_dirs(self):
            return ["/path"]

        def fetchers_dirs(self):
            return ["/path"]

        def hook_dirs(self):
            return ["/path"]

        def paludis_command(self):
            return "paludis"

        def set_paludis_command(self, s):
            pass

        def root(self):
            return "/"

        def reduced_uid(self):
            return 0

        def reduced_gid(self):
            return 0

        def mirrors(self, mirror):
            return ["mirror"]

        def set_names(self):
            return ["set"]

        def set(self, set):
            return AllDepSpec()

        def default_destinations(self):
            e = EnvironmentMaker.instance.make_from_spec("")
            return [x for x in e.package_database.repositories]

        def default_distribution(self):
            return EnvironmentImplementation.default_distribution(self)

    def test_01_environment_implementation(self):
        test_env(self.SubEnv())

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