From e43d1f7a51b24ba5f1ce66963ff9b83a02ad2014 Mon Sep 17 00:00:00 2001 From: mfhepp Date: Sat, 27 Jan 2024 18:23:06 +0100 Subject: [PATCH] added tests --- cdf.sh | 19 ++---- run_tests.sh | 31 +++++++++ tests/README.md | 20 ++++++ tests/fixtures/shortcuts/.gitkeep | 0 tests/fixtures/testfolders/bar/bar1.txt | 0 tests/fixtures/testfolders/foo/foo1.txt | 0 tests/test_addfav.sh | 49 +++++++++++++++ tests/test_cdf_manual_symlink.sh | 68 ++++++++++++++++++++ tests/test_cdfpath.sh | 83 +++++++++++++++++++++++++ 9 files changed, 257 insertions(+), 13 deletions(-) create mode 100755 run_tests.sh create mode 100644 tests/README.md create mode 100644 tests/fixtures/shortcuts/.gitkeep create mode 100644 tests/fixtures/testfolders/bar/bar1.txt create mode 100644 tests/fixtures/testfolders/foo/foo1.txt create mode 100755 tests/test_addfav.sh create mode 100755 tests/test_cdf_manual_symlink.sh create mode 100755 tests/test_cdfpath.sh diff --git a/cdf.sh b/cdf.sh index 6533673..ca2a2d6 100644 --- a/cdf.sh +++ b/cdf.sh @@ -37,6 +37,9 @@ _cdf_list() { addfav() { # Define addfav command, adds PWD as a symbolic link + if ! _cdf_check_path; then + return 1 + fi if [ $# -eq 0 ] || [ "$1" = "--help" ]; then printf "ADDFAV: Adds the current directory as a shortcut to CDFPATH\n" printf 'Usage: addfav NAME\n\n' @@ -45,15 +48,9 @@ addfav() { printf ' addfav --help: Show help\n' printf ' addfav --list: List all available shortcuts\n' elif [ "$1" = "--list" ]; then - if ! _cdf_check_path; then - return 1 - fi # List all available shortcuts _cdf_list else - if ! _cdf_check_path; then - return 1 - fi # Check that the current directory is neither the place for the symlinks nor a subdirectory therof CURRENT_DIR=$(pwd) if [[ "$CURRENT_DIR" = "$CDFPATH" || "$CURRENT_DIR" == "$CDFPATH"/* ]]; then @@ -78,6 +75,9 @@ addfav() { cdf() { # Define cdf command ("change to favorite") + if ! _cdf_check_path; then + return 1 + fi if [ $# -eq 0 ] || [ "$1" = "--help" ]; then printf "CDF: Change to a directory via a shortcut from CDFPATH\n" printf 'Usage: cdf NAME\n\n' @@ -86,17 +86,10 @@ cdf() { printf ' cdf --help: Show help\n' printf ' cdf --list: List all available shortcuts\n\n' printf 'Hint: Use the TAB key for autocomplete with available shortcuts\n' - elif [ "$1" = "--list" ]; then - if ! _cdf_check_path; then - return 1 - fi # List all available shortcuts _cdf_list else - if ! _cdf_check_path; then - return 1 - fi filepath="$CDFPATH"/"$1" if [ -L "$filepath" ]; then echo "Following the symbolic link: $filepath" diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..0ca0f55 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# run_tests.sh +# Runs all tests in ./tests directory + +# Directory containing the scripts +script_dir="./tests" +# Save the current directory +current_dir=$(pwd) + +# Loop over all .sh files in the directory +for script in "$script_dir"/*.sh; do + # Check if the file is a regular file (not a directory) + if [ -f "$script" ]; then + echo "Running test script: $script" + # Change to the directory where the script is located + script_dir=$(dirname "$script") + cd "$script_dir" + # Run the script + bash "$(basename "$script")" + # Capture the exit status + status=$? + # Return to the original directory before possibly exiting + cd "$current_dir" + if [ $status -ne 0 ]; then + echo "ERROR: Test $script failed with status $status" + exit $status + fi + fi +done + +echo "OK: All tests passed" diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..892a1e0 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,20 @@ +# This folder containts a simple test suite + +## Running tests + +```bash +./run_tests.sh +``` +This runs all tests found in `./tests/*`. + +## Implemented + +- Check if `cdf` and `addfav` fail if `CDFPATH` is not set or is not a directory, and that they pass if both is fine. +- Test if a directory can be added and results in a proper symlink in the proper place. +- Test if a manually created shortcut/symlink works with `cdf NAME`. + +## Ideas for future tests + +- Test if creating a shortcut for a path that is itself a symlink fails. +- Test if creating a shortcut for a path that is not identical with its realpath fails. +- Test if `cdf --list` returns the proper list of shortcuts. diff --git a/tests/fixtures/shortcuts/.gitkeep b/tests/fixtures/shortcuts/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/testfolders/bar/bar1.txt b/tests/fixtures/testfolders/bar/bar1.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/testfolders/foo/foo1.txt b/tests/fixtures/testfolders/foo/foo1.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_addfav.sh b/tests/test_addfav.sh new file mode 100755 index 0000000..7f0a91a --- /dev/null +++ b/tests/test_addfav.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +# Set-up fixture +source ../cdf.sh +BACKUP_PATH=$CDFPATH +export CDFPATH="../../shortcuts" +symlink_path="${CDFPATH%/}/bar" + +# All paths are relative to this directory +cd ./fixtures/testfolders/bar + +# Check that symlink does not yet exist +if [ -L "$symlink_path" ]; then + echo "ERROR: Symlink at $symlink_path already exists." + exit 1 +else + echo "Test Passed: No previous symlink $symlink_path." +fi + +# Test if symlink can be created +addfav bar +if [ ! -L "$symlink_path" ]; then + echo "ERROR: Symlink at $symlink_path has not been created." + exit 1 +fi + +# Test if symlink points to the proper path +real_path=$(realpath "$symlink_path") +if [ "$real_path" = "$PWD" ]; then + echo "Test Passed, $symlink_path points to $PWD" +else + echo "ERROR: Symlink at $symlink_path does not point to $PWD." + echo " DEBUG: $real_path" + exit 1 +fi + +# Clean up +# Delete symlink in a secure way (rm with symlinks can be a beast!) +# Check if the variable is not empty and points to a file that is a symbolic link +if [[ -n "$symlink_path" && -L "$symlink_path" ]]; then + echo "INFO: Removing temporary symbolic link: $symlink_path" + rm "$symlink_path" +else + echo "ERROR: \$symlink_path is either empty or $symlink_path is not a symbolic link." + echo "Cannot remove temporary symbolic link." +fi +export CDFPATH=$BACKUP_PATH + +echo "OK: All tests from $0 passed" diff --git a/tests/test_cdf_manual_symlink.sh b/tests/test_cdf_manual_symlink.sh new file mode 100755 index 0000000..2f7cf5e --- /dev/null +++ b/tests/test_cdf_manual_symlink.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Test function +test_cdf() { + # Test if cdf foo works + cd ../bar + cdf foo + if [ "$PWD" = "$real_path" ]; then + echo "Test Passed: Current directory is $real_path" + else + echo "ERROR: cdf foo failed, current directory is $PWD, should be $real_path" + return 1 + fi +} + +# Set-up fixture +source ../cdf.sh +BACKUP_PATH=$CDFPATH +export CDFPATH="../../shortcuts" +symlink_path="${CDFPATH%/}/foo" +# All paths are relative to this directory +cd ./fixtures/testfolders/foo +# Check that symlink does not yet exist +if [ -L "$symlink_path" ]; then + echo "ERROR: Symlink at $symlink_path already exists" + exit 1 +else + echo "Test Passed: No previous symlink $symlink_path" +fi +# Create symlink manually +ln -s "$PWD" "$symlink_path" +# Test if symlink has been be created +if [ ! -L "$symlink_path" ]; then + echo "ERROR: Symlink at $symlink_path has not been created" + exit 1 +fi +# Test if symlink points to the proper path +real_path=$(realpath "$symlink_path") +if [ "$real_path" = "$PWD" ]; then + echo "Test Passed, $symlink_path points to $PWD" +else + echo "ERROR: Symlink at $symlink_path does not point to $PWD" + echo " DEBUG: $real_path" + return 1 +fi + +# Run test(s) +test_cdf +status=$? +# Clean-up +# Delete symlink in a secure way (rm with symlinks can be a beast!) +# Check if the variable is not empty and points to a file that is a symbolic link +if [[ -n "$symlink_path" && -L "$symlink_path" ]]; then + echo "INFO: Removing temporary symbolic link: $symlink_path" + rm "$symlink_path" +else + echo "ERROR: \$symlink_path is either empty or $symlink_path is not a symbolic link" + echo "Cannot remove temporary symbolic link" +fi +if [ $status -ne 0 ]; then + echo "ERROR: At least one test failed, aborting" + export CDFPATH=$BACKUP_PATH + exit $status +else + echo "OK: All tests from $0 passed" +fi + + diff --git a/tests/test_cdfpath.sh b/tests/test_cdfpath.sh new file mode 100755 index 0000000..1bf33cb --- /dev/null +++ b/tests/test_cdfpath.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +# Test function +test_path_not_set() { + command="$1" + # Run the script or command + "$command" > /dev/null 2>&1 + # Capture the exit status + local status=$? + # Check if the exit status is 1 + if [ $status -eq 1 ]; then + echo "Test Passed: $command exited with status 1 when CDFPATH is not set." + else + echo "Test FAILED: $command did not exit with status 1 (actual status: $status) when CDFPATH is not set." + return 1 + fi +} + +test_path_set_valid() { + command="$1" + # Run the script or command + "$command" > /dev/null 2>&1 + # Capture the exit status + local status=$? + # Check if the exit status is 0 + if [ $status -eq 0 ]; then + echo "Test Passed: $command exited with status 0 when CDFPATH is set and directory exists." + else + echo "Test FAILED: $command exited with status $status even if CDFPATH is set and directory exists." + echo " DEBUG: CDFPATH=$CDFPATH" + echo " DEBUG: PWD=$PWD" + return 1 + fi +} + +test_path_set_invalid() { + # Run the script without arguments + command="$1" + "$command" > /dev/null 2>&1 + # Capture the exit status + local status=$? + # Check if the exit status is 1 + if [ $status -eq 1 ]; then + echo "Test Passed: $command exited with status 1 when CDFPATH is set but directory does not exist." + else + echo "Test FAILED: $command exited with status $status when CDFPATH is set but directory does not exist." + echo " DEBUG: CDFPATH=$CDFPATH" + echo " DEBUG: PWD=$PWD" + return 1 + fi +} + +# Test runner for catching the first breaking test +run_test() { + test_command="$@" + $test_command + status=$? + if [ $status -ne 0 ]; then + echo "ERROR: At least one test failed, aborting" + # Clean-up + export CDFPATH=$BACKUP_PATH + exit $status + fi +} + +# Run the test +# Set-up fixture +source ../cdf.sh +BACKUP_PATH=$CDFPATH +# Tests if missing CDFPATH exits with an error +unset CDFPATH +run_test test_path_not_set "cdf" +run_test test_path_not_set "addfav" +# Tests if set CDFPATH to existing directory exits with no error +export CDFPATH="./fixtures/shortcuts" +run_test test_path_set_valid "cdf" +run_test test_path_set_valid "addfav" +# Tests if set CDFPATH to non-existing directory exits with an error +export CDFPATH="./fixtures/shortcuts123" +run_test test_path_set_invalid "cdf" +run_test test_path_set_invalid "addfav" +export CDFPATH=$BACKUP_PATH +echo "OK: All tests from $0 passed" \ No newline at end of file