bmabey-spork 0.4.4 → 0.5.9

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.
Files changed (37) hide show
  1. data/README.rdoc +51 -23
  2. data/assets/bootstrap.rb +1 -1
  3. data/bin/spork +0 -0
  4. data/features/cucumber_rails_integration.feature +113 -0
  5. data/features/diagnostic_mode.feature +41 -0
  6. data/features/rails_delayed_loading_workarounds.feature +80 -0
  7. data/features/rspec_rails_integration.feature +90 -0
  8. data/features/steps/rails_steps.rb +53 -0
  9. data/features/steps/sandbox_steps.rb +98 -0
  10. data/features/support/env.rb +98 -0
  11. data/features/unknown_app_framework.feature +42 -0
  12. data/lib/spork.rb +85 -26
  13. data/lib/spork/app_framework.rb +74 -0
  14. data/lib/spork/app_framework/rails.rb +158 -0
  15. data/lib/spork/app_framework/rails_stub_files/application.rb +3 -0
  16. data/lib/spork/app_framework/rails_stub_files/application_controller.rb +3 -0
  17. data/lib/spork/app_framework/rails_stub_files/application_helper.rb +3 -0
  18. data/lib/spork/app_framework/unknown.rb +6 -0
  19. data/lib/spork/custom_io_streams.rb +25 -0
  20. data/lib/spork/diagnoser.rb +103 -0
  21. data/lib/spork/forker.rb +70 -0
  22. data/lib/spork/runner.rb +28 -10
  23. data/lib/spork/server.rb +88 -46
  24. data/lib/spork/server/cucumber.rb +21 -9
  25. data/lib/spork/server/rspec.rb +1 -1
  26. data/spec/spec_helper.rb +93 -35
  27. data/spec/spork/app_framework/rails_spec.rb +22 -0
  28. data/spec/spork/app_framework/unknown_spec.rb +12 -0
  29. data/spec/spork/app_framework_spec.rb +16 -0
  30. data/spec/spork/diagnoser_spec.rb +105 -0
  31. data/spec/spork/forker_spec.rb +44 -0
  32. data/spec/spork/runner_spec.rb +2 -2
  33. data/spec/spork/server/cucumber_spec.rb +25 -0
  34. data/spec/spork/server/rspec_spec.rb +17 -3
  35. data/spec/spork/server_spec.rb +32 -17
  36. data/spec/spork_spec.rb +112 -13
  37. metadata +33 -3
@@ -1,28 +1,40 @@
1
+ require 'cucumber'
2
+
1
3
  class Spork::Server::Cucumber < Spork::Server
2
4
  CUCUMBER_PORT = 8990
3
5
  CUCUMBER_HELPER_FILE = File.join(Dir.pwd, "features/support/env.rb")
4
-
6
+
5
7
  class << self
6
8
  def port
7
- CUCUMBER_PORT
9
+ (ENV['CUCUMBER_DRB'] || CUCUMBER_PORT).to_i
8
10
  end
9
-
11
+
10
12
  def helper_file
11
13
  CUCUMBER_HELPER_FILE
12
14
  end
13
15
 
16
+ # REMOVE WHEN SUPPORT FOR PRE-0.4 IS DROPPED
14
17
  attr_accessor :step_mother
15
18
  end
16
-
19
+
20
+ # REMOVE WHEN SUPPORT FOR PRE-0.4 IS DROPPED
17
21
  def step_mother
18
22
  self.class.step_mother
19
23
  end
20
-
24
+
21
25
  def run_tests(argv, stderr, stdout)
22
- require 'cucumber/cli/main'
23
- ::Cucumber::Cli::Main.step_mother = step_mother
24
- ::Cucumber::Cli::Main.new(argv, stdout, stderr).execute!(step_mother)
26
+ begin
27
+ ::Cucumber::Cli::Main.new(argv, stdout, stderr).execute!(::Cucumber::StepMother.new)
28
+ rescue NoMethodError => pre_cucumber_0_4 # REMOVE WHEN SUPPORT FOR PRE-0.4 IS DROPPED
29
+ ::Cucumber::Cli::Main.new(argv, stdout, stderr).execute!(step_mother)
30
+ end
25
31
  end
