Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Restructure library a bit... and use rspec

  • Loading branch information...
commit 0a91a97027c516012ebee8be7fb1c5235c25a0c9 1 parent 33ec2a1
Peter Kieltyka authored
View
3  Gemfile
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
View
26 Gemfile.lock
@@ -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!
View
23 README.rdoc → README.md
@@ -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
@@ -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.
View
22 Rakefile
@@ -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
View
1  VERSION
@@ -1 +0,0 @@
-0.1.1
View
17 test/benchmark.rb → benchmark/run.rb
@@ -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|
@@ -20,4 +27,4 @@
raise if output != "Hello\nWorld\n"
end
end
-end
+end
View
23 lib/subexec.rb
@@ -1,6 +1,6 @@
# = Subexec
# * by Peter Kieltyka
-# * http://github/nulayer/subprocess
+# * http://github/nulayer/subexec
#
# === Description
#
@@ -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)
@@ -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
View
0  test/helloworld.sh → spec/helloworld.sh
File renamed without changes
View
26 spec/spec_helper.rb
@@ -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')
View
42 spec/subexec_spec.rb
@@ -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
View
15 subexec.gemspec
@@ -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
-
View
13 test/helper.rb
@@ -1,13 +0,0 @@
-require 'rubygems'
-require 'shoulda'
-
-begin
- require 'ruby-debug'
-rescue LoadError
-end
-
-require File.join(File.dirname(__FILE__), 'loader')
-
-#
-class Test::Unit::TestCase
-end
View
9 test/loader.rb
@@ -1,9 +0,0 @@
-ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
-TEST_ROOT = File.join(ROOT, 'test')
-
-$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
-$:.unshift(File.dirname(__FILE__))
-require 'subexec'
-
-# Test program
-TEST_PROG = File.join(TEST_ROOT, 'helloworld.sh')
View
45 test/subexec_test.rb
@@ -1,45 +0,0 @@
-$LOAD_PATH << File.dirname(__FILE__)
-require 'helper'
-
-class SubexecTest < Test::Unit::TestCase
- context "Subexec class" do
-
- should "run helloworld script" do
- sub = Subexec.run "#{TEST_PROG} 1"
- assert_equal sub.output, "Hello\nWorld\n"
- assert_equal sub.exitstatus, 0
- end
-
- should "timeout helloworld script" do
- sub = Subexec.run "#{TEST_PROG} 2", :timeout => 1
- if RUBY_VERSION >= '1.9'
- assert_equal sub.exitstatus, nil
- else
- # Ruby 1.8 doesn't support the timeout, so the
- # subprocess will have to exit on its own
- assert_equal sub.exitstatus, 0
- end
- end
-
- should "set LANG env variable on request" do
- original_lang = `echo $LANG`
-
- sub = Subexec.run "echo $LANG", :lang => "fr_FR.UTF-8"
- assert_equal sub.output, "fr_FR.UTF-8\n"
- sub = Subexec.run "echo $LANG", :lang => "C"
- assert_equal sub.output, "C\n"
- sub = Subexec.run "echo $LANG", :lang => "en_US.UTF-8"
- assert_equal sub.output, "en_US.UTF-8\n"
-
- assert_equal `echo $LANG`, original_lang
- end
-
- should "default LANG to C" do
- sub = Subexec.run "echo $LANG"
- assert_equal sub.output, "C\n"
- end
-
- end
-
-end
-
Please sign in to comment.
Something went wrong with that request. Please try again.