0

RSpec intercepting backticks, preventing sudo?

I'm porting some code from MiniTest to RSpec. Said code now fails.

In IRB:

`sudo echo 1`   # returns 1

RSpec:

# example.rb
class Example
  def self.doexec
    `sudo echo 1`
  end
end

# example_spec.rb
it '#doexec' do
  Example.doexec.should include("1")
end

...when executed:

rspec spec/lib/example_spec.rb
# spec fails, stderror:
sudo: no tty present and no askpass program specified

Is this a bug, feature, or any idea of how to work it (without hardwiring in the password)?

Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
  • You should run it from console.. then it will ask you root password, and you need to give it, and then it will run. – Arup Rakshit Jul 16 '14 at 18:43

1 Answers1

0

Here is a way, you can try :-

test.rb

#!/usr/bin/env ruby

class Example
  def self.do_this
    `echo <your password> | sudo -S echo 11`
  end
end

test_spec.rb

require_relative "../test.rb"

describe Example do
  it '#do_this' do
    expect(Example.do_this).to include("1")
  end
end

Now run your file :-

arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
root's password:.

Finished in 0.07274 seconds (files took 0.13673 seconds to load)
1 example, 0 failures

Read this post.

Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317