Skip to content

Commit

Permalink
upgrade test suite to RSpec 3
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shevtsov committed Apr 22, 2015
1 parent 2f03c5d commit 28d0b73
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
2 changes: 1 addition & 1 deletion headless.gemspec
Expand Up @@ -15,5 +15,5 @@ spec = Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")

s.add_development_dependency 'rake'
s.add_development_dependency "rspec", "~> 2.6"
s.add_development_dependency "rspec", "~> 3"
end
66 changes: 33 additions & 33 deletions spec/headless_spec.rb
Expand Up @@ -9,61 +9,61 @@
context "instantiation" do
context "when Xvfb is not installed" do
before do
Headless::CliUtil.stub(:application_exists?).and_return(false)
allow(Headless::CliUtil).to receive(:application_exists?).and_return(false)
end

it "raises an error" do
lambda { Headless.new }.should raise_error(Headless::Exception)
expect { Headless.new }.to raise_error(Headless::Exception)
end
end

context "when Xvfb is not started yet" do
it "starts Xvfb" do
Headless.any_instance.should_receive(:system).with("/usr/bin/Xvfb :99 -screen 0 1280x1024x24 -ac >/dev/null 2>&1 &").and_return(true)
expect_any_instance_of(Headless).to receive(:system).with("/usr/bin/Xvfb :99 -screen 0 1280x1024x24 -ac >/dev/null 2>&1 &").and_return(true)

headless = Headless.new
end

it "allows setting screen dimensions" do
Headless.any_instance.should_receive(:system).with("/usr/bin/Xvfb :99 -screen 0 1024x768x16 -ac >/dev/null 2>&1 &").and_return(true)
expect_any_instance_of(Headless).to receive(:system).with("/usr/bin/Xvfb :99 -screen 0 1024x768x16 -ac >/dev/null 2>&1 &").and_return(true)

headless = Headless.new(:dimensions => "1024x768x16")
end
end

context "when Xvfb is already running" do
before do
Headless::CliUtil.stub(:read_pid).with('/tmp/.X99-lock').and_return(31337)
Headless::CliUtil.stub(:read_pid).with('/tmp/.X100-lock').and_return(nil)
allow(Headless::CliUtil).to receive(:read_pid).with('/tmp/.X99-lock').and_return(31337)
allow(Headless::CliUtil).to receive(:read_pid).with('/tmp/.X100-lock').and_return(nil)
end

context "and display reuse is allowed" do
let(:options) { {:reuse => true} }

it "should reuse the existing Xvfb" do
Headless.new(options).display.should == 99
expect(Headless.new(options).display).to eq 99
end
end

context "and display reuse is not allowed" do
let(:options) { {:reuse => false} }

it "should pick the next available display number" do
Headless.new(options).display.should == 100
expect(Headless.new(options).display).to eq 100
end

context "and display number is explicitly set" do
let(:options) { {:reuse => false, :display => 99} }

it "should fail with an exception" do
lambda { Headless.new(options) }.should raise_error(Headless::Exception)
expect { Headless.new(options) }.to raise_error(Headless::Exception)
end

context "and autopicking is allowed" do
let(:options) { {:reuse => false, :display => 99, :autopick => true} }

it "should pick the next available display number" do
Headless.new(options).display.should == 100
expect(Headless.new(options).display).to eq 100
end
end
end
Expand All @@ -72,23 +72,23 @@

context 'when Xvfb is started, but by another user' do
before do
Headless::CliUtil.stub(:read_pid).with('/tmp/.X99-lock') { raise Errno::EPERM }
Headless::CliUtil.stub(:read_pid).with('/tmp/.X100-lock').and_return(nil)
allow(Headless::CliUtil).to receive(:read_pid).with('/tmp/.X99-lock') { raise Errno::EPERM }
allow(Headless::CliUtil).to receive(:read_pid).with('/tmp/.X100-lock').and_return(nil)
end

context "and display autopicking is not allowed" do
let(:options) { {:autopick => false} }

it "should fail with and exception" do
lambda { Headless.new(options) }.should raise_error(Headless::Exception)
expect { Headless.new(options) }.to raise_error(Headless::Exception)
end
end