26
32
  end
27
33
 
28
- Spork::Server::Cucumber.step_mother = self
34
+ begin
35
+ step_mother = ::Cucumber::StepMother.new
36
+ step_mother.load_rb_language
37
+ Spork::Server::Cucumber.step_mother = step_mother
38
+ rescue NoMethodError => pre_cucumber_0_4 # REMOVE WHEN SUPPORT FOR PRE-0.4 IS DROPPED
39
+ Spork::Server::Cucumber.step_mother = self
40
+ end
@@ -3,7 +3,7 @@ class Spork::Server::RSpec < Spork::Server
3
3
  RSPEC_HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")
4
4
 
5
5
  def self.port
6
- RSPEC_PORT
6
+ (ENV['RSPEC_DRB'] || RSPEC_PORT).to_i
7
7
  end
8
8
 
9
9
  def self.helper_file
@@ -1,49 +1,107 @@
1
1
  require 'rubygems'
2
- require 'spork'
2
+ require 'spec'
3
+
4
+ unless $spec_helper_loaded
5
+ $spec_helper_loaded = true
3
6
 
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.
7
+ $LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
8
+ SPEC_TMP_DIR = File.expand_path('tmp', File.dirname(__FILE__))
8
9
 
9
- end
10
+ require 'spork'
11
+ require 'spork/runner.rb'
12
+ require 'spork/server.rb'
13
+ require 'spork/diagnoser.rb'
14
+ require 'stringio'
15
+ require 'fileutils'
10
16
 
11
- Spork.each_run do
12
- # This code will be run each time you run your specs.
17
+ Spec::Runner.configure do |config|
18
+ config.before(:each) do
19
+ $test_stdout = StringIO.new
20
+ $test_stderr = StringIO.new
21
+ @current_dir = nil
22
+ end
13
23
 
14
- end
24
+ config.after(:each) do
25
+ FileUtils.rm_rf(SPEC_TMP_DIR) if File.directory?(SPEC_TMP_DIR)
26
+
27
+ end
28
+
29
+ def create_file(filename, contents)
30
+ FileUtils.mkdir_p(SPEC_TMP_DIR) unless File.directory?(SPEC_TMP_DIR)
31
+
32
+ in_current_dir do
33
+ FileUtils.mkdir_p(File.dirname(filename))
34
+ File.open(filename, 'wb') { |f| f << contents }
35
+ end
36
+ end
37
+
38
+ def in_current_dir(&block)
39
+ Dir.chdir(current_dir, &block)
40
+ end
41
+
42
+ def current_dir
43
+ @current_dir ||= SPEC_TMP_DIR
44
+ end
45
+
46
+ def change_current_dir(sub_path)
47
+ @current_dir = File.expand_path(sub_path, SPEC_TMP_DIR)
48
+ end
49
+ end
15
50
 
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
51
 
52
+ module Spec
53
+ module Matchers
54
+ class IncludeAStringLike
55
+ def initialize(substring_or_regex)
56
+ case substring_or_regex
57
+ when String
58
+ @regex = Regexp.new(Regexp.escape(substring_or_regex))
59
+ when Regexp
60
+ @regex = substring_or_regex
61
+ else
62
+ raise ArgumentError, "don't know what to do with the #{substring_or_regex.class} you provided"
63
+ end
64
+ end
27
65
 
66
+ def matches?(list_of_strings)
67
+ @list_of_strings = list_of_strings
68
+ @list_of_strings.any? { |s| s =~ @regex }
69
+ end
70
+ def failure_message
71
+ "#{@list_of_strings.inspect} expected to include a string like #{@regex.inspect}"
72
+ end
73
+ def negative_failure_message
74
+ "#{@list_of_strings.inspect} expected to not include a string like #{@regex.inspect}, but did"
75
+ end
76
+ end
28
77
 
