diff --git a/Cargo.lock b/Cargo.lock index 1ee34f2..fbc9dc3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -355,9 +355,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.7.1" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50" +checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" [[package]] name = "bytesize" @@ -376,9 +376,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.20" +version = "1.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45bcde016d64c21da4be18b655631e5ab6d3107607e71a73a9f53eb48aae23fb" +checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" dependencies = [ "shlex", ] @@ -1628,7 +1628,7 @@ dependencies = [ [[package]] name = "matrix-commander" -version = "0.9.5" +version = "0.9.6" dependencies = [ "clap", "colored", @@ -1646,6 +1646,7 @@ dependencies = [ "serde_json", "thiserror", "tokio", + "tokio-test", "tracing", "tracing-subscriber", "update-informer", @@ -3228,6 +3229,19 @@ dependencies = [ "tokio-util", ] +[[package]] +name = "tokio-test" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7" +dependencies = [ + "async-stream", + "bytes", + "futures-core", + "tokio", + "tokio-stream", +] + [[package]] name = "tokio-util" version = "0.7.12" @@ -3446,9 +3460,9 @@ checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "unicode-normalization" -version = "0.1.23" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" dependencies = [ "tinyvec", ] @@ -3504,7 +3518,7 @@ dependencies = [ "serde", "serde_json", "url", - "webpki-roots 0.26.5", + "webpki-roots 0.26.6", ] [[package]] @@ -3713,9 +3727,9 @@ checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" [[package]] name = "webpki-roots" -version = "0.26.5" +version = "0.26.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bd24728e5af82c6c4ec1b66ac4844bdf8156257fccda846ec58b42cd0cdbe6a" +checksum = "841c67bff177718f1d4dfefde8d8f0e78f9b6589319ba88312f567fc5841a958" dependencies = [ "rustls-pki-types", ] diff --git a/Cargo.toml b/Cargo.toml index 91e61e9..3e7363f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ [package] name = "matrix-commander" -version = "0.9.5" +version = "0.9.6" edition = "2021" description = "simple but convenient CLI-based Matrix client app for sending and receiving" documentation = "https://docs.rs/matrix-commander" @@ -60,10 +60,13 @@ url = { version = "2.5", features = ["serde"] } [dev-dependencies] +# "matrix-commander-rs" will be the Rust program +# to perform tests on async functions +tokio-test = "0.4" + # this is to distinguish it from "matrix-commander" which is the Python program # For people that have both Python and Rust installed: # "matrix-commander" will remain the Python program -# "matrix-commander-rs" will be the Rust program [[bin]] name = "matrix-commander-rs" path = "src/main.rs" diff --git a/VERSION b/VERSION index b0bb878..85b7c69 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.5 +0.9.6 diff --git a/scripts/workflow.sh b/scripts/workflow.sh index 238c73e..31ed772 100755 --- a/scripts/workflow.sh +++ b/scripts/workflow.sh @@ -64,20 +64,21 @@ select opt in "${options[@]}"; do done PS3='Please enter your choice: ' -OPT1="cargo test # run this testcase" -OPT2="tests/test-send.sh # run this testcase" -OPT3="tests/test-send.sh # run this testcase" -OPT4="tests/test-send.sh # run this testcase" -OPT5="tests/test-send.sh # run this testcase" -OPT6="tests/test-send.sh # run this testcase" +OPT1="cargo test # run this testcase" +OPT2="tests/test-version.sh # run this testcase" +OPT3="tests/test-send.sh # run this testcase" +OPT4="tests/test-devices.sh # run this testcase" +OPT5="tests/test-rooms.sh # run this testcase" +OPT6="tests/test-get-profile.sh # run this testcase" +OPT7="tests/test-get-display-name.sh # run this testcase" OPTC="Continue" OPTQ="Quit" -options=("$OPT1" "$OPT2" "$OPT3" "$OPT4" "$OPT5" "$OPT6" "$OPTC" "$OPTQ") +options=("$OPT1" "$OPT2" "$OPT3" "$OPT4" "$OPT5" "$OPT6" "$OPT7" "$OPTC" "$OPTQ") select opt in "${options[@]}"; do if [ "${REPLY,,}" == "c" ]; then opt="$OPTC"; fi if [ "${REPLY,,}" == "q" ]; then opt="$OPTQ"; fi case ${opt} in - "$OPT1" | "$OPT2" | "$OPT3" | "$OPT4" | "$OPT5" | "$OPT6") + "$OPT1" | "$OPT2" | "$OPT3" | "$OPT4" | "$OPT5" | "$OPT6" | "$OPT7") OPTE=${opt%%#*} # remove everything after first # echo "Performing: $OPTE" $OPTE diff --git a/src/main.rs b/src/main.rs index 962d003..99449df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -648,7 +648,7 @@ impl fmt::Display for LogLevel { /// Enumerator used for --output option #[derive(Clone, Debug, Copy, PartialEq, Default, ValueEnum)] -enum Output { +pub enum Output { // None: only useful if one needs to know if option was used or not. // Sort of like an or instead of an Option. // We do not need to know if user used the option or not, @@ -2455,30 +2455,41 @@ pub async fn readme() { } /// Prints the version information -pub fn version() { +pub fn version(output: Output) { let version = if stdout().is_terminal() { get_version().green() } else { get_version().normal() }; - println!(); - println!( - " _| _| _|_|_| {}", - get_prog_without_ext() - ); - print!(" _|_| _|_| _| _~^~^~_ "); - println!("a rusty vision of a Matrix CLI client"); - println!( - " _| _| _| _| \\) / o o \\ (/ version {}", - version - ); - println!( - " _| _| _| '_ - _' repo {}", - get_pkg_repository() - ); - print!(" _| _| _|_|_| / '-----' \\ "); - println!("please submit PRs to make the vision a reality"); - println!(); + match output { + Output::Text => { + println!(); + println!( + " _| _| _|_|_| {}", + get_prog_without_ext() + ); + print!(" _|_| _|_| _| _~^~^~_ "); + println!("a rusty vision of a Matrix CLI client"); + println!( + " _| _| _| _| \\) / o o \\ (/ version {}", + version + ); + println!( + " _| _| _| '_ - _' repo {}", + get_pkg_repository() + ); + print!(" _| _| _|_|_| / '-----' \\ "); + println!("please submit PRs to make the vision a reality"); + println!(); + } + Output::JsonSpec => (), + _ => println!( + "{{\"program\": {:?}, \"version\": {:?}, \"repo\": {:?}}}", + get_prog_without_ext(), + get_version(), + get_pkg_repository() + ), + } } /// Prints the installed version and the latest crates.io-available version @@ -3521,8 +3532,8 @@ async fn main() -> Result<(), Error> { debug!("get-masterkey option is {:?}", ap.get_masterkey); match ap.version { - None => (), // do nothing - Some(None) => crate::version(), // print version + None => (), // do nothing + Some(None) => crate::version(ap.output), // print version Some(Some(Version::Check)) => crate::version_check(), } if ap.contribute { @@ -4295,9 +4306,43 @@ mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; + // for testing async functions + // see: https://blog.x5ff.xyz/blog/async-tests-tokio-rust/ + macro_rules! aw { + ($e:expr) => { + tokio_test::block_on($e) + }; + } + + #[test] + fn test_usage() { + assert_eq!(usage(), ()); + } + + #[test] + fn test_help() { + assert_eq!(help(), ()); + } + + #[test] + fn test_manual() { + assert_eq!(manual(), ()); + } + + #[test] + fn test_readme() { + assert_eq!(aw!(readme()), ()); + } + #[test] fn test_version() { - assert_eq!(version(), ()); + assert_eq!(version(Output::Text), ()); + assert_eq!(version(Output::Json), ()); + } + + #[test] + fn test_version_check() { + assert_eq!(version_check(), ()); } #[test] diff --git a/tests/test-devices.sh b/tests/test-devices.sh new file mode 100755 index 0000000..2089a15 --- /dev/null +++ b/tests/test-devices.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +if [ -t 1 ]; then + # echo terminal + green="\e[1;37;1;42m" + off="\e[0m" + red="\e[1;37;1;41m" +else + # echo "not a terminal", e.g. being piped out + green="" + off="" + red="" +fi + +# just in case PATH is not set correctly +PATH="./target/debug/:./target/release/:../target/debug/:../target/release/:.:./matrix_commander-rs:../matrix_commander-rs:$PATH" + +# One may set similar values in the terminal before calling the script. +# export MCRS_OPTIONS="-d --room \!...some.room.id:matrix.example.org " + +# getting some optional arguments +if [ "$MCRS_OPTIONS" != "" ]; then + echo "Exellent. Variable MCRS_OPTIONS already set. " \ + "Using \"$MCRS_OPTIONS\" as additional options for testing." +else + echo "Optionally, set variable \"MCRS_OPTIONS\" for further options." +fi + +echo "rustc version is: $(rustc -vV | xargs)" +echo "rustup version is: $(rustup --version)" +echo "cargo version is: $(cargo --version)" +echo "GITHUB_WORKFLOW = $GITHUB_WORKFLOW" +echo "GITHUB_REPOSITORY = $GITHUB_REPOSITORY" +echo "MCRS_OPTIONS = $MCRS_OPTIONS" + +if [[ "$GITHUB_WORKFLOW" != "" ]]; then # if in Github Action Workflow + echo "I am in Github Action Workflow $GITHUB_WORKFLOW." +fi + +failures=0 + +function test1() { + echo "=== Test 1: get devices in text format ===" + matrix-commander-rs --devices $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} + +function test2() { + echo "=== Test 2: get devices in JSON format ===" + fofile="/tmp/matrix-comander-rs-test-devices.json" + matrix-commander-rs --devices --output json $MCRS_OPTIONS >$fofile + res=$? + if [ "$res" == "0" ]; then + if cat $fofile | jq -e . >/dev/null 2>&1; then + echo "Parsed JSON successfully and got something other than false/null" + echo "SUCCESS" + else + echo "Failed to parse JSON, or got false/null" + echo >&2 "FAILURE" + let failures++ + fi + else + echo >&2 "FAILURE" + let failures++ + fi + rm $fofile +} + +test1 +test2 + +failtext="failure" +if [ "$failures" != "1" ]; then + failtext+="s" ## append an "s" to the end +fi +if [ "$failures" == "0" ]; then + echo -e "${green}OK: Finished test series with $failures failures.${off}" +else + echo -e "${red}ERROR: Finished test series with $failures $failtext.${off}" +fi + +exit $failures diff --git a/tests/test-get-display-name.sh b/tests/test-get-display-name.sh new file mode 100755 index 0000000..a3ac011 --- /dev/null +++ b/tests/test-get-display-name.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +if [ -t 1 ]; then + # echo terminal + green="\e[1;37;1;42m" + off="\e[0m" + red="\e[1;37;1;41m" +else + # echo "not a terminal", e.g. being piped out + green="" + off="" + red="" +fi + +# just in case PATH is not set correctly +PATH="./target/debug/:./target/release/:../target/debug/:../target/release/:.:./matrix_commander-rs:../matrix_commander-rs:$PATH" + +# One may set similar values in the terminal before calling the script. +# export MCRS_OPTIONS="-d --room \!...some.room.id:matrix.example.org " + +# getting some optional arguments +if [ "$MCRS_OPTIONS" != "" ]; then + echo "Exellent. Variable MCRS_OPTIONS already set. " \ + "Using \"$MCRS_OPTIONS\" as additional options for testing." +else + echo "Optionally, set variable \"MCRS_OPTIONS\" for further options." +fi + +echo "rustc version is: $(rustc -vV | xargs)" +echo "rustup version is: $(rustup --version)" +echo "cargo version is: $(cargo --version)" +echo "GITHUB_WORKFLOW = $GITHUB_WORKFLOW" +echo "GITHUB_REPOSITORY = $GITHUB_REPOSITORY" +echo "MCRS_OPTIONS = $MCRS_OPTIONS" + +if [[ "$GITHUB_WORKFLOW" != "" ]]; then # if in Github Action Workflow + echo "I am in Github Action Workflow $GITHUB_WORKFLOW." +fi + +failures=0 + +function test1() { + echo "=== Test 1: get display-name in text format ===" + matrix-commander-rs --get-display-name $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} + +function test2() { + echo "=== Test 2: get display-name in JSON format ===" + fofile="/tmp/matrix-comander-rs-test-get-display-name.json" + matrix-commander-rs --get-display-name --output json $MCRS_OPTIONS >$fofile + res=$? + if [ "$res" == "0" ]; then + if cat $fofile | jq -e . >/dev/null 2>&1; then + echo "Parsed JSON successfully and got something other than false/null" + echo "SUCCESS" + else + echo "Failed to parse JSON, or got false/null" + echo >&2 "FAILURE" + let failures++ + fi + else + echo >&2 "FAILURE" + let failures++ + fi + rm $fofile +} + +test1 +test2 + +failtext="failure" +if [ "$failures" != "1" ]; then + failtext+="s" ## append an "s" to the end +fi +if [ "$failures" == "0" ]; then + echo -e "${green}OK: Finished test series with $failures failures.${off}" +else + echo -e "${red}ERROR: Finished test series with $failures $failtext.${off}" +fi + +exit $failures diff --git a/tests/test-get-profile.sh b/tests/test-get-profile.sh new file mode 100755 index 0000000..f70eeca --- /dev/null +++ b/tests/test-get-profile.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +if [ -t 1 ]; then + # echo terminal + green="\e[1;37;1;42m" + off="\e[0m" + red="\e[1;37;1;41m" +else + # echo "not a terminal", e.g. being piped out + green="" + off="" + red="" +fi + +# just in case PATH is not set correctly +PATH="./target/debug/:./target/release/:../target/debug/:../target/release/:.:./matrix_commander-rs:../matrix_commander-rs:$PATH" + +# One may set similar values in the terminal before calling the script. +# export MCRS_OPTIONS="-d --room \!...some.room.id:matrix.example.org " + +# getting some optional arguments +if [ "$MCRS_OPTIONS" != "" ]; then + echo "Exellent. Variable MCRS_OPTIONS already set. " \ + "Using \"$MCRS_OPTIONS\" as additional options for testing." +else + echo "Optionally, set variable \"MCRS_OPTIONS\" for further options." +fi + +echo "rustc version is: $(rustc -vV | xargs)" +echo "rustup version is: $(rustup --version)" +echo "cargo version is: $(cargo --version)" +echo "GITHUB_WORKFLOW = $GITHUB_WORKFLOW" +echo "GITHUB_REPOSITORY = $GITHUB_REPOSITORY" +echo "MCRS_OPTIONS = $MCRS_OPTIONS" + +if [[ "$GITHUB_WORKFLOW" != "" ]]; then # if in Github Action Workflow + echo "I am in Github Action Workflow $GITHUB_WORKFLOW." +fi + +failures=0 + +function test1() { + echo "=== Test 1: get profile in text format ===" + matrix-commander-rs --get-profile $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} + +function test2() { + echo "=== Test 2: get profile in JSON format ===" + fofile="/tmp/matrix-comander-rs-test-get-profile.json" + matrix-commander-rs --get-profile --output json $MCRS_OPTIONS >$fofile + res=$? + if [ "$res" == "0" ]; then + if cat $fofile | jq -e . >/dev/null 2>&1; then + echo "Parsed JSON successfully and got something other than false/null" + echo "SUCCESS" + else + echo "Failed to parse JSON, or got false/null" + echo >&2 "FAILURE" + let failures++ + fi + else + echo >&2 "FAILURE" + let failures++ + fi + rm $fofile +} + +test1 +test2 + +failtext="failure" +if [ "$failures" != "1" ]; then + failtext+="s" ## append an "s" to the end +fi +if [ "$failures" == "0" ]; then + echo -e "${green}OK: Finished test series with $failures failures.${off}" +else + echo -e "${red}ERROR: Finished test series with $failures $failtext.${off}" +fi + +exit $failures diff --git a/tests/test-rooms.sh b/tests/test-rooms.sh new file mode 100755 index 0000000..ad1214f --- /dev/null +++ b/tests/test-rooms.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +if [ -t 1 ]; then + # echo terminal + green="\e[1;37;1;42m" + off="\e[0m" + red="\e[1;37;1;41m" +else + # echo "not a terminal", e.g. being piped out + green="" + off="" + red="" +fi + +# just in case PATH is not set correctly +PATH="./target/debug/:./target/release/:../target/debug/:../target/release/:.:./matrix_commander-rs:../matrix_commander-rs:$PATH" + +# One may set similar values in the terminal before calling the script. +# export MCRS_OPTIONS="-d --room \!...some.room.id:matrix.example.org " + +# getting some optional arguments +if [ "$MCRS_OPTIONS" != "" ]; then + echo "Exellent. Variable MCRS_OPTIONS already set. " \ + "Using \"$MCRS_OPTIONS\" as additional options for testing." +else + echo "Optionally, set variable \"MCRS_OPTIONS\" for further options." +fi + +echo "rustc version is: $(rustc -vV | xargs)" +echo "rustup version is: $(rustup --version)" +echo "cargo version is: $(cargo --version)" +echo "GITHUB_WORKFLOW = $GITHUB_WORKFLOW" +echo "GITHUB_REPOSITORY = $GITHUB_REPOSITORY" +echo "MCRS_OPTIONS = $MCRS_OPTIONS" + +if [[ "$GITHUB_WORKFLOW" != "" ]]; then # if in Github Action Workflow + echo "I am in Github Action Workflow $GITHUB_WORKFLOW." +fi + +failures=0 + +function test1() { + echo "=== Test 1: get rooms in text format ===" + matrix-commander-rs --rooms $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} + +function test2() { + echo "=== Test 2: get rooms in JSON format ===" + fofile="/tmp/matrix-comander-rs-test-rooms.json" + matrix-commander-rs --rooms --output json $MCRS_OPTIONS >$fofile + res=$? + if [ "$res" == "0" ]; then + if cat $fofile | jq -e . >/dev/null 2>&1; then + echo "Parsed JSON successfully and got something other than false/null" + echo "SUCCESS" + else + echo "Failed to parse JSON, or got false/null" + echo >&2 "FAILURE" + let failures++ + fi + else + echo >&2 "FAILURE" + let failures++ + fi + rm $fofile +} + +test1 +test2 + +failtext="failure" +if [ "$failures" != "1" ]; then + failtext+="s" ## append an "s" to the end +fi +if [ "$failures" == "0" ]; then + echo -e "${green}OK: Finished test series with $failures failures.${off}" +else + echo -e "${red}ERROR: Finished test series with $failures $failtext.${off}" +fi + +exit $failures diff --git a/tests/test-send.sh b/tests/test-send.sh new file mode 100755 index 0000000..edc90e3 --- /dev/null +++ b/tests/test-send.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +if [ -t 1 ]; then + # echo terminal + green="\e[1;37;1;42m" + off="\e[0m" + red="\e[1;37;1;41m" +else + # echo "not a terminal", e.g. being piped out + green="" + off="" + red="" +fi + +# just in case PATH is not set correctly +PATH="./target/debug/:./target/release/:../target/debug/:../target/release/:.:./matrix_commander-rs:../matrix_commander-rs:$PATH" + +# One may set similar values in the terminal before calling the script. +# export MCRS_OPTIONS="-d --room \!...some.room.id:matrix.example.org " + +# getting some optional arguments +if [ "$MCRS_OPTIONS" != "" ]; then + echo "Exellent. Variable MCRS_OPTIONS already set. " \ + "Using \"$MCRS_OPTIONS\" as additional options for testing." +else + echo "If desired, set variable \"MCRS_OPTIONS\" for further options." +fi + +echo "rustc version is: $(rustc -vV | xargs)" +echo "rustup version is: $(rustup --version)" +echo "cargo version is: $(cargo --version)" +echo "GITHUB_WORKFLOW = $GITHUB_WORKFLOW" +echo "GITHUB_REPOSITORY = $GITHUB_REPOSITORY" +echo "MCRS_OPTIONS = $MCRS_OPTIONS" + +if [[ "$GITHUB_WORKFLOW" != "" ]]; then # if in Github Action Workflow + echo "I am in Github Action Workflow $GITHUB_WORKFLOW." +fi + +failures=0 + +function test1() { + echo "=== Test 1: send a message ===" + matrix-commander-rs -m foo $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} + +function test2() { + echo "=== Test 2: send two messages ===" + matrix-commander-rs -m "foo" "bar" $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} +function test3() { + echo "=== Test 3: send two messages ===" + res=$(matrix-commander-rs -m "foo" "bar" -d $MCRS_OPTIONS 2>&1 >/dev/null | grep "message send successful" | wc -l) + if [ "$res" == "2" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" + let failures++ + fi +} + +test1 +test2 +test3 + +failtext="failure" +if [ "$failures" != "1" ]; then + failtext+="s" ## append an "s" to the end +fi +if [ "$failures" == "0" ]; then + echo -e "${green}OK: Finished test series with $failures failures.${off}" +else + echo -e "${red}ERROR: Finished test series with $failures $failtext.${off}" +fi + +exit $failures diff --git a/tests/test-version.sh b/tests/test-version.sh new file mode 100755 index 0000000..ce811e4 --- /dev/null +++ b/tests/test-version.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +if [ -t 1 ]; then + # echo terminal + green="\e[1;37;1;42m" + off="\e[0m" + red="\e[1;37;1;41m" +else + # echo "not a terminal", e.g. being piped out + green="" + off="" + red="" +fi + +# just in case PATH is not set correctly +PATH="./target/debug/:./target/release/:../target/debug/:../target/release/:.:./matrix_commander-rs:../matrix_commander-rs:$PATH" + +# One may set similar values in the terminal before calling the script. +# export MCRS_OPTIONS="-d --room \!...some.room.id:matrix.example.org " + +# getting some optional arguments +if [ "$MCRS_OPTIONS" != "" ]; then + echo "Exellent. Variable MCRS_OPTIONS already set. " \ + "Using \"$MCRS_OPTIONS\" as additional options for testing." +else + echo "Optionally, set variable \"MCRS_OPTIONS\" for further options." +fi + +echo "rustc version is: $(rustc -vV | xargs)" +echo "rustup version is: $(rustup --version)" +echo "cargo version is: $(cargo --version)" +echo "GITHUB_WORKFLOW = $GITHUB_WORKFLOW" +echo "GITHUB_REPOSITORY = $GITHUB_REPOSITORY" +echo "MCRS_OPTIONS = $MCRS_OPTIONS" + +if [[ "$GITHUB_WORKFLOW" != "" ]]; then # if in Github Action Workflow + echo "I am in Github Action Workflow $GITHUB_WORKFLOW." +fi + +failures=0 + +function test1() { + echo "=== Test 1: get version in text format ===" + matrix-commander-rs --version $MCRS_OPTIONS + res=$? + if [ "$res" == "0" ]; then + echo "SUCCESS" + else + echo >&2 "FAILURE" echo "FAILURE" + let failures++ + fi +} + +function test2() { + echo "=== Test 2: get version in JSON format ===" + fofile="/tmp/matrix-comander-rs-test-version.json" + matrix-commander-rs --version --output json $MCRS_OPTIONS >$fofile + res=$? + if [ "$res" == "0" ]; then + if cat $fofile | jq -e . >/dev/null 2>&1; then + echo "Parsed JSON successfully and got something other than false/null" + echo "SUCCESS" + else + echo "Failed to parse JSON, or got false/null" + echo >&2 "FAILURE" echo "FAILURE" + let failures++ + fi + else + echo >&2 "FAILURE" echo "FAILURE" + let failures++ + fi + rm $fofile +} + +test1 +test2 + +failtext="failure" +if [ "$failures" != "1" ]; then + failtext+="s" ## append an "s" to the end +fi +if [ "$failures" == "0" ]; then + echo -e "${green}OK: Finished test series with $failures failures.${off}" +else + echo -e "${red}ERROR: Finished test series with $failures $failtext.${off}" +fi + +exit $failures