Limit rakefile settings to specific enviorment - heroku

I have a problem, my rake file looks like this:
require File.expand_path('../config/application', __FILE__)
require 'cucumber'
require "cucumber/rake/task"
Whois::Application.load_tasks
Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format pretty"
end
And the main reason of importing cucumber is to run tests with CI. But recently I've got a problem.
When I deployed to heroku and tried to run heroku run rake assets:precompile, I've got:
rake aborted!
no such file to load -- cucumber
As far as I understand, it can't find the cucumber that's only available in test env.
How can I tell rake, to use cure only in test env?

You'd need to wrap it in something like;
if Rails.env.test?
require 'cucumber'
require "cucumber/rake/task"
end

Related

Rake tasks won't run inside of my sinatra app

I have a sinatra app with a Rakefile.rake and server.rb file. In the server.rb file I have
get '/' do
rake :test
end
the app loads up but crashes when I load localhost:4567/ and says undefined method 'rake' for sinatra application. I try and require the file by using
require '/home/user/project/Rakefile'
and I also try Rakefile.rake but both give me an error that reads "require cannot load such file." I'm using Ubuntu.
I'm not sure why sinatra can't load the rakefile, but when I run rake test in terminal that works.
Any help would be great.
get '/' do
system 'rake test'
end

Trying to run migrations in Sinatra but can't load Sinatra app

I'm looking to run migrations for a Sinatra app called "sinatra_active_record_start" but can't get my settings right.
When I run bundle exec rake -T I get:
LoadError: cannot load such file -- sinatra_active_record_start
/Users/jasonnappy/ga_wdi/exisiting_resources/wdi_london/resources/materials/local/06-server-applications/ruby/sinatra/active-record/sinatra_active_record_start/Rakefile:1:in `require'
Same as when I run:
bundle exec rake db:create_migration first_migration
My Gemfile is:
source "https://rubygems.org"
gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem "rake"
gem "thin"
My Rakefile is:
require "sinatra_active_record_start"
require "sinatra/activerecord/rake"
namespace :db do
desc "Migrate the database"
task(:migrate => :environment) do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
end
end
The top of app.rb is:
require "bundler/setup"
require "sinatra"
require "activerecord"
require "sinatra/activerecord"
I know there are some redundancies, but at this point, I'm just trying to plug in and make something work that I found on the internet.
First, it doesn't look like you are requiring an adapter for your database. Adding one, like
gem "sqlite3"
to your Gemfile, should fix that.
Second, sinatra/activerecord creates migrations in a directory called "db/migrate" by default. That's where your migrations should live, not the root directory.
Move your migration there and remove
require "sinatra_active_record_start"
from your Rakefile. That's the code that is causing the immediate error. You shouldn't need to require each migration in the Rakefile.
Following these steps should make your migrations run, although you should rename the file to follow ActiveRecord convention. Run
rake db:create_migration NAME='sinatra_active_record_start'
to create a new one with a timestamp.
"Sinatra Active Record Starter Kit" is an example repo to help you get started.

How to Require a Custom Gem

I'm writing a gem that depends on another gem I've created.
In my host gem, I'm requiring my gem as a dependency like this:
$:.push File.expand_path('../lib', __FILE__)
Gem::Specification.new do |s|
s.require_path = "lib"
s.files = Dir["lib/**/*"]
s.test_files = Dir["spec/**/*"]
s.add_dependency "my_other_gem"
end
My gemfile looks like this:
source "http://rubygems.org"
gem 'my_other_gem' path: '../my_other_gem', require: 'my_other_gem'
gemspec
And inside the host gem, I've got a class that requires my_other_gem:
require 'my_other_gem'
In my_other_gem, inside lib/my_other_gem.rb, I've got two more require classes. So it looks like this:
require 'my_other_gem/foo'
require 'my_other_gem/bar'
When I spin up IRB in the host app and run require 'my_other_gem', I get this error
LoadError: cannot load such file -- my_other_gem
When I'm playing in the my_other_gem directory and I spin up IRB, the same require 'my_other_gem' command does not error out. Everything runs normally. But for some reason I can't require my_other_gem when I'm in my host gem.
What step am I missing?
How do you start irb? You need to run it in the bundler context with bundle exec.
I just tried and if I just run irb, I get the same error.
But if I run bundle exec irb, it works.

rake cucumber hook doesn't work

every one
I am new with cucumber & rake in ruby. I have completed a example of cucumber test. My test run ok in Ruby Mine by jetbrain (well, hook Before & After work well). When I try to using Cucumber rake task to start cucumber, the cucumber start and run well except the hook script. I found that both Before and After hook (and another hook) doesn't call when rake run the cucumber job
require 'cucumber'
require 'cucumber/rake/task'
require 'rubygems'
require 'rake'
require File.dirname(__FILE__) + "/features/support/selenum_drivers"
task :features => [:chrome_runner]
Cucumber::Rake::Task.new (:chrome_runner) do |t|
SeleniumCommon.configure do |config|
config.selenium_chrome = true
end
t.cucumber_opts = "config=chrome features -f pretty -f progress -r features/all_features -r features/step_definitions"
end
Here is my Rake
Any idea for this.
Thank you
You might try using the cucumber.yml for the cucumber_opts, and placing the other configuration in a support file.
Here a gist I use for javascript, chrome, headless and more.

How do I include gems in Rake files without specifying them in a Gemfile?

I am trying to write my first Rake file. It looks something like this:
lib\tasks\routes_check.rake:
require 'curb'
task :route_test do
puts "checking routes"
end
Running rake route_test fails with the message:
rake aborted!
no such file to load -- curb
I ran it with --trace and the trace is here.
I can't figure out why this does not work. In the same lib/tasks directory, I saved a .rb file which looks like:
task.rb:
require 'curb'
puts "Hello"
When I run this file with ruby task.rb, it works fine. What am I doing wrong here?
Rails applications by default use Bundler, so you need to add gem "curb" to your Gemfile and then ”install” (register) the new gem to Rails bundle with gem install.

Resources