summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_resolver_api_set.rb
blob: 6942b8587dede852bfdd065e9b4a1cfc9c0842d4 (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
# frozen_string_literal: true
require 'rubygems/test_case'

class TestGemResolverAPISet < Gem::TestCase
  def setup
    super

    @DR = Gem::Resolver
    @dep_uri = URI "#{@gem_repo}api/v1/dependencies"
  end

  def test_initialize
    set = @DR::APISet.new

    assert_equal URI('https://rubygems.org/api/v1/dependencies'), set.dep_uri
    assert_equal URI('https://rubygems.org'),                     set.uri
    assert_equal Gem::Source.new(URI('https://rubygems.org')),    set.source
  end

  def test_initialize_deeper_uri
    set = @DR::APISet.new 'https://rubygemsserver.com/mygems/api/v1/dependencies'

    assert_equal URI('https://rubygemsserver.com/mygems/api/v1/dependencies'), set.dep_uri
    assert_equal URI('https://rubygemsserver.com/mygems/'),                    set.uri
    assert_equal Gem::Source.new(URI('https://rubygemsserver.com/mygems/')), set.source
  end

  def test_initialize_uri
    set = @DR::APISet.new @dep_uri

    assert_equal URI("#{@gem_repo}api/v1/dependencies"), set.dep_uri
    assert_equal URI("#{@gem_repo}"), set.uri
  end

  def test_find_all
    spec_fetcher

    data = [
      { :name         => 'a',
        :number       => '1',
        :platform     => 'ruby',
        :dependencies => [], },
    ]

    @fetcher.data["#{@dep_uri}?gems=a"] = Marshal.dump data

    set = @DR::APISet.new @dep_uri

    a_dep = @DR::DependencyRequest.new dep('a'), nil

    expected = [
      @DR::APISpecification.new(set, data.first)
    ]

    assert_equal expected, set.find_all(a_dep)
  end

  def test_find_all_cache
    spec_fetcher

    data = [
      { :name         => 'a',
        :number       => '1',
        :platform     => 'ruby',
        :dependencies => [], },
    ]

    @fetcher.data["#{@dep_uri}?gems=a"] = Marshal.dump data

    set = @DR::APISet.new @dep_uri

    a_dep = @DR::DependencyRequest.new dep('a'), nil

    set.prefetch [a_dep]

    expected = [
      @DR::APISpecification.new(set, data.first)
    ]

    assert_equal expected, set.find_all(a_dep)

    @fetcher.data.delete "#{@dep_uri}?gems=a"
  end

  def test_find_all_local
    set = @DR::APISet.new @dep_uri
    set.remote = false

    a_dep = @DR::DependencyRequest.new dep('a'), nil

    assert_empty set.find_all(a_dep)
  end

  def test_find_all_missing
    spec_fetcher

    @fetcher.data["#{@dep_uri}?gems=a"] = Marshal.dump []

    set = @DR::APISet.new @dep_uri

    a_dep = @DR::DependencyRequest.new dep('a'), nil

    assert_empty set.find_all(a_dep)

    @fetcher.data.delete "#{@dep_uri}?gems=a"

    assert_empty set.find_all(a_dep)
  end

  def test_prefetch
    spec_fetcher

    data = [
      { :name         => 'a',
        :number       => '1',
        :platform     => 'ruby',
        :dependencies => [], },
    ]

    @fetcher.data["#{@dep_uri}?gems=a,b"] = Marshal.dump data
    @fetcher.data["#{@dep_uri}?gems=b"]   = Marshal.dump []

    set = @DR::APISet.new @dep_uri

    a_dep = @DR::DependencyRequest.new dep('a'), nil
    b_dep = @DR::DependencyRequest.new dep('b'), nil

    set.prefetch [a_dep, b_dep]

    assert_equal %w[a-1], set.find_all(a_dep).map {|s| s.full_name }
    assert_empty          set.find_all(b_dep)
  end

  def test_prefetch_cache
    spec_fetcher

    data = [
      { :name         => 'a',
        :number       => '1',
        :platform     => 'ruby',
        :dependencies => [], },
    ]

    @fetcher.data["#{@dep_uri}?gems=a"] = Marshal.dump data

    set = @DR::APISet.new @dep_uri

    a_dep = @DR::DependencyRequest.new dep('a'), nil
    b_dep = @DR::DependencyRequest.new dep('b'), nil

    set.prefetch [a_dep]

    @fetcher.data.delete "#{@dep_uri}?gems=a"
    @fetcher.data["#{@dep_uri}?gems=b"]   = Marshal.dump []

    set.prefetch [a_dep, b_dep]
  end

  def test_prefetch_cache_missing
    spec_fetcher

    data = [
      { :name         => 'a',
        :number       => '1',
        :platform     => 'ruby',
        :dependencies => [], },
    ]

    @fetcher.data["#{@dep_uri}?gems=a,b"] = Marshal.dump data

    set = @DR::APISet.new @dep_uri

    a_dep = @DR::DependencyRequest.new dep('a'), nil
    b_dep = @DR::DependencyRequest.new dep('b'), nil

    set.prefetch [a_dep, b_dep]

    @fetcher.data.delete "#{@dep_uri}?gems=a,b"

    set.prefetch [a_dep, b_dep]
  end

  def test_prefetch_local
    spec_fetcher

    data = [
      { :name         => 'a',
        :number       => '1',
        :platform     => 'ruby',
        :dependencies => [], },
    ]

    @fetcher.data["#{@dep_uri}?gems=a,b"] = Marshal.dump data
    @fetcher.data["#{@dep_uri}?gems=b"]   = Marshal.dump []

    set = @DR::APISet.new @dep_uri
    set.remote = false

    a_dep = @DR::DependencyRequest.new dep('a'), nil
    b_dep = @DR::DependencyRequest.new dep('b'), nil

    set.prefetch [a_dep, b_dep]

    assert_empty set.instance_variable_get :@data
  end
end