Skip to content
This repository has been archived by the owner on Nov 23, 2018. It is now read-only.

Commit

Permalink
Restructure library a bit... and use rspec
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kieltyka committed Oct 31, 2011
1 parent 33ec2a1 commit 0a91a97
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 116 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source :rubygems

gemspec
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
PATH
remote: .
specs:
subexec (0.2.0)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rake (0.9.2.2)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
rspec-core (2.7.1)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.7.0)

PLATFORMS
ruby

DEPENDENCIES
rake
rspec (~> 2.7.0)
subexec!
23 changes: 12 additions & 11 deletions README.rdoc → README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
= Subexec
# Subexec
by Peter Kieltyka
http://github/nulayer/subexec

=== Description
## Description

Subexec is a simple library that spawns an external command with
an optional timeout parameter. It relies on Ruby 1.9's Process.spawn
Expand All @@ -12,18 +12,19 @@ Useful for libraries that are Ruby wrappers for CLI's. For example,
resizing images with ImageMagick's mogrify command sometimes stalls
and never returns control back to the original process. Enter Subexec.

=== Usage
## Usage

sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 5
puts sub.output # returns: hello
puts sub.exitstatus # returns: 0
```ruby
sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 5
puts sub.output # returns: hello
puts sub.exitstatus # returns: 0

sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 1
puts sub.output # returns:
puts sub.exitstatus # returns:
sub = Subexec.run "echo 'hello' && sleep 3", :timeout => 1
puts sub.output # returns:
puts sub.exitstatus # returns:`
```


=== Limitations
## Limitations

Only Ruby 1.9 can spawn non-blocking subprocesses, via Process.spawn.
So Ruby 1.8 support is sheerly for backwards compatibility.
22 changes: 7 additions & 15 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/gempackagetask'
require 'bundler'
Bundler::GemHelper.install_tasks

spec = eval(File.read('subexec.gemspec'))
Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
end

Rake::TestTask.new(:test) do |test|
test.libs << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
end

desc "Benchmark"
task :benchmark do
sh "ruby test/benchmark.rb"
sh "ruby benchmark/run.rb"
end

task :default => :test
task :default => :spec
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

17 changes: 12 additions & 5 deletions test/benchmark.rb → benchmark/run.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
require 'rubygems'
require 'benchmark'
begin
require 'bundler'
Bundler.setup(:default, :development)
rescue LoadError => e
# Fall back on doing an unlocked resolve at runtime.
$stderr.puts e.message
$stderr.puts "Try running `bundle install`"
exit!
end

$LOAD_PATH << File.dirname(__FILE__)
require 'loader'
require 'benchmark'

TEST_PROG = File.join(File.expand_path('../spec', File.dirname(__FILE__)), 'helloworld.sh')

n = 100
Benchmark.bm(20) do |x|
Expand All @@ -20,4 +27,4 @@
raise if output != "Hello\nWorld\n"
end
end
end
end
23 changes: 12 additions & 11 deletions lib/subexec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# = Subexec
# * by Peter Kieltyka
# * http://github/nulayer/subprocess
# * http://github/nulayer/subexec
#
# === Description
#
Expand All @@ -26,14 +26,15 @@
# puts sub.exitstatus # returns:

class Subexec
VERSION = '0.2.0'

attr_accessor :pid
attr_accessor :command
attr_accessor :timeout
attr_accessor :timer
attr_accessor :output
attr_accessor :exitstatus
attr_accessor :lang
attr_accessor :pid,
:command,
:timeout,
:timer,
:output,
:exitstatus,
:lang

def self.run(command, options={})
sub = new(command, options)
Expand All @@ -42,9 +43,9 @@ def self.run(command, options={})
end

def initialize(command, options={})
self.command = command
self.timeout = options[:timeout] || -1 # default is to never timeout
self.lang = options[:lang] || "C"
self.command = command
self.timeout = options[:timeout] || -1 # default is to never timeout
self.lang = options[:lang] || "C"
self.exitstatus = 0
end

Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
begin
require 'bundler'
Bundler.setup(:default, :development)
rescue LoadError => e
# Fall back on doing an unlocked resolve at runtime.
$stderr.puts e.message
$stderr.puts "Try running `bundle install`"
exit!
end

$:.unshift(File.dirname(__FILE__))
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

begin
require 'ruby-debug'
rescue LoadError
end

require 'subexec'
require 'rspec'

RSpec.configure do |config|
config.mock_with :rspec
end

TEST_PROG = File.join(File.dirname(__FILE__), 'helloworld.sh')
42 changes: 42 additions & 0 deletions spec/subexec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe Subexec do
context 'Subexec class' do

it 'run helloworld script' do
sub = Subexec.run "#{TEST_PROG} 1"
sub.output.should == "Hello\nWorld\n"
sub.exitstatus.should == 0
end

it 'timeout helloworld script' do
sub = Subexec.run "#{TEST_PROG} 2", :timeout => 1
if RUBY_VERSION >= '1.9'
sub.exitstatus.should == nil
else
# Ruby 1.8 doesn't support the timeout, so the
# subprocess will have to exit on its own
sub.exitstatus.should == 0
end
end

it 'set LANG env variable on request' do
original_lang = `echo $LANG`

sub = Subexec.run "echo $LANG", :lang => "fr_FR.UTF-8"
sub.output.should == "fr_FR.UTF-8\n"
sub = Subexec.run "echo $LANG", :lang => "C"
sub.output.should == "C\n"
sub = Subexec.run "echo $LANG", :lang => "en_US.UTF-8"
sub.output.should == "en_US.UTF-8\n"

`echo $LANG`.should == original_lang
end

it 'default LANG to C' do
sub = Subexec.run "echo $LANG"
sub.output.should == "C\n"
end

end
end
15 changes: 9 additions & 6 deletions subexec.gemspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
version = File.read("VERSION").strip
$:.push File.expand_path('../lib', __FILE__)
require 'subexec'

Gem::Specification.new do |s|
s.name = 'subexec'
s.version = version
s.version = Subexec::VERSION
s.platform = Gem::Platform::RUBY
s.summary = "Subexec spawns an external command with a timeout"
s.description = "Subexec spawns an external command with a timeout"
s.summary = "Subexec spawns a subprocess with an optional timeout"
s.description = s.summary

s.authors = ["Peter Kieltyka"]
s.email = ["peter@nulayer.com"]
s.homepage = "http://github.com/nulayer/subexec"

s.files = Dir['README.rdoc', 'VERSION', 'lib/**/*']
s.files = Dir['README.md', 'lib/**/*']
s.require_path = 'lib'

s.add_development_dependency('rake')
s.add_development_dependency('rspec', ['~> 2.7.0'])
end

13 changes: 0 additions & 13 deletions test/helper.rb

This file was deleted.

9 changes: 0 additions & 9 deletions test/loader.rb

This file was deleted.

45 changes: 0 additions & 45 deletions test/subexec_test.rb

This file was deleted.

0 comments on commit 0a91a97

Please sign in to comment.