Skip to content

Commit

Permalink
added a bit of testing
Browse files Browse the repository at this point in the history
- cargo test
- tests/test-*.sh
  • Loading branch information
8go committed Sep 19, 2024
1 parent 3bc0dc5 commit 01b166a
Show file tree
Hide file tree
Showing 11 changed files with 637 additions and 44 deletions.
34 changes: 24 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.5
0.9.6
17 changes: 9 additions & 8 deletions scripts/workflow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 68 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sync>.
// We do not need to know if user used the option or not,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
88 changes: 88 additions & 0 deletions tests/test-devices.sh
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 01b166a

Please sign in to comment.