78
+ def include_a_string_like(substring_or_regex)
79
+ IncludeAStringLike.new(substring_or_regex)
80
+ end
81
+ end
82
+ end
29
83
 
30
- require 'rubygems'
31
- require 'spec'
84
+ module Spork::TestIOStreams
85
+ def self.included(klass)
86
+ klass.send(:extend, ::Spork::TestIOStreams::ClassMethods)
87
+ end
88
+
89
+ def stderr
90
+ self.class.stderr
91
+ end
32
92
 
33
- $LOAD_PATH.unshift(File.dirname(__FILE__))
34
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
35
- SPEC_TMP_DIR = File.dirname(__FILE__) + "/tmp"
36
- require 'spork'
37
- require 'spork/runner.rb'
38
- require 'spork/server.rb'
39
- require 'stringio'
40
-
41
- Spec::Runner.configure do |config|
42
- config.before(:each) do
43
- $test_stdout = StringIO.new
44
- end
93
+ def stdout
94
+ self.class.stdout
95
+ end
96
+
97
+ module ClassMethods
98
+ def stderr
99
+ $test_stderr
100
+ end
45
101
 
46
- config.after(:each) do
47
- FileUtils.rm_rf(SPEC_TMP_DIR)
102
+ def stdout
103
+ $test_stdout
104
+ end
105
+ end
48
106
  end
49
107
  end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Spork::AppFramework::Rails do
