Override "rspec" command to run tests - ruby

This seems simple but I can't find a reputable solution via Google or searching SO.
I'm using foreman with a Rails 4 app to load ENV via a .env file. To run my tests properly, I have to execute foreman run rspec [optional files].
This gets tedious and occasionally I forget the foreman run part. I'd like to override the rspec command for a single app so that:
rspec [files] => foreman start [files]
Looked at binstubs but I don't fully understand them and they don't look exactly like what I want.
I can create a bash script that does this, but now that's specific to my local machine instead of built into the app codebase.

I ended up creating a script in my ~/scripts folder (where I store my own scripts added to my PATH).
Here's what I have in ~/scripts/tester (with executable permissions):
#!/bin/bash
#==================================================
# Created: 2014-04-01 / Updated: 2014-04-01
# Desc: Shorten syntax to run tests properly.
#==================================================
# For developers who may not use bash at all...
# $0 = filename and path
# $1 = first arg...
cmd="foreman run rspec $1"
echo "#--------------------------------------------------"
echo "# EXECUTING: $0"
echo "# '$cmd'"
echo "#--------------------------------------------------"
$cmd
I can execute with tester in my rails app directory and it will run foreman run rspec which executes all tests or I can pass in specific files or wildcard-names and it will run the matched tests.
It outputs the file location so if I pass this script on to others they know what files being run so they can modify it...I do this because I've actually run into a situation where a new developer was Googling a "tester" script for rails wondering why he couldn't find it as part of core rails...this way newbies know exactly what's being run and where the file's located at.
Chose tester as the script name because it wouldn't clash with any other known commands AFAIK.
Sample input:
tester => foreman run rspec # all specs
tester spec/models => foreman run rspec spec/models # run all model specs

Related

Ruby Aruba testing can't find command in PATH-variable

I'm teaching myself the basics of how to make my own Ruby Gem using Bundler's guide. However when I get to setting up CLI tests with aruba/cucumber I keep running into a problem:
Command "co2domain" not found in PATH-variable "C:/.../PATH variables". (Aruba::LaunchError)
The only differences I made is to change some of the names of the example as I'd eventually like to build a gem that converts company names to their proper domains.
Here is my company.feature file:
Feature: Company
In order to portray Company
As a CLI
I want to be as objective as possible
Scenario: Positive number
When I run `co2domain portray --num 2`
Then the output should contain "positive"
Scenario: Negative number
When I run `co2domain portray --num -22`
Then the output should contain "negative"
This is my company.rb file:
module Co2domain
class Company
def self.portray(num)
if num > 0
"positive"
else
"negative"
end
end
end
end
As I am a beginner and the guide is for beginners I feel like I'm missing something small but important. Help appreciated.
The error you are seeing is equivalent to the sh: foodie: command not found error found in the guide you are using. The difference is you are using a windows machine, and the guide is using a *nix machine.
The cucumber tests are testing running a command from the shell, and the shell is not able to find the command to run. If you add the directory your program is in to your PATH, or move your program to a directory in your path, cucumber should be able to run it.

Rspec not found when running from shell script

I'm trying to write a shell script to execute my rspecs in a project, but the script gives the error rspec: not found.
This is the script...
#!/bin/sh
command rspec spec spec/pos_spec.rb
$SHELL
...outputs
/home/joebloggs/GitHub/repo/test: 2: /home/joebloggs/GitHub/repo/test: rspec: not found
The line rspec spec repo/pos_spec.rb works if I type it directly into the shell, so I'm not sure why it can't find rspec. How can I get this to work?
$ echo 'rspec spec/models/employers_spec.rb' > my_shell.sh
$ sh my_shell.sh
Output:
Finished in 4.48 seconds
1 example, 0 failures
Randomized with seed 9820
So I kinda got it to work, and thought I'd share.
If I double click on the sh file, it opens a terminal, but fails. And I determined that it was due to the PATH variable being incomplete. More specifically it was missing these entries...
/home/joebloggs/.rvm/gems/ruby-2.3.0/bin:
/home/joebloggs/.rvm/gems/ruby-2.3.0#global/bin:
/home/joebloggs/.rvm/rubies/ruby-2.3.0/bin:
However, if I then used the exact same terminal that failed the script because it couldn't find the rspec command, and then ran ./test.sh, it worked. I could also directly type rspec spec spec/pos_spec.rb and it would also work. So it just seems that starting the script from a double click changes the PATH variable momentarily?
Anyway, I wont mark this as the correct answer, as it doesn't solve the original problem.
TLDR: Don't double click the sh file, run it from terminal using ./script.sh.

