aboutsummaryrefslogtreecommitdiff
path: root/paludis/merger/merger_TEST.cc
blob: 8afdd1bed24200e75719888a3eccc65531d83218 (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
 *
 * 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 "merger.hh"
#include <paludis/environments/test/test_environment.hh>
#include <paludis/hooker.hh>
#include <test/test_framework.hh>
#include <test/test_runner.hh>
#include <fstream>
#include <iterator>
#include <list>

using namespace paludis;
using namespace test;

namespace paludis
{
    class HookTestEnvironment :
        public TestEnvironment
    {
        private:
            mutable tr1::shared_ptr<Hooker> hooker;
            mutable std::list<std::pair<FSEntry, bool> > hook_dirs;

        public:
            HookTestEnvironment(const FSEntry & hooks);

            virtual ~HookTestEnvironment();

            virtual HookResult perform_hook(const Hook &) const
                PALUDIS_ATTRIBUTE((warn_unused_result));

    };

    HookTestEnvironment::HookTestEnvironment(const FSEntry & hooks)
    {
        if (hooks.is_directory())
            hook_dirs.push_back(std::make_pair(hooks, false));
    }

    HookTestEnvironment::~HookTestEnvironment()
    {
    }

    HookResult
    HookTestEnvironment::perform_hook(const Hook & hook) const
    {
        if (! hooker)
        {
            hooker.reset(new Hooker(this));
            for (std::list<std::pair<FSEntry, bool> >::const_iterator h(hook_dirs.begin()),
                    h_end(hook_dirs.end()) ; h != h_end ; ++h)
                hooker->add_dir(h->first, h->second);
        }
        return hooker->perform_hook(hook);
    }
}


namespace
{
    struct TestMerger :
        Merger
    {
        TestMerger(const MergerOptions & opts) :
            Merger(opts)
        {
        }

        void record_install_file(const FSEntry &, const FSEntry &, const std::string &)
        {
        }

        void record_install_dir(const FSEntry &, const FSEntry &)
        {
        }

        void record_install_sym(const FSEntry &, const FSEntry &)
        {
        }

        void on_error(bool is_check, const std::string & s)
        {
            if (is_check)
                make_check_fail();
            else
                throw MergerError(s);
        }

        void on_warn(bool, const std::string &)
        {
        }

        void display_override(const std::string &) const
        {
        }

        bool config_protected(const FSEntry &, const FSEntry &)
        {
            return false;
        }

        std::string make_config_protect_name(const FSEntry & src, const FSEntry &)
        {
            return src.basename() + ".cfgpro";
        }
    };

    class MergerTest :
        public TestCase
    {
        public:
            FSEntry image_dir;
            FSEntry root_dir;
            HookTestEnvironment env;
            TestMerger merger;

            bool repeatable() const
            {
                return false;
            }

        protected:
            MergerTest(EntryType src_type, EntryType dst_type, int n = 0) :
                TestCase("merge " + stringify(src_type) + " over " + stringify(dst_type) + (0 == n ? "" : " "
                            + stringify(n))),
                image_dir("merger_TEST_dir/image_" + stringify(src_type) + "_over_" + stringify(dst_type)
                        + (0 == n ? "" : "_" + stringify(n))),
                root_dir("merger_TEST_dir/root_" + stringify(src_type) + "_over_" + stringify(dst_type)
                        + (0 == n ? "" : "_" + stringify(n))),
                env(FSEntry("merger_TEST_dir/hooks")),
                merger(MergerOptions::create()
                        .image(image_dir)
                        .root(root_dir)
                        .environment(&env)
                        .no_chown(true))
            {
            }

            MergerTest(const std::string & custom_test) :
                TestCase("merge " + custom_test + " test"),
                image_dir("merger_TEST_dir/image_" + custom_test),
                root_dir("merger_TEST_dir/root_" + custom_test),
                env(FSEntry("merger_TEST_dir/hooks")),
                merger(MergerOptions::create()
                        .image(image_dir)
                        .root(root_dir)
                        .environment(&env)
                        .no_chown(true))
            {
            }
 
    };
}

namespace test_cases
{
    struct MergerTestSymNothing : MergerTest
    {
        MergerTestSymNothing() : MergerTest(et_sym, et_nothing) { }

        void run()
        {
            TEST_CHECK(! (root_dir / "sym").exists());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "sym").is_symbolic_link());
            TEST_CHECK_EQUAL((root_dir / "sym").readlink(), "image_dst");
        }
    } test_merger_sym_nothing;

    struct MergerTestSymSym : MergerTest
    {
        MergerTestSymSym() : MergerTest(et_sym, et_sym) { }

        void run()
        {
            TEST_CHECK((root_dir / "sym").is_symbolic_link());
            TEST_CHECK_EQUAL((root_dir / "sym").readlink(), "root_dst");

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "sym").is_symbolic_link());
            TEST_CHECK_EQUAL((root_dir / "sym").readlink(), "image_dst");
        }
    } test_merger_sym_sym;

    struct MergerTestSymFile : MergerTest
    {
        MergerTestSymFile() : MergerTest(et_sym, et_file) { }

        void run()
        {
            TEST_CHECK((root_dir / "sym").is_regular_file());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "sym").is_symbolic_link());
            TEST_CHECK_EQUAL((root_dir / "sym").readlink(), "image_dst");
        }
    } test_merger_sym_file;

    struct MergerTestSymDir : MergerTest
    {
        MergerTestSymDir() : MergerTest(et_sym, et_dir) { }

        void run()
        {
            TEST_CHECK((root_dir / "sym").is_directory());

            TEST_CHECK(! merger.check());
            TEST_CHECK_THROWS(merger.merge(), MergerError);

            TEST_CHECK((root_dir / "sym").is_directory());
        }
    } test_merger_sym_dir;

    struct MergerTestDirNothing : MergerTest
    {
        MergerTestDirNothing() : MergerTest(et_dir, et_nothing) { }

        void run()
        {
            TEST_CHECK(! (root_dir / "dir").exists());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "dir").is_directory());
        }
    } test_merger_dir_nothing;

    struct MergerTestDirDir : MergerTest
    {
        MergerTestDirDir() : MergerTest(et_dir, et_dir) { }

        void run()
        {
            TEST_CHECK((root_dir / "dir").is_directory());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "dir").is_directory());
        }
    } test_merger_dir_dir;

    struct MergerTestDirFile : MergerTest
    {
        MergerTestDirFile() : MergerTest(et_dir, et_file) { }

        void run()
        {
            TEST_CHECK((root_dir / "dir").is_regular_file());

            TEST_CHECK(! merger.check());
            TEST_CHECK_THROWS(merger.merge(), MergerError);

            TEST_CHECK((root_dir / "dir").is_regular_file());
        }
    } test_merger_dir_file;

    struct MergerTestDirSym1 : MergerTest
    {
        MergerTestDirSym1() : MergerTest(et_dir, et_sym, 1) { }

        void run()
        {
            TEST_CHECK((root_dir / "dir").is_symbolic_link());
            TEST_CHECK((root_dir / "dir").realpath().is_directory());
            TEST_CHECK(! (root_dir / "dir" / "file").exists());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "dir").is_symbolic_link());
            TEST_CHECK((root_dir / "dir").realpath().is_directory());
            TEST_CHECK((root_dir / "dir" / "file").is_regular_file());
        }
    } test_merger_dir_sym_1;

    struct MergerTestDirSym2 : MergerTest
    {
        MergerTestDirSym2() : MergerTest(et_dir, et_sym, 2) { }

        void run()
        {
            TEST_CHECK((root_dir / "dir").is_symbolic_link());
            TEST_CHECK((root_dir / "dir").realpath().is_regular_file());

            TEST_CHECK(! merger.check());
            TEST_CHECK_THROWS(merger.merge(), MergerError);

            TEST_CHECK((root_dir / "dir").is_symbolic_link());
            TEST_CHECK((root_dir / "dir").realpath().is_regular_file());
        }
    } test_merger_dir_sym_2;

    struct MergerTestDirSym3 : MergerTest
    {
        MergerTestDirSym3() : MergerTest(et_dir, et_sym, 3) { }

        void run()
        {
            TEST_CHECK((root_dir / "dir").is_symbolic_link());
            TEST_CHECK_THROWS((root_dir / "dir").realpath(), FSError);

            TEST_CHECK(! merger.check());
            TEST_CHECK_THROWS(merger.merge(), MergerError);

            TEST_CHECK((root_dir / "dir").is_symbolic_link());
            TEST_CHECK_THROWS((root_dir / "dir").realpath(), FSError);
        }
    } test_merger_dir_sym_3;

    struct MergerTestFileNothing : MergerTest
    {
        MergerTestFileNothing() : MergerTest(et_file, et_nothing) { }

        void run()
        {
            TEST_CHECK(! (root_dir / "file").exists());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "file").is_regular_file());
            std::ifstream f(stringify(root_dir / "file").c_str());
            TEST_CHECK(f);
            std::string fs(std::string((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()));
            TEST_CHECK_EQUAL(fs, "image contents\n");
        }
    } test_merger_file_nothing;

    struct MergerTestFileFile : MergerTest
    {
        MergerTestFileFile() : MergerTest(et_file, et_file) { }

        void run()
        {
            TEST_CHECK((root_dir / "file").is_regular_file());
            std::ifstream b(stringify(root_dir / "file").c_str());
            TEST_CHECK(b);
            std::string bs((std::istreambuf_iterator<char>(b)), std::istreambuf_iterator<char>());
            TEST_CHECK_EQUAL(bs, "root contents\n");

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "file").is_regular_file());
            std::ifstream f(stringify(root_dir / "file").c_str());
            TEST_CHECK(f);
            std::string fs((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
            TEST_CHECK_EQUAL(fs, "image contents\n");
        }
    } test_merger_file_file;

    struct MergerTestFileSym : MergerTest
    {
        MergerTestFileSym() : MergerTest(et_file, et_sym) { }

        void run()
        {
            TEST_CHECK((root_dir / "file1").is_symbolic_link());
            TEST_CHECK((root_dir / "file2").is_symbolic_link());
            TEST_CHECK((root_dir / "file3").is_symbolic_link());

            TEST_CHECK(merger.check());
            merger.merge();

            TEST_CHECK((root_dir / "file1").is_regular_file());
            std::ifstream f(stringify(root_dir / "file1").c_str());
            TEST_CHECK(f);
            std::string fs((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
            TEST_CHECK_EQUAL(fs, "image 1 contents\n");

            TEST_CHECK((root_dir / "file2").is_regular_file());
            std::ifstream f2(stringify(root_dir / "file2").c_str());
            TEST_CHECK(f2);
            std::string fs2((std::istreambuf_iterator<char>(f2)), std::istreambuf_iterator<char>());
            TEST_CHECK_EQUAL(fs2, "image 2 contents\n");

            TEST_CHECK((root_dir / "file3").is_regular_file());
            std::ifstream f3(stringify(root_dir / "file3").c_str());
            TEST_CHECK(f3);
            std::string fs3((std::istreambuf_iterator<char>(f3)), std::istreambuf_iterator<char>());
            TEST_CHECK_EQUAL(fs3, "image 3 contents\n");
        }
    } test_merger_file_sym;

    struct MergerTestFileDir : MergerTest
    {
        MergerTestFileDir() : MergerTest(et_file, et_dir) { }

        void run()
        {
            TEST_CHECK((root_dir / "file").is_directory());

            TEST_CHECK(! merger.check());
            TEST_CHECK_THROWS(merger.merge(), MergerError);

            TEST_CHECK((root_dir / "file").is_directory());
        }
    } test_merger_file_dir;

    struct MergerOverrideTest : MergerTest
    {
        MergerOverrideTest() : MergerTest("override") { }

        void run()
        {
            TEST_CHECK((image_dir / "dir_skip_me").is_directory());
            TEST_CHECK((image_dir / "dir_install_me").is_directory());
            TEST_CHECK((image_dir / "file_skip_me").is_regular_file());
            TEST_CHECK((image_dir / "file_install_me").is_regular_file());
            TEST_CHECK((image_dir / "sym_skip_me").is_symbolic_link());
            TEST_CHECK((image_dir / "sym_install_me").is_symbolic_link());

            TEST_CHECK(merger.check());
            merger.merge();


            TEST_CHECK(! (root_dir / "dir_skip_me").exists());
            TEST_CHECK((root_dir / "dir_install_me").is_directory());
            TEST_CHECK(! (root_dir / "file_skip_me").exists());
            TEST_CHECK((root_dir / "file_install_me").is_regular_file());
            TEST_CHECK(! (root_dir / "sym_skip_me").exists());
            TEST_CHECK((root_dir / "sym_install_me").is_symbolic_link());
        }
    } test_merger_override;
}