4
+ describe ".version" do
5
+ it "detects the current version of rails" do
6
+ create_file("config/environment.rb", "RAILS_GEM_VERSION = '2.1.0'")
7
+ in_current_dir do
8
+ Spork::AppFramework::Rails.new.version.should == "2.1.0"
9
+ end
10
+
11
+ create_file("config/environment.rb", 'RAILS_GEM_VERSION = "2.1.0"')
12
+ in_current_dir do
13
+ Spork::AppFramework::Rails.new.version.should == "2.1.0"
14
+ end
15
+
16
+ create_file("config/environment.rb", 'RAILS_GEM_VERSION = "> 2.1.0"')
17
+ in_current_dir do
18
+ Spork::AppFramework::Rails.new.version.should == "> 2.1.0"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Spork::AppFramework::Unknown do
4
+ it "requires bootstrapping" do
5
+ Spork::AppFramework::Unknown.new.bootstrap_required?.should == true
6
+ end
7
+
8
+ it "has no known entry point" do
9
+ Spork::AppFramework::Unknown.new.entry_point.should be_nil
10
+ end
11
+ end
12
+
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Spork::AppFramework do
4
+ describe ".detect_framework" do
5
+ it "detects when rails is installed and available" do
6
+ create_file("config/environment.rb", "RAILS_GEM_VERSION = '2.1.0'")
7
+ in_current_dir do
8
+ Spork::AppFramework.detect_framework.short_name.should == "Rails"
9
+ end
10
+ end
11
+
12
+ it "returns Unknown when no framework known detected" do
13
+ Spork::AppFramework.detect_framework.short_name.should == "Unknown"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,105 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+
4
+ describe Spork::Diagnoser do
5
+ after(:each) do
6
+ Spork::Diagnoser.remove_hook!
7
+ Spork::Diagnoser.loaded_files.clear
8
+ end
9
+
10
+ def run_simulation(directory, filename = nil, contents = nil, &block)
11
+ FileUtils.mkdir_p(directory)
12
+ Dir.chdir(directory) do
13
+ if filename
14
+ File.open(filename, 'wb') { |f| f << contents }
15
+ Spork::Diagnoser.install_hook!
16
+ require filename
17
+ end
18
+ yield if block_given?
19
+ end
20
+ end
21
+
22
+ it "installs it's hook and tells you when files have been loaded" do
23
+ run_simulation(SPEC_TMP_DIR, 'my_awesome_library_include.rb', '1 + 5')
24
+ Spork::Diagnoser.loaded_files.keys.should include_a_string_like('my_awesome_library_include')
25
+ end
26
+
27
+ it 'excludes files outside of Dir.pwd' do
28
+ run_simulation(SPEC_TMP_DIR + '/project_root', '../external_dependency.rb', '1 + 5')
29
+ Spork::Diagnoser.loaded_files.keys.should_not include_a_string_like('external_dependency')
30
+ end
31
+
32
+ it "excludes files outside of Dir.pwd but in ruby's include path" do
33
+ directory = SPEC_TMP_DIR + '/project_root'
34
+ external_dependency_dir = SPEC_TMP_DIR + '/external_dependency'
35
+ $: << external_dependency_dir
36
+ FileUtils.mkdir_p(directory)
37
+ FileUtils.mkdir_p(external_dependency_dir)
38
+ Dir.chdir(directory) do
39
+ File.open(external_dependency_dir + '/the_most_awesome_external_dependency_ever.rb', 'wb') { |f| f << 'funtimes = true' }
40
+ Spork::Diagnoser.install_hook!
41
+ require 'the_most_awesome_external_dependency_ever'
42
+ end
43
+
44
+ Spork::Diagnoser.loaded_files.keys.should_not include_a_string_like('the_most_awesome_external_dependency_ever')
45
+ $:.pop
46
+ end
47
+
48
+ it "expands files to their fully their fully qualified path" do
49
+ directory = SPEC_TMP_DIR + '/project_root'
50
+ lib_directory = directory + '/lib'
51
+ $: << lib_directory
52
+ FileUtils.mkdir_p(lib_directory)
53
+ Dir.chdir(directory) do
54
+ File.open(lib_directory + "/the_most_awesome_lib_file_ever.rb", "wb") { |f| f << "funtimes = true" }
55
+ Spork::Diagnoser.install_hook!
56
+ require 'the_most_awesome_lib_file_ever'
57
+ end
58
+
59
+ Spork::Diagnoser.loaded_files.keys.should include_a_string_like('lib/the_most_awesome_lib_file_ever')
60
+ $:.pop
61
+ end
62
+
63
+ it "can tell the difference between a folder in the project path and a file in an external path" do
64
+ directory = SPEC_TMP_DIR + '/project_root'
65
+ external_dependency_dir = SPEC_TMP_DIR + '/external_dependency'
66
+ $: << external_dependency_dir
67
+ FileUtils.mkdir_p(directory)
68
+ FileUtils.mkdir_p(external_dependency_dir)
69
+ Dir.chdir(directory) do
70
+ FileUtils.mkdir_p(directory + '/a_popular_folder_name')
71
+ File.open(external_dependency_dir + '/a_popular_folder_name.rb', 'wb') { |f| f << 'funtimes = true' }
72
+ Spork::Diagnoser.install_hook!
73
+ require 'a_popular_folder_name'
74
+ end
75
+
76
+ Spork::Diagnoser.loaded_files.keys.should_not include_a_string_like('a_popular_folder_name')
77
+ $:.pop
78
+ end
79
+
80
+ it "filters backtrace beyond the last line matching the entry point" do
81
+ Spork::Diagnoser.install_hook!("test_filter/environment.rb")
82
+ create_file("test_filter/environment.rb", "require 'test_filter/app.rb'")
83
+ create_file("test_filter/app.rb", "require 'test_filter/my_model.rb'")
84
+ create_file("test_filter/my_model.rb", "'my model here'")
85
+ in_current_dir do
86
+ require 'test_filter/environment.rb'
87
+ end
88
+ f = Spork::Diagnoser.loaded_files
89
+ f[f.keys.grep(/app.rb/).first].last.should include('test_filter/environment.rb')
90
+ f[f.keys.grep(/my_model.rb/).first].last.should include('test_filter/environment.rb')
91
+ f[f.keys.grep(/environment.rb/).first].should == []
92
+ end
93
+
94
+ describe ".output_results" do
95
+ it "outputs the results relative to the current directory" do
96
+ Spork::Diagnoser.loaded_files["/project_path/lib/file.rb"] = "/project_path/lib/parent_file.rb:35"
97
+ Dir.stub!(:pwd).and_return("/project_path")
98
+ out = StringIO.new
99
+ Spork::Diagnoser.output_results(out)
100
+ out.string.should =~ %r([^/]lib/file.rb)
101
+ out.string.should =~ %r([^/]lib/parent_file.rb)
102
+ out.string.should_not include("/project_path/")
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Spork::Forker do
4
+ describe ".new" do
5
+ it "runs a block in a fork" do
6
+ $var = "hello world"
7
+ Spork::Forker.new { $var = "booyah" }.result
8
+ $var.should == "hello world"
9
+ end
10
+ end
11
+
12
+ describe "#result" do
13
+ it "returns the result" do
14
+ Spork::Forker.new { "results" }.result.should == "results"
15
+ end
16
+ end
17
+
18
+ describe "#running?" do
19
+ it "reports when the fork is running" do
20
+ forker = Spork::Forker.new { sleep 0.1 }
21
+ forker.running?.should == true
22
+ forker.result
23
+ sleep 0.1
24
+ forker.running?.should == false
25
+ end
26
+ end
27
+
28
+ describe "#abort" do
29
+ it "aborts a fork and returns nil for the result" do
30
+ started_at = Time.now
31
+ ended_at = nil
32
+ forker = Spork::Forker.new { sleep 5 }
33
+ Thread.new do
34
+ forker.result.should == nil
35
+ ended_at = Time.now
36
+ end
37
+ sleep 0.5
38
+ forker.abort
39
+ sleep 0.1
40
+ (ended_at - started_at).should be_close(0.5, 0.1)
41
+ forker.running?.should == false
42
+ end
43
+ end
44
+ end
@@ -11,7 +11,7 @@ describe Spork::Runner do
11
11
 
