github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/includes/expect-that.sh (about)

     1  # expect_that the ability to work with interactive commands with expect tool.
     2  # The expect_script argument is a expect script body.
     3  # The default timeout is 10 seconds.
     4  # NOTE: The expect tool must (!) be install via apt, because strictly-confined expect-snap
     5  # does not allow to execute scripts in tests folder.
     6  #
     7  # ```
     8  # expect_that <command> <expect_script> [<timeout>]
     9  # ```
    10  expect_that() {
    11  	local command expect_script timeout filename
    12  
    13  	command=${1}
    14  	filename=$(echo "${command}" | tr ' ' '-')
    15  	expect_script=${2}
    16  	timeout=${3:-10} # default timeout: 10s
    17  
    18  	cat >"${TEST_DIR}/${filename}.exp" <<EOF
    19  #!/usr/bin/expect -f
    20  proc abort { } { puts "Fail" }
    21  expect_before timeout abort
    22  
    23  set timeout ${timeout}
    24  spawn ${command}
    25  match_max 100000
    26  
    27  ${expect_script}
    28  
    29  expect eof
    30  wait
    31  
    32  EOF
    33  
    34  	expect "${TEST_DIR}/${filename}.exp"
    35  
    36  }