aboutsummaryrefslogtreecommitdiff
path: root/paludis/resolver/resolver_test.cc
blob: 8978ac9a868ccaf4df52a407ac82cad59b2e5cd7 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2009 Ciaran McCreesh
 *
 * 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
 */

#include <paludis/resolver/resolver_test.hh>
#include <paludis/resolver/constraint.hh>
#include <paludis/resolver/resolvent.hh>
#include <paludis/resolver/resolver.hh>
#include <paludis/resolver/resolution.hh>
#include <paludis/resolver/resolutions.hh>
#include <paludis/resolver/resolver_functions.hh>
#include <paludis/resolver/suggest_restart.hh>
#include <paludis/resolver/decision.hh>
#include <paludis/util/map.hh>
#include <paludis/util/wrapped_forward_iterator-impl.hh>
#include <paludis/util/make_shared_ptr.hh>
#include <paludis/util/sequence.hh>
#include <paludis/util/indirect_iterator-impl.hh>
#include <paludis/util/accept_visitor.hh>
#include <paludis/util/fs_entry.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/repositories/fake/fake_installed_repository.hh>
#include <paludis/repository_factory.hh>
#include <paludis/package_database.hh>
#include <paludis/user_dep_spec.hh>
#include <algorithm>
#include "config.h"

using namespace paludis;
using namespace paludis::resolver;
using namespace paludis::resolver::resolver_test;
using namespace test;

std::string
paludis::resolver::resolver_test::from_keys(const std::tr1::shared_ptr<const Map<std::string, std::string> > & m,
        const std::string & k)
{
    Map<std::string, std::string>::ConstIterator mm(m->find(k));
    if (m->end() == mm)
        return "";
    else
        return mm->second;
}

bool
paludis::resolver::resolver_test::care_about_dep_fn(const Resolvent &, const std::tr1::shared_ptr<const Resolution> &, const SanitisedDependency &)
{
    return true;
}

const std::tr1::shared_ptr<Constraints>
paludis::resolver::resolver_test::initial_constraints_for_fn(
        const InitialConstraints & initial_constraints,
        const Resolvent & resolvent)
{
    InitialConstraints::const_iterator i(initial_constraints.find(resolvent));
    if (i == initial_constraints.end())
        return make_shared_ptr(new Constraints);
    else
        return i->second;
}

std::tr1::shared_ptr<Resolvents>
paludis::resolver::resolver_test::get_resolvents_for_fn(const PackageDepSpec & spec,
        const std::tr1::shared_ptr<const Reason> &)
{
    std::tr1::shared_ptr<Resolvents> result(new Resolvents);
    result->push_back(Resolvent(spec, make_shared_ptr(new SlotName("0")), dt_slash));
    return result;
}

namespace
{
    struct IsSuggestionVisitor
    {
        bool is_suggestion;

        IsSuggestionVisitor() :
            is_suggestion(true)
        {
        }

        void visit(const DependencyRequiredLabel &)
        {
            is_suggestion = false;
        }

        void visit(const DependencyRecommendedLabel &)
        {
            is_suggestion = false;
        }

        void visit(const DependencySuggestedLabel &)
        {
        }
    };

#ifdef ENABLE_VIRTUALS_REPOSITORY
    std::string virtuals_repo_keys(const std::string & k)
    {
        if (k == "format")
            return "virtuals";
        else if (k == "root")
            return "/";
        else
            return "";
    }

    std::string installed_virtuals_repo_keys(const std::string & k)
    {
        if (k == "format")
            return "installed_virtuals";
        else if (k == "root")
            return "/";
        else
            return "";
    }
#endif
}

bool
paludis::resolver::resolver_test::is_suggestion(const SanitisedDependency & dep)
{
    if (dep.active_dependency_labels()->suggest_labels()->empty())
        return false;

    IsSuggestionVisitor v;
    std::for_each(indirect_iterator(dep.active_dependency_labels()->suggest_labels()->begin()),
            indirect_iterator(dep.active_dependency_labels()->suggest_labels()->end()),
            accept_visitor(v));
    return v.is_suggestion;
}

bool
paludis::resolver::resolver_test::take_dependency_fn(
        const Resolvent &,
        const SanitisedDependency & dep,
        const std::tr1::shared_ptr<const Reason> &)
{
    return ! is_suggestion(dep);
}

