#script-runner #command-line-interface #asciinema #automation #testing #recording #read-line

app anticipate-cli

Script runner with asciinema support

4 releases (breaking)

0.6.0 Feb 24, 2024
0.5.0 Feb 24, 2024
0.4.0 Feb 23, 2024
0.3.0 Feb 22, 2024

#3 in #asciinema

MIT/Apache

47KB
1K SLoC

Anticipate

Script based automation like expect(1) with support for asciinema.

Perfect for demos and testing command line interfaces.

Install

cargo install anticipate-cli

Example

Create a directory, initialize a server config, print the config file, start the server and then send SIGTERM to shut it down.

mkdir -p target/server/accounts
#$ readline

server init target/config.toml --path server/accounts
#$ readline

cat target/config.toml
#$ expect path = "server/accounts"

server start target/config.toml
#$ sendcontrol ^C

See the examples and fixtures folders for more examples.

Usage

Running

To execute scripts use the run command:

anticipate \
  run \
  --parallel \
  --log \
  tests/examples/*.sh

Recording

To record using asciinema writing a .cast file for each input file into the target directory overwriting any existing files:

anticipate \
  record \
  --parallel \
  --overwrite \
  --log \
  target \
  tests/examples/*.sh

See the progam help for more options.

Syntax

Environment variables are interpolated for commands sent to the pseudo terminal which makes it easier to share values across scripts.

export NAME=foo
anticipate rec -o target tests/examples/interpolate.sh
asciinema play target/interpolate.cast

Pragma

Use a pragma as the first instruction to set the command to execute:

#!/bin/bash

If a relative path is given it is resolved relative to the script:

#!../programs/script.sh

Send Line

Raw text is sent as a line to the pseudo-terminal:

ls -la

Or you can use the sendline command explicitly:

#$ sendline ls -la

Send Control

To send a control character, for example Ctrl+C:

#$ sendcontrol ^C

Expect

Expect waits for a string to appear in the program output:

#$ expect Documents

Regex

To wait for a pattern to appear in the program output use regex:

#$ regex [0-9]

Read Line

Read a line of program output:

#$ readline

Sleep

Wait for a number of milliseconds:

#$ sleep 500

Send

Send text to the program without flushing the stream:

#$ send echo

Flush

Flush the buffer being sent to the pseudo-terminal:

#$ flush

Wait

Wait for the prompt to appear:

#$ wait

Clear

Clear the screen and reset the cursor position:

#$ clear

Include

Include instructions from a script file:

#$ include ../shared.sh

Paths are resolved relative to the parent directory of the script file.

See Also

Credits

The core module is a fork of expectrl and the syntax is inspired by asciinema-automation.

License

MIT or Apache-2.0

Dependencies

~13–46MB
~664K SLoC