summaryrefslogtreecommitdiff
path: root/spec/bundler/remote_specification_spec.rb
blob: 8115e026d8f58841571b860677bc5a73742d0d19 (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
# frozen_string_literal: true

RSpec.describe Bundler::RemoteSpecification do
  let(:name)         { "foo" }
  let(:version)      { Gem::Version.new("1.0.0") }
  let(:platform)     { Gem::Platform::RUBY }
  let(:spec_fetcher) { double(:spec_fetcher) }

  subject { described_class.new(name, version, platform, spec_fetcher) }

  it "is Comparable" do
    expect(described_class.ancestors).to include(Comparable)
  end

  it "can match platforms" do
    expect(described_class.ancestors).to include(Bundler::MatchPlatform)
  end

  describe "#fetch_platform" do
    let(:remote_spec) { double(:remote_spec, :platform => "jruby") }

    before { allow(spec_fetcher).to receive(:fetch_spec).and_return(remote_spec) }

    it "should return the spec platform" do
      expect(subject.fetch_platform).to eq("jruby")
    end
  end

  describe "#full_name" do
    context "when platform is ruby" do
      it "should return the spec name and version" do
        expect(subject.full_name).to eq("foo-1.0.0")
      end
    end

    context "when platform is nil" do
      let(:platform) { nil }

      it "should return the spec name and version" do
        expect(subject.full_name).to eq("foo-1.0.0")
      end
    end

    context "when platform is a non-ruby platform" do
      let(:platform) { "jruby" }

      it "should return the spec name, version, and platform" do
        expect(subject.full_name).to eq("foo-1.0.0-jruby")
      end
    end
  end

  describe "#<=>" do
    let(:other_name)         { name }
    let(:other_version)      { version }
    let(:other_platform)     { platform }
    let(:other_spec_fetcher) { spec_fetcher }

    shared_examples_for "a comparison" do
      context "which exactly matches" do
        it "returns 0" do
          expect(subject <=> other).to eq(0)
        end
      end

      context "which is different by name" do
        let(:other_name) { "a" }
        it "returns 1" do
          expect(subject <=> other).to eq(1)
        end
      end

      context "which has a lower version" do
        let(:other_version) { Gem::Version.new("0.9.0") }
        it "returns 1" do
          expect(subject <=> other).to eq(1)
        end
      end

      context "which has a higher version" do
        let(:other_version) { Gem::Version.new("1.1.0") }
        it "returns -1" do
          expect(subject <=> other).to eq(-1)
        end
      end

      context "which has a different platform" do
        let(:other_platform) { Gem::Platform.new("x86-mswin32") }
        it "returns -1" do
          expect(subject <=> other).to eq(-1)
        end
      end
    end

    context "comparing another Bundler::RemoteSpecification" do
      let(:other) do
        Bundler::RemoteSpecification.new(other_name, other_version,
          other_platform, nil)
      end

      it_should_behave_like "a comparison"
    end

    context "comparing a Gem::Specification" do
      let(:other) do
        Gem::Specification.new(other_name, other_version).tap do |s|
          s.platform = other_platform
        end
      end

      it_should_behave_like "a comparison"
    end

    context "comparing a non sortable object" do
      let(:other) { Object.new }
      let(:remote_spec) { double(:remote_spec, :platform => "jruby") }

      before do
        allow(spec_fetcher).to receive(:fetch_spec).and_return(remote_spec)
        allow(remote_spec).to receive(:<=>).and_return(nil)
      end

      it "should use default object comparison" do
        expect(subject <=> other).to eq(nil)
      end
    end
  end

  describe "#__swap__" do
    let(:spec) { double(:spec, :dependencies => []) }
    let(:new_spec) { double(:new_spec, :dependencies => [], :runtime_dependencies => []) }

    before { subject.instance_variable_set(:@_remote_specification, spec) }

    it "should replace remote specification with the passed spec" do
      expect(subject.instance_variable_get(:@_remote_specification)).to be(spec)
      subject.__swap__(new_spec)
      expect(subject.instance_variable_get(:@_remote_specification)).to be(new_spec)
    end
  end

  describe "#sort_obj" do
    context "when platform is ruby" do
      it "should return a sorting delegate array with name, version, and -1" do
        expect(subject.sort_obj).to match_array(["foo", version, -1])
      end
    end

    context "when platform is not ruby" do
      let(:platform) { "jruby" }

      it "should return a sorting delegate array with name, version, and 1" do
        expect(subject.sort_obj).to match_array(["foo", version, 1])
      end
    end
  end

  describe "method missing" do
    context "and is present in Gem::Specification" do
      let(:remote_spec) { double(:remote_spec, :authors => "abcd") }

      before do
        allow(subject).to receive(:_remote_specification).and_return(remote_spec)
        expect(subject.methods.map(&:to_sym)).not_to include(:authors)
      end

      it "should send through to Gem::Specification" do
        expect(subject.authors).to eq("abcd")
      end
    end
  end

  describe "respond to missing?" do
    context "and is present in Gem::Specification" do
      let(:remote_spec) { double(:remote_spec, :authors => "abcd") }

      before do
        allow(subject).to receive(:_remote_specification).and_return(remote_spec)
        expect(subject.methods.map(&:to_sym)).not_to include(:authors)
      end

      it "should send through to Gem::Specification" do
        expect(subject.respond_to?(:authors)).to be_truthy
      end
    end
  end
end