summaryrefslogtreecommitdiff
path: root/spec/bundler/commands/list_spec.rb
blob: c890646a816cc4f3274ede2887de542cfae223f6 (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
314
315
# frozen_string_literal: true

require "json"

RSpec.describe "bundle list" do
  def find_gem_name(json:, name:)
    parse_json(json)["gems"].detect {|h| h["name"] == name }
  end

  def parse_json(json)
    JSON.parse(json)
  end

  context "in verbose mode" do
    it "logs the actual flags passed to the command" do
      install_gemfile <<-G
        source "https://gem.repo1"
      G

      bundle "list --verbose"

      expect(out).to include("Running `bundle list --verbose`")
    end
  end

  context "with name-only and paths option" do
    it "raises an error" do
      bundle "list --name-only --paths", raise_on_error: false

      expect(err).to eq "The `--name-only` and `--paths` options cannot be used together"
    end
  end

  context "with without-group and only-group option" do
    it "raises an error" do
      bundle "list --without-group dev --only-group test", raise_on_error: false

      expect(err).to eq "The `--only-group` and `--without-group` options cannot be used together"
    end
  end

  context "with invalid format option" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"
      G
    end

    it "raises an error" do
      bundle "list --format=nope", raise_on_error: false

      expect(err).to eq "Unknown option`--format=nope`. Supported formats: `json`"
    end
  end

  describe "with without-group option" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"

        gem "myrack"
        gem "rspec", :group => [:test]
        gem "rails", :group => [:production]
      G
    end

    context "when group is present" do
      it "prints the gems not in the specified group" do
        bundle "list --without-group test"

        expect(out).to include("  * myrack (1.0.0)")
        expect(out).to include("  * rails (2.3.2)")
        expect(out).not_to include("  * rspec (1.2.7)")
      end

      it "prints the gems not in the specified group with json" do
        bundle "list --without-group test --format=json"

        gem = find_gem_name(json: out, name: "myrack")
        expect(gem["version"]).to eq("1.0.0")
        gem = find_gem_name(json: out, name: "rails")
        expect(gem["version"]).to eq("2.3.2")
        gem = find_gem_name(json: out, name: "rspec")
        expect(gem).to be_nil
      end
    end

    context "when group is not found" do
      it "raises an error" do
        bundle "list --without-group random", raise_on_error: false

        expect(err).to eq "`random` group could not be found."
      end
    end

    context "when multiple groups" do
      it "prints the gems not in the specified groups" do
        bundle "list --without-group test production"

        expect(out).to include("  * myrack (1.0.0)")
        expect(out).not_to include("  * rails (2.3.2)")
        expect(out).not_to include("  * rspec (1.2.7)")
      end

      it "prints the gems not in the specified groups with json" do
        bundle "list --without-group test production --format=json"

        gem = find_gem_name(json: out, name: "myrack")
        expect(gem["version"]).to eq("1.0.0")
        gem = find_gem_name(json: out, name: "rails")
        expect(gem).to be_nil
        gem = find_gem_name(json: out, name: "rspec")
        expect(gem).to be_nil
      end
    end
  end

  describe "with only-group option" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"

        gem "myrack"
        gem "rspec", :group => [:test]
        gem "rails", :group => [:production]
      G
    end

    context "when group is present" do
      it "prints the gems in the specified group" do
        bundle "list --only-group default"

        expect(out).to include("  * myrack (1.0.0)")
        expect(out).not_to include("  * rspec (1.2.7)")
      end

      it "prints the gems in the specified group with json" do
        bundle "list --only-group default --format=json"

        gem = find_gem_name(json: out, name: "myrack")
        expect(gem["version"]).to eq("1.0.0")
        gem = find_gem_name(json: out, name: "rspec")
        expect(gem).to be_nil
      end
    end

    context "when group is not found" do
      it "raises an error" do
        bundle "list --only-group random", raise_on_error: false

        expect(err).to eq "`random` group could not be found."
      end
    end

    context "when multiple groups" do
      it "prints the gems in the specified groups" do
        bundle "list --only-group default production"

        expect(out).to include("  * myrack (1.0.0)")
        expect(out).to include("  * rails (2.3.2)")
        expect(out).not_to include("  * rspec (1.2.7)")
      end

      it "prints the gems in the specified groups with json" do
        bundle "list --only-group default production --format=json"

        gem = find_gem_name(json: out, name: "myrack")
        expect(gem["version"]).to eq("1.0.0")
        gem = find_gem_name(json: out, name: "rails")
        expect(gem["version"]).to eq("2.3.2")
        gem = find_gem_name(json: out, name: "rspec")
        expect(gem).to be_nil
      end
    end
  end

  context "with name-only option" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"

        gem "myrack"
        gem "rspec", :group => [:test]
      G
    end

    it "prints only the name of the gems in the bundle" do
      bundle "list --name-only"

      expect(out).to include("myrack")
      expect(out).to include("rspec")
    end

    it "prints only the name of the gems in the bundle with json" do
      bundle "list --name-only --format=json"

      gem = find_gem_name(json: out, name: "myrack")
      expect(gem.keys).to eq(["name"])
      gem = find_gem_name(json: out, name: "rspec")
      expect(gem.keys).to eq(["name"])
    end
  end

  context "with paths option" do
    before do
      build_repo2 do
        build_gem "myrack", "1.2" do |s|
          s.executables = "myrackup"
        end

        build_gem "bar"
      end

      build_git "git_test", "1.0.0", path: lib_path("git_test")

      build_lib("gemspec_test", path: tmp("gemspec_test")) do |s|
        s.add_dependency "bar", "=1.0.0"
      end

      install_gemfile <<-G
        source "https://gem.repo2"
        gem "myrack"
        gem "rails"
        gem "git_test", :git => "#{lib_path("git_test")}"
        gemspec :path => "#{tmp("gemspec_test")}"
      G
    end

    it "prints the path of each gem in the bundle" do
      bundle "list --paths"
      expect(out).to match(%r{.*\/rails\-2\.3\.2})
      expect(out).to match(%r{.*\/myrack\-1\.2})
      expect(out).to match(%r{.*\/git_test\-\w})
      expect(out).to match(%r{.*\/gemspec_test})
    end

    it "prints the path of each gem in the bundle with json" do
      bundle "list --paths --format=json"

      gem = find_gem_name(json: out, name: "rails")
      expect(gem["path"]).to match(%r{.*\/rails\-2\.3\.2})
      expect(gem["git_version"]).to be_nil

      gem = find_gem_name(json: out, name: "myrack")
      expect(gem["path"]).to match(%r{.*\/myrack\-1\.2})
      expect(gem["git_version"]).to be_nil

      gem = find_gem_name(json: out, name: "git_test")
      expect(gem["path"]).to match(%r{.*\/git_test\-\w})
      expect(gem["git_version"]).to be_truthy
      expect(gem["git_version"].strip).to eq(gem["git_version"])

      gem = find_gem_name(json: out, name: "gemspec_test")
      expect(gem["path"]).to match(%r{.*\/gemspec_test})
      expect(gem["git_version"]).to be_nil
    end
  end

  context "when no gems are in the gemfile" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"
      G
    end

    it "prints message saying no gems are in the bundle" do
      bundle "list"
      expect(out).to include("No gems in the Gemfile")
    end

    it "prints empty json" do
      bundle "list --format=json"
      expect(parse_json(out)["gems"]).to eq([])
    end
  end

  context "without options" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"

        gem "myrack"
        gem "rspec", :group => [:test]
      G
    end

    it "lists gems installed in the bundle" do
      bundle "list"
      expect(out).to include("  * myrack (1.0.0)")
    end

    it "lists gems installed in the bundle with json" do
      bundle "list --format=json"

      gem = find_gem_name(json: out, name: "myrack")
      expect(gem["version"]).to eq("1.0.0")
    end
  end

  context "when using the ls alias" do
    before do
      install_gemfile <<-G
        source "https://gem.repo1"

        gem "myrack"
        gem "rspec", :group => [:test]
      G
    end

    it "runs the list command" do
      bundle "ls"
      expect(out).to include("Gems included by the bundle")
    end
  end
end