File: ruby_solver_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 (165 lines) | stat: -rw-r--r-- 4,520 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
require "spec_helper"

describe Solve::RubySolver do
  describe "ClassMethods" do
    describe "::timeout" do
      subject { described_class.timeout }

      it "returns 30,000 by default" do
        expect(subject).to eql(30_000)
      end

      context "when the SOLVE_TIMEOUT env variable is set" do
        before { ENV.stub(:[]).with("SOLVE_TIMEOUT") { "30" } }

        it "returns the value multiplied by a thousand" do
          expect(subject).to eql(30_000)
        end
      end
    end

    describe "::activate" do

      it "is a no-op" do
        described_class.activate
      end

    end
  end

  let(:graph) { double(Solve::Graph) }
  let(:demands) { [["mysql"], ["nginx"]] }
  subject(:solver) { described_class.new(graph, demands, dependency_source: "Berksfile") }

  it "has a list of demands as ruby literals" do
    solver.demands_array.should == demands
  end

  it "has a list of demands as model objects" do
    expected = [
      Solve::Demand.new(solver, "mysql"),
      Solve::Demand.new(solver, "nginx"),
    ]
    solver.demands.should == expected
  end

  it "has a graph" do
    solver.graph.should == graph
  end

  describe "when the constraints are solvable" do
    let(:graph) do
      graph = Solve::Graph.new
      graph.artifact("A", "1.0.0")
      graph.artifact("B", "1.0.0").depends("A")
      graph
    end

    let(:demands) { [["A"], ["B"]] }

    it "gives the solution as a Hash" do
      solver.resolve.should == { "A" => "1.0.0", "B" => "1.0.0" }
    end

    it "gives the solution in sorted form" do
      solver.resolve(sorted: true).should == [["A", "1.0.0"], ["B", "1.0.0"]]
    end
  end

  describe "when the constraints are not solvable" do
    let(:error) do
      begin
        solver.resolve
      rescue => e
        e
      else
        raise "Expected resolve to cause an error"
      end
    end

    context "and molinillo identifies missing artifacts" do
      let(:graph) do
        graph = Solve::Graph.new
        graph.artifact("A", "1.0.0")
        graph
      end

      let(:demands) { [ ["Z"] ] }

      it "raises an error detailing the missing artifacts" do
        expect(error).to be_a_kind_of(Solve::Errors::NoSolutionError)
        expected_error = <<~ERROR_MESSAGE
          Unable to satisfy the following requirements:

          - `Z (>= 0.0.0)` required by `Berksfile`
        ERROR_MESSAGE
        expect(error.to_s).to eq(expected_error)
      end
    end

    context "and molinillo identifies constraints that exclude all known versions" do
      let(:graph) do
        graph = Solve::Graph.new
        graph.artifact("A", "1.0.0")
        graph
      end

      let(:demands) { [ ["A", "> 1.0.0"] ] }

      it "raises an error detailing the missing artifacts" do
        expect(error).to be_a_kind_of(Solve::Errors::NoSolutionError)
        expected_error = <<~ERROR_MESSAGE
          Unable to satisfy the following requirements:

          - `A (> 1.0.0)` required by `Berksfile`
        ERROR_MESSAGE
        expect(error.to_s).to eq(expected_error)
      end
    end

    context "and molinillo identifies dependency conflicts" do
      let(:graph) do
        graph = Solve::Graph.new
        graph.artifact("A", "1.0.0").depends("B").depends("C")
        graph.artifact("B", "1.0.0").depends("D", "= 1.0.0")
        graph.artifact("C", "1.0.0").depends("D", "= 2.0.0")
        graph.artifact("D", "1.0.0")
        graph.artifact("D", "2.0.0")
        graph
      end

      let(:demands) { [ [ "A" ] ] }

      it "raises an error detailing the missing artifacts" do
        expect(error).to be_a_kind_of(Solve::Errors::NoSolutionError)
        expected_error = <<~ERROR_MESSAGE
          Unable to satisfy the following requirements:

          - `D (= 1.0.0)` required by `B-1.0.0`
          - `D (= 2.0.0)` required by `C-1.0.0`
        ERROR_MESSAGE
        expect(error.to_s).to eq(expected_error)
      end
    end
  end

  describe "supporting Serializer interface" do
    let(:serializer) { Solve::Solver::Serializer.new }

    before do
      graph.stub(:artifacts).and_return([])
    end

    it "implements the required interface" do
      problem = Solve::Problem.from_solver(solver)
      json_string = serializer.serialize(problem)
      problem_data = JSON.parse(json_string)
      expected_demands = [
        { "name" => "mysql", "constraint" => ">= 0.0.0" },
        { "name" => "nginx", "constraint" => ">= 0.0.0" },
      ]

      problem_data["demands"].should =~ expected_demands
    end
  end
end