rspec-console 0.5.2 → 0.6

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f24189139498af2e2f3711517f59f961655da36
4
- data.tar.gz: 235a18e64431545763550f4c245051189db128f6
3
+ metadata.gz: 0e63502234311fe86e7f6313f2330e06727d401e
4
+ data.tar.gz: b39390aed9d22566014802cd0e2a29b1d99bf0c6
5
5
  SHA512:
6
- metadata.gz: beb4a616ecd2ff1775780a12e000949c800f06748f759c1f5dd19309e5e66c54324d77804307cb0f00c90af5b013e79c23505109c4175e9ba77f854ca1a003c2
7
- data.tar.gz: 40cd7a7d61f6dd1e52b42aa31addbc465f853f28a9ce5b9b7f831c2a9efc55cc93748122d2ae1804938f6f8832040ff5ff000a5a11356ff4bbb5f4dfc010c922
6
+ metadata.gz: 6cc98cfed58de7e6d9b7e56bb90a5a3ff3908a6f0022367cbab1a1934041395320237f28be5b90d5b33b130987dd3d5d661bad9c6cf96bd69d26e3a3ee028c6b
7
+ data.tar.gz: 548488ee091260bf4cd09c8a16c31ba6b3796d945da8f2a66cf24382cdb4c3b84b8c34dc724ef89cf0a9f54c1ecaf02572b8799cacf5407c75adcb1d42d33438
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
- RSpec Console
1
+ RSpec Console [![Build Status](https://travis-ci.org/nviennot/rspec-console.svg)](https://travis-ci.org/nviennot/rspec-console)
2
2
  =============
3
3
 
4
4
  RSpec Console allows you to run your RSpec tests in a Rails console.
5
5
  Best served chilled with [irb-config](https://github.com/nviennot/irb-config).
6
6
 
7
+ It is especially helpful when working with [jRuby](http://jruby.org/), because it will keep an active JVM running for you. This drastically reduces the feedback loop of doing TDD in jRuby -- and all without messing with nail-gun!
8
+
7
9
  ### Watch the screencast
8
10
 
9
11
  [![Watch the screencast!](https://s3.amazonaws.com/velvetpulse/screencasts/irb-config-screencast.jpg)](http://velvetpulse.com/2012/11/19/improve-your-ruby-workflow-by-integrating-vim-tmux-pry/)
@@ -60,10 +62,12 @@ Finished in 0.12654 seconds
60
62
  ~/prj/crowdtap/sniper (test) >
61
63
  ```
62
64
 
63
- TODO
64
- ----
65
+ Authors
66
+ -------
67
+
68
+ * [Alex Moore-Niemi](https://github.com/mooreniemi)
69
+ * Nicolas Viennot
65
70
 
66
- * Testing
67
71
 
68
72
  License
69
73
  -------
data/lib/rspec-console.rb CHANGED
@@ -1,40 +1,48 @@
1
1
  module RSpecConsole
2
- autoload :ConfigCache, 'rspec-console/config_cache'
3
- autoload :Runner, 'rspec-console/runner'
4
- autoload :Pry, 'rspec-console/pry'
2
+ autoload :ConfigCache, 'rspec-console/config_cache'
3
+ autoload :RSpecState, 'rspec-console/rspec_state'
4
+ autoload :Runner, 'rspec-console/runner'
5
+ autoload :Pry, 'rspec-console/pry'
5
6
 
6
- class << self; attr_accessor :hooks; end
7
- self.hooks = []
7
+ class << self; attr_accessor :before_run_callbacks; end
8
+ self.before_run_callbacks = []
8
9
 
9
10
  def self.run(*args)
10
11
  Runner.run(args)
11
12
  end
12
13
 
13
- def self.register_hook(&hook)
14
- self.hooks << hook
14
+ def self.before_run(&hook)
15
+ self.before_run_callbacks << hook
15
16
  end
16
17
 
17
18
  Pry.setup if defined?(::Pry)
18
19
 
19
20
  # We only want the test env
20
- register_hook do
21
- if defined?(Rails) && !Rails.env =~ /test/
22
- raise "Rails env must be set as test (use `rails console test` to launch the console)."
21
+ before_run do
22
+ if defined?(Rails) && !(Rails.env =~ /test/)
23
+ raise 'Please run in test mode (run `rails console test`).'
23
24
  end
24
25
  end
25
26
 
26
27
  # Emit warning when reload cannot be called, or call reload!
27
- register_hook do
28
+ before_run do
29
+ class String
30
+ def red
31
+ "\033[31m#{self}\033[0m"
32
+ end
33
+ end
28
34
  if defined?(Rails)
29
35
  if Rails.application.config.cache_classes
30
- STDERR.puts <<-MSG
31
- [WARNING]
32
- Rails's cache_classes must be turned off.
33
- Turn off in config/environments/test.rb:
36
+ STDERR.puts <<-MSG.gsub(/^ {10}/, '')
37
+ #{"[ WARNING ]".red }
38
+ Rails's cache_classes must be turned off.
39
+ Turn it off in config/environments/test.rb:
34
40
 
35
- Rails.application.configure do
36
- conig.cache_classes = false
37
- end
41
+ Rails.application.configure do
42
+ config.cache_classes = false
43
+ end
44
+
45
+ Otherwise, code relading does not work.
38
46
  MSG
39
47
  else
40
48
  ActionDispatch::Reloader.cleanup!
@@ -44,5 +52,5 @@ Turn off in config/environments/test.rb:
44
52
  end
45
53
 
46
54
  # Reloading FactoryGirl if necessary
47
- register_hook { FactoryGirl.reload if defined?(FactoryGirl) }
55
+ before_run { FactoryGirl.reload if defined?(FactoryGirl) }
48
56
  end
@@ -8,75 +8,104 @@ class RSpecConsole::ConfigCache
8
8
  # modules (FactoryGirl, Mocha,...) and we don't want to replace requires with
9
9
  # load all around the place.
10
10
  #
11
- # Instead, we cache whatever is done to RSpec.configuration during the
12
- # first invokration of require('spec_helper').
13
- # This is done by interposing the Proxy class on top of RSpec.configuration.
14
- #
15
- attr_accessor :proxy, :recorded_config, :shared_examples_groups
11
+ # Instead, we proxy and record whatever is done to RSpec.configuration during
12
+ # the first invocation of require('spec_helper'). This is done by interposing
13
+ # the RecordingProxy class on of RSpec.configuration.
14
+ attr_accessor :config_proxy, :shared_examples_copy
16
15
 
17
- def initialize
18
- ::RSpec.instance_eval do
19
- def self.configuration=(value)
20
- @configuration = value
16
+ class RecordingProxy < Struct.new(:target, :recorded_messages)
17
+ [:include, :extend].each do |method|
18
+ define_method(method) do |*args|
19
+ method_missing(method, *args)
21
20
  end
22
21
  end
22
+
23
+ def method_missing(method, *args, &block)
24
+ self.recorded_messages << [method, args, block]
25
+ self.target.send(method, *args, &block)
26
+ end
23
27
  end
24
28
 
25
- def cache
26
- if self.proxy
27
- # replay
28
- ::RSpec.configure do |config|
29
- self.recorded_config.each do |msg|
30
- config.send(msg[:method], *msg[:args], &msg[:block])
31
- end
32
- end
33
- ::RSpec.world.shared_example_groups.merge!(self.shared_examples_groups || {}) rescue nil
29
+ def record_configuration(&configuration_block)
30
+ ensure_configuration_setter!
34
31
 
35
- else
36
- # record
37
- real_config = ::RSpec.configuration
38
- self.recorded_config = []
39
- self.proxy = Proxy.new(self.recorded_config, real_config)
40
- ::RSpec.configuration = self.proxy
41
- yield
42
- ::RSpec.configuration = real_config
43
- self.shared_examples_groups = ::RSpec.world.shared_example_groups.dup rescue nil
44
-
45
- # rspec-rails/lib/rspec/rails/view_rendering.rb add methods on the
46
- # configuration singleton. Need advice to copy them without going down
47
- # the road with object2module.
48
- end
32
+ original_config = ::RSpec.configuration
33
+ ::RSpec.configuration = RecordingProxy.new(original_config, [])
34
+
35
+ configuration_block.call # spec helper is called during this yield, see #reset
36
+
37
+ self.config_proxy = ::RSpec.configuration
38
+ ::RSpec.configuration = original_config
49
39
 
50
- # Well, instead of copying them, we redirect them to the configuration
51
- # proxy. Looks like it good enough.
52
- proxy = self.proxy
53
- ::RSpec.configuration.singleton_class.send(:define_method, :method_missing) do |method, *args, &block|
54
- proxy.send(method, *args, &block)
40
+ stash_shared_examples
41
+
42
+ forward_rspec_config_singleton_to(self.config_proxy)
43
+ end
44
+
45
+ def replay_configuration
46
+ ::RSpec.configure do |config|
47
+ self.config_proxy.recorded_messages.each do |method, args, block|
48
+ # reporter caches config.output_stream which is not good as it
49
+ # prevents the runner to use a custom stdout.
50
+ next if method == :reporter
51
+ config.send(method, *args, &block)
52
+ end
55
53
  end
56
54
 
55
+ restore_shared_examples
56
+
57
+ forward_rspec_config_singleton_to(self.config_proxy)
57
58
  end
58
- end
59
59
 
60
- class Proxy < Struct.new(:output, :target)
61
- [:include, :extend].each do |method|
62
- define_method(method) do |*args|
63
- method_missing(method, *args)
60
+ def has_recorded_config?
61
+ !!self.config_proxy
62
+ end
63
+
64
+ def forward_rspec_config_singleton_to(config_proxy)
65
+ # an old version of rspec-rails/lib/rspec/rails/view_rendering.rb adds
66
+ # methods on the configuration singleton. This takes care of that.
67
+ ::RSpec.configuration.singleton_class
68
+ .send(:define_method, :method_missing, &config_proxy.method(:send))
69
+ end
70
+
71
+ def stash_shared_examples
72
+ if rspec2?
73
+ self.shared_examples_copy = ::RSpec.world.shared_example_groups.dup
74
+ else
75
+ self.shared_examples_copy = ::RSpec.world.shared_example_group_registry
76
+ .send(:shared_example_groups).dup
64
77
  end
78
+ rescue Exception => e
79
+ STDERR.puts "[rspec-console] WARN: #{e.class} #{e}"
65
80
  end
66
81
 
67
- def method_missing(method, *args, &block)
68
- self.output << {:method => method, :args => args, :block => block}
69
- self.target.send(method, *args, &block)
82
+ def restore_shared_examples
83
+ return if self.shared_examples_copy.nil?
84
+
85
+ if rspec2?
86
+ ::RSpec.world.shared_example_groups.merge!(self.shared_examples_copy)
87
+ else
88
+ self.shared_examples_copy.each do |context, name_blocks|
89
+ name_blocks.each do |name, block|
90
+ ::RSpec.world.shared_example_group_registry.add(context, name, &block)
91
+ end
92
+ end
93
+ end
94
+ rescue Exception => e
95
+ STDERR.puts "[rspec-console] WARN: #{e.class} #{e}"
70
96
  end
71
- end
72
97
 
73
- # For compatibility with Ruby 1.8.x outside of the Rails framework
74
- if RUBY_VERSION =~ /1.8/
75
- unless defined?(Rails)
76
- class Object
77
- def singleton_class
78
- class << self; self; end
98
+ def ensure_configuration_setter!
99
+ return if RSpec.respond_to?(:configuration=)
100
+
101
+ ::RSpec.instance_eval do
102
+ def self.configuration=(value)
103
+ @configuration = value
79
104
  end
80
105
  end
81
106
  end
107
+
108
+ def rspec2?
109
+ Gem.loaded_specs['rspec-core'].version < Gem::Version.new('3')
110
+ end
82
111
  end
@@ -1,30 +1,32 @@
1
- module RSpecConsole::Pry
2
- def self.setup
3
- ::Pry::CommandSet.new(&method(:rspec_command))
1
+ module RSpecConsole
2
+ module Pry
3
+ def self.setup
4
+ ::Pry::CommandSet.new(&method(:rspec_command))
4
5
  .tap { |cmd| ::Pry::Commands.import cmd }
5
- end
6
+ end
6
7
 
7
- def self.rspec_command(cmd)
8
- cmd.create_command "rspec", "Runs specs; to silence ActiveRecord output use SILENCE_AR=true" do
9
- group "Testing"
8
+ def self.rspec_command(cmd)
9
+ cmd.create_command "rspec", "Runs specs; to silence ActiveRecord output use SILENCE_AR=true" do
10
+ group "Testing"
10
11
 
11
- def process(*args)
12
- with_ar_silenced { RSpecConsole::Runner.run(args) }
13
- end
12
+ def process(*args)
13
+ with_ar_silenced { RSpecConsole::Runner.run(args) }
14
+ end
14
15
 
15
- def with_ar_silenced(&block)
16
- return block.call unless defined?(ActiveRecord) && ENV['SILENCE_AR']
17
- begin
18
- old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, nil
19
- block.call
20
- ensure
21
- ActiveRecord::Base.logger = old_logger
16
+ def with_ar_silenced(&block)
17
+ return block.call unless defined?(ActiveRecord) && ENV['SILENCE_AR']
18
+ begin
19
+ old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, nil
20
+ block.call
21
+ ensure
22
+ ActiveRecord::Base.logger = old_logger
23
+ end
22
24
  end
23
- end
24
25
 
25
- def complete(input)
26
- require 'bond'
27
- super + Bond::Rc.files(input.split(" ").last || '')
26
+ def complete(input)
27
+ require 'bond'
28
+ super + Bond::Rc.files(input.split(" ").last || '')
29
+ end
28
30
  end
29
31
  end
30
32
  end
@@ -0,0 +1,48 @@
1
+ module RSpecConsole
2
+ class RSpecState
3
+ class << self
4
+ def reset
5
+ require 'rspec/core'
6
+ raise 'Please use RSpec 2.10.0 or later' if obsolete_rspec?
7
+
8
+ ::RSpec::Core::Configuration.class_eval { define_method(:command) { 'rspec' } }
9
+ ::RSpec::Core::Runner.disable_autorun!
10
+ ::RSpec.reset
11
+
12
+ if config_cache.has_recorded_config?
13
+ config_cache.replay_configuration
14
+ else
15
+ config_cache.record_configuration(&rspec_configuration)
16
+ end
17
+ end
18
+
19
+ def rspec_configuration
20
+ proc do
21
+ ::RSpec.configure do |config|
22
+ config.color_enabled = true if config.respond_to?(:color_enabled=)
23
+ config.color = true if config.respond_to?(:color=)
24
+ end
25
+
26
+ $LOAD_PATH << './spec'
27
+ try_load('spec_helper')
28
+ try_load('rails_helper')
29
+ end
30
+ end
31
+
32
+ def try_load(file)
33
+ begin
34
+ require file
35
+ rescue LoadError
36
+ end
37
+ end
38
+
39
+ def config_cache
40
+ @config_cache ||= RSpecConsole::ConfigCache.new
41
+ end
42
+
43
+ def obsolete_rspec?
44
+ Gem.loaded_specs['rspec-core'].version < Gem::Version.new('2.10.0')
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,44 +1,16 @@
1
- class RSpecConsole::Runner
2
- class << self
3
- def reset(args)
4
- require 'rspec/core'
1
+ # This class wraps the core rspec runner and manages the environment around it.
2
+ module RSpecConsole
3
+ class Runner
4
+ class << self
5
+ def run(args, options={})
6
+ RSpecConsole.before_run_callbacks.each(&:call)
7
+ RSpecConsole::RSpecState.reset
5
8
 
6
- if Gem.loaded_specs['rspec-core'].version < Gem::Version.new('2.9.10')
7
- raise 'Please use RSpec 2.9.10 or later'
8
- end
9
-
10
- ::RSpec::Core::Runner.disable_autorun!
11
- ::RSpec::Core::Configuration.class_eval { define_method(:command) { 'rspec' } }
12
- ::RSpec.reset
13
-
14
- config_cache.cache do
15
- ::RSpec.configure do |config|
16
- config.output_stream = STDOUT
17
- config.color_enabled = true if config.respond_to?(:color_enabled=)
18
- config.color = true if config.respond_to?(:color=)
19
- end
20
-
21
- $LOAD_PATH << './spec'
22
- require "spec_helper"
23
- begin
24
- require "rails_helper"
25
- rescue LoadError
26
- end
27
- end
28
- end
9
+ stdout = options[:stdout] || $stdout
10
+ stderr = options[:stderr] || $stderr
29
11
 
30
- def run(args)
31
- RSpecConsole.hooks.each(&:call)
32
- reset(args)
33
- if defined?(::RSpec::Core::CommandLine)
34
- ::RSpec::Core::CommandLine.new(args).run(STDERR, STDOUT)
35
- else
36
- ::RSpec::Core::Runner.run(args, STDERR, STDOUT)
12
+ ::RSpec::Core::Runner.run(args, stderr, stdout)
37
13
  end
38
14
  end
39
-
40
- def config_cache
41
- @config_cache ||= RSpecConsole::ConfigCache.new
42
- end
43
15
  end
44
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: '0.6'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Viennot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-02 00:00:00.000000000 Z
11
+ date: 2015-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bond
@@ -25,7 +25,35 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
29
57
  requirement: !ruby/object:Gem::Requirement
30
58
  requirements:
31
59
  - - ">="
@@ -49,8 +77,8 @@ files:
49
77
  - lib/rspec-console.rb
50
78
  - lib/rspec-console/config_cache.rb
51
79
  - lib/rspec-console/pry.rb
80
+ - lib/rspec-console/rspec_state.rb
52
81
  - lib/rspec-console/runner.rb
53
- - lib/rspec-console/version.rb
54
82
  homepage: http://github.com/nviennot/rspec-console
55
83
  licenses:
56
84
  - MIT
@@ -1,3 +0,0 @@
1
- module RSpecConsole
2
- VERSION = '0.5.2'
3
- end