aboutsummaryrefslogtreecommitdiff
path: root/ruby/environment_TEST.rb
blob: 7144bdf5a530c822970d2388cc273792bbffc0c0 (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
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=80 :

#
# Copyright (c) 2006, 2007 Ciaran McCreesh <ciaranm@ciaranm.org>
# Copyright (c) 2007 Richard Brown <rbrown@gentoo.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
#

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)
            end
        end
    end

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

        def test_query_use
###            pde = PackageDatabaseEntry.new("x/x", VersionSpec.new("1.0"), "testrepo")

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

            pid = env.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/bar-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).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 = EnvironmentMaker.instance.make_from_spec("")
        end

        def pid
            env.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/bar-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).first
        end

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

            pid2 = env.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/baz-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).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 = EnvironmentMaker.instance.make_from_spec("")
        end

        def pid
            env.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/bar-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).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.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/bar-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).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(EnvironmentMaker.instance.make_from_spec(""))
        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(EnvironmentMaker.instance.make_from_spec(""))
        end

        def test_adapt_use
            pid = env.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/bar-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).first

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

            env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'enabled', false);
            assert ! env.query_use('enabled', pid);
            env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'not_enabled', true);
            assert env.query_use("not_enabled", pid)
            env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'sometimes_enabled', false);
            assert ! env.query_use("sometimes_enabled", pid)
        end

        def test_adapt_use_bad
            assert_raise TypeError do
                env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'not_enabled', 'lemon');
            end
            assert_raise TypeError do
                env.adapt_use(PackageDepSpec.new(123, PackageDepSpecParseMode::Permissive), 'not_enabled', false);
            end
            assert_raise TypeError do
                env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 7, false);
            end
        end
    end

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

        def test_clear_adaptions
            pid = env.package_database.query(Query::Matches.new(PackageDepSpec.new('=foo/bar-1.0::testrepo', PackageDepSpecParseMode::Permissive)), QueryOrder::RequireExactlyOne).first

            assert env.query_use("enabled", pid)

            env.adapt_use(PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive), 'enabled', false);
            assert ! env.query_use('enabled', pid);

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

###    class TestCase_NoConfigEnvironmentMaskReasons < Test::Unit::TestCase
###        def env
###            NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")
###        end
###
###        def test_mask_reasons
###            p = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo")
###
###            m = env.mask_reasons(p)
###            assert m.empty?
###        end
###
###        def test_mask_reasons_not_empty
###            p = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("2.0"), "testrepo")
###
###            m = env.mask_reasons(p)
###            assert ! m.empty?
###            assert m.include?(MaskReason::Keyword)
###            assert_equal([MaskReason::Keyword], m.to_a)
###        end
###
###        def test_mask_reasons_no_such_repo
###            p = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "nosuchrepo")
###
###            assert_raise Paludis::NoSuchRepositoryError do
###                env.mask_reasons p
###            end
###        end
###
###        def test_mask_reasons_bad
###            assert_raise ArgumentError do
###                env.mask_reasons(1, 2)
###            end
###            assert_raise TypeError do
###                env.mask_reasons(123)
###            end
###        end
###    end

    class TestCase_EnvironmentPackageDatabase < Test::Unit::TestCase
        def env
            @env or @env = EnvironmentMaker.instance.make_from_spec("")
        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 = EnvironmentMaker.instance.make_from_spec("")
###        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",
                                   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_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
end