UseExisting
paludis::resolver::resolver_test::get_use_existing_fn(
        const Resolvent &,
        const PackageDepSpec &,
        const std::tr1::shared_ptr<const Reason> &)
{
    return ue_never;
}

ResolverTestCase::ResolverTestCase(const std::string & t, const std::string & s, const std::string & e,
        const std::string & l) :
    TestCase(s)
{
    std::tr1::shared_ptr<Map<std::string, std::string> > keys(new Map<std::string, std::string>);
    keys->insert("format", "exheres");
    keys->insert("names_cache", "/var/empty");
    keys->insert("location", stringify(FSEntry::cwd() / ("resolver_TEST_" + t + "_dir") / "repo"));
    keys->insert("profiles", stringify(FSEntry::cwd() / ("resolver_TEST_" + t + "_dir") / "repo/profiles/profile"));
    keys->insert("layout", l);
    keys->insert("eapi_when_unknown", e);
    keys->insert("eapi_when_unspecified", e);
    keys->insert("profile_eapi", e);
    keys->insert("distdir", stringify(FSEntry::cwd() / ("resolver_TEST_" + t + "_dir") / "distdir"));
    keys->insert("builddir", stringify(FSEntry::cwd() / ("resolver_TEST_" + t + "_dir") / "build"));
    repo = RepositoryFactory::get_instance()->create(&env,
            std::tr1::bind(from_keys, keys, std::tr1::placeholders::_1));
    env.package_database()->add_repository(1, repo);

    keys.reset(new Map<std::string, std::string>);
    keys->insert("format", "vdb");
    keys->insert("names_cache", "/var/empty");
    keys->insert("provides_cache", "/var/empty");
    keys->insert("location", stringify(FSEntry::cwd() / ("resolver_TEST_" + t + "_dir") / "installed"));
    keys->insert("builddir", stringify(FSEntry::cwd() / ("resolver_TEST_" + t + "_dir") / "build"));
    inst_repo = RepositoryFactory::get_instance()->create(&env,
            std::tr1::bind(from_keys, keys, std::tr1::placeholders::_1));
    env.package_database()->add_repository(1, inst_repo);

    fake_inst_repo.reset(new FakeInstalledRepository(&env, RepositoryName("fake-inst"), true, false));
    env.package_database()->add_repository(1, fake_inst_repo);

#ifdef ENABLE_VIRTUALS_REPOSITORY
    env.package_database()->add_repository(0, RepositoryFactory::get_instance()->create(&env, virtuals_repo_keys));
    env.package_database()->add_repository(0, RepositoryFactory::get_instance()->create(&env, installed_virtuals_repo_keys));
#endif
}

const std::tr1::shared_ptr<const ResolutionLists>
ResolverTestCase::get_resolutions(const PackageDepSpec & target)
{
    InitialConstraints initial_constraints;

    while (true)
    {
        try
        {
            Resolver resolver(&env, make_named_values<ResolverFunctions>(
                        value_for<n::care_about_dep_fn>(&care_about_dep_fn),
                        value_for<n::get_initial_constraints_for_fn>(
                            std::tr1::bind(&initial_constraints_for_fn, std::tr1::ref(initial_constraints),
                                std::tr1::placeholders::_1)),
                        value_for<n::get_resolvents_for_fn>(&get_resolvents_for_fn),
                        value_for<n::get_use_existing_fn>(&get_use_existing_fn),
                        value_for<n::take_dependency_fn>(&take_dependency_fn)
                        ));
            resolver.add_target(target);
            resolver.resolve();
            return resolver.resolution_lists();
        }
        catch (const SuggestRestart & e)
        {
            initial_constraints.insert(std::make_pair(e.resolvent(), make_shared_ptr(new Constraints))).first->second->add(e.suggested_preset());
        }
    }
}

const std::tr1::shared_ptr<const ResolutionLists>
ResolverTestCase::get_resolutions(const std::string & target)
{
    PackageDepSpec target_spec(parse_user_package_dep_spec(target, &env, UserPackageDepSpecOptions()));
    return get_resolutions(target_spec);
}

