aboutsummaryrefslogtreecommitdiffstats
path: root/spec/bundler/cache/git_spec.rb
blob: 1cb278e912303af618a8e635e7c2d932b1150e6e (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
# frozen_string_literal: true

RSpec.describe "git base name" do
  it "base_name should strip private repo uris" do
    source = Bundler::Source::Git.new("uri" => "git@github.com:bundler.git")
    expect(source.send(:base_name)).to eq("bundler")
  end

  it "base_name should strip network share paths" do
    source = Bundler::Source::Git.new("uri" => "//MachineName/ShareFolder")
    expect(source.send(:base_name)).to eq("ShareFolder")
  end
end

%w[cache package].each do |cmd|
  RSpec.describe "bundle #{cmd} with git" do
    it "copies repository to vendor cache and uses it" do
      git = build_git "foo"
      ref = git.ref_for("master", 11)

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle "config set cache_all true"
      bundle cmd
      expect(bundled_app("vendor/cache/foo-1.0-#{ref}")).to exist
      expect(bundled_app("vendor/cache/foo-1.0-#{ref}/.git")).not_to exist
      expect(bundled_app("vendor/cache/foo-1.0-#{ref}/.bundlecache")).to be_file

      FileUtils.rm_rf lib_path("foo-1.0")
      expect(the_bundle).to include_gems "foo 1.0"
    end

    it "copies repository to vendor cache and uses it even when installed with bundle --path" do
      git = build_git "foo"
      ref = git.ref_for("master", 11)

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle "install --path vendor/bundle"
      bundle "config set cache_all true"
      bundle cmd

      expect(bundled_app("vendor/cache/foo-1.0-#{ref}")).to exist
      expect(bundled_app("vendor/cache/foo-1.0-#{ref}/.git")).not_to exist

      FileUtils.rm_rf lib_path("foo-1.0")
      expect(the_bundle).to include_gems "foo 1.0"
    end

    it "runs twice without exploding" do
      build_git "foo"

      install_gemfile! <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle "config set cache_all true"
      bundle! cmd
      bundle! cmd

      expect(out).to include "Updating files in vendor/cache"
      FileUtils.rm_rf lib_path("foo-1.0")
      expect(the_bundle).to include_gems "foo 1.0"
    end

    it "tracks updates" do
      git = build_git "foo"
      old_ref = git.ref_for("master", 11)

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle "config set cache_all true"
      bundle cmd

      update_git "foo" do |s|
        s.write "lib/foo.rb", "puts :CACHE"
      end

      ref = git.ref_for("master", 11)
      expect(ref).not_to eq(old_ref)

      bundle! "update", :all => true
      bundle "config set cache_all true"
      bundle! cmd

      expect(bundled_app("vendor/cache/foo-1.0-#{ref}")).to exist
      expect(bundled_app("vendor/cache/foo-1.0-#{old_ref}")).not_to exist

      FileUtils.rm_rf lib_path("foo-1.0")
      run! "require 'foo'"
      expect(out).to eq("CACHE")
    end

    it "tracks updates when specifying the gem" do
      git = build_git "foo"
      old_ref = git.ref_for("master", 11)

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle "config set cache_all true"
      bundle! cmd

      update_git "foo" do |s|
        s.write "lib/foo.rb", "puts :CACHE"
      end

      ref = git.ref_for("master", 11)
      expect(ref).not_to eq(old_ref)

      bundle "update foo"

      expect(bundled_app("vendor/cache/foo-1.0-#{ref}")).to exist
      expect(bundled_app("vendor/cache/foo-1.0-#{old_ref}")).not_to exist

      FileUtils.rm_rf lib_path("foo-1.0")
      run "require 'foo'"
      expect(out).to eq("CACHE")
    end

    it "uses the local repository to generate the cache" do
      git = build_git "foo"
      ref = git.ref_for("master", 11)

      gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-invalid")}', :branch => :master
      G

      bundle %(config set local.foo #{lib_path("foo-1.0")})
      bundle "install"
      bundle "config set cache_all true"
      bundle cmd

      expect(bundled_app("vendor/cache/foo-invalid-#{ref}")).to exist

      # Updating the local still uses the local.
      update_git "foo" do |s|
        s.write "lib/foo.rb", "puts :LOCAL"
      end

      run "require 'foo'"
      expect(out).to eq("LOCAL")
    end

    it "copies repository to vendor cache, including submodules" do
      build_git "submodule", "1.0"

      git = build_git "has_submodule", "1.0" do |s|
        s.add_dependency "submodule"
      end

      Dir.chdir(lib_path("has_submodule-1.0")) do
        sys_exec "git submodule add #{lib_path("submodule-1.0")} submodule-1.0"
        `git commit -m "submodulator"`
      end

      install_gemfile <<-G
        git "#{lib_path("has_submodule-1.0")}", :submodules => true do
          gem "has_submodule"
        end
      G

      ref = git.ref_for("master", 11)
      bundle "config set cache_all true"
      bundle cmd

      expect(bundled_app("vendor/cache/has_submodule-1.0-#{ref}")).to exist
      expect(bundled_app("vendor/cache/has_submodule-1.0-#{ref}/submodule-1.0")).to exist
      expect(the_bundle).to include_gems "has_submodule 1.0"
    end

    it "displays warning message when detecting git repo in Gemfile", :bundler => "< 3" do
      build_git "foo"

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle cmd

      expect(err).to include("Your Gemfile contains path and git dependencies.")
    end

    it "does not display warning message if cache_all is set in bundle config" do
      build_git "foo"

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G

      bundle "config set cache_all true"
      bundle cmd
      bundle cmd

      expect(err).not_to include("Your Gemfile contains path and git dependencies.")
    end

    it "caches pre-evaluated gemspecs" do
      git = build_git "foo"

      # Insert a gemspec method that shells out
      spec_lines = lib_path("foo-1.0/foo.gemspec").read.split("\n")
      spec_lines.insert(-2, "s.description = `echo bob`")
      update_git("foo") {|s| s.write "foo.gemspec", spec_lines.join("\n") }

      install_gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G
      bundle "config set cache_all true"
      bundle cmd

      ref = git.ref_for("master", 11)
      gemspec = bundled_app("vendor/cache/foo-1.0-#{ref}/foo.gemspec").read
      expect(gemspec).to_not match("`echo bob`")
    end

    it "can install after #{cmd} with git not installed" do
      build_git "foo"

      gemfile <<-G
        gem "foo", :git => '#{lib_path("foo-1.0")}'
      G
      bundle! "config set cache_all true"
      bundle! cmd, "all-platforms" => true, :install => false, :path => "./vendor/cache"

      simulate_new_machine
      with_path_as "" do
        bundle! "config set deployment true"
        bundle! :install, :local => true
        expect(the_bundle).to include_gem "foo 1.0"
      end
    end
  end
end