Rake tasks won't run inside of my sinatra app - ruby

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

Related

Initializing an empty sinatra application

I am trying to create an empty Sinatra application with a config.ru file. Right now the only way I know how is to initialise it with cucumber but doing that creates extra files which I don't need including step definitions etc. When I run 'gem install Sinatra' in my working directory it says:
Successfully installed sinatra-1.4.5
Parsing documentation for sinatra-1.4.5
Done installing documentation for sinatra after 0 seconds
1 gem installed
Yet there is no config.ru file.
How do I initialise a Sinatra app so I have just the config.ru?
From the sinatra documentation
first create a file called app.rb in your working directory containing the following code:
require 'sinatra'
get '/' do
'Hello world!'
end
now create a file called config.ru with the following content:
require './app'
run Sinatra::Application
now run
rackup -p4567
to run the application

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.

Limit rakefile settings to specific enviorment

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

Ruby "no such file to load" error running spec from rake task

I have written tests using rspec. When I run default rake task by running
bundle exec rake spec
it gives following error, even though previously it used to work fine.
`require': no such file to load -- sinatra/base (LoadError)
but if I run tests using simple
ruby spec/humongous_spec.rb
It runs perfectly fine. I don't what is wrong with this.
Add this line to your Rakefile
require 'bundler/setup'
Bundler.require

Passenger Rack app 'cannot infer basepath'

I have a simple config.ru file for my Sinatra app.
require 'sinatra'
require 'app'
run Sinatra::Application
However, Passenger is failing with the error no such file to load -- app. I've tried using the 1.9 method require_relative but that now causes the error cannot infer basepath.
I'm currently using the very hacky require File.join(File.dirname(__FILE__), 'app' ), which is just horrible and I don't fancy doing that every time I want to require a file.
Is there any reason why Ruby isn't acting as usual?
In ruby 1.9.2 the current directory is no more in LOAD_PATH.
So what if your LOAD_PATH and add current_directory if is not made.

Resources