aboutsummaryrefslogtreecommitdiff
path: root/ruby/dep_list_TEST.rb
blob: bd769887e4fc72c25b4c17f727236f2cf3cce761 (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
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=80 :
#
# 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() + '/dep_list_TEST_dir/home'

require 'test/unit'
require 'Paludis'

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

module Paludis
    module Shared
        def dlo
            DepListOptions.new(
                DepListReinstallOption::Never,
                DepListReinstallScmOption::Never,
                DepListTargetType::Package,
                DepListUpgradeOption::Always,
                DepListDowngradeOption::AsNeeded,
                DepListNewSlotsOption::Always,
                DepListFallBackOption::AsNeededExceptTargets,
                DepListDepsOption::Discard,
                DepListDepsOption::TryPost,
                DepListDepsOption::TryPost,
                DepListDepsOption::Pre,
                DepListDepsOption::PreOrPost,
                DepListDepsOption::Post,
                DepListDepsOption::TryPost,
                DepListSuggestedOption::Show,
                DepListCircularOption::Error,
                DepListUseOption::Standard,
                DepListBlocksOption::Accumulate,
                DepListOverrideMasks.new,
                false
            )
        end

        def default_options
            {
                :reinstall => DepListReinstallOption::Never,
                :reinstall_scm => DepListReinstallScmOption::Never,
                :target_type => DepListTargetType::Package,
                :upgrade => DepListUpgradeOption::Always,
                :downgrade => DepListDowngradeOption::AsNeeded,
                :new_slots => DepListNewSlotsOption::Always,
                :fall_back => DepListFallBackOption::AsNeededExceptTargets,
                :installed_deps_pre => DepListDepsOption::Discard,
                :installed_deps_runtime => DepListDepsOption::TryPost,
                :installed_deps_post => DepListDepsOption::TryPost,
                :uninstalled_deps_pre => DepListDepsOption::Pre,
                :uninstalled_deps_runtime => DepListDepsOption::PreOrPost,
                :uninstalled_deps_post => DepListDepsOption::Post,
                :uninstalled_deps_suggested => DepListDepsOption::TryPost,
                :suggested => DepListSuggestedOption::Show,
                :circular => DepListCircularOption::Error,
                :use => DepListUseOption::Standard,
                :blocks => DepListBlocksOption::Accumulate,
                :override_masks => DepListOverrideMasks.new,
                :dependency_tags => false
            }
        end

        def dlo_hash
            DepListOptions.new(options_hash)
        end

        def options_hash
            {
                :reinstall => DepListReinstallOption::Always,
                :reinstall_scm => DepListReinstallScmOption::Always,
                :target_type => DepListTargetType::Set,
                :upgrade => DepListUpgradeOption::AsNeeded,
                :downgrade => DepListDowngradeOption::Warning,
                :new_slots => DepListNewSlotsOption::AsNeeded,
                :fall_back => DepListFallBackOption::AsNeeded,
                :installed_deps_pre => DepListDepsOption::Pre,
                :installed_deps_runtime => DepListDepsOption::Discard,
                :installed_deps_post => DepListDepsOption::Discard,
                :uninstalled_deps_pre => DepListDepsOption::Discard,
                :uninstalled_deps_runtime => DepListDepsOption::Discard,
                :uninstalled_deps_post => DepListDepsOption::Discard,
                :uninstalled_deps_suggested => DepListDepsOption::Discard,
                :suggested => DepListSuggestedOption::Discard,
                :circular => DepListCircularOption::Discard,
                :use => DepListUseOption::TakeAll,
                :blocks => DepListBlocksOption::Error,
                :override_masks => DepListOverrideMasks.new,
                :dependency_tags => true
            }
        end

        def dlo_default
            DepListOptions.new()
        end

        def env
            @env or @env = EnvironmentMaker.instance.make_from_spec("")
        end

        def dl
            DepList.new(env,dlo)
        end

        def pda
            PackageDepSpec.new('foo/bar', PackageDepSpecParseMode::Permissive)
        end

        def dd
            env.default_destinations
        end
    end

    class TestCase_DepListOptions < Test::Unit::TestCase
        include Shared

        def test_create
            assert_nothing_raised do
                dlo
            end

            assert_nothing_raised do
                dlo_hash
            end

            assert_nothing_raised do
                dlo_default
            end
        end

        def test_members
            options = dlo_default
            #This will fail if the defaults change, please also update the rdoc.
            default_options.each_pair do |method, value|
                assert_respond_to options, method
                next if method == :override_masks
                assert_equal value, options.send(method)
                #check setters work
                assert_nothing_raised do
                    options.send("#{method}=", options_hash[method])
                    assert_equal options_hash[method], options.send(method)
                end
            end
            assert_kind_of DepListOverrideMasks, options.override_masks
            assert_nothing_raised do
                options.override_masks = DepListOverrideMasks.new
            end
        end

        def test_bad_create
            options_hash.each_key do |key|
                assert_raises ArgumentError do
                    DepListOptions.new(options_hash.delete(key))
                end
            end
        end
    end

    class TestCase_DepList < Test::Unit::TestCase
        include Shared

        def test_create
            assert_nothing_raised do
                dl
            end
        end

        def test_respond
            dep_list = dl
            [:add, :clear, :already_installed?, :each, :options].each {|sym| assert_respond_to dep_list, sym}
        end

        def test_add
            assert_nothing_raised do
                dl.add(pda, dd)
            end
        end

        def test_clear
            assert_nothing_raised do
                dl.clear
            end
        end

        def test_already_installed?
            dep_spec = pda
            dep_list = dl
            assert !dep_list.already_installed?(dep_spec, dd)
            dep_list.add(dep_spec, dd)
            assert dep_list.already_installed?(dep_spec, dd)
        end

        def test_each
            assert_kind_of DepListEntry, dl.add(pda, dd).entries.first
        end

        def test_errors
            assert_raise AllMaskedError do
                dl.add(PackageDepSpec.new('foo/ba', PackageDepSpecParseMode::Permissive), dd)
            end

            begin
                dl.add(PackageDepSpec.new('foo/ba', PackageDepSpecParseMode::Permissive), dd)
            rescue AllMaskedError => error
                assert_equal 'foo/ba', error.query
            end

        end

        def test_options
            dep_list = dl
            assert_kind_of DepListOptions, dep_list.options
            assert_equal DepListReinstallOption::Never, dep_list.options.reinstall

            assert_nothing_raised do
                dep_list.options.reinstall = DepListReinstallOption::Always
            end

            assert_equal DepListReinstallOption::Always, dep_list.options.reinstall

        end
    end

    class TestCase_DepListEntry < Test::Unit::TestCase
        include Shared

        def dle
            dl.add(pda, dd).entries.first
        end

        def test_create
            assert_raise NoMethodError do
                DepListEntry.new
            end
        end

        def test_methods
            dep_list_entry = dle
            {
                :package => PackageDatabaseEntry, :metadata => VersionMetadata,
                :state=> Integer, :tags => [String], :destinations => [Repository]
            }.each_pair do |method, returns|
                assert_respond_to dep_list_entry, method
                if returns.kind_of? Array
                    assert_kind_of Array, dep_list_entry.send(method)
                    dep_list_entry.send(method).each {|x| assert_kind_of returns.first, x}
                else
                    assert_kind_of returns, dep_list_entry.send(method)
                end
            end
        end
    end


    class TestCase_DepListOverrideMasks < Test::Unit::TestCase
        def test_create
            m = DepListOverrideMasks.new
        end

        def test_each
            m = DepListOverrideMasks.new
            assert_equal [], m.to_a
        end

        def test_empty
            m = DepListOverrideMasks.new
            assert m.empty?
            m.set DepListOverrideMask::Licenses
            assert !m.empty?
        end

        def test_set
            m = DepListOverrideMasks.new
            m.set DepListOverrideMask::Licenses
            m.set DepListOverrideMask::ProfileMasks

            assert ! m.empty?
            assert_equal 2, m.entries.length

            assert m.include?(DepListOverrideMask::Licenses)
            assert m.include?(DepListOverrideMask::ProfileMasks)
        end

        def test_clear
            m = DepListOverrideMasks.new
            m.set DepListOverrideMask::Licenses
            m.set DepListOverrideMask::ProfileMasks
            m.set DepListOverrideMask::TildeKeywords

            assert_equal 3, m.entries.length
            m.reset DepListOverrideMask::TildeKeywords
            assert_equal 2, m.entries.length
            m.reset
            assert m.empty?
        end
    end
end