How can I run my rake tests twice on purpose? - ruby

I need to run my rake tests twice in order to test the caching system of my Ruby gem. Before both the tests run, I need to clear my app's cache with
require 'lib/gem_name'
Gem.cache.clear
Then I just need to run the test task twice. I've tried putting the above code at the top of my Rakefile and listing the test files two times in my rake task, but I receive cannot require such file errors due to the gem lib paths not being loaded properly.
I need an efficient way to run the test twice, rather than running the cache-emptying code in IRB and then running rake test two times on the command line.
My Rakefile looks like this:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << 'test'
t.test_files = FileList['tests/test_*.rb']
t.loader = :testrb
end
desc 'Run gem tests'
task default: :test

You can either reenable the task like:
task :test do |task|
task.execute 1
task.execute 2
task.execute 3
end
which is superior to reenable since reenable would look like this:
task :test do |task|
task.invoke 1
task.reenable
task.invoke 2
task.reenable
task.invoke 3
end
You can read more about execute vs invoke here: https://blog.simplificator.com/2014/12/30/rake-execute-vs-invoke/

With the help of #maxple's comments and #OneNeptune's answer I found a way to achieve what I want. In my modified Rakefile below I set up a new task to clear the cache, and then a new test task which empties the cache and runs the tests twice.
require 'rake/testtask'
desc 'Clear gem cache'
task :clear_cache do
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'gem_name'
Gem.cache.clear
end
desc 'Perform tests exactly once'
Rake::TestTask.new(:run_tests) do |t|
t.libs << 'test'
t.test_files = FileList['tests/test_*.rb']
t.loader = :testrb
end
desc 'Performs test setup and runs tests twice'
task :test do
Rake::Task['clear_cache'].execute
Rake::Task['run_tests'].execute
Rake::Task['run_tests'].execute
end
task default: :test

Related

Guard warning "warning: loading in progress, circular require considered harmful"

I have a simple Ruby test envinvorment set up with:
minitest, guard, guard-minitest, and terminal-notifier-guard.
I'm using the following Rakefile so my tests are run by default because that's what Travis CI does by default.
require 'rake/testtask'
task :default => [:test]
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = "test/test_*"
end
The tests do run and pass but I get multiple screens worth of warnings. I found an answer and another answer.
But it seems like those solutions are specific to rails and rspec.
Why am I getting these warnings?
You can find the full project on GitHub and the full error output in this gist
If you just want to turn off the warnings, you can do so in the rake test task setup:
require 'rake/testtask'
task :default => [:test]
Rake::TestTask.new do |t|
t.libs << 'test'
t.pattern = "test/test_*"
t.warning = false
end

How do you run all the project tests in one go in ruby?

I have all my tests in /test in a ruby project and I need a way to run them all with one command. I came up with this:
task :default => [:test]
task :test do
ruby "test/test_1.rb"
ruby "test/test_2.rb"
end
Which if fine but it runs every single test one at a time rather than showing the overall number of failed vs passed tests.
For the record I am using the native testing ruby framework (eg. Test::Unit::TestCase)
Anyone can help me improve this poor state of affairs?
Thanks
This would do:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.verbose = true
t.test_files = FileList['test/**/test_*.rb']
end

running rake task with rufus-scheduler and sinatra -- it only runs one time instead of the desired interval

i have a rakefile in the same folder as my app.rb file, I can successfully call the rakefile task once, but it doesnt run the task more than once. It should run every 3 seconds.
require 'sinatra'
require 'rufus/scheduler'
require 'rake'
class MySchedule < Sinatra::Base
scheduler = Rufus::Scheduler.new
rake = Rake::Application.new
Rake.application = rake
rake.init
rake.load_rakefile
scheduler.every '3s' do
rake[:first_test].invoke
end
end
MySchedule.new
solved, was able to successfully call using system 'rake first_test'
source: Rake tasks won't run inside of my sinatra app

Set default rake task outside of Rakefile

Is it possible to set the default rake task from outside of the Rakefile. The standard behaviour is to have the following inside your Rakefile:
task :default => :my_awesome_task
This fails when I move it to a different file and then require the file in the Rakefile, because it cannot evaluate what task is...
Any ideas?
EDIT:
Rakefile
require 'rake_tasks'
rake_tasks.rb
require 'rspec/core/rake_task'
task :default => :spec
RSpec::Core::RakeTask.new(:spec) do |task|
task.exclude_pattern = 'spec/integration/**/*_spec.rb'
end

Why is this Albacore task, defined in a class, not working?

I put this class together, but it is not working to actually run a build. I inserted some other logging and no errors are being raised. I declare myself as a noob on Ruby so hoping some lovely Ruby Expert can spot any idiotic error I have!
require 'rubygems'
require 'albacore'
require 'albacore/support/albacore_helper'
require 'rake'
require 'rake/tasklib'
class Build_Assembly
def build(build_properties)
puts 'doing an assembly build'
msbuild :compile do |msb|
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
msb.execute
end
end
end
I have also tried using :build rather than :compile.
I have seen albacore working and realise the power of it, just need to get my skills a little honed hopefully
the msbuild method that you are calling is never executing because Albacore is a suite of rake tasks, and msbuild is one of those tasks that wants to be executed by rake, not directly in another method, though it can be done.
Calling msb.execute inside of the do |msb| ... end block won't execute the task, because this block is not evaluated until the rake task itself is executed.
you have a few of options for getting this to work. option #1 is the recommended and intended use of Albacore. I strongly recommend using rake and Albacore as they were meant to be used, so that you don't run into problems in the future. option #2 and #3 will work right now, but changes to the API of rake or Albacore can break these without notice. of course, you can use them however you want or need.
1. turn this into a rake script instead of a class and method
# rakefile.rb
require 'albacore'
task :default => [:compile]
msbuild :compile do |msb|
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
end
and then run this via a rake, by calling rake from the command line, in the same folder as the rakefile.rb
2. use Task[:compile].execute to execute the task within your method.
since the msbuild call is a rake task and not a standard method that executes it's code immediately, you have to manually execute the task that is created behind the scenes. this will make your existing code work, but it's not really the recommended way of working with rake tasks.
require 'rubygems'
require 'albacore'
require 'albacore/support/albacore_helper'
require 'rake'
require 'rake/tasklib'
class Build_Assembly
def build(build_properties)
puts 'doing an assembly build'
msbuild :compile do |msb|
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
end
Task[:compile].execute
end
end
3. use the MSBuild class directly, instead of the msbuild rake task
if you really do need to call msbuild from within a method, then you should avoid using the msbuild task and call the MSBuild class directly. this let's you work directly with the code that you want... no more going through rake to call something that you can call directly
require 'rubygems'
require 'albacore'
require 'albacore/support/albacore_helper'
require 'rake'
require 'rake/tasklib'
class Build_Assembly
def build(build_properties)
puts 'doing an assembly build'
msb = MSBuild.new
puts 'running build'
msb.properties :configuration => :Debug
msb.targets :Clean, :Build
msb.solution = build_properties.solution_file_location
msb.execute
end
end
this is the only scenario in which you need to call .execute, manually.

Resources