aboutsummaryrefslogtreecommitdiff
path: root/ruby/environment_TEST.rb
blob: becac4f324099649cf7ef5c7b30f1287435fd75d (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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=80 :

#
# Copyright (c) 2006, 2007, 2008 Ciaran McCreesh
# Copyright (c) 2007, 2008 Richard Brown
#
# 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
#

ENV["PALUDIS_HOME"] = Dir.getwd().to_s + "/environment_TEST_dir/home";

require 'test/unit'
require 'Paludis'

Paludis::Log.instance.log_level = Paludis::LogLevel::Warning

module Paludis
    class TestCase_Environment < Test::Unit::TestCase
        def test_no_create
            assert_raise NoMethodError do
                x = Environment.new()
            end
        end
    end

    class TestCase_NoConfigEnvironment < Test::Unit::TestCase
        def test_create
            assert_nothing_raised do
                e = NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
                assert_kind_of Environment, e
                assert_kind_of NoConfigEnvironment, e
            end

            assert_nothing_raised do
                e = NoConfigEnvironment.new(Dir.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo"))
                assert_kind_of Environment, e
                assert_kind_of NoConfigEnvironment, e
            end

            assert_nothing_raised do
                e = NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo", '/var/empty')
                assert_kind_of Environment, e
                assert_kind_of NoConfigEnvironment, e
            end

            assert_raise TypeError do
                e = NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo", 7)
            end

            assert_raise TypeError do
                e = NoConfigEnvironment.new(7, '/var/empty')
            end

            assert_raise ArgumentError do
                e = NoConfigEnvironment.new
            end

            assert_raise ArgumentError do
                e = NoConfigEnvironment.new(1,2,3,4,5)
            end
        end
    end

    class TestCase_EnvironmentUse < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def test_query_use
            pid = env[Selection::RequireExactlyOne.new(Generator::Matches.new(
                Paludis::parse_user_package_dep_spec('=foo/bar-1.0::testrepo', env, [])))].first

            assert env.query_use("enabled", pid)
            assert ! env.query_use("not_enabled", pid)
            assert env.query_use("sometimes_enabled", pid)
        end

        def test_query_use_bad
            assert_raise ArgumentError do
                env.query_use(1, 2, 3)
            end
            assert_raise ArgumentError do
                env.query_use(123)
            end
        end
    end

    class TestCase_EnvironmentAcceptLicense < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def pid
            env[Selection::RequireExactlyOne.new(Generator::Matches.new(
                Paludis::parse_user_package_dep_spec('=foo/bar-1.0::testrepo', env, [])))].first
        end

        def test_accept_license
            assert env.accept_license('GPL-2', pid)
            assert !env.accept_license('Failure', pid)

            pid2 = env[Selection::RequireExactlyOne.new(Generator::Matches.new(
                Paludis::parse_user_package_dep_spec('=foo/baz-1.0::testrepo', env, [])))].first
            assert env.accept_license('GPL-2', pid2)
            assert env.accept_license('Failure', pid2)
        end

        def test_accept_license_bad
            assert_raise TypeError do
                env.accept_keywords('license','a string')
            end
        end
    end

    class TestCase_EnvironmentAcceptKeywords < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def pid
            env[Selection::RequireExactlyOne.new(Generator::Matches.new(
                Paludis::parse_user_package_dep_spec('=foo/bar-1.0::testrepo', env, [])))].first
        end

        def test_accept_keywords
            assert env.accept_keywords(['test'], pid)
            assert !env.accept_keywords(['test2'], pid)
            assert env.accept_keywords(['test','testtest'], pid)
            assert env.accept_keywords(['test2','testtest'], pid)
            assert !env.accept_keywords(['test2','test3'], pid)
        end

        def test_accept_keywords_bad
            assert_raise TypeError do
                env.accept_keywords('test',pid)
            end

            assert_raise TypeError do
                env.accept_keywords([],'a string')
            end
        end
    end

    class TestCase_NoConfigEnvironmentUse < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_query_use
            pid = env[Selection::RequireExactlyOne.new(Generator::Matches.new(
                Paludis::parse_user_package_dep_spec('=foo/bar-1.0::testrepo', env, [])))].first
            assert ! env.query_use("foo", pid)
        end

        def test_query_use_bad
            assert_raise ArgumentError do
                env.query_use(1, 2, 3)
            end
            assert_raise ArgumentError do
                env.query_use(123)
            end
        end
    end

    class TestCase_AdaptedEnvironment < Test::Unit::TestCase
        def env
            @env or @env = AdaptedEnvironment.new(EnvironmentFactory.instance.create(""))
        end

        def test_create
            assert_nothing_raised do
                env
            end
        end

        def test_create_bad
            assert_raise ArgumentError do
                AdaptedEnvironment.new
            end
        end
    end

    class TestCase_AdaptedEnvironmentAdaptUse < Test::Unit::TestCase
        def env
            @env or @env = AdaptedEnvironment.new(EnvironmentFactory.instance.create(""))
        end

        def test_adapt_use
            pid = env[Selection::RequireExactlyOne.new(
                Generator::Matches.new(Paludis::parse_user_package_dep_spec('=foo/bar-1.0::testrepo', env, [])))].first

            assert env.query_use("enabled", pid)
            assert ! env.query_use("not_enabled", pid)
            assert env.query_use("sometimes_enabled", pid)

            env.adapt_use(Paludis::parse_user_package_dep_spec('foo/bar', env, []), 'enabled', false);
            assert ! env.query_use('enabled', pid);
            env.adapt_use(Paludis::parse_user_package_dep_spec('foo/bar', env, []), 'not_enabled', true);
            assert env.query_use("not_enabled", pid)
            env.adapt_use(Paludis::parse_user_package_dep_spec('foo/bar', env, []), 'sometimes_enabled', false);
            assert ! env.query_use("sometimes_enabled", pid)
        end

        def test_adapt_use_bad
            assert_raise TypeError do
                env.adapt_use(Paludis::parse_user_package_dep_spec('foo/bar', env, []), 'not_enabled', 'lemon');
            end
            assert_raise TypeError do
                env.adapt_use(Paludis::parse_user_package_dep_spec('foo/bar', env, []), 7, false);
            end
        end
    end

    class TestCase_AdaptedEnvironmentClearAdaptions < Test::Unit::TestCase
        def env
            @env or @env = AdaptedEnvironment.new(EnvironmentFactory.instance.create(""))
        end

        def test_clear_adaptions
            pid = env[Selection::RequireExactlyOne.new(Generator::Matches.new(
                Paludis::parse_user_package_dep_spec('=foo/bar-1.0::testrepo', env, [])))].first

            assert env.query_use("enabled", pid)

            env.adapt_use(Paludis::parse_user_package_dep_spec('foo/bar', env, []), 'enabled', false);
            assert ! env.query_use('enabled', pid);

            env.clear_adaptions;
            assert env.query_use("enabled", pid)
        end
    end

    class TestCase_EnvironmentPackageDatabase < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def db
            env.package_database
        end

        def test_package_database
            assert_kind_of PackageDatabase, db
            assert_equal "testrepo", db.fetch_repository("testrepo").name
        end
    end

    class TestCase_NoConfigEnvironmentPackageDatabase < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def db
            env.package_database
        end

        def test_package_database
            assert_kind_of PackageDatabase, db
            assert_equal "testrepo", db.fetch_repository("testrepo").name
        end
    end

    class TestCase_EnvironmentPackageSet < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def test_package_set
            assert_kind_of DepSpec, env.set('everything')
        end

        def test_package_set_error
            assert_raise SetNameError do
                env.set('broken#')
            end
        end
    end

    class TestCase_NoConfigEnvironmentPackageSet < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_package_set
            assert_kind_of DepSpec, env.set('everything')
        end

        def test_package_set_error
            assert_raise SetNameError do
                env.set('broken#')
            end
        end
    end

    class TestCase_NoConfigEnvirontmentPortageRepository < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_portage_repository
            assert_kind_of Repository, env.main_repository
        end
    end

    class TestCase_NoConfigEnvirontmentMasterRepository < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def env_master
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo",
                                   "/var/empty",
                                   "slaverepo",
                                   [Dir.getwd().to_s + "/environment_TEST_dir/slaverepo"])
        end

        def test_master_repository
            assert_nil env.master_repository
            assert_kind_of Repository, env_master.master_repository
        end
    end

    class TestCase_EnvironmentRoot < Test::Unit::TestCase
        def test_root
            assert_kind_of String, env.root
        end
    end

    class TestCase_EnvironmentDefaultDestinations < Test::Unit::TestCase
        def test_default_destinations
            assert_kind_of Array, env.default_destinations
        end
    end

    class TestCase_EnvironmentRoot < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_root
            assert_kind_of String, env.root
        end
    end

    class TestCase_EnvironmentDistribution < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_distribution
            assert_kind_of String, env.distribution
            assert_equal "gentoo", env.distribution
        end
    end

    class TestCase_EnvironmentDefaultDestinations < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_default_destinations
            assert_kind_of Array, env.default_destinations
        end
    end

    class TestCase_EnvironmentSetAcceptUnstable < Test::Unit::TestCase
        def env
            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_set_accept_unstable
            assert_respond_to env, :accept_unstable=
            assert_nothing_raised do
                env.accept_unstable=true
                env.accept_unstable=false
            end
        end
    end

    class TestCase_EnvironmentMirrors < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def test_respond_and_return
            assert_respond_to env, :mirrors
            assert_kind_of Array, env.mirrors('')
        end


        def test_mirrors_star
            star_mirrors = env.mirrors('*')
            assert_equal 2, star_mirrors.length
            assert star_mirrors.include?('http://a')
            assert star_mirrors.include?('http://b')
        end

        def test_named_mirror
            assert_equal ['http://c'], env.mirrors('testmirror')
        end

        def test_empty_mirror
            assert env.mirrors('missingmirror').empty?
        end
    end

    class TestCase_EnvironmentQuery < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def db
            return env.package_database
        end

        def pda
            Paludis::parse_user_package_dep_spec('=foo/bar-1.0', env, [])
        end

        def pda2
            Paludis::parse_user_package_dep_spec('foo/bar', env, [])
        end

        def test_arg_count
            assert_raise ArgumentError do
                env[1, 2];
            end
        end

        def test_package_database_query
            a = env[Selection::AllVersionsSorted.new(Generator::Matches.new(pda))]
            assert_kind_of Array, a
            assert_equal 1, a.length
            pid = a.first
            assert_kind_of PackageID, pid
            assert_equal 'foo/bar', pid.name
            assert_equal '1.0', pid.version.to_s
            assert_equal 'testrepo', pid.repository_name

            a = env[Selection::AllVersionsSorted.new(Generator::Matches.new(pda) | Filter::SupportsAction.new(InstallAction))]
            assert_kind_of Array, a
            assert_equal 1, a.length
            pid = a.first
            assert_kind_of PackageID, pid
            assert_equal 'foo/bar', pid.name
            assert_equal '1.0', pid.version.to_s
            assert_equal 'testrepo', pid.repository_name

            a = env[Selection::AllVersionsSorted.new(Generator::Matches.new(pda))]
            assert_kind_of Array, a
            assert_equal 1, a.length
            pid = a.first
            assert_kind_of PackageID, pid
            assert_equal 'foo/bar', pid.name
            assert_equal '1.0', pid.version.to_s
            assert_equal 'testrepo', pid.repository_name

            a = env[Selection::AllVersionsSorted.new(Generator::Matches.new(pda2) | Filter::SupportsAction.new(InstallAction))]
            assert_kind_of Array, a
            assert_equal 2, a.length
            pid = a.shift
            assert_kind_of PackageID, pid
            assert_equal 'foo/bar', pid.name
            assert_equal '1.0', pid.version.to_s
            assert_equal 'testrepo', pid.repository_name
            pid2 = a.shift
            assert_kind_of PackageID, pid2
            assert_equal pid.name, pid2.name
            assert_equal '2.0', pid2.version.to_s
            assert_equal pid.repository_name, pid2.repository_name

            a = env[Selection::AllVersionsSorted.new(Generator::Package.new('foo/bar'))]
            assert_kind_of Array, a
            assert_equal 2, a.length
            pid = a.shift
            assert_kind_of PackageID, pid
            assert_equal 'foo/bar', pid.name
            assert_equal '1.0', pid.version.to_s
            assert_equal 'testrepo', pid.repository_name
            pid2 = a.shift
            assert_kind_of PackageID, pid2
            assert_equal pid.name, pid2.name
            assert_equal '2.0', pid2.version.to_s
            assert_equal pid.repository_name, pid2.repository_name


            a = env[Selection::AllVersionsUnsorted.new(Generator::Matches.new(Paludis::parse_user_package_dep_spec(
                '>=foo/bar-27', env, [])))]
            assert a.empty?

            a = env[Selection::AllVersionsUnsorted.new(Generator::Matches.new(pda2) | Filter::SupportsAction.new(InstalledAction))]
            assert a.empty?
        end
    end

    class TestCase_EnvironmentMetadataKeys < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentFactory.instance.create("")
        end

        def ncenv
            @ncenv or @ncenv = NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
        end

        def test_format_key
            assert_respond_to env, :format_key
            assert_not_nil env.format_key
            assert_kind_of MetadataStringKey, env.format_key
            assert_equal 'paludis', env.format_key.value

            assert_respond_to ncenv, :format_key
            assert_not_nil ncenv.format_key
            assert_kind_of MetadataStringKey, ncenv.format_key
            assert_equal 'no_config', ncenv.format_key.value
        end

        def test_config_location_key
            assert_respond_to env, :config_location_key
            assert_not_nil env.config_location_key
            assert_kind_of MetadataFSEntryKey, env.config_location_key
            assert_equal Dir.getwd().to_s + "/environment_TEST_dir/home/.paludis", env.config_location_key.value

            assert_respond_to ncenv, :config_location_key
            assert_nil ncenv.config_location_key
        end
    end
end