Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mfhepp committed Jan 27, 2024
1 parent 6350ba7 commit e43d1f7
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 13 deletions.
19 changes: 6 additions & 13 deletions cdf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -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.
Empty file.
Empty file.
Empty file.
49 changes: 49 additions & 0 deletions tests/test_addfav.sh
Original file line number Diff line number Diff line change
@@ -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"
68 changes: 68 additions & 0 deletions tests/test_cdf_manual_symlink.sh
Original file line number Diff line number Diff line change
@@ -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


83 changes: 83 additions & 0 deletions tests/test_cdfpath.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit e43d1f7

Please sign in to comment.