Rspec-core: config.output_stream doesn't work with --format=documentation

Created on 24 Mar 2016  路  11Comments  路  Source: rspec/rspec-core

Add a spec_helper that sets config.output_stream as documented, with

RSpec.configure {|c| c.output_stream = File.open('saved_output', 'w') }

Run using:

rspec --format=documentation

The redirect doesn't work.

Bug Small

Most helpful comment

Thanks for the heas-up, @kzkn!
Tested, the above case works fine, the documentation-formatted output is saved to a file.

All 11 comments

Can you provide more details about what doesn't work in your case? The above works perfectly for me.

The file gets created, but nothing gets written to it. I'll attach a tiny project archive. It has a .rspec file, so you can just run

rspec spec/sample_spec.rb

rspecproblem.zip

Also (and this is not an rspec issue) it doesn't meet my needs anyhow because it doesn't redirect the other standard output from the tests - enabled for example by my tracing environment variables - this is kinda the point of saving the output for me. My gist does provide a version (using fork) that works for me.

For future reference that zip is exactly the kind of thing needed for us to hunt down problems, and what I asked for via twitter in the first place ;) In this case it's a load order issue, one I thought we'd fixed.

As a work around for now load the spec helper via your .rspec file.

I asked on twitter initially because I assumed that I was doing something stupid that you'd spot straight away, or that I'd stumbled on a problem you'd known about for ages and could quickly set me straight. I was a bit taken aback by your dismissive attitude at first, but on reflection I realised it should be easy to reproduce, so I did.

(While writing, I'm seeing big problems with Ruby 2.2.2 and class << self - changing my code to use singleton_class fixed it, but rspec still barfs if I stop in the debugger anywhere. Seen this? Perhaps I can repro it for you also).

Attached zip is still a correct repro, though you need to remove the included .rspec file to see it. (That's the work around that @JonRowe suggested.)

(For the class << self issue - nope doesn't ring any bells, repro appreciated though I do realise I'm 9 months late!)

I thought maybe if we got lucky #2243 would fix this but it didn't

I found a reason behind the bug: formatter on creating save current value of output_stream like this:

#<RSpec::Core::Formatters::DocumentationFormatter:0x007facd94f9358
  @example_group=nil,
  @group_level=0,
  @output=#<IO:<STDOUT>>>

After that any changes of output_stream in config will not change output of already created formatters. Also the bug may be reproduced without .rspec file, only by config:

RSpec.configure do |c| 
  c.formatter = :documentation
  c.output_stream = File.open('saved_output', 'w')
end

In this case saved_output will be empty in result.

I made a fix and will make pr soon.

is this issue already fixed by #2401 ?

Thanks for the heas-up, @kzkn!
Tested, the above case works fine, the documentation-formatted output is saved to a file.

Was this page helpful?
0 / 5 - 0 ratings