Ruby Gem Testing Workflow - ruby

What is the standard for testing while creating ruby gem?
Do most people run something like guard, or write tests and trigger them manually from the command line?

In Ruby there isn't really a "standard", instead people usually use what works well for them. If you don't like things like guard and prefer to run the tests from the command line, run the tests from the command line.
When I'm coding I run my tests manually from the command-line, but I also have a cron job that will run the tests against the latest commits and email the results every night.

Related

Globbing doesn't work with Minitest - Only one file is run

I have placed all my specs in specs/*.rb.
However, when I run Minitest with ruby spec/**/*_spec.rb, only one file is run.
What gives?
This is not minitest specific, but Ruby. You are effectively running a ruby program which knows nothing about the program being run.
Ruby does not support running multiple files at once afaik, so if you want to get a similar result you could try something like:
for file in spec/**/*_spec.rb; do ruby $file; done
UPDATE: for what you want you should probably create a Rake task as described here
You can use the testrbl third party gem to run multiple Minitest files on the command line. You could also use the mtest bin from maxitest extensions.
Using a for loop in bash will incur overhead of loading your application/library for every test you pass it. If you have just ten tests, and you're testing a Rails app that takes 5 seconds to boot, that's over a minute of totally unnecessary load time.

how to run a simple file on heroku

say I've got my rails app on github and am deploying the github repo on heroku.
I've got a situation where I have a simple text file with bunch of words (it is in my github repo). I want to insert these words (using a simple ruby program) into a database. Instead of using the tap command, is it possible in heroku to just run my simple ruby program and insert the words into the database...or maybe just show them on the terminal?
maybe confusing but basically I want to know how to run simple ruby script from heroku command line?
With cedar, you can run bash:
heroku run bash
Put your ruby script in a bin directory and git push it to Heroku. Now you can execute a shell command in the heroku console.
For example, if your Ruby script is bin/foo.rb, you can run the following command in the Heroku console:
`ruby bin/foo.rb`
Note the use of backticks.
Since you're talking about a Rails app on Heroku, how about using rails runner:
heroku run bundle exec rails runner ./path/to/script.rb -a <your-app>
Have a look at the RailsGuides for rails runner for more details.
Alternatively, turn that script into a rake task if runner is not your cup of tea (eg, for recurring tasks).
cd /path/to/my/local/repository
heroku console
require 'my_word_importing_script'
Failing that, try a simple Sinatra application as importer.rb?
require 'sinatra'
require 'sequel'
configure do
// connect to the database with sequel
end
get '/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds' do
words = YAML.load(File.join(File.dirname(__FILE__), "my_list_of_words.yaml"))
words.each do |word|
// Your logic for inserting into the database with sequel
end
end
Hitting http://example.com/import/a-long-unguessable-url-fdsjklgfuiwfnjfkdsklfds in your browser would kick off the import. Handy for an external cron task.
You would also need a config.ru file in the repo:
require 'importer'
run Sinatra::Application
If you want to run arbitrary local Ruby files on Heroku, check out the blog post at
http://www.22ideastreet.com/debug/run-local-scripts-on-heroku
There are some things to watch out for (long run times, etc.) but it might be useful if you have a file that you haven't checked in that you want to test or run on a Heroku instance.

Is it possible to run a Ruby project via Rake?

I've got a Ruby project started with NetBeans, so the Rake file has been generated. Is there a way that I can run the project over the command line?
It runs fine when I use F6 through NetBeans, as does my automated test suite with Alt+F6. I'm essentially looking for something like...
$ rake run
Does this exist?
The goal of ruby programming is (generally) to either write a web application, or write a program that can be run from the command line.
For a web application a rake run option might be worthwhile, but really the most common web applicaition framework is Rails, and for rails, you can just run a dedicated webserver running your web app with script/server.
For a commandline program, just run whichever ruby file you have intended as the main file (the one with the code that runs at startup). Ruby doesn't have any of the difficulties that Java does (e.g. having a jar file with the right Main-class attribute, and getting the classpath right, etc...). So you don't really need a rake run target, because there's no complexity that needs to be hidden in the rakefile.
Although Ken's right, you can certainly make a rake task to run your program. In lib/tasks/project.rake:
namespace :project do
task :run do
call_your_code()
end
end
and then rake project:run will do what you want.

Rake vs. Thor for automation scripts?

I want to automate things like:
Creating a new Ruby on Rails application with pre-selected database, Git initialize it, create a Heroku project, commit all files, etc.
Upload all files in folder to another computer through SSH, but do not overwrite files.
Upgrade Ubuntu, install all basic packages through apt-get.
From what I understand, tools for this are Rake and Thor, however, which one should I use?
Rake seems to me more de-facto and popular. I have heard people recommending Thor.
How do these stand to each other in a rundown?
Rake and Thor serve different purposes.
Rake is a general build script tool that is project-specific. In other words, you put your rakefile into your project folder and in your project's source control, and you can create, build and do other automation tasks that are specific to your project in that rakefile. Rake requires a rakefile to run.
Thor is a general purpose command line scripting tool that makes it very easy to re-use scripts across many projects and to do project setup, etc., like you are suggesting. Thor allows you to "install" an executable script that you can call from anywhere on your system, similar to calling "ruby", "gem" or "rake" command lines. However, Thor's scripts are more suited to general purpose, cross-application automation because the Thor script does not rely on a file sitting in your project-specific folder. A Thor script is the entire script, packed and installed for re-use anywhere.
Based on your stated needs, you are better off using Thor because you will be able to install your script in one location and have it work anywhere on your system. You will not be bound to where a Rake file is sitting or anything like that.
By the way, Rails 3 uses Thor for pretty much everything that is not project specific. You still have a Rake file and you still run things like "rake db:migrate" or "rake test:units". Thor is used for things like "rails new ...", "rails server" and "rails generate ..." The use of Thor AND Rake in Rails 3 is the perfect illustration of where each of these tools is best suited.
For setting up Ubuntu chores, Chef might be a better option.
From their web site:
Chef is an open source systems integration framework, built to bring the benefits of server configuration management to your entire infrastructure.
It's written in Ruby and there are tons of Chef recipes/cookbooks. Chef will handle setting up Ubuntu and installing packages, servers, etc.
I don't know if you are working with virtual machines, but Vagrant will set up a virtual machine and then use Chef to configure it.
There is something important to mention here.
http://guides.rubyonrails.org/generators.html in its section 8 Application Templates.
You can execute git commands, select gems, capify project.
And you could also execute system commands to satisfy your last point: Upgrade Ubuntu, install all basic packages through apt-get.
I would go with puppet.
By the way, maybe vagrant is useful to you?

cucumber debugger not stopping

I'm trying to debug a simple BDD test using cucumber. In order to do that, I inserted a debugger statement where I would like to break the control flow. But it seems that cucumber ignores this statement.
I'm running the tests executing:
rake cucumber:wip
It's rather simple, so I think the code isn' t worth pasting here. Thanks in advance
You don't have the rails-debug gem installed and/or included.
Make sure rails-debug listed in your Gemfile
If it's still not being included, make sure you run cucumber like:
bundle exec cucumber ...

Resources