How to run several tests in rspec

I run several tests via rspec. The names are test1.rb, test2.rb, and so on. How do I run all of them in a queue and not one by one in the linux console? I tried an
rspec -e"test"
variant, but the console says all examples were filtered out. Please help.
Before I proceed, please note that all specs filename should end with _spec.rb. In your case it should be: test1_spec.rb, test2_spec.rb, and so on. I'd encourage you to checkout RSpec documentation on command line usage. To run all specs which are inside ./spec directory. You can run:
$ rspec .
You can pass --order option to run all specs in random order: $ rspec --order rand. If you want to run particular spec files then you can pass path/to/your/file:
$ rspec spec/test1_spec.rb spec/test2_spec.rb
Please check -e or --example command option, which runs the specs with description passed as argument to -e option for more clarity.

Running `cucumber feature/test.feature` in ruby shows weird behaviour

#!/usr/bin/ruby
`cucumber feature/test.feature`
running the above code issue lots of cucumber feature/test.feature commands.. why ?
When i see processes list there are 30 to 50 processes running cucumber command
also ruby program never terminates
Try running your feature files from outside the 'features' folder. Think this will solve the issue.(tested using the command line)
User:project user$ ls
features
User:project user$cucumber example.feature
The first line instructs the shell to run myapp.rb, which is, AFAIU, this script itself. Namely, each execution of the script recursively runs itself again.
Try the following:
#!/usr/bin/ruby
`cucumber feature/test.feature`
Or, even better, directly from the CLI:
cucumber feature/test.feature
To run all the tests simply issue the cucumber command without args:
cucumber

Pass ruby script file to rails console

Is there a way to pass ruby file, foo.rb to rails console. Expected results would be after console starts rails environment to run file.
Or any other way which would allow me to execute file in rails environment, triggered from command prompt.
Actually, the simplest way is to run it with load inside the rails console
load './path/to/foo.rb'
You can use
bundle exec rails runner "eval(File.read 'your_script.rb')"
UPDATE:
What we also have been using a lot lately is to load the rails environment from within the script itself. Consider doit.rb:
#!/usr/bin/env ruby
require "/path/to/rails_app/config/environment"
# ... do your stuff
This also works if the script or the current working directory are not within the rails app's directory.
In the meantime, this solution has been supported.
rails r PATH_TO_RUBY_FILE
Much simpler now.
Consider creating a rake task.
For code that I need to create records or support migrations, for example, I often create a rake task like that from this answer. For example:
In lib/tasks/example.rake:
namespace :example do
desc "Sample description you'd see if you ran: 'rake --tasks' in the terminal"
task create_user: :environment do
User.create! first_name: "Foo", last_name: "Bar"
end
end
And then in the terminal run:
rake example:create_user
script/console --irb=pry < test.rb > test.log
simple, dirty, and block the process at the end, but it does the job exactly like I wanted.
Of these approaches mentioned earlier, none seemed clean and ideal like you would expect a standalone script to run (not get eval-ed or piped via < redirection), but finally this works perfect for me:
(for Rails 3)
Insert at the top of your script:
#!/usr/bin/env ruby
APP_PATH = File.expand_path(appdir = '/srv/staging/strat/fundmgr/config/application', __FILE__)
require File.expand_path(appdir + '/../boot', __FILE__)
require APP_PATH
# set Rails.env here if desired
Rails.application.require_environment!
# your code here...
Of course, set your own Rails app path in the APP_PATH line.
That way, I can avoid having to enter any interactive irb or rails c and can test my script.rb from the shell prompt, before eg. scheduling it in crontab.
It smoothly supports command-line parameters, too, and minimizes the levels of wrappers before getting to your code.
CREDIT (also shows a Rails 2 example)
http://zerowidth.com/2011/03/18/standalone-script-runner-bin-scripts-in-rails.html
Here's the hack I'm using:
rr() {
rails_root="$(bundle exec rails runner "puts Rails.root")"
rp="$(relpath "$1" "$rails_root")"
bundle exec rails runner "eval(File.read '$rp')"
}
relpath() {python -c "import os.path; print os.path.relpath('$1','${2:-$PWD}')";}
Example:
cd ~/rails_project/app/helpers
rr my_script.rb
Based on #moritz's answer here. I changed it, since the working directory for File.read is the Rails project root.
I know this is some serious heresy, using python to help a ruby script. But I couldn't find a relpath method built into ruby.
Credit: relpath() was taken from #MestreLion, Convert absolute path into relative path given a current directory using Bash

Resources