Skip to content

Commit

Permalink
Migrate expect based test script to bats
Browse files Browse the repository at this point in the history
  • Loading branch information
ghubstan committed Apr 28, 2020
1 parent cfb7e32 commit ac87c23
Showing 1 changed file with 62 additions and 139 deletions.
201 changes: 62 additions & 139 deletions cli/test.sh
Original file line number Diff line number Diff line change
@@ -1,156 +1,79 @@
#!/bin/bash
#!/usr/bin/env bats
#
# References & examples for expect:
# bats v0.4.0 project
#
# - https://pantz.org/software/expect/expect_examples_and_tips.html
# - https://stackoverflow.com/questions/13982310/else-string-matching-in-expect
# - https://gist.github.com/Fluidbyte/6294378
# - https://www.oreilly.com/library/view/exploring-expect/9781565920903/ch04.html
# https://github.com/sstephenson/bats/tree/v0.4.0
#
# Prior to running this script, run:
#
# ./bisq-daemon --apiPassword=xyz
#
# To run this script:
#
# cd <project-root-dir>
# bats cli/bats-test.sh
#
# The data directory used must contain an unencrypted wallet with a 0 BTC balance

# Ensure project root is the current working directory
cd $(dirname $0)/..

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST unsupported cmd error"
set expected "Error: '\''bogus'\'' is not a supported method"
spawn ./bisq-cli --password=xyz bogus
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test unsupported method error" {
run ./bisq-cli --password=xyz bogus
[ "$status" -eq 1 ]
echo "actual output: $output" >&2 # printed only on test failure
[ "$output" = "Error: 'bogus' is not a supported method" ]
}

OUTPUT=$(expect -c '
puts "TEST unrecognized option error"
set expected "Error: bogus is not a recognized option"
spawn ./bisq-cli --bogus getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test unrecognized option error" {
run ./bisq-cli --bogus getversion
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: bogus is not a recognized option" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST missing required password option error"
set expected "Error: missing required '\''password'\'' option"
spawn ./bisq-cli getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test missing required password option error" {
run ./bisq-cli getversion
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: missing required 'password' option" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST getversion (incorrect password error)"
set expected "Error: incorrect '\''password'\'' rpc header value"
spawn ./bisq-cli --password=bogus getversion
expect {
$expected { puts "PASS\n" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test incorrect password error" {
run ./bisq-cli --password=bogus getversion
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: incorrect 'password' rpc header value" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST getversion (password value in quotes)"
set expected "1.3.2"
# Note: have to define quoted argument in a variable as "''value''"
set pwd_in_quotes "''xyz''"
spawn ./bisq-cli --password=$pwd_in_quotes getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test getversion call with quoted password" {
run ./bisq-cli --password="xyz" getversion
[ "$status" -eq 0 ]
echo "actual output: $output" >&2
[ "$output" = "1.3.2" ]
}

OUTPUT=$(expect -c '
puts "TEST getversion"
set expected "1.3.2"
spawn ./bisq-cli --password=xyz getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test getversion" {
run ./bisq-cli --password=xyz getversion
[ "$status" -eq 0 ]
[ "$output" = "1.3.2" ]
}

OUTPUT=$(expect -c '
puts "TEST getbalance"
# exp_internal 1
set expected "0.00000000"
spawn ./bisq-cli --password=xyz getbalance
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test getbalance (available & unlocked wallet with 0 btc balance)" {
run ./bisq-cli --password=xyz getbalance
[ "$status" -eq 0 ]
[ "$output" = "0.00000000" ]
}

OUTPUT=$(expect -c '
puts "TEST running with no options or arguments prints help text"
# exp_internal 1
set expected "Bisq RPC Client"
spawn ./bisq-cli
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test help displayed on stderr if no options or arguments" {
run ./bisq-cli
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Bisq RPC Client" ]
[ "${lines[1]}" = "Usage: bisq-cli [options] <method>" ]
# TODO add asserts after help text is modified for new endpoints
}

echo "TEST --help option prints help text"
./bisq-cli --help
@test "test --help option" {
run ./bisq-cli --help
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Bisq RPC Client" ]
[ "${lines[1]}" = "Usage: bisq-cli [options] <method>" ]
# TODO add asserts after help text is modified for new endpoints
}

0 comments on commit ac87c23

Please sign in to comment.