bool
ResolverTestCase::ResolutionListChecks::check_qpn(const QualifiedPackageName & q, const std::tr1::shared_ptr<const Resolution> & r)
{
    if ((! r) || (! r->decision()) || (! r->decision()->if_package_id()))
        return false;

    return r->decision()->if_package_id()->name() == q;
}

bool
ResolverTestCase::ResolutionListChecks::check_kind(const DecisionKind k,
        const QualifiedPackageName & q, const std::tr1::shared_ptr<const Resolution> & r)
{
    if ((! r) || (! r->decision()))
        return false;

    return r->decision()->kind() == k && r->resolvent().package() == q;
}

std::string
ResolverTestCase::ResolutionListChecks::check_kind_msg(const DecisionKind k,
        const QualifiedPackageName & q, const std::tr1::shared_ptr<const Resolution> & r)
{
    if (! r)
        return "Expected " + stringify(k) + " " + stringify(q) + " but got finished";
    else if (! r->decision())
        return "Expected " + stringify(k) + " " + stringify(q) + " but got undecided for " + stringify(r->resolvent());
    else
        return "Expected " + stringify(k) + " " + stringify(q) + " but got " + stringify(r->decision()->kind()) + " "
            + stringify(r->resolvent().package());
}

std::string
ResolverTestCase::ResolutionListChecks::check_generic_msg(const std::string & q, const std::tr1::shared_ptr<const Resolution> & r)
{
    if (! r)
        return "Expected " + stringify(q) + " but got finished";
    else if (! r->decision())
        return "Expected " + stringify(q) + " but got undecided for " + stringify(r->resolvent());
    else if (! r->decision()->if_package_id())
        return "Expected " + stringify(q) + " but got decided nothing (kind " + stringify(r->decision()->kind()) + ") for "
            + stringify(r->resolvent());
    else
        return "Expected " + stringify(q) + " but got " + stringify(r->decision()->if_package_id()->name()) + " for "
            + stringify(r->resolvent());
}

std::string
ResolverTestCase::ResolutionListChecks::check_qpn_msg(const QualifiedPackageName & q, const std::tr1::shared_ptr<const Resolution> & r)
{
    return check_generic_msg(stringify(q), r);
}

bool
ResolverTestCase::ResolutionListChecks::check_finished(const std::tr1::shared_ptr<const Resolution> & r)
{
    return ! r;
}

std::string
ResolverTestCase::ResolutionListChecks::check_finished_msg(const std::tr1::shared_ptr<const Resolution> & r)
{
    return check_generic_msg("finished", r);
}

ResolverTestCase::ResolutionListChecks &
ResolverTestCase::ResolutionListChecks::qpn(const QualifiedPackageName & q)
{
    checks.push_back(std::make_pair(
                std::tr1::bind(&check_qpn, q, std::tr1::placeholders::_1),
                std::tr1::bind(&check_qpn_msg, q, std::tr1::placeholders::_1)
                ));
    return *this;
}

ResolverTestCase::ResolutionListChecks &
ResolverTestCase::ResolutionListChecks::kind(const DecisionKind k, const QualifiedPackageName & q)
{
    checks.push_back(std::make_pair(
                std::tr1::bind(&check_kind, k, q, std::tr1::placeholders::_1),
                std::tr1::bind(&check_kind_msg, k, q, std::tr1::placeholders::_1)
                ));
    return *this;
}

ResolverTestCase::ResolutionListChecks &
ResolverTestCase::ResolutionListChecks::finished()
{
    checks.push_back(std::make_pair(
                &check_finished,
                &check_finished_msg
                ));
    return *this;
}

void
ResolverTestCase::check_resolution_list(
        const std::tr1::shared_ptr<const Resolutions> & list, const ResolutionListChecks & checks)
{
    Resolutions::ConstIterator r(list->begin()), r_end(list->end());
    for (ResolutionListChecks::List::const_iterator c(checks.checks.begin()), c_end(checks.checks.end()) ;
            c != c_end ; ++c)
    {
        if (r == r_end)
            TEST_CHECK_MESSAGE(c->first(make_null_shared_ptr()), c->second(make_null_shared_ptr()));
        else
            TEST_CHECK_MESSAGE(c->first(*r), c->second(*r));
        ++r;
    }
}

void
ResolverTestCase::install(const std::string & c, const std::string & p, const std::string & v)
{
    fake_inst_repo->add_version(c, p, v);
}