How to pass options to rspec when run via autorun? Or, have rspec read from stdin?

39 views
Skip to first unread message

Robb Shecter

unread,
Mar 18, 2016, 10:27:04 PM3/18/16
to rspec
I'd like to run RSpec with stdin instead of a file. And I want to control the formatter and other options. I'm finding this a little tricky, because:

  • I don't see an RSpec CLI option for reading from stdin.
  • Although RSpec can be run via ruby, ( $ cat example_spec.rb | ruby -rrspec/autorun ) I don't see how to pass options. I can only pass options via the standard configuration file lookup.
So: I can either use rspec, and set all the options I need but not read from stdin, or use ruby and read from stdin, but not be able to set options cleanly.

Is there a way to do this which I've missed? Thanks!

Robb Shecter

unread,
Mar 18, 2016, 10:45:45 PM3/18/16
to rspec
Ah hah, Ruby does support the - parameter, although it's not document in the -h output. Therefore, this works and satisfies my two requirements:


$ cat example_spec.rb | ruby -rrspec/autorun - --format json



I.e., I can run RSpec on standard input, while setting options.

Myron Marston

unread,
Mar 20, 2016, 10:50:16 PM3/20/16
to rs...@googlegroups.com
Thanks for letting us know what you figured out, Robb!

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/b57d8d09-cb80-486c-a624-162e0568b9ef%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Robb Shecter

unread,
Mar 28, 2016, 2:24:35 AM3/28/16
to rspec
On Sunday, March 20, 2016 at 7:50:16 PM UTC-7, Myron Marston wrote:
Thanks for letting us know what you figured out, Robb!

Sure, you're welcome. Could you tell me, if it's possible to do this purely using the rspec library; that is, without shelling out?

Here's my working code:

  def self.run_spec(source_code)
    stdin
, stdout, stderr = Open3.popen3('ruby -rrspec/autorun - --format json')
    stdin
.puts source_code
    stdin
.close
    output
= stdout.gets
   
raise "Error running rspec: #{stderr.gets}" if output.nil?


    JSON
.parse(output)
 
end


 

Robb Shecter

unread,
Mar 28, 2016, 3:36:15 AM3/28/16
to rspec
I'm having problems using RSpec::Core::Runner.run within a Ruby instance because new invocations don't seem to pass the stream options fully through. E.g.:

BaseFormatter(#<File:/tmp/887763046.output>)

start
(#<struct RSpec::Core::Notifications::StartNotification count=1, load_time=1.0752>), output is #<File:/tmp/887763046.output>

BaseFormatter(#<File:/tmp/3180149450.output>)

start
(#<struct RSpec::Core::Notifications::StartNotification count=2, load_time=1.089341>), output is #<File:/tmp/887763046.output (closed)>

rake aborted
!

IOError: closed stream



And that's after I've hacked a few places which do explicitly block changing the output stream after a first run.

Robb Shecter

unread,
Mar 28, 2016, 3:53:52 AM3/28/16
to rspec
I've succeeded in making it reentrant by this standard multi-threading technique of dup'ing these global objects:

      def initialize(options, configuration=RSpec.configuration, world=RSpec.world)                
       
@options       = options.dup                                                                
       
@configuration = configuration.dup                      
       
@world         = world.dup
     
end


Are you interested in this work toward reentrant-safety and thread-safety?
 

Reply all
Reply to author
Forward
0 new messages