run some scripts or all scripts in ruby rake - ruby

How can I do the scenario below: (the purpose is to be able to run all selenium scripts I have or only run some scripts I want if I pass test files as arguments from terminal window with the below code and scenario:
if test files are passed in terminal window
then system will run below codes:
scripts = ENV[scripts].plit(',')
FileList[scripts].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
if test files are NOT passed in terminal window (it means I want to run all scripts I have in my test suite), then system will run below codes:
FileList['test*.rb'].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}

If I understand correctly, can you just use an if statement?
task :default do
if ENV[scripts]
scripts = ENV[scripts].split(',')
FileList[scripts].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
else
FileList['test*.rb'].each { |file|
system("ruby #{file} > #{directory_name}/#{file}.out")}
end
end

Related

Can I use guards to chef if a windows service is running?

I'm writing a chef recipe and on this I need to perform an operation (run a batch) only if a service is not working.
I use this snippet:
batch 'run commnad' do
cwd target_path + '/bin/win64'
code 'command to be executed'
not_if '::Win32::Service.exists?("Service name")'
end
But it does not seems to work. After seeing this question I changed the process using an if clause instead of the guard and it works fine:
if !::Win32::Service.exists?("Service name") then
batch 'Install zabbix agent' do
cwd target_path + '/bin/win64'
code 'command to be executed'
end
end
But this should not be, for what I understood, the right way to manage this, so I'm wondering: why is the guard not working properly?
Thanks,
Michele.
The way you wrote your not_if statement runs the command as a shell script.
The shell doesn't know Ruby code, so the whole command will fail.
Need to first:
require win32/service
In order to use not_if with Ruby code you should put it inside a block instead:
not_if { ::Win32::Service.exists?("Service name") }
See some more examples here (search for not_if on the page):
https://docs.chef.io/resource_common.html
Here is the working example (Chef 13)
require 'win32/service'
windows_service "jenkins" do
action [:stop, :disable]
only_if { ::Win32::Service.exists?("jenkins")}
end

How to run multiple ruby scripts simultaneously

I am trying to run multiple ruby scripts simultaneously on my mac, and I'm not having any luck. I can see the the ruby processes start up, but then they immediately stop. The script works fine as a single process, no errors. Here are some examples of things I've tried.
10.times do
system "nohup ruby program.rb \"arg1 arg2\" &"
end
10.times do
`nohup ruby program.rb \"arg1 arg2\" &`
end
10.times do
system "ruby program.rb \"arg1 arg2\""
end
Do you need to start it from ruby for any specific reason? Why don't you start it 10 times directly from bash? Like:
$ for i in seq 1 10; do nohup ruby foo.rb \&; done
Let me know..
nohup redirects its output to a file $HOME/nohup.out, unless it is explicitly redirected. You should redirect the output of each invocation to a different file.
Also, for the safe side, I would redirect stdin to /dev/null - just in case the called program reads from stdin.
10.times do |i|
system "nohup ruby program.rb 'arg1 arg2' </dev/null >#{ENV['HOME']}/nohup#{i}.out &"
end
BTW (and off topic): Are you sure, that you want to pass arg1 arg2 as a SINGLE argument to program.rb?
You can build a solution with fork, exec and wait of the module Process.
# start child processes
10.times { fork { exec(cmd) } }
# wait for child processes
10.times { |pid| Process.wait }
Or a bit longer to play around with (Tested with Ruby 1.8.7 on Ubuntu). Added rescue nil to suppress error when waiting.
10.times do |i|
fork do
ruby_cmd = "sleep(#{10-i});puts #{i}"
exec("ruby -e \"#{ruby_cmd}\"")
end
end
10.times { Process.wait rescue nil }
puts "Finished!"

Custom Rake copy file to current directory in Mac

Is it possible to make a rake command that copies a file from a Mac and saves it into current directory?
I've tried using cp commands but it doesn't work.
This is what I've tried:
namespace :generate do
desc "Generate empty html5 index"
task :index do
#cp Dir['~/.rake/templates/index.html'], '.'
# cp "~/.rake/templates/index.html ."
end
end
I've just found the answer, I had to use sh command to execute shell commands in rake. Reference here
require 'fileutils'
namespace :generate do
desc "Generate empty html5 index"
task :index do
sh %{ cp ~/.rake/templates/index.html . }
end
end

Run cucumber test from hooks file

Is there a way to start a scenario from inside of the hooks file. In the After hook I am grabbing the line of the scenario that failed and the file or feature that the scenario is in and formatted it so that I can run that line in the cmd and it will run just the failed scenario.
Example: features\homepage.feature:8 environment='http://stage.homepage.local/'
Now I need help running this in the After hook on a failed scenario
After() do |scenario|
if scenario.failed?
#code here w/ cucumber features\homepage.feature:8 environment='http://stage.homepage.local/'
end
end
Can this even be done?
We can run the scenario from hooks file. System is the keyword that used to execute the given command in the console.
Try the below code
After() do |scenario|
if scenario.failed?
run_cmd = "cucumber features\homepage.feature:8 environment='http://stage.homepage.local/'"
system run_cmd
end
end

How to print to stdout from system command in Rake?

I have the following Rakefile
desc "Runs tests"
namespace :test do
task :api do
`mocha`
end
end
When I run the command rake test:api, I don't get the nice output of dots that I would if I just ran the command mocha.
How do I print the output of a command real-time in a rake task?
You can just put the output:
puts `mocha`
The backticks ` are calling the command mocha and return the output of the command.
You may also use %x{}:
puts %x{mocha}
Or you use system:
system('mocha')
Or you store the output for later use in a variable:
output = `mocha`
puts output

Resources