ruby at_exit exit status - ruby

Can i determine selves process exit status in at_exit block?
at_exit do
if this_process_status.success?
print 'Success'
else
print 'Failure'
end
end

using idea from tadman
at_exit do
if $!.nil? || ($!.is_a?(SystemExit) && $!.success?)
print 'success'
else
code = $!.is_a?(SystemExit) ? $!.status : 1
print "failure with code #{code}"
end
end
or without Perlisms:
require 'English'
at_exit do
if $ERROR_INFO.nil? || ($ERROR_INFO.is_a?(SystemExit) && $ERROR_INFO.success?)
print 'success'
else
code = $ERROR_INFO.is_a?(SystemExit) ? $ERROR_INFO.status : 1
print "failure with code #{code}"
end
end

Although the documentation on this is really thin, $! is set to be the last exception that occurs, and after an exit() call this is a SystemExit exception. Putting those two together you get this:
at_exit do
if ($!.success?)
print 'Success'
else
print 'Failure'
end
end

Related

Capybara / Ruby - Trying to return to the beginning of the loop when an error shows up

I'm trying to return to the loop beginnig when an error shows up as the code below.
I'm using the command "next" when a casual error occurs but it is not coming back for the loop beginning.
describe 'Test', :test do
before(:each) do
visit '/admin'
end
it 'Adding new images' do
image = 'barcelona.jpg'
#imagem = Dir.pwd + '/spec/fixtures/' + image
produto = '1'
100.times do
visit '/admin/' + produto
if page.has_no_css?('#mensagem > h1')
within_frame(:xpath, "//*[#id='app-content']/main/div/iframe") do
find('#ctl00_Conteudo_tbxNome_txtId').set 'test_name'
find('#ctl00_Conteudo_BtnSalvar').click
if page.has_no_css?('#mensagem > h1')
find('#ctl00_Conteudo_tbxIdArquivoControle_lnkInserirArquivo').click
attach_file('ctl00_Conteudo_tbxIdArquivoControle_tbxArquivo', #imagem)
find('#ctl00_Conteudo_tbxIdArquivoControle_btnEnviar').click
if page.has_no_css?('#mensagem > h1')
find('#skuTabNavigation a[href="#tabImages"]').click
expect(page).to have_content image
puts 'Test ok'
else
puts 'Error was presented, starting over..'
next
end
else
puts 'Error was presented, starting over..'
next
end
end
else
puts 'Error was presented, starting over..'
next
end
end
end
end
I would like that every time when the system goes to "else" condition, it restart the loop.
I don't think there is a direct way to move back to the initial iteration of a loop. redo exists but it only moves you back to the current iteration.
In this case, you probably want to change the way you're looping so you can more easily control when to start/stop. For example:
i = 0
while i <= 100 do
if page.has_no_css?('#mensagem > h1')
i = 0
puts 'Error'
next
end
i += 1
end
So you don't have to reset the loop index and call puts each time you could rescue an error:
class MyError < StandardError; end
i = 0
while i <= 100 do
begin
if page.has_no_css?('#mensagem > h1')
raise MyError, 'thing was missing'
end
puts i
i += 1
rescue MyError => boom
puts "Error: #{boom.message}"
i = 0
redo
end
end

Ruby - unexpected return (LocalJumpError)

Ruby 2.0
Why would the code below give unexpected return (LocalJumpError) ?
# some code here
puts "Scanning for xml files .."
zip_files = Dir.entries(directory).select { |f| File.extname(f) == '.zip' }
if(zip_files.count == 0)
puts "No files found, exiting..."
return
end
# more code here ( if files found)
Error: unexpected return (LocalJumpError)
No files found, exiting...
[Finished in 0.9s with exit code 1]
You're not in a method. You cannot return from there. If you want to exit early, use exit.
Alternatively, you could rescue the LocalJumpError
puts "Scanning for xml files .."
zip_files = Dir.entries(directory).select { |f| File.extname(f) == '.zip' }
begin
return unless zip_files.count > 0
# more code here ( if files found)
rescue LocalJumpError
puts "No files found, exiting..."
end

Continuously read from STDOUT and STDERR in real time

Say we have a small application (example.rb) which behave like the following ruby code:
#!/usr/bin/env ruby
done = false
out =Thread.new do
cnt = 0
while !done
STDOUT.puts "out #{cnt}"
cnt += 1
sleep 1
end
end
err = Thread.new do
cnt = 0
while !done
STDERR.puts "err #{cnt}"
cnt += 1
sleep 1
end
end
while true
i = STDIN.gets
if i == "q\n"
puts "Quiting"
done = true
break
end
end
out.join
err.join
exit 42
It print something to stdout and to stderr, it must be quited by writing "q\n" to stdin, and when it exit a value is returned in the return code.
Now, I would like to write a small ruby script which can run this program in an external process, where stdout and stdin are captured, and when the external process should be terminated this is done by writing "q\n" to its stdin. This program is called monitor.rb.
This is what I have tried here:
#!/usr/bin/env ruby
require 'open3'
class Monitor
#cmd
attr_accessor :return_code
def initialize cmd
#cmd = cmd
end
def run
#runner_thread = Thread.new do
Open3::popen3(#cmd) do |stdin, stdout, stderr, thread|
puts "#{Time.now} #{#cmd} is running as pid: #{thread.pid}"
stdin.sync = true;
stdout.sync = true;
stderr.sync = true;
#stdin = stdin
t_out = Thread.new do
stdout.readlines do |l|
puts "#{Time.now} STDOUT> #{l}"
end
end
t_err = Thread.new do
stderr.readlines do |l|
puts "#{Time.now} STDERR> #{l}"
end
end
thread.join
t_err.join
t_out.join
#return_code = thread.value
end
end
end
def quit
puts "Quiting"
#stdin.puts "q"
#stdin.close
#runner_thread.join
end
end
mon = Monitor.new "./example.rb"
mon.run
sleep 5
mon.quit
puts "Return code: #{mon.return_code}"
Question 1: What is wrong with my code since the output of the external process is not being printed?
Question 2: Can this be done in a more elegant way, and what would that look like?
The code must be able to run on Linux and portability is not a priority, I uses ruby 2.0.
When run example.rb in a terminal I get:
$ ./example.rb
out 0
err 0
out 1
err 1
out 2
err 2
q
Quiting
When I run the monitor application I get:
$ ./monitor.rb
2013-11-19 14:39:20 +0100 ./example.rb is running as pid: 7228
Quiting
Return code: pid 7228 exit 42
I expected the monitor.rb to print the output from example.rb
Try changing your t_out and t_err threads to use the following code. readlines will read the entire file at once and stdout and stderr will block until your script exits. I think this is why you were not getting any output.
while l = stdout.gets
puts "#{Time.now} STDOUT> #{l}"
end
This should print out to the screen as soon as any output is available.

How can I do readline arguments completion?

I have a Ruby app which uses readline with command completion.
After the first string (the command) was typed, I would like to be able to complete its arguments. The arguments list should be based on the chosen command.
Does someone have a quick example?
These are the commands:
COMMANDS = [
'collect', 'watch'
].sort
COLLECT = [
'stuff', 'otherstuff'
].sort
comp = proc do |s|
COMMANDS.grep( /^#{Regexp.escape(s)}/ )
end
Readline.completion_proc = comp
Each time I press TAB, the proc block is executed and a command from the COMMANDS array is matched.
After one of the commands was fully matched I would like to start searching for the argument only in the COLLECT array.
Since your question popped up first every time I looked for something like this I want to share my code for any one else.
#!/usr/bin/env ruby
require 'readline'
module Shell
PROMPT = "shell> "
module InputCompletor
CORE_WORDS = %w[ clear help show exit export]
SHOW_ARGS = %w[ list user ]
EXPORT_ARGS = %w[ file ]
COMPLETION_PROC = proc { |input|
case input
when /^(show|export) (.*)/
command = $1
receiver = $2
DISPATCH_TABLE[$1].call($2)
when /^(h|s|c|e.*)/
receiver = $1
CORE_WORDS.grep(/^#{Regexp.quote(receiver)}/)
when /^\s*$/
puts
CORE_WORDS.map{|d| print "#{d}\t"}
puts
print PROMPT
end
}
def self.show(receiver)
if SHOW_ARGS.grep(/^#{Regexp.quote(receiver)}/).length > 1
SHOW_ARGS.grep(/^#{Regexp.quote(receiver)}/)
elsif SHOW_ARGS.grep(/^#{Regexp.quote(receiver)}/).length == 1
"show #{SHOW_ARGS.grep(/^#{Regexp.quote(receiver)}/).join}"
end
end
def self.export(receiver)
if EXPORT_ARGS.grep(/^#{Regexp.quote(receiver)}/).length > 1
EXPORT_ARGS.grep(/^#{Regexp.quote(receiver)}/)
elsif EXPORT_ARGS.grep(/^#{Regexp.quote(receiver)}/).length == 1
"export #{EXPORT_ARGS.grep(/^#{Regexp.quote(receiver)}/).join}"
end
end
DISPATCH_TABLE = {'show' => lambda {|x| show(x)} ,
'export' => lambda {|x| export(x)}}
end
class CLI
Readline.completion_append_character = ' '
Readline.completer_word_break_characters = "\x00"
Readline.completion_proc = Shell::InputCompletor::COMPLETION_PROC
def initialize
while line = Readline.readline("#{PROMPT}",true)
Readline::HISTORY.pop if /^\s*$/ =~ line
begin
if Readline::HISTORY[-2] == line
Readline::HISTORY.pop
end
rescue IndexError
end
cmd = line.chomp
case cmd
when /^clear/
system('clear')
when /^help/
puts 'no help here'
when /show list/
puts 'nothing to show'
when /^show\s$/
puts 'missing args'
when /export file/
puts 'nothing to export'
when /^export\s$/
puts 'missing args'
when /^exit/
exit
end
end
end
end
end
Shell::CLI.new
After thinking a while, the solution was very simple:
comp = proc do |s|
if Readline.line_buffer =~ /^.* /
COLLECT.grep( /^#{Regexp.escape(s)}/ )
else
COMMANDS.grep( /^#{Regexp.escape(s)}/ )
end
end
Now I just need to turn it into something more flexible/usable.

Why can't I 'break' out of this Ruby block?

I have this Ruby block:
status = ''
build.parse do |entry|
puts "parsing an item"
puts entry.title
if entry.title =~ /FAILURE/ then
puts "failure"
status = "FAILURE"
else
status = "SUCCESS"
end
puts status
break entry if status == "FAILURE"
end
For some unknown reason to me I can't seem to break out of it? I realise the block is a little weird it's semi-copied from here:
http://macruby.labs.oreilly.com/ch03.html#_xml_parsing
Honestly my Ruby is poor but I'm trying to write a little mac app that involves some RSS parsing.
The regex matches and status gets set to "FAILURE" but it doesn't break out the block/loop. Am I doing something obviously wrong?
Cheers,
Adam
you dont need the 'then' in your if block
#entries = ["this is a FAILURE", "this is a success"]
status = ''
#entries.each do |entry|
if entry =~ /FAILURE/
puts "failure"
status = "failure"
else
status = "success"
end
puts "status == #{status}"
break if status == "failure"
end
as a side note, it would be more idiomatic to write this as:
status = #entries.any?{|e| e =~ /FAILURE/} ? 'failure' : 'succes'
when you are dealing with enumerable objects, like arrays it is nice to use the tools built into Ruby.
http://apidock.com/ruby/Enumerable/any%3F
Try break if status == "FAILURE"

Resources