summaryrefslogtreecommitdiff
path: root/spec/bundler/manifest_spec.rb
blob: b843e5262e485a59d291b066aeebe0d14ef6555f (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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "Bundler::Manifest" do

  def dep(name, version, options = {})
    Bundler::Dependency.new(name, {:version => version}.merge(options))
  end

  before(:each) do
    @sources = %W(file://#{gem_repo1} file://#{gem_repo2})
    @deps = []
    @deps << dep("rails", "2.3.2")
    @deps << dep("rack", "0.9.1")
  end

  describe "Manifest with dependencies" do

    before(:each) do
      @manifest = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "rails", "2.3.2"
        gem "rack",  "0.9.1"
      Gemfile
      @saved_load_path, @saved_loaded_features = $:.dup, $".dup
    end

    after(:each) do
      Object.send(:remove_const, :VerySimpleForTests) if defined?(VerySimpleForTests)
      $:.replace @saved_load_path
      $".replace @saved_loaded_features
    end

    it "has a list of sources and dependencies" do
      @manifest.sources.should == @sources.map { |s| URI.parse(s) }
      @manifest.dependencies.should == @deps
    end

    it "bundles itself (running all of the steps)" do
      @manifest.install

      gems = %w(rack-0.9.1 actionmailer-2.3.2
        activerecord-2.3.2 activesupport-2.3.2
        rake-0.8.7 actionpack-2.3.2
        activeresource-2.3.2 rails-2.3.2)

      tmp_gem_path.should have_cached_gems(*gems)
      tmp_gem_path.should have_installed_gems(*gems)
    end

    it "skips fetching the source index if all gems are present" do
      @manifest.install
      Bundler::Finder.should_not_receive(:new)
      @manifest.install
    end

    it "logs 'Done' when done" do
      @manifest.install
      @log_output.should have_log_message("Done.")
    end


    it "does the full fetching if a gem in the cache does not match the manifest" do
      @manifest.install

      m = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "rails", "2.3.2"
        gem "rack",  "1.0.0"
      Gemfile

      m.install

      gems = %w(rack-1.0.0 actionmailer-2.3.2
        activerecord-2.3.2 activesupport-2.3.2
        rake-0.8.7 actionpack-2.3.2
        activeresource-2.3.2 rails-2.3.2)

      tmp_gem_path.should have_cached_gems(*gems)
      tmp_gem_path.should have_installed_gems(*gems)
    end

    it "removes gems that are not needed anymore" do
      @manifest.install
      tmp_gem_path.should have_cached_gem("rack-0.9.1")
      tmp_gem_path.should have_installed_gem("rack-0.9.1")
      tmp_bindir("rackup").should exist

      m = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "rails", "2.3.2"
      Gemfile

      m.install

      tmp_gem_path.should_not have_cached_gem("rack-0.9.1")
      tmp_gem_path.should_not have_installed_gem("rack-0.9.1")
      tmp_bindir("rackup").should_not exist
      @log_output.should have_log_message("Deleting gem: rack-0.9.1")
      @log_output.should have_log_message("Deleting bin file: rackup")
    end

    it "removes stray specfiles" do
      spec = tmp_gem_path("specifications", "omg.gemspec")
      FileUtils.mkdir_p(tmp_gem_path("specifications"))
      FileUtils.touch(spec)
      @manifest.install
      spec.should_not exist
    end

    it "removes any stray directories in gems that are not to be installed" do
      dir = tmp_gem_path("gems", "omg")
      FileUtils.mkdir_p(dir)
      @manifest.install
      dir.should_not exist
    end

    it "raises a friendly exception if the manifest doesn't resolve" do
      build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "rails", "2.3.2"
        gem "rack",  "0.9.1"
        gem "active_support", "2.0"
      Gemfile
      Dir.chdir(tmp_dir)

      lambda do
        Bundler::CLI.run(:bundle)
      end.should raise_error(SystemExit)

      @log_output.should have_log_message(/rails \(= 2\.3\.2.*rack \(= 0\.9\.1.*active_support \(= 2\.0/m)
    end
  end

  describe "runtime" do

    it "makes gems available via Manifest#activate" do
      m = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "very-simple", "1.0.0"
      Gemfile

      m.install
      m.manifest.activate

      $:.any? do |p|
        File.expand_path(p) == File.expand_path(tmp_gem_path("gems", "very-simple-1.0", "lib"))
      end.should be_true
    end

    it "makes gems available" do
      m = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "very-simple", "1.0.0"
      Gemfile

      m.install
      m.manifest.activate
      m.manifest.require_all

      $".any? do |f|
        File.expand_path(f) ==
          File.expand_path(tmp_gem_path("gems", "very-simple-1.0", "lib", "very-simple.rb"))
      end
    end

    it "is able to work system gems" do
      m = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        gem "very-simple"
      Gemfile

      m.install
      out = run_in_context "require 'rake' ; puts Rake"
      out.should == "Rake"
    end

    it "it does not work with system gems if system gems have been disabled" do
      m = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        gem "very-simple"
        disable_system_gems
      Gemfile

      m.install
      out = run_in_context "begin ; require 'rake' ; rescue LoadError ; puts('WIN') ; end"
      out.should == "WIN"
    end

    ["Running with system gems", "Running without system gems"].each_with_index do |desc, i|
      describe desc do
        before(:each) do
          m = build_manifest <<-Gemfile
            sources.clear
            source "file://#{gem_repo1}"
            gem "very-simple"
            #{'disable_system_gems' if i == 1}
          Gemfile
          m.install
        end

        it "sets loaded_from on the specs" do
          out = run_in_context "puts(Gem.loaded_specs['very-simple'].loaded_from || 'FAIL')"
          out.should_not == "FAIL"
        end

        it "finds the gems in the source_index" do
          out = run_in_context "puts Gem.source_index.find_name('very-simple').length"
          out.should == "1"
        end

        it "still finds the gems in the source_index even if refresh! is called" do
          out = run_in_context "Gem.source_index.refresh! ; puts Gem.source_index.find_name('very-simple').length"
          out.should == "1"
        end
      end
    end
  end

  describe "environments" do
    before(:each) do
      @manifest = build_manifest <<-Gemfile
        sources.clear
        source "file://#{gem_repo1}"
        source "file://#{gem_repo2}"
        gem "very-simple", "1.0.0", :only => "testing"
        gem "rack",        "1.0.0"
      Gemfile

      @manifest.install
      @manifest = @manifest.manifest
    end

    it "can provide a list of environments" do
      @manifest.environments.should == ["testing", "default"]
    end

    it "knows what gems are in an environment" do
      @manifest.gems_for("testing").should match_gems(
        "very-simple" => ["1.0"], "rack" => ["1.0.0"])

      @manifest.gems_for("production").should match_gems(
        "rack" => ["1.0.0"])
    end

    it "can create load path files for each environment" do
      tmp_gem_path('environments', 'testing.rb').should have_load_paths(tmp_gem_path,
        "very-simple-1.0" => %w(bin lib),
        "rack-1.0.0"      => %w(bin lib)
      )

      tmp_gem_path('environments', 'default.rb').should have_load_paths(tmp_gem_path,
        "rack-1.0.0" => %w(bin lib)
      )

      File.exist?(tmp_gem_path('environments', "production.rb")).should be_false
    end

    it "requires the Rubygems library" do
      out = run_in_context "puts 'Gem'"
      out.should == "Gem"
    end

    it "Gem.loaded_specs has the gems that are included" do
      out = run_in_context %'puts Gem.loaded_specs.map{|k,v|"\#{k} - \#{v.version}"}'
      out.should include("rack - 1.0.0")
    end

    it "Gem.loaded_specs has the gems that are included in the testing environment" do
      env = tmp_gem_path('environments', 'testing.rb')
      out = run_in_context env, %'puts Gem.loaded_specs.map{|k,v|"\#{k} - \#{v.version}"}'
      out.should include("rack - 1.0.0")
      out.should include("very-simple - 1.0")
    end
  end
end