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

#!/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

Related

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.

Override "rspec" command to run tests

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

Ensuring Programs Run In Ordered Sequence

This is my situation:
I want to run Python scripts sequentially in sequence, starting with scriptA.py. When scriptA.py finishes, scriptB.py should run, followed by scriptC.py. After these scripts have run in order, I need to run an rsync command.
I plan to create bash script like this:
#!/bin/sh
python scriptA.py
python scriptB.py
python scriptC.py
rsync blablabla
Is this the best solution for perfomance and stability ?
To run a command only after the previous command has completed successfully, you can use a logical AND:
python scriptA.py && python scriptB.py && python scriptC.py && rsync blablabla
Because the whole statement will be true only if all are true, bash "short-circuits" and only starts the next statement when the preceding one has completed successfully; if one fails, it stops and doesn't start the next command.
Is that the behavior you're looking for?
If you have some experience with python it will almost certainly be better to write a python script that imports and executes the relevant functions from the other script. That way you will be able to use pythons exceptions handling. Also you can run the rsync from within python.

Calling Rspec with syntax like ruby -I

I am trying to use https://github.com/rifraf/Vendorize which is run using a command like
D:\projects\SomeLibrary\lib>ruby -I..\..\Vendorize\lib -rvendorize some_lib.rb
It does something clever where it intercepts required files and logs them, but only the ones that get executed in your command line. On it's documentation pages it says
You can run the program several times with different options if the
required files depend on the options.
Or just run your tests…
I want to run all the tests with the -I function from the command line above, so that all the different avenues of code are run, and the libraries loaded (and logged). Given that I can run them like:
D:\projects\SomeLibrary\lib>rspec ..\spec\some_spec.rb
How do I do this? Thanks!
NB: I am a/ a ruby newbie and b/ running windows
I would try writing something like this at the top of some_spec.rb:
require_relative '..\..\Vendorize\lib\vendorize'
You might need to change that a bit depending on what your working directory is.
Then just runs your specs with rspec as you normally do without any extra commands.
If that doesn't work, then locate the rspec.rb executable and run:
ruby -I..\..\Vendorize\lib -rvendorize path/to/rspec.rb ..\spec\some_spec.rb

How can I make RSpec output to console when run as a command %x[rspec] from Ruby script?

I have a class with an instance method that runs RSpec using the %x[] notation:
class TestRunner
def run_rspec
# do stuff
%x[rspec spec -c -f documentation]
# do more stuff
end
end
When I do this:
> tr = TestRunner.new
> tr.run_rspec
The documentation (group and example names) does not appear in the console.
To contrast, when I run rspec straight from the command line I get this:
$ rspec spec -c -f documentation
a group name
an example
another example
...
I don't want to do this:
puts %x[rspec spec -c -f documentation
Because then the output all spits out in one huge clump at the very end. I want it to run in "real time," with each example showing up as each test is run.
Is there a way, with the setup I have, to get RSpec to announce what it's doing, as it's doing it (as it does when run normally from the command line)?
I've been advised that system() and the other shell methods can be dangerous to use, so I've opted to switch to the even-better approach of using RSpec itself:
RSpec::Core::Runner.run(['spec', '-c', '-f', 'documentation'])
rather than calling it via shell from my Ruby script.
Ruby offers several options for running programs from the command line. I was using %x[], the wrong choice for my use case.
Solution: Use system(), not %x[] -- rspec will write to STDOUT in real-time when I call it with system('rspec spec').
Some background in case it's helpful to anyone who stumbles upon this question:
Consider the differences between Ruby's command-line options:
%x[command] accumulates the result of command and returns it, in one chunk.
exec('command') will output command as command runs, but will replace whatever process called it -- i.e., if you use exec in your Ruby script, your Ruby script won't finish.
system('command') executes command in a subshell, and returns to the calling script.
This is why system was the choice for my script.

Resources