File: solutions_spec.rb

package info (click to toggle)
ruby-solve 4.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,708 kB
  • sloc: ruby: 36,626; makefile: 3
file content (316 lines) | stat: -rw-r--r-- 9,892 bytes parent folder | download
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
require "spec_helper"
__END__
describe "Solutions", :gecode do

  before do
    Solve.engine = :gecode
  end

  it "chooses the correct artifact for the demands" do
    graph = Solve::Graph.new
    graph.artifact("mysql", "2.0.0")
    graph.artifact("mysql", "1.2.0")
    graph.artifact("nginx", "1.0.0").depends("mysql", "= 1.2.0")

    result = Solve.it!(graph, [["nginx", "= 1.0.0"], ["mysql"]])

    result.should eql("nginx" => "1.0.0", "mysql" => "1.2.0")
  end

  it "chooses the best artifact for the demands" do
    graph = Solve::Graph.new
    graph.artifact("mysql", "2.0.0")
    graph.artifact("mysql", "1.2.0")
    graph.artifact("nginx", "1.0.0").depends("mysql", ">= 1.2.0")

    result = Solve.it!(graph, [["nginx", "= 1.0.0"], ["mysql"]])

    result.should eql("nginx" => "1.0.0", "mysql" => "2.0.0")
  end

  it "raises NoSolutionError when a solution cannot be found" do
    graph = Solve::Graph.new
    graph.artifact("mysql", "1.2.0")

    lambda {
      Solve.it!(graph, ["mysql", ">= 2.0.0"])
    }.should raise_error(Solve::Errors::NoSolutionError)
  end

  it "find the correct solution when backtracking in variables introduced via demands" do
    graph = Solve::Graph.new

    graph.artifact("D", "1.2.0")
    graph.artifact("D", "1.3.0")
    graph.artifact("D", "1.4.0")
    graph.artifact("D", "2.0.0")
    graph.artifact("D", "2.1.0")

    graph.artifact("C", "2.0.0").depends("D", "= 1.2.0")
    graph.artifact("C", "2.1.0").depends("D", ">= 2.1.0")
    graph.artifact("C", "2.2.0").depends("D", "> 2.0.0")

    graph.artifact("B", "1.0.0").depends("D", "= 1.0.0")
    graph.artifact("B", "1.1.0").depends("D", "= 1.0.0")
    graph.artifact("B", "2.0.0").depends("D", ">= 1.3.0")
    graph.artifact("B", "2.1.0").depends("D", ">= 2.0.0")

    graph.artifact("A", "1.0.0").depends("B", "> 2.0.0")
    graph.artifact("A", "1.0.0").depends("C", "= 2.1.0")
    graph.artifact("A", "1.0.1").depends("B", "> 1.0.0")
    graph.artifact("A", "1.0.1").depends("C", "= 2.1.0")
    graph.artifact("A", "1.0.2").depends("B", "> 1.0.0")
    graph.artifact("A", "1.0.2").depends("C", "= 2.0.0")

    result = Solve.it!(graph, [["A", "~> 1.0.0"], ["D", ">= 2.0.0"]])

    result.should eql("A" => "1.0.1",
                      "B" => "2.1.0",
                      "C" => "2.1.0",
                      "D" => "2.1.0")
  end

  it "finds the correct solution when there is a circular dependency" do
    graph = Solve::Graph.new

    graph.artifact("A", "1.0.0").depends("B", "1.0.0")
    graph.artifact("B", "1.0.0").depends("C", "1.0.0")
    graph.artifact("C", "1.0.0").depends("A", "1.0.0")

    result = Solve.it!(graph, [["A", "1.0.0"]])

    result.should eql("A" => "1.0.0",
                      "B" => "1.0.0",
                      "C" => "1.0.0")
  end

  it "finds the correct solution when there is a p shaped depenency chain" do
    graph = Solve::Graph.new

    graph.artifact("A", "1.0.0").depends("B", "1.0.0")
    graph.artifact("B", "1.0.0").depends("C", "1.0.0")
    graph.artifact("C", "1.0.0").depends("B", "1.0.0")

    result = Solve.it!(graph, [["A", "1.0.0"]])

    result.should eql("A" => "1.0.0",
                      "B" => "1.0.0",
                      "C" => "1.0.0")
  end

  it "finds the correct solution when there is a diamond shaped dependency" do
    graph = Solve::Graph.new

    graph.artifact("A", "1.0.0")
      .depends("B", "1.0.0")
      .depends("C", "1.0.0")
    graph.artifact("B", "1.0.0")
      .depends("D", "1.0.0")
    graph.artifact("C", "1.0.0")
      .depends("D", "1.0.0")
    graph.artifact("D", "1.0.0")

    result = Solve.it!(graph, [["A", "1.0.0"]])

    result.should eql("A" => "1.0.0",
                      "B" => "1.0.0",
                      "C" => "1.0.0",
                      "D" => "1.0.0")
  end

  it "solves when packages and constraints have prerelease elements" do
    graph = Solve::Graph.new

    graph.artifact("A", "1.0.0")
      .depends("B", ">= 1.0.0-alpha")
    graph.artifact("B", "1.0.0-alpha")
      .depends("C", "1.0.0")
    graph.artifact("C", "1.0.0")

    result = Solve.it!(graph, [["A", "1.0.0"]])

    result.should eql("A" => "1.0.0",
                      "B" => "1.0.0-alpha",
                      "C" => "1.0.0")

  end

  it "solves when packages and constraints have build elements" do
    graph = Solve::Graph.new

    graph.artifact("A", "1.0.0")
      .depends("B", ">= 1.0.0+build")
    graph.artifact("B", "1.0.0+build")
      .depends("C", "1.0.0")
    graph.artifact("C", "1.0.0")

    result = Solve.it!(graph, [["A", "1.0.0"]])

    result.should eql("A" => "1.0.0",
                      "B" => "1.0.0+build",
                      "C" => "1.0.0")

  end

  it "fails with a self dependency" do
    graph = Solve::Graph.new

    graph.artifact("bottom", "1.0.0")
    graph.artifact("middle", "1.0.0").depends("top", "= 1.0.0").depends("middle")

    demands = [["bottom", "1.0.0"], ["middle", "1.0.0"]]

    expect { Solve.it!(graph, demands, { sorted: true } ) }.to raise_error do |error|
      error.should be_a(Solve::Errors::NoSolutionError)
    end
  end

  it "gives an empty solution when there are no demands" do
    graph = Solve::Graph.new
    result = Solve.it!(graph, [])
    result.should eql({})
  end

  it "tries all combinations until it finds a solution" do

    graph = Solve::Graph.new

    graph.artifact("A", "1.0.0").depends("B", "~> 1.0.0")
    graph.artifact("A", "1.0.1").depends("B", "~> 1.0.0")
    graph.artifact("A", "1.0.2").depends("B", "~> 1.0.0")

    graph.artifact("B", "1.0.0").depends("C", "~> 1.0.0")
    graph.artifact("B", "1.0.1").depends("C", "~> 1.0.0")
    graph.artifact("B", "1.0.2").depends("C", "~> 1.0.0")

    graph.artifact("C", "1.0.0").depends("D", "1.0.0")
    graph.artifact("C", "1.0.1").depends("D", "1.0.0")
    graph.artifact("C", "1.0.2").depends("D", "1.0.0")

    # ensure we can't find a solution in the above
    graph.artifact("D", "1.0.0").depends("A", "< 0.0.0")

    # Add a solution to the graph that should be reached only after
    #   all of the others have been tried
    #   it must be circular to ensure that no other branch can find it
    graph.artifact("A", "0.0.0").depends("B", "0.0.0")
    graph.artifact("B", "0.0.0").depends("C", "0.0.0")
    graph.artifact("C", "0.0.0").depends("D", "0.0.0")
    graph.artifact("D", "0.0.0").depends("A", "0.0.0")

    demands = [["A"]]

    result = Solve.it!(graph, demands)

    result.should eql({ "A" => "0.0.0",
                        "B" => "0.0.0",
                        "C" => "0.0.0",
                        "D" => "0.0.0" })

  end

  it "correctly resolves when a resolution exists but it is not the latest" do
    graph = Solve::Graph.new

    graph.artifact("get-the-old-one", "1.0.0")
      .depends("locked-mid-1", ">= 0.0.0")
      .depends("locked-mid-2", ">= 0.0.0")
    graph.artifact("get-the-old-one", "0.5.0")

    graph.artifact("locked-mid-1", "2.0.0").depends("old-bottom", "= 2.0.0")
    graph.artifact("locked-mid-1", "1.3.0").depends("old-bottom", "= 0.5.0")
    graph.artifact("locked-mid-1", "1.0.0")

    graph.artifact("locked-mid-2", "2.0.0").depends("old-bottom", "= 2.1.0")
    graph.artifact("locked-mid-2", "1.4.0").depends("old-bottom", "= 0.5.0")
    graph.artifact("locked-mid-2", "1.0.0")

    graph.artifact("old-bottom", "2.1.0")
    graph.artifact("old-bottom", "2.0.0")
    graph.artifact("old-bottom", "1.0.0")
    graph.artifact("old-bottom", "0.5.0")

    demands = [["get-the-old-one"]]

    result = Solve.it!(graph, demands)

    # ruby solver result:
    #
    # "get-the-old-one" => "1.0.0",
    # "locked-mid-1" => "2.0.0",
    # "locked-mid-2" => "1.0.0",
    # "old-bottom" => "2.0.0"

    result.should eql({
      "get-the-old-one" => "1.0.0",
      "locked-mid-1" => "1.0.0",
      "locked-mid-2" => "2.0.0",
      "old-bottom" => "2.1.0",
    })
  end

  describe "when options[:sorted] is true" do
    describe "with a simple list of dependencies" do
      it "returns a sorted list of dependencies" do
        graph = Solve::Graph.new

        graph.artifact("A", "1.0.0").depends("B", "= 1.0.0")
        graph.artifact("B", "1.0.0").depends("C", "= 1.0.0")
        graph.artifact("C", "1.0.0")

        demands = [["A"]]

        result = Solve.it!(graph, demands, { sorted: true })

        result.should eql([
          ["C", "1.0.0"],
          ["B", "1.0.0"],
          ["A", "1.0.0"],
        ])
      end
    end

    # The order that the demands come in determines the order of artifacts
    # in the solver's variable_table. This must not determine the sort order
    describe "with a constraint that depends upon an earlier constrained artifact" do
      it "returns a sorted list of dependencies" do
        graph = Solve::Graph.new

        graph.artifact("B", "1.0.0").depends("A", "= 1.0.0")
        graph.artifact("A", "1.0.0").depends("C", "= 1.0.0")
        graph.artifact("C", "1.0.0")

        demands = [["A"], ["B"]]

        result = Solve.it!(graph, demands, { sorted: true } )

        result.should eql([
          ["C", "1.0.0"],
          ["A", "1.0.0"],
          ["B", "1.0.0"],
        ])
      end
    end

    describe "when the solution is cyclic" do
      it "raises a Solve::Errors::UnsortableSolutionError which contains the unsorted solution" do
        graph = Solve::Graph.new

        graph.artifact("A", "1.0.0").depends("B", "= 1.0.0")
        graph.artifact("B", "1.0.0").depends("C", "= 1.0.0")
        graph.artifact("C", "1.0.0").depends("A", "= 1.0.0")

        demands = [["A"]]

        expect { Solve.it!(graph, demands, { sorted: true } ) }.to raise_error do |error|
          error.should be_a(Solve::Errors::UnsortableSolutionError)
          error.unsorted_solution.should eql({
            "A" => "1.0.0",
            "B" => "1.0.0",
            "C" => "1.0.0",
          })
        end
      end
    end
  end
end