How do I implement a custom buildr task defined externally? - ruby

I have a task which I'm trying to refactor into an external module so that I can later separate it from this project and use it for other projects.
When I try to run my task, I get an error:
Buildr aborted!
NoMethodError : undefined method `path_to' for nil:NilClass
Essentially it seems like the code for the project.task block is never called.
The code is as follows. Some of the code comes from the working code for the compile task, so I know that those bits are probably correct. Other parts come from documented examples for buildr, which from experience earlier today can be taken with a grain (or sometimes an entire lake) of salt. I can't figure out what I've done wrong, though.
module MacAppBundle
include Buildr::Extension
class MacAppBundleTask < Rake::Task
attr_accessor :app_name
def initialize(*args)
super
enhance do |task|
#TODO: #project is always nil here because associate_with is never called
app = #project.path_to("target/#{app_name}.app")
if File.exists?(app)
FileUtils.rm_rf(app)
end
Dir.mkdir(app)
#... omitting copying the rest of the stuff into the bundle ...
end
end
protected
def associate_with(project)
#project = project
end
end
before_define do |project|
mac_app_bundle = MacAppBundleTask.define_task('mac_app_bundle')
project.task 'mac_app_bundle' do |task|
#TODO: This code never executes. Why?
mac_app_bundle.send :associate_with, project
project.local_task('mac_app_bundle')
end
end
after_define do |project|
#TODO: This bit is definitely questionable because I can't find any documentation
# or working examples of similar code.
task('mac_app_bundle' => project.package(:jar))
end
def mac_app_bundle
task('mac_app_bundle')
end
end
class Buildr::Project
include MacAppBundle
end
#TODO: Find a place to move this. Seems weird to have to call it in global scope.
Project.local_task('mac_app_bundle') do |name|
puts "Creating Mac OS X app bundle for #{name}"
end

The correct way appears to be:
before_define do |project|
mac_app_bundle = MacAppBundleTask.define_task('mac_app_bundle')
mac_app_bundle.send :associate_with, project
project.task('mac_app_bundle')
end

Related

Extending Modules from a gem instead of monkey patching

Assuming that "clean" Ruby monkey patching isn't an option, and all gem extension methods need to be completely contained within their own namespace, and given a structure like
module ARubyGem
class GemClassOne
def method1
# instance method to be available when calling extended module
end
def self.method2
# singleton method to also be available in extended module
end
end
end
Then which of these is the better way to extend a gem if the original gem code has some singleton methods that begin with "self"?
module ARubyGemExtension
class GemClassOneExtension < ARubyGem::GemClassOne
def method_to_override
# new code here
end
end
end
vs.
module ARubyGemExtension
include ARubyGem
class GemClassOneExtension
def method_to_override
# new code here
end
end
end
Seems like you would need to go with:
module ARubyGemExtension
class GemClassOneExtension < ARubyGem::GemClassOne
def method_to_override
# new code here
end
end
end
(Remember to namespace GemClassOne.)
I don't know anything about the gem you're using, but unless it's specifically set up for providing methods through include, I think you may not get what you're after.

Database Cleaner not working in minitest rails

My Minitest controller tests are working fine if I run them alone using rake minitest:controllers but when I run rake minitest:all then I get validation failed error. It is because email is already used in model tests. I used DatabaseCleaner to clean the database but unable to clean database.
My code for database cleaner:
require "database_cleaner"
DatabaseCleaner.strategy = :transaction
class MiniTest::Rails::ActionController::TestCase
include Devise::TestHelpers
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
Short answer:
gem install "minitest-around"
Long answer:
before/after or setup/teardown in minitest are NOT hooks as in rspec, therefore you can't have multiple before/after or setup/teardown in minitest, since what they do is just redefining the method.
To solve this issue, you can use minitest-around, which adds support for multiple before/after or setup/teardown and around, simply add the gem to your test group:
# put in your Gemfile
gem 'minitest-around', group: :test
For setting up the database_cleaner, you can have it as you want, following is an example of the setup:
# tests/support/database_cleaner.rb
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
class Minitest::Rails::ActionController::TestCase
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
And in your test files:
# tests/your/test/file_test.rb
require 'support/database_cleaner'
# assertions here ...
That's it, see the Github for detailed info.
If for whatever reason you don't want to add the 'minitest-around' gem (to have more than one setup and teardown method), you can do this in your test_helper.rb...
require "database_cleaner"
DatabaseCleaner.strategy = :transaction
module AroundEachTest
def before_setup
super
DatabaseCleaner.start
end
def after_teardown
super
DatabaseCleaner.clean
end
end
class Minitest::Test
include AroundEachTest
end
I found my mistake ,may be it helps someone else ..
I should write DatabaseCleaner.start in setup of every model test where setup is defined, as i am overwriting setup method in every test file.
This is why I like Minitest; no fancy DSL to block thinking about how to use Ruby properly.
My setup is as follows:
In test_helper.rb
class MyTest < Minitest::Test
def setup
DatabaseCleaner.start
end
def teardown
DatabaseCleaner.clean
end
end
Then I just subclass this in any test that need database cleaning. Note the call super first cleans the db before any subclass-specific setup. The same call to super would need to be included in any subclass teardown method, but this can usually be omitted entirely.
class FooTest < MyTest
def setup
super
#foo = Foo.new(bar: 'whatever')
end
def test_save
#foo.save
assert_equal 1, Foo.count
end
end
If I need to subclass MyTest further (e.g. for integration tests) I include its own setup & teardown methods with calls to super so it goes right up the inheritance tree.
You can DRY up that repetition with this
DatabaseCleaner.strategy = :truncation
class MiniTest::Spec
before :each do
DatabaseCleaner.clean
end
end
This example subclasses the spec runner, but you can pick your test environment of choice.

Trouble w/ Rubymine and unit tests

DISCLAIMER: I am very new to ruby, still trying to get my feet wet, so this could be hugely stupid issue.
I'm trying to get a very simple project and one unit test working, and the Universe is throwing a LifeException (I just can't figure this out)
Using Rubymine 4.0.1 on Mac OS X, 10.7.3.
Launched RubyMine and created a new project (not Rails) "TestExample"
Created a new Ruby Class, file is my_class.rb.
class MyClass
def say_hi
puts "Hi!"
end
end
my = MyClass.new
my.say_hi
Create a new TestUnit Test Template, file is "my_test.rb"
require "test/unit"
class MyTest < Test::Unit::TestCase
def test_create
#my = MyClass.new
end
end
At this point I have two issues:
1. How do I 'require' my class in my tests? If I change the above test case to:
require "test/unit"
require "my_class"
class MyTest < Test::Unit::TestCase
def test_create
#my = MyClass.new
end
end
and attempt to run my "All tests in: TestExample" configuration, I get a "Exception message: cannot load such a file -- my_class". The Tests folder and working directory are pointed to the location of the files. (every file is in the same folder)
The other is a "Unable to attach test reporter to test framework".
I've googled and attempted to figure this out to no avail. I realize this is two questions in one, and if I could just get tests working, I'd be happy.
Thanks for any input and don't laugh to hard at my uber-ruby-noobness.
[Update] - This only happens when using the RVM: ruby-1-9.3-p125 SDK. If I use the ruby-1.8.7-p249(/usr/bin/ruby), it does work. This has to be some configuration issue.
For the first question, try:
require_relative 'my_class'
I can't answer the second.

How to monkeypatch Ruby properly?

I'm trying to monkeypatch a line in Net class in the standard library. I created a file called patches.rb into the lib folder of the project and added this
module Net
class HTTP < Protocol
module HTTPHeader
def initialize_http_header(initheader)
#header = {}
return unless initheader
initheader.each do |key, value|
#header[key.downcase] = [value.strip] rescue ""
end
end
end
end
end
But it doesn't work. Am I doing this right? (That parallels the inheritance hierarchy exactly.)
Edit: part of the problem was I had to put the file in the initalizers folder. But still seeing the same error.
Since things in the lib/ directory are only loaded on demand, you may have more success putting patches like this in config/initializers/ where they are automatically loaded after the stack has been initialized.
You can also collapse the definition for extensions to something like this:
module Net::HTTP::HTTPHeader
# ... (redefined methods) ...
end

Testing a rake task in rspec (and cucumber)

I'm new to Ruby, and I've been trying to learn Rake, RSpec, and Cucumber. I found some code that will help me test my Rake tasks, but I'm having trouble getting it to work. I was told here: http://blog.codahale.com/2007/12/20/rake-vs-rspec-fight/ to drop this:
def describe_rake_task(task_name, filename, &block)
require "rake"
describe "Rake task #{task_name}" do
attr_reader :task
before(:all) do
#rake = Rake::Application.new
Rake.application = #rake
load filename
#task = Rake::Task[task_name]
end
after(:all) do
Rake.application = nil
end
def invoke!
for action in task.instance_eval { #actions }
instance_eval(&action)
end
end
instance_eval(&block)
end
end
into my spec_helper.rb file.
I've managed to take this code out and run it in my cucumber steps like this:
When /^I run the update_installers task$/ do
#rake = Rake::Application.new
Rake.application = #rake
load "lib/tasks/rakefile.rb"
#task = Rake::Task["update_installers"]
for action in #task.instance_eval { #actions }
instance_eval(&action)
end
instance_eval(&block)
Rake.application = nil
end
but when I try to get things working in rspec, I get the following error.
ArgumentError in 'Rake task
install_grapevine should install to
the mygrapevine directory'
wrong number of arguments (1 for 2)
/spec/spec_helper.rb: 21:in instance_eval'
/spec/spec_helper.rb: 21:inblock in invoke!'
/spec/spec_helper.rb: 20:in each'
/spec/spec_helper.rb: 20:ininvoke!'
/spec/tasks/rakefile_spec.rb:12:in `block (2 levels) in
'
Unfortunately, I've got just under a week of ruby under by belt, so the metaprogramming stuff is over my head. Could anyone point me in the right direction?
This works for me: (Rails3/ Ruby 1.9.2)
When /^the system does it's automated tasks$/ do
require "rake"
#rake = Rake::Application.new
Rake.application = #rake
Rake.application.rake_require "tasks/cron"
Rake::Task.define_task(:environment)
#rake['cron'].invoke
end
Substitute your rake task name here and also note that your require may be "lib/tasks/cron" if you don't have the lib folder in your load path.
I agree that you should only do minimal work in the Rake task and push the rest to models for ease of testing. That being said I think it's important to ensure that the code is ACTUALLY run in my cron tasks during my integration tests so I think very mild testing of the rake tasks is justified.
Since testing rake is just too much for me, I tend to move this problem around. Whenever I find myself with a long rake task that I want to test, I create a module/class in lib/ and move all the code from the task there. This leaves the task to a single line of Ruby code, that delegates to something more testable (class, module, you name it). The only thing that remains untested is whether the rake task invokes the right line of code (and passes the right parameters), but I think that is OK.
It might be useful to tell us which is the 21nd line of your spec_helper.rb. But given that the approach you posted digs deep in rake (referring to its instance variables), I would entirely abandon it for what I suggested in the previous paragraph.
I've just spent a little while getting cucumber to run a rake task so I thought I'd share my approach. Note: This is using Ruby 2.0.0 and Rake 10.0.4, but I don't think the behaviour has changed since previous versions.
There are two parts to this. The first is easy: with a properly set up instance of Rake::Application then we can access tasks on it by calling #[] (eg rake['data:import']). Once we have a task we can run it by calling #invoke and passing in the arguments (eg rake['data:import'].invoke('path/to/my/file.csv').
The second part is more awkward: properly setting up an instance of Rake::Application to work with. Once we've done require 'rake' we have access to the Rake module. It already has an application instance, available from Rake.application, but it's not yet set up — it doesn't know about any of our rake tasks. It does, however, know where to find our Rakefile, assuming we've used one of the standard file names: rakefile, Rakefile, rakefile.rb or Rakefile.rb.
To load the rakefile we just need to call #load_rakefile on the application, but before we can do that we need to call #handle_options. The call to #handle_options populates options.rakelib with a default value. If options.rakelib is not set then the #load_rakefile method will blow up, as it expects options.rakelib to be enumerable.
Here's the helper I've ended up with:
module RakeHelper
def run_rake_task(task_name, *args)
rake_application[task_name].invoke(*args)
end
def rake_application
require 'rake'
#rake_application ||= Rake.application.tap do |app|
app.handle_options
app.load_rakefile
end
end
end
World(RakeHelper)
Pop that code into a file in features/support/ and then just use run_rake_task in your steps, eg:
When /^I import data from a CSV$/ do
run_rake_task 'data:import', 'path/to/my/file.csv'
end
The behavior might have changed since the correct answer was posted. I was experiencing problems executing two scenarios that needed to run the same rake task (only one was being executed despite me using .execute instead of .invoke). I thought to share my approach to solve the issue (Rails 4.2.5 and Ruby 2.3.0).
I tagged all the scenarios that require rake with #rake and I defined a hook to setup rake only once.
# hooks.rb
Before('#rake') do |scenario|
unless $rake
require 'rake'
Rake.application.rake_require "tasks/daily_digest"
# and require other tasks
Rake::Task.define_task(:environment)
$rake = Rake::Task
end
end
(Using a global variable is suggested here: https://github.com/cucumber/cucumber/wiki/Hooks#running-a-before-hook-only-once)
In the step definition I simply called $rake
# step definition
Then(/^the daily digest task is run$/) do
$rake['collector:daily_digest'].execute
end
Any feedback is welcome.

Resources