context "and display autopicking is allowed" do
let(:options) { {:autopick => true} }

it "should pick the next display number" do
Headless.new(options).display.should == 100
expect(Headless.new(options).display).to eq 100
end
end
end
Expand All @@ -98,35 +98,35 @@
let(:headless) { Headless.new }
describe "#start" do
it "switches to the headless server" do
ENV['DISPLAY'].should == ":31337"
expect(ENV['DISPLAY']).to eq ":31337"
headless.start
ENV['DISPLAY'].should == ":99"
expect(ENV['DISPLAY']).to eq ":99"
end
end

describe "#stop" do
it "switches back from the headless server" do
ENV['DISPLAY'].should == ":31337"
expect(ENV['DISPLAY']).to eq ":31337"
headless.start
ENV['DISPLAY'].should == ":99"
expect(ENV['DISPLAY']).to eq ":99"
headless.stop
ENV['DISPLAY'].should == ":31337"
expect(ENV['DISPLAY']).to eq ":31337"
end
end

describe "#destroy" do
before do
Headless::CliUtil.stub(:read_pid).and_return(4444)
allow(Headless::CliUtil).to receive(:read_pid).and_return(4444)
end

it "switches back from the headless server and terminates the headless session" do
Process.should_receive(:kill).with('TERM', 4444)
expect(Process).to receive(:kill).with('TERM', 4444)

ENV['DISPLAY'].should == ":31337"
expect(ENV['DISPLAY']).to eq ":31337"
headless.start
ENV['DISPLAY'].should == ":99"
expect(ENV['DISPLAY']).to eq ":99"
headless.destroy
ENV['DISPLAY'].should == ":31337"
expect(ENV['DISPLAY']).to eq ":31337"
end
end
end
Expand All @@ -135,12 +135,12 @@
let(:headless) { Headless.new }

it "returns video recorder" do
headless.video.should be_a_kind_of(Headless::VideoRecorder)
expect(headless.video).to be_a_kind_of(Headless::VideoRecorder)
end

it "returns the same instance" do
recorder = headless.video
headless.video.should be_eql(recorder)
expect(headless.video).to eq recorder
end
end

Expand All @@ -152,19 +152,19 @@
end

it "raises an error if imagemagick is not installed, with default options" do
Headless::CliUtil.stub(:application_exists?).with('import').and_return(false)
allow(Headless::CliUtil).to receive(:application_exists?).with('import').and_return(false)

expect { headless.take_screenshot('a.png') }.to raise_error(Headless::Exception)
end

it "raises an error if imagemagick is not installed, with using: :imagemagick" do
Headless::CliUtil.stub(:application_exists?).with('import').and_return(false)
allow(Headless::CliUtil).to receive(:application_exists?).with('import').and_return(false)

expect { headless.take_screenshot('a.png', :using => :imagemagick) }.to raise_error(Headless::Exception)
end

it "raises an error if xwd is not installed, with using: :xwd" do
Headless::CliUtil.stub(:application_exists?).with('xwd').and_return(false)
allow(Headless::CliUtil).to receive(:application_exists?).with('xwd').and_return(false)

expect { headless.take_screenshot('a.png', :using => :xwd) }.to raise_error(Headless::Exception)
end
Expand All @@ -191,11 +191,11 @@
private

def stub_environment
Headless::CliUtil.stub(:application_exists?).and_return(true)
Headless::CliUtil.stub(:read_pid).and_return(nil)
Headless::CliUtil.stub(:path_to).and_return("/usr/bin/Xvfb")
allow(Headless::CliUtil).to receive(:application_exists?).and_return(true)
allow(Headless::CliUtil).to receive(:read_pid).and_return(nil)
allow(Headless::CliUtil).to receive(:path_to).and_return("/usr/bin/Xvfb")

# TODO this is wrong. But, as long as Xvfb is started inside the constructor (which is also wrong), I don't see another option to make tests pass
Headless.any_instance.stub(:ensure_xvfb_is_running).and_return(true)
allow_any_instance_of(Headless).to receive(:ensure_xvfb_is_running).and_return(true)
end
end
35 changes: 22 additions & 13 deletions spec/video_recorder_spec.rb
Expand Up @@ -7,30 +7,39 @@

