What's the usage of `expect_before` command in Expect? - expect

The doc says:
takes the same arguments as expect, however it returns immediately.
But what does it mean by "returns immediately" ?
What's the usage of this command could be ?

Image you have spawned a program that, say, randomly asks "Are you sure [yn]?"
Imagine this program has 100 questions that need to be answered.
You don't want to have to conditionally expect an "Are you sure" question for each of those 100 questions.
Expect lets you do:
spawn /some/annoying/program
expect_before {
"Are you sure \[yn]?" {
send "y\r"
exp_continue
}
}
expect "first question"
send "first answer\r"
# and so on.
Now you are covered: expect implicitly adds the expect_before code into each expect command.

Related

How to escape slot in IBM watson assistant?

I want to know how to escape while using slot
for example, there is a slot need entities #time, #date, #place
so user said "next sunday, in the market"
In this moment, not include #time. so slot asked "I need time information too"
but user doesn't answer about time, just input "main menu" because he want to go main node (welcome message)
In conclusion, I wanna make when user in slot, but input other intent, just quit slot(Ignore all data from slot) and go intent node.
How can I do that? It's seem to be impossible..
As #data_henrik suggests the way to handle this is through a digression. When you hit the "I don't know what time" or : "go back to the main menu" digression, you can perform operations to break out of the slot operation.
There are a number of ways to break of slots:
You can clear the dialog stack - <? clearDialogStack() ?> which will go back to the beginning.
You could set the required time field to a temporary value; set some handling variables; then handle the redirection in a post slots node, which checks for digression set variables.

Send ctrl-c to channel in ruby's Net::SSH gem

An older version of Net::SSH had #send_signal. Since that method no longer seems to be available, have tried sending "\cC" via #send_data, and also tried closing the channel, but the remote command continues running.
What's the right way to send signals to a Net::SSH::Channel now?
You want to use request_pty to create a pseudo-terminal. You can use that terminal to send an interrupt command.
Unless you have a PTY, you can't send control characters. This can be as easy as calling request_pty.
It took me a while to figure out, mostly as my system is a bit more complicated - I have multiple SSH sessions running and another thread needs to cause all channels to terminate, so my code looks something like this:
def do_funny_things_to_server host,user
Net::SSH.start(host, user) do |ssh|
#connections << ssh
ssh.open_channel do |chan|
chan.on_data do |ch, data|
puts data
end
# get a PTY with no echo back, this is mostly to hide the "^C" that
# is otherwise output when I interrupt the channel
chan.request_pty(modes: [ [ Net::SSH::Connection::Term::ECHO, 0] ])
chan.exec "tail -f /some/log/file"
end
ssh.loop(0.1)
#connections.delete ssh
end
end
def cancel_all
#connections.each do |ssh|
ssh.channels.each do |id,chan|
chan.send_data "\x03"
end
end
end
###Rant:
There's so little documentation about how to use request_pty parameters that it can be said to not exist. I had to figure out modes: by reading both the source of net-ssh and the SSH RFC and sprinkling some educated guesses about the meaning of the word "flags" in section 8 of the RFC.
Someone pointed in another relevant (though not Ruby specific) answer that there's a "signal" message that can be used to send signals over the SSH connection, but also that OpenSSH (the implementation I use for the server) does not support it. If your server supports it, you might want to try to use it like this:
channel.send_channel_request 'signal', :string, 'INT'
See "signal" channel message in the RFC and the buffer.rb source file to understand the parameters. Insert here the expected rant about complete lack of documentation of how to use send_channel_request. The above suggestion is mostly to document for myself how to use this method.
The answer linked above also mentions an SSH extension called "break" which is supposedly supported by OpenSSH, but I couldn't get it to work to interrupt a session.

Rspec STDIN gets

How can I use gets method inside a test?
I want to write an interactive spec, in my spec I login to a site, which asks confirmation sms. I don't know that sms code before I run spec, that's why I enter sms code during test run.
When I try to do something like sms = gets.chomp
I get the following error:
Errno::ENOENT:
No such file or directory - spec/login/login_spec.rb
In your spec you want to use $stdin. Your code should look something like this:
it "sends an SMS and verifies it" do
SMSVerifier.send_verification_code(test_phone_number)
print "Enter the sms code you received: "
code = $stdin.gets.chomp
SMSVerifier.check_verification_code(test_phone_number, code).should == true
end
As a matter of principle, rspec, and unit-tests in general should not be interactive. By actually sending an SMS in your test you need to:
Have the relevant phone at hand
Wait for the SMS to arrive (how long before you decide it fails?)
Read the SMS and fill it (correctly!) back in your running test.
This means that your spec is not automated, and that instead of running it tens of times a day (as unit-tests were meant to be run), you will run it once a week, if at all, because it would be such a pain to run.
Leave live SMS tests to system tests, and unit-test this functionality by stubbing the actual send behavior, and check the received parameters:
it "sends an SMS and verifies it" do
sent_text = nil
expect(SMSSender).to receive(:send).with(test_phone_number, an_instance_of(String)) do |num, text|
sent_text = text
end
SMSVerifier.send_verification_code(test_phone_number)
SMSVerifier.check_verification_code(test_phone_number, sent_text).should be_true
end

default timeout handler for expect script

I have a expect script that need to fail when certain any of the expect condition is not meet. For example:
expect "Hello World"
If the expect script does not find "Hello World" in certain amount of time, it should fail. According to expect manual, I can add a condition timeout in the expect, but I have many expect statements in the script and I don't want to add a timeout for all of them. Is there a better way to do it?
expect_after {
timeout {
puts "a default timeout clause for all subsequent expect commands"
}
}
Subsequent expect commands can still provide their own timeout clauses that will take precedence over the above.

How to open an email message using applescript?

I am writing a small applescript which retrieves all "unread" messages in the viewer and loops them.
I have two goals to complete:
I need to get the subject of each message and perform a regular expression to see if it's suitable for step 2 (ex: get emails with subject {.*})
I need to open each message on a separate window and after 4 seconds, I need to close that window and proceed with the next message
Do you know how to do these?
Thanks in advance.
The following applescript works for me, but I'm not sure how to do the regex matching. You can use the unix 'grep' function with applescript's 'do shell script' command, but I'm no expert in how to use grep properly. I'll leave that for someone else to answer.
on run
tell application "Mail"
set myInbox to mailbox "INBOX" of account 1
set myMessages to every message of myInbox
repeat with theMessage in myMessages
if read status of theMessage is false then
if my subjectIsInteresting(subject of theMessage) then
open theMessage
delay 4
close window 1
end if
end if
end repeat
end tell
end run
on subjectIsInteresting(subject)
-- do some regex magic here
return true -- for now
end subjectIsInteresting
For regexes -- If you're running the script on your own machine, or can distribute it bundled, you could use Satimage's Smile extension (http://www.satimage.fr/software/en/downloads/index.html) which adds regexes to Applescript.
I know you already have your answer but have you looked into Automator? For most standard scripts such as this, it can be less painful if you aren't too familiar with AppleScript. It's not very 'programmy' but it's quick and you'll spend less time debugging.

Resources