summaryrefslogtreecommitdiff
path: root/spec/bundler/install/gems/sudo_spec.rb
blob: 3e5d38ea4c9f29a5c685df766f6420dc49c81a02 (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
# frozen_string_literal: true

RSpec.describe "when using sudo", :sudo => true do
  describe "and BUNDLE_PATH is writable" do
    context "but BUNDLE_PATH/build_info is not writable" do
      let(:subdir) do
        system_gem_path("cache")
      end

      before do
        bundle "config set path.system true"
        subdir.mkpath
        sudo "chmod u-w #{subdir}"
      end

      after do
        sudo "chmod u+w #{subdir}"
      end

      it "installs" do
        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "rack"
        G

        expect(out).to_not match(/an error occurred/i)
        expect(system_gem_path("cache/rack-1.0.0.gem")).to exist
        expect(the_bundle).to include_gems "rack 1.0"
      end
    end
  end

  describe "and GEM_HOME is owned by root" do
    before :each do
      bundle "config set path.system true"
      chown_system_gems_to_root
    end

    it "installs" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack", '1.0'
        gem "thin"
      G

      expect(system_gem_path("gems/rack-1.0.0")).to exist
      expect(system_gem_path("gems/rack-1.0.0").stat.uid).to eq(0)
      expect(the_bundle).to include_gems "rack 1.0"
    end

    it "installs rake and a gem dependent on rake in the same session" do
      build_repo2 do
        build_gem "another_implicit_rake_dep" do |s|
          s.extensions << "Rakefile"
          s.write "Rakefile", <<-RUBY
            task :default do
              path = File.expand_path("../lib", __FILE__)
              FileUtils.mkdir_p(path)
              File.open("\#{path}/another_implicit_rake_dep.rb", "w") do |f|
                f.puts "ANOTHER_IMPLICIT_RAKE_DEP = 'YES'"
              end
            end
          RUBY
        end
      end

      gemfile <<-G
          source "#{file_uri_for(gem_repo2)}"
          gem "rake"
          gem "another_implicit_rake_dep"
      G
      bundle "install"
      expect(system_gem_path("gems/another_implicit_rake_dep-1.0")).to exist
    end

    it "installs when BUNDLE_PATH is owned by root" do
      bundle_path = tmp("owned_by_root")
      FileUtils.mkdir_p bundle_path
      sudo "chown -R root #{bundle_path}"

      ENV["BUNDLE_PATH"] = bundle_path.to_s
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack", '1.0'
      G

      expect(bundle_path.join(Bundler.ruby_scope, "gems/rack-1.0.0")).to exist
      expect(bundle_path.join(Bundler.ruby_scope, "gems/rack-1.0.0").stat.uid).to eq(0)
      expect(the_bundle).to include_gems "rack 1.0"
    end

    it "installs when BUNDLE_PATH does not exist" do
      root_path = tmp("owned_by_root")
      FileUtils.mkdir_p root_path
      sudo "chown -R root #{root_path}"
      bundle_path = root_path.join("does_not_exist")

      ENV["BUNDLE_PATH"] = bundle_path.to_s
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack", '1.0'
      G

      expect(bundle_path.join(Bundler.ruby_scope, "gems/rack-1.0.0")).to exist
      expect(bundle_path.join(Bundler.ruby_scope, "gems/rack-1.0.0").stat.uid).to eq(0)
      expect(the_bundle).to include_gems "rack 1.0"
    end

    it "installs extensions/" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "very_simple_binary"
      G

      expect(system_gem_path("gems/very_simple_binary-1.0")).to exist
      binary_glob = system_gem_path("extensions/*/*/very_simple_binary-1.0")
      expect(Dir.glob(binary_glob).first).to be
    end
  end

  describe "and BUNDLE_PATH is not writable" do
    before do
      bundle "config set --local path .bundle"
      sudo "chmod ugo-w .bundle"
    end

    after do
      sudo "chmod ugo+w .bundle"
    end

    it "installs" do
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack", '1.0'
      G

      expect(local_gem_path("gems/rack-1.0.0")).to exist
      expect(the_bundle).to include_gems "rack 1.0"
    end

    it "cleans up the tmpdirs generated" do
      require "tmpdir"
      Dir.glob("#{Dir.tmpdir}/bundler*").each do |tmpdir|
        FileUtils.remove_entry_secure(tmpdir)
      end

      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack"
      G
      tmpdirs = Dir.glob("#{Dir.tmpdir}/bundler*")

      expect(tmpdirs).to be_empty
    end
  end

  describe "and GEM_HOME is not writable" do
    it "installs" do
      bundle "config set path.system true"
      gem_home = tmp("sudo_gem_home")
      sudo "mkdir -p #{gem_home}"
      sudo "chmod ugo-w #{gem_home}"

      system_gems :bundler, :path => gem_home

      gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack", '1.0'
      G

      bundle :install, :env => { "GEM_HOME" => gem_home.to_s, "GEM_PATH" => nil }
      expect(gem_home.join("bin/rackup")).to exist
      expect(the_bundle).to include_gems "rack 1.0", :env => { "GEM_HOME" => gem_home.to_s, "GEM_PATH" => nil }

      sudo "rm -rf #{tmp("sudo_gem_home")}"
    end
  end

  describe "and root runs install" do
    let(:warning) { "Don't run Bundler as root." }

    before do
      gemfile %(source "#{file_uri_for(gem_repo1)}")
    end

    it "warns against that" do
      bundle :install, :sudo => :preserve_env
      expect(err).to include(warning)
    end

    context "when ENV['BUNDLE_SILENCE_ROOT_WARNING'] is set" do
      it "skips the warning" do
        bundle :install, :sudo => :preserve_env, :env => { "BUNDLE_SILENCE_ROOT_WARNING" => "true" }
        expect(err).to_not include(warning)
      end
    end

    context "when silence_root_warning = false" do
      it "warns against that" do
        bundle :install, :sudo => :preserve_env, :env => { "BUNDLE_SILENCE_ROOT_WARNING" => "false" }
        expect(err).to include(warning)
      end
    end
  end
end