describe "instantiation" do
before do
Headless::CliUtil.stub(:application_exists?).and_return(false)
allow(Headless::CliUtil).to receive(:application_exists?).and_return(false)
end

it "throws an error if ffmpeg is not installed" do
lambda { Headless::VideoRecorder.new(99, "1024x768x32") }.should raise_error(Headless::Exception)
expect { Headless::VideoRecorder.new(99, "1024x768x32") }.to raise_error(Headless::Exception)
end
end

describe "#capture" do
before do
allow(Headless::CliUtil).to receive(:path_to).and_return('ffmpeg')
end

it "starts ffmpeg" do
Headless::CliUtil.stub(:path_to).and_return('ffmpeg')
Headless::CliUtil.should_receive(:fork_process).with(/ffmpeg -y -r 30 -s 1024x768 -f x11grab -i :99 -g 600 -vcodec qtrle/, "/tmp/.headless_ffmpeg_99.pid", '/dev/null')
expect(Headless::CliUtil).to receive(:fork_process).with(/^ffmpeg -y -r 30 -s 1024x768 -f x11grab -i :99 -g 600 -vcodec qtrle [^ ]+$/, "/tmp/.headless_ffmpeg_99.pid", '/dev/null')

recorder = Headless::VideoRecorder.new(99, "1024x768x32")
recorder.start_capture
end

it "starts ffmpeg with specified codec" do
Headless::CliUtil.stub(:path_to).and_return('ffmpeg')
Headless::CliUtil.should_receive(:fork_process).with(/ffmpeg -y -r 30 -s 1024x768 -f x11grab -i :99 -g 600 -vcodec libvpx/, "/tmp/.headless_ffmpeg_99.pid", '/dev/null')
expect(Headless::CliUtil).to receive(:fork_process).with(/^ffmpeg -y -r 30 -s 1024x768 -f x11grab -i :99 -g 600 -vcodec libvpx [^ ]+$/, "/tmp/.headless_ffmpeg_99.pid", '/dev/null')

recorder = Headless::VideoRecorder.new(99, "1024x768x32", {:codec => 'libvpx'})
recorder.start_capture
end

it "starts ffmpeg from ffmpeg provider with correct parameters" do
expect(Headless::CliUtil).to receive(:fork_process).with(/^ffmpeg -y -r 30 -s 1024x768 -f x11grab -i :99 -vcodec qtrle [^ ]+$/, "/tmp/.headless_ffmpeg_99.pid", '/dev/null')

recorder = Headless::VideoRecorder.new(99, "1024x768x32", {:provider => :ffmpeg})
recorder.start_capture
end
end

context "stopping video recording" do
Expand All @@ -46,18 +55,18 @@

describe "using #stop_and_save" do
it "stops video recording and saves file" do
Headless::CliUtil.should_receive(:kill_process).with(pidfile, :wait => true)
File.should_receive(:exists?).with(tmpfile).and_return(true)
FileUtils.should_receive(:mv).with(tmpfile, filename)
expect(Headless::CliUtil).to receive(:kill_process).with(pidfile, :wait => true)
expect(File).to receive(:exists?).with(tmpfile).and_return(true)
expect(FileUtils).to receive(:mv).with(tmpfile, filename)

subject.stop_and_save(filename)
end
end

describe "using #stop_and_discard" do
it "stops video recording and deletes temporary file" do
Headless::CliUtil.should_receive(:kill_process).with(pidfile, :wait => true)
FileUtils.should_receive(:rm).with(tmpfile)
expect(Headless::CliUtil).to receive(:kill_process).with(pidfile, :wait => true)
expect(FileUtils).to receive(:rm).with(tmpfile)

subject.stop_and_discard
end
Expand All @@ -67,7 +76,7 @@
private

def stub_environment
Headless::CliUtil.stub(:application_exists?).and_return(true)
Headless::CliUtil.stub(:fork_process).and_return(true)
allow(Headless::CliUtil).to receive(:application_exists?).and_return(true)
allow(Headless::CliUtil).to receive(:fork_process).and_return(true)
end
end

0 comments on commit 28d0b73

Please sign in to comment.