timcharper-spork 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ = Spork
2
+
3
+ * http://github.com/timcharper/spork
4
+
5
+ == SYNOPSIS:
6
+
7
+ Spork is a Drb spec server (similar to the script/spec_server provided by rspec-rails), except rather than using the Rails constant unloading to reload your files, it forks a copy of the server each time you run your specs. The result? Spork runs more solid: it doesn't get corrupted over time, and it properly handles modules and any voo-doo meta programming you may have put in your app.
8
+
9
+ Because Spork uses Kernel.fork, it only works on POSIX systems. This means Windows users are not invited to this party. Sorry :(
10
+
11
+ Spork is still experimental, but is performing solid for us.
12
+
13
+ == INSTALL:
14
+
15
+ [sudo] gem install timcharper-spork --source http://gems.github.com/
16
+
17
+ alternatively:
18
+
19
+ git clone git://github.com/timcharper/spork.git
20
+ cd spork
21
+ gem build spork.gemspec
22
+ sudo gem install spork.gemspec
23
+
24
+ == Usage
25
+
26
+ From a terminal, change to your project directory.
27
+
28
+ Then, bootstrap your spec/spec_helper.rb file.
29
+
30
+ spork --bootstrap
31
+
32
+ Next, edit spec/spec_helper.rb and follow the instructions that were put at the top.
33
+
34
+ Finally, run spork. A spec DRb server will be running!
35
+
36
+ spork
37
+
38
+ To get the TextMate RSpec bundle to use spork, go to config->advanced->shell variables, and add TM_RSPEC_OPTS=--drb.
39
+
40
+ To run from the command line, use spec --drb spec/lib/my_spec.rb
41
+
42
+ Or, you could add --drb to your spec.opts file.
43
+
44
+ == Some potential issues and ways to overcome them:
45
+
46
+ === ActiveRecord reports "connection has gone away" for the first few specs
47
+
48
+ Not sure why this happens, but if you simply add a line to re-establish your database connection on each line as follows, the problem goes away:
49
+
50
+ Spork.each_run do
51
+ ActiveRecord::Base.establish_connection # make sure that the db connection is ready.
52
+ end
53
+
54
+ === Couldn't find formatter class Spec::Runner::Formatter::TextMateFormatter Make sure the --require option is specified *before* --format
55
+
56
+ On one of our projects, many of us using TextMate with spork, only one developer got this error message while the rest of us ran just fine. I don't know exactly why it happened, but requiring the textmate formatter in the prefork block made it go away, like this:
57
+
58
+ Spork.prefork do
59
+ gem "rspec", "= 1.2.6"
60
+ require 'spec'
61
+ ...
62
+ require 'spec/runner/formatter/text_mate_formatter'
63
+ ...
64
+ end
65
+
66
+ == Kudos to
67
+
68
+ * Ben Mabey - help with documentation, testing, suggestions.
69
+ * David Chelimsky - for the fine RSpec testing framework, and the original rspec-rails spec_server implementation, which Spork has built upon.
70
+ * Lead Media Partners - just for being an awesome place to work.
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+
4
+ Spork.prefork do
5
+ # Loading more in this block will cause your specs to run faster. However,
6
+ # if you change any configuration or code from libraries loaded here, you'll
7
+ # need to restart spork for it take effect.
8
+
9
+ end
10
+
11
+ Spork.each_run do
12
+ # This code will be run each time you run your specs.
13
+
14
+ end
15
+
16
+ # --- Instructions ---
17
+ # - Sort through your spec_helper file. Place as much environment loading
18
+ # code that you don't normally modify during development in the
19
+ # Spork.prefork block.
20
+ # - Place the rest under Spork.each_run block
21
+ # - Any code that is left outside of the blocks will be ran during preforking
22
+ # and during each_run!
23
+ # - These instructions should self-destruct in 10 seconds. If they don't,
24
+ # feel free to delete them.
25
+ #
26
+
27
+
28
+
29
+
data/bin/spork ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ gem 'test-unit', '1.2.3' if RUBY_VERSION.to_f >= 1.9
3
+
4
+ require 'rubygems'
5
+ require 'spork'
6
+ require 'optparse'
7
+
8
+ ENV["DRB"] = 'true'
9
+ ENV["RAILS_ENV"] ||= 'test' if Spork.using_rails?
10
+
11
+ options = {}
12
+ parser = OptionParser.new
13
+ parser.on("-d", "--daemon") {|ignore| options[:daemon] = true }
14
+ parser.on("-b", "--bootstrap") {|ignore| options[:bootstrap] = true }
15
+ parser.on("-p", "--pid PIDFILE"){|pid| options[:pid] = pid }
16
+ parser.parse!(ARGV)
17
+
18
+ unless File.exist?(Spork::SPEC_HELPER_FILE)
19
+ puts <<-USEFUL_ERROR
20
+ Bummer!
21
+
22
+ I can't find the file spec/spec_helper.rb, which I need in order to run.
23
+
24
+ Are you running me from a project directory that has rspec set up?
25
+ USEFUL_ERROR
26
+ exit 1
27
+ end
28
+
29
+ if options[:bootstrap]
30
+ if Spork.bootstrap
31
+ exit 0
32
+ else
33
+ exit 1
34
+ end
35
+ end
36
+
37
+ require 'spork/spec_server'
38
+ Spork.preload || exit(1)
39
+
40
+ if options[:daemon]
41
+ ::Spork::SpecServer.daemonize(options[:pid])
42
+ else
43
+ ::Spork::SpecServer.run
44
+ end
45
+
@@ -0,0 +1,58 @@
1
+ require 'drb/drb'
2
+ require 'rbconfig'
3
+
4
+ # This is based off of spec_server.rb from rspec-rails (David Chelimsky), which was based on Florian Weber's TDDMate
5
+ class Spork::SpecServer
6
+ DRB_PORT = 8989
7
+ def self.restart_test_server
8
+ puts "restarting"
9
+ config = ::Config::CONFIG
10
+ ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
11
+ command_line = [ruby, $0, ARGV].flatten.join(' ')
12
+ exec(command_line)
13
+ end
14
+
15
+ def self.daemonize(pid_file = nil)
16
+ return yield if $DEBUG
17
+ pid = Process.fork{
18
+ Process.setsid
19
+ trap("SIGINT"){ exit! 0 }
20
+ trap("SIGTERM"){ exit! 0 }
21
+ trap("SIGHUP"){ restart_test_server }
22
+ File.open("/dev/null"){|f|
23
+ STDERR.reopen f
24
+ STDIN.reopen f
25
+ STDOUT.reopen f
26
+ }
27
+ run
28
+ }
29
+ puts "spec_server launched (PID: %d)" % pid
30
+ File.open(pid_file,"w"){|f| f.puts pid } if pid_file
31
+ exit! 0
32
+ end
33
+
34
+ def self.run
35
+ trap("USR2") { ::Spork::SpecServer.restart_test_server } if Signal.list.has_key?("USR2")
36
+ DRb.start_service("druby://127.0.0.1:#{DRB_PORT}", ::Spork::SpecServer.new)
37
+ puts "Spork is ready and listening on #{DRB_PORT}!"
38
+ DRb.thread.join
39
+ end
40
+
41
+ def run(argv, stderr, stdout)
42
+ $stdout = stdout
43
+ $stderr = stderr
44
+ child_pid = Kernel.fork do
45
+ Spork.running!
46
+ load ::Spork::SPEC_HELPER_FILE
47
+
48
+ ::Spec::Runner::CommandLine.run(
49
+ ::Spec::Runner::OptionParser.parse(
50
+ argv,
51
+ $stderr,
52
+ $stdout
53
+ )
54
+ )
55
+ end
56
+ Process.wait(child_pid)
57
+ end
58
+ end
data/lib/spork.rb ADDED
@@ -0,0 +1,67 @@
1
+ module Spork
2
+ SPEC_HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")
3
+
4
+ def self.prefork(&block)
5
+ return if @already_preforked
6
+ @already_preforked = true
7
+ yield
8
+ end
9
+
10
+ def self.each_run(&block)
11
+ return if @state == :preforking || (@state != :not_using_spork && @already_run)
12
+ @already_run = true
13
+ yield
14
+ end
15
+
16
+ def self.preforking!
17
+ @state = :preforking
18
+ end
19
+
20
+ def self.running!
21
+ @state = :running
22
+ end
23
+
24
+ def self.state
25
+ @state ||= :not_using_spork
26
+ end
27
+
28
+ def self.using_rails?
29
+ File.exist?("config/environment.rb")
30
+ end
31
+
32
+ def self.using_prefork?
33
+ File.read(SPEC_HELPER_FILE).include?("Spork.prefork")
34
+ end
35
+
36
+ def self.bootstrap
37
+ puts "Bootstrapping #{SPEC_HELPER_FILE}"
38
+ contents = File.read(SPEC_HELPER_FILE)
39
+ bootstrap_code = File.read(File.dirname(__FILE__) + "/../assets/bootstrap.rb")
40
+ File.open(SPEC_HELPER_FILE, "wb") do |f|
41
+ f.puts bootstrap_code
42
+ f.puts contents
43
+ end
44
+
45
+ puts "Done. Edit #{SPEC_HELPER_FILE} now with your favorite text editor and follow the instructions."
46
+ true
47
+ end
48
+
49
+ def self.preload
50
+ if using_prefork?
51
+ puts "Loading Spork.prefork block..."
52
+ Spork.preforking!
53
+ load SPEC_HELPER_FILE
54
+ else
55
+ puts "spec_helper.rb is has not been sporked. Run spork --bootstrap to do so."
56
+ # are we in a rails app?
57
+ if using_rails?
58
+ puts "Preloading Rails environment"
59
+ require "config/environment.rb"
60
+ else
61
+ puts "There's nothing I can really do for you. Bailing."
62
+ return false
63
+ end
64
+ end
65
+ true
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timcharper-spork
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.3"
5
+ platform: ruby
6
+ authors:
7
+ - Tim Harper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A forking Drb spec server
17
+ email:
18
+ - timcharper+spork@gmail.com
19
+ executables:
20
+ - spork
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - README.rdoc
27
+ - lib/spork
28
+ - lib/spork/spec_server.rb
29
+ - lib/spork.rb
30
+ - assets/bootstrap.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/timcharper/spork
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main
36
+ - README.rdoc
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project: spork
54
+ rubygems_version: 1.2.0
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: spork 0.3
58
+ test_files: []
59
+