-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
257 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |