When will the examples.txt file will be created in Rspec?

Viewed 207

I wanted to copy examples.txt file into another examples_1.txt file so that on re-run i can have another copy while examples.txt file gets over-ridden.

Problem: I have my code like this. I copied the example.txt file in after(:suite) section but I'm getting error.

enter image description here

Im getting this error. When I checked, it was due to examples.txt file was not even present. Any idea on when examples.txt file will be created so that i can copy them once the file is created.

  No such file or directory @ rb_sysopen - reports/examples.txt
# ./spec/spec_helper.rb:94:in `block in <top (required)>'
# ./spec/spec_helper.rb:89:in `<top (required)>'
1 Answers

This is where it happens: https://github.com/rspec/rspec-core/blob/e7c5d030966a7e8dad3e0a67c61920c4f2437c15/lib/rspec/core/runner.rb#L90

      # Configures and runs a spec suite.
      #
      # @param err [IO] error stream
      # @param out [IO] output stream
      def run(err, out)
        setup(err, out)
        return @configuration.reporter.exit_early(@configuration.failure_exit_code) if RSpec.world.wants_to_quit

        run_specs(@world.ordered_example_groups).tap do
          persist_example_statuses
        end
      end

If you explore run_specs in the same file (https://github.com/rspec/rspec-core/blob/e7c5d030966a7e8dad3e0a67c61920c4f2437c15/lib/rspec/core/runner.rb#L113) you'll see the hooks are already done before this method returns and the examples.txt is persisted. And no one anticipated your case, so no "after_persistence_file_saved" hook is there for you to use.

Some possible options would be:

  • monkey patch def persist_example_status method and do the extra copying (hacky, comes with all problems of monkeypatching), or
  • run rspec ... && cp reports/examples.txt reports.examples_1.txt (much simpler, less hacky, makes very little assumptions).
  • open a PR to rspec-core introducing a hook that you need (I don't think it will be likely to be accepted since much simpler solution exists)

Each has its pros and cons, but those seem to be out of the scope of this question.

Related