I am generating a file in a temp directory.
If the file to be generated is different from an already existing file, then I wan't to update it, and run a command. If it's the same, then I can do nothing.
My code looks like this:
errstatus = 0
if FileUtils.identical?('/var/tmp/newfile', '/var/tmp/originalfile')
$stderr.puts "Files are the same, nothing to do"
else
$stderr.puts "Files are different, let's update them."
if FileUtils.cp_r '/var/tmp/newfile', '/var/tmp/originalfile'
$stderr.puts "File copied successfully."
if system('systemcommand1 here')
$stderr.puts "command ran OK"
if system('systemcommand2 here')
$stderr.puts "next command ran ok"
else
$stderr.puts "command 2 failed"
errstatus = 1
end
else
$stderr.puts "command 1 failed"
errstatus = 1
end
end
end
When I run it when the files are different, I get the output:
Files are different, let's update them.
It runs with FileUtils.cp_r without any errors, but it doesn't tell that the File was copied successfully, or run the system command. I have used the same syntax to evaluate FileUtils.identical?, but it does not work for FileUtils.cp_r.
Where am I going wrong here?
FileUtils::cp_r returns nil (which in Ruby is falsy) if the copying was successful, raises an error if not. It will never return a truthy value, so making it a condition does not make sense.
Since it doesn't have an else statement right now, simply remove the if before it. If you want error handling for your cp_r, you will need to wrap it into a begin..rescue block.
Maybe it's because the if FileUtils.cp_r '/var/tmp/newfile', '/var/tmp/originalfile' doesn't have an else clause so if it returns false or throws and exception it doesn't enter the if.
Related
execute 'install_insatller' do
cwd "abc"
command reg_cmd
ignore_failure true
log 'STDERROR'
only_if { ::File.exist?('abc') }
end
this is just an expample code
I want to print the log message only if the failure occurs else continue the installation.
ignore_failure true means that if there is an error, we show the usual error display but don't abort the converge. If you want behavior other than that, you'll probably have to write something yourself.
If you want to capture output and take decission based on that then please use Mixlib shellout instead of execute.
if ::File.exist?('abc')
require 'mixlib/shellout'
cmnd = Mixlib::ShellOut.new(reg_cmd)
cmnd.run_command
if cmnd.error?
puts cmnd.stderr
else
<write your code here to continue with installation>
end
end
I'm not terribly familiar with Ruby testing and my searches haven't yielded an answer to my specific question.
Currently, I have an app that raises StandardError to exit on certain conditions. The drawback to this is that the exit code is always 1.
I want to use exit() to provide unique codes to the exit conditions.
Currently, the project spec tests the StandardError mechanism as follows:
it "should raise an exception if no unignored project coverage file files were found" do
fixtures_project.ignore_list = ["*fixturesTests*", "*fixtures*"]
expect {fixtures_project.coverage_files}.to raise_error(StandardError)
end
I want to do something like assert($?.exit == 102), but I still need the fixtures_project.coverage_files to fire before hand in order to get the exit code.
I've tried variations of assert_equal(102, fixtures_project.coverage_files, "Hello there") inside the it/end with no luck. I'm sure this is probably simple Ruby, but I haven't grokked this bit yet.
You were right in trying to use $? object. Just call $?.exitstatus to get your exit status.
it "should raise an exception if no unignored project coverage file files were found" do
fixtures_project.ignore_list = ["*fixturesTests*", "*fixtures*"]
expect {fixtures_project.coverage_files}.to raise_error(StandardError)
expect($?.exitstatus).to eq(102)
end
I have a method which check if the file exists, If the file does not exist then it should call another method which prompts the questions. Below is the sample code.
def readFile1()
flag = false
begin
#ssh.exec!("cd #{##home_dir}")
puts "\nChecking if file exists on #{#hostname}\n"
if #ssh.exec!("sh -c '[ -f "#{file_name}" ]; echo $?'").to_i == 0
flag = true
puts "File exists on #{#hostname}"
display()
else
puts "File does not exist. Please answer following questions."
prompt()
end
rescue => e
puts "readFile1 failed... #{e}"
end
return exists
end
def prompt()
puts "\nDo you want to enter the new file location? [y/n]"
ans = gets.chomp
puts "New location is #{ans}"
end
When I am calling readFile method, if the file does not exists, it prints Do you want to enter the new file location? [y/n] and does not wait for the user to enter the value but immediately prints the rescue block and quits. Below is the Output if file does not exists.
Checking if file exists on LNXAPP
File does not exist. Please answer following questions.
Do you want to enter the new file location? [y/n]
readFile1 failed... No such file or directory - LNXAPP
I want the user to enter the values for the questions but it's not happening.Need help in fixing this.
Your code is not ideal.
You should check if the file exists via:
if File.exist? location_of_your_file_goes_here
The begin/rescue is then not required, because
you already check before whether the file exists.
Also two spaces should be better than one tab,
at least when you display on a site such as here.
Reason is simple - it makes your code easier
to read for others.
You also don't need the flag variable if I am
right - try to omit it and use solely File.exist?
there.
Also you wrote:
"When I am calling readFile method"
But you have no method called readFile().
Your method is called readFile1().
I know that you probably know this too, but
you must be very specific so that the ruby
parser understands precisely what you mean,
and that what you describe with words also
matches to the code you use.
Another issue I see with your code is that
you do this:
return exists
but what is "exists" here? A variable?
A method? It has not been defined elsewhere
in your code.
Try to make your code as simple and as logical
as possible.
I have a script which I start by a drag and drop of a file from Explorer onto a link on the Windows Desktop to my script, so the filename becomes the parameter of my script. This is handy for users who don't know how to start my script with a valid parameter. This part works perfectly.
But when I raise an error in the script, the errormessage is displayed but the console window closes without giving the user the time to read the message.
Putting a gets or a sleep at the end of the script or after the raise doesn't help.
How do I fix that please ?
if ARGV[0]
filename = ARGV[0]
else
raise "No filename given"
end
#some other code with wrong data
error = true
if error
raise "An error has occured, wrong data"
end
Add this around your entire program:
begin
all your code...
rescue
puts $!
system('pause')
end
On page 35, in the book "Exploring Expect", it says "The return command can be used to make a source command return. Otherwise, source returns only after executing the last command in the file."
I don't quite understand what it's getting at. I'd like to know what this means and how this is useful.
Thank you.
an example:
main script:
puts "about to source external file"
set some_var true
source something.exp
puts "done sourcing"
something.exp
puts "in something.exp"
if {$some_var} return
puts "you won't see this if some_var is true"
Basically, when the 'return' command is encountered, 'expect' will stop processing the script further, and return to the parent script (if any) that ran it using the 'source' command. It's much like 'return' in any programming language.