summaryrefslogtreecommitdiff
path: root/spec/support/sudo.rb
blob: d57ffcbbc76751e692fd6e7eee5d9c2856852942 (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
module Spec
  module Sudo
    def self.sudo?
      @which_sudo ||= `which sudo`.strip
      !@which_sudo.empty?
    end

    module Describe
      def describe_sudo(*args, &blk)
        return unless Sudo.sudo?
        describe(*args) do
          before :each do
            pending "sudo tests are broken"
            chown_system_gems
          end

          instance_eval(&blk)
        end
      end
    end

    def self.included(klass)
      klass.extend Describe
    end

    def sudo?
      Sudo.sudo?
    end

    def sudo(cmd)
      cmd = "sudo #{cmd}" if sudo?
      sys_exec(cmd)
    end

    def chown_system_gems
      sudo "chown -R root #{system_gem_path}"
    end
  end
end