sunknudsen
sunknudsen

Reputation: 7250

Expect -re not failing on eof

I have the following expect script.

This is test.exp

#!/usr/bin/expect

# exp_internal 1
# log_file -noappend ~/expect.log

# Use `send_log` to print to log file

set timeout 30

set bold [exec tput bold]
set red [exec tput setaf 1]
set green [exec tput setaf 2]
set normal [exec tput sgr0]

proc test_label {value} {
  upvar bold bold
  upvar normal normal
  puts "Running ${bold}${value}${normal}…"
}
proc test_send {value} {
  sleep 0.1
  send "$value"
}
proc test_failed {} {
  upvar bold bold
  upvar red red
  upvar normal normal
  sleep 0.1
  puts "${bold}${red}Failed${normal}"
  exit 1
}
proc test_ok {{force_close false}} {
  upvar bold bold
  upvar green green
  upvar normal normal
  sleep 0.1
  puts "${bold}${green}OK${normal}"
  if {$force_close} {
    close
  }
}

expect_before {
  default {
    test_failed
  }
}

This is electrum.exp

#!/usr/bin/expect

source ./test.exp

test_label "Should create Electrum mnemonic"

spawn qr-backup.sh --create-electrum-mnemonic

expect {
  -re {Format USB flash drive \(y or n\)\?} {
    test_send "n\r"
  }
}

expect {
  -re {\[sudo\] password for pi:} {
    test_send "$env(password)\r"
  }
}

expect {
  -re {Creating Electrum mnemonic…}
}

expect {
  -re {([a-z]+ ?){24}} {
    test_ok true
  }
}

Why doesn’t script fail when last line returned by spawn qr-backup.sh --create-electrum-mnemonic is electrum: error: unrecognized arguments: --nbits 264?

Upvotes: 2

Views: 154

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

Note this from the expect man page:

expect_before [expect_args]

Unless overridden by a -i flag, expect_before patterns match against the spawn id defined at the time that the expect_before command was executed (not when its pattern is matched).

(emphasis mine)

No spawn id was active when the expect_before command was executed.

Upvotes: 1

sunknudsen
sunknudsen

Reputation: 7250

Figured it out!

Solved using eof statement.

expect {
  -re {([a-z]+ ?){24}} {
    test_ok true
  }
  eof {
    test_failed
  }
}

Upvotes: 2

Related Questions