[HOOKS] spec_helper aware of full test suite being run? After hook to run code after running all spe

Sorry if multiple postings of this show up. I had to RTFM a few times
before I figured out how to post correctly.

I use VCR to record interactions, and when the recordings change
filenames,
and directory structure, over time, as the test suite evolves, the old
recordings, no longer used, are rarely pruned.

I would like to have the ability to run rspec with an option to check
for
unused VCR recordings, and another to both check and delete unused
recordings.

I need a way to know if the full spec suite is being run. I think this
can
be determined with some fancy testing on ARGV.

I can get the full list of recordings like this (or similar):

FileList[“spec/fixtures/vcr/**/*.yml”].reject{|file|File.directory?(file)}

And then prune the list as each recording is used by the spec suite,
afterwards having a list of the recordings that should be deleted.

I need a hook that will run after the complete spec suite is run, before
exiting the process, while still in the context of the thread, and the
variable containing my list of files to delete is still accessible.

Failing that I can write the list to a file, and hook in the deletion in
the
rake spec task. The downside of this is I currently run the test suite
with
bundle exec rspec spec, and don’t really use the rake spec task.

Aside: It would involve some (lots?) of work to get rake spec working
in
this project. Currently when I run rake spec there is no output at
all.
It appears to load the rails env, and do nothing. Any one have an idea
about that?

Update: I have fixed rake spec. Someone else on the team had removed
the gem dependency from the development group in the Gemfile. Once
added back rake spec worked fine.

Some additional data:
rspec 2.5.1
rspec-rails 2.5.1
rails 3.0.4
ruby 1.9.2p0
rake 0.8.7

I have gotten all of this working now with the exception of a way to
hook some code to be run after finishing all specs while still in the
same thread. When I run Rake::Task[“spec”].invoke within another rake
task the list of VCR cassettes that have been ‘touched’ by the tests
is not retained after the invoke method executes. I’ve tried storing
it in a class instance variable @@file_list, as well as a global ruby
var $file_list, with no dice.

I think the only option to persist the list of file is now to write
the file list to a yaml, and then pull it back out. :frowning:

On Thu, Mar 31, 2011 at 11:16 AM, Peter B.
[email protected]wrote:

I have implemented a full solution. Had to persist the list of unused VCR
cassettes to the file system.
Cheers!
It would be still be cool if there was an after hook on rspec, as there is
on capistrano!


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

http://relishapp.com/rspec/rspec-core/v/2-5/dir/hooks/before-and-after-hooks

I have implemented a full solution. Had to persist the list of unused
VCR
cassettes to the file system.
Cheers!
It would be still be cool if there was an after hook on rspec, as there
is
on capistrano!

Wow! That’s perfect, I think. I’ll have to play around with it and
make
sure the after suite can be made to only run code if the entire suite
has
been run, but it looks promising!

  • Peter

The before and after :suite hooks work for running code before and after
rspec runs all the tests specified, but I need it to explicitly not
run
when
anything less than every single spec is being run. In other words is we
are
running rspec spec or rake spec I need this before and after :suite
hooks to
run (to clean-up VCR cassettes). If we are not running the full suite,
then
it would mistakenly delete the VCR cassettes that were not used by the
spec
run.

So I need it to not run the before and after :suite code if running, for
example, rake spec:models or rspec spec/controllers. I am not sure if
rspec, or spec helper is aware of the full test suite being run. ARGV
won’t
work because when rake spec runs it executes rspec with every individual
spec’s path, so there is no way to tell from ARGV if it is the full
suite,
without getting really hacky.

On Fri, Apr 1, 2011 at 8:34 AM, Peter B. [email protected]
wrote:

rspec, or spec helper is aware of the full test suite being run. ARGV won’t
work because when rake spec runs it executes rspec with every individual
spec’s path, so there is no way to tell from ARGV if it is the full suite,
without getting really hacky.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

You could make your own task that depends on “spec”:

task :task_to_run_all_spec_and_do_something_with_vcr => :spec do

my VCR code

end

Obviously, this wouldn’t work with “rspec spec”