12
12
  it "shows an error message if no matching server was found" do
13
13
  Spork::Runner.new(["argle_bargle"], @out, @err).run.should == false
14
- @out.string.should include(%("argle_bargle" didn't match a supported test framework))
14
+ @err.string.should include(%("argle_bargle" didn't match a supported test framework))
15
15
  end
16
16
 
17
17
  it "defaults to use rspec over cucumber" do
@@ -44,7 +44,7 @@ describe Spork::Runner do
44
44
  Spork::Server::RSpec.should_receive(:preload).and_return(true)
45
45
  Spork::Server::RSpec.should_receive(:run).and_return(true)
46
46
  Spork::Runner.new(['rspec'], @out, @err).run
47
- @out.string.should include("Using RSpec")
47
+ @err.string.should include("Using RSpec")
48
48
  end
49
49
 
50
50
  it "outputs a list of supported servers, along with supported asterisk" do
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Spork::Server::Cucumber do
4
+ before(:each) do
5
+ @server = Spork::Server::Cucumber.new
6
+ end
7
+
8
+ it "uses the CUCUMBER_PORT for it's default port" do
9
+ @server.port.should == Spork::Server::Cucumber::CUCUMBER_PORT
10
+ end
11
+
12
+ it "uses ENV['CUCUMBER_DRB'] as port if present" do
13
+ orig = ENV['CUCUMBER_DRB']
14
+ begin
15
+ ENV['CUCUMBER_DRB'] = "9000"
16
+ @server.port.should == 9000
17
+ ensure
18
+ ENV['CUCUMBER_DRB'] = orig
19
+ end
20
+ end
21
+
22
+ it "uses the CUCUMBER_HELPER_FILE for it's helper_file" do
23
+ @server.helper_file.should == Spork::Server::Cucumber::CUCUMBER_HELPER_FILE
24
+ end
25
+ end