Skip to content

Commit

Permalink
added bashscripts standalone test
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Jan 25, 2025
1 parent bee371f commit 126073f
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 66 deletions.
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
default: bash_interactive
bash_interactive:
docker-compose -f testenv/shellfuncs/bashenv/docker-compose.yaml build
docker-compose -f testenv/shellfuncs/bashenv/docker-compose.yaml run --rm fasttravelcli_bash_interactive
default: all

all: bash_script

bash_script_interactive:
docker-compose -f testenv/shellfuncs/docker-compose.yaml build fasttravelcli_bash_script_interactive
docker-compose -f testenv/shellfuncs/docker-compose.yaml run --rm fasttravelcli_bash_script_interactive

bash_script:
docker-compose -f testenv/shellfuncs/docker-compose.yaml up fasttravelcli_bash_script --build
50 changes: 50 additions & 0 deletions shells/tests/bashscripts/interactive_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash


# This test script is designed to test the ftmain.sh file independent of fastTravelCLI's binary.
# Use by calling the simtest function.
# This script will build simulations to run through automatically.
# setup.sh and ftmain.sh need to be sourced prior to execution of these simulations. This step is added to the .bashrc in Docker.
# To manually test specific commands, utilize the testcmd function found in setup.sh.



simtest() {
# User can set number of simulations or use default of 15
if [[ $# -ne 1 ]]; then
sims=15
else
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Argument must be a number! Arg: $1" >&2
exit 1
fi
sims=$1
fi

# Generate a random simulation
# Setup script will define the commands
generateSim() {
local totalcmds=$1
# Start the simulation more towards the middle of the history stack
local sim=("[" "[" "[" "[" "[" "[")
for ((i = 0; i < totalcmds; i++)); do
local idx=$(( RANDOM % ${#commands[@]} ))
sim+=("${commands[idx]}")
done
echo "${sim[@]}"
}

# Create the simulation
simulation=($(generateSim "$sims"))

# Process the commands
for cmd in "${simulation[@]}"; do
echo -e "--- History stack: \n $(ft__phist)\n-----"
echo -e "--- Currently at directory: $(pwd)\n--->"

ft "$cmd"

echo "<---"

done
}
149 changes: 109 additions & 40 deletions shells/tests/bashscripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,118 @@


# This test script is designed to test the ftmain.sh file independent of fastTravelCLI's binary.
# Use by calling the simtest function.
# This script will build simulations to run through automatically.
# setup.sh and ftmain.sh need to be sourced prior to execution of these simulations. This step is included in the .bashrc Docker builds.
# To manually test specific commands, utilize the testcmd function found in setup.sh.
# This script will run through predefined set of commands to simulate a user journey.
# This is designed to run automatically with no interactivity in a docker container.



simtest() {
# User can set number of simulations or use default of 15
if [[ $# -ne 1 ]]; then
sims=15
# Source required scripts
source ~/.fzf.bash
source setup.sh
source ftmain.sh



# TODO
# - change this hashmap into two arrays so that order is maintained.
# - add check for 'already top of stack' and 'already bottom of stack'

# Test commands and their expected outputs
test_cmds=(
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ["
"ft ]"
"ft ]"
"ft ]"
"ft .."
"ft -"
"ft ]"
)

test_expected=(
"["
"["
"["
"["
"["
"["
"["
"["
"["
"["
"["
"["
"["
"[\nAlready at tail of history stack."
"]"
"]"
"]"
".."
"-"
"]\nAlready at head of history stack."
)

# Variable to hold overall test result
all_tests_passed=true

# Run commands, capture output, and compare to expected
i=0
for command in "${test_cmds[@]}"; do

# Create a temporary file to capture output
tempfile=$(mktemp)
# Run the command in the current shell, redirecting output to the tempfile
eval "$command" > "$tempfile" 2>&1
# Read the captured output into a variable
output=$(<"$tempfile")
# Clean up the temp file
rm "$tempfile"

# Compare output to expected
expected=$(echo -e "${test_expected[$i]}")
if [[ "$output" == "$expected" ]]; then
echo "PASS: '$command' output matched expected."
else
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Argument must be a number! Arg: $1" >&2
exit 1
fi
sims=$1
echo "FAIL: '$command' output did not match expected."
echo " Expected: $expected"
echo " Got: $output"
all_tests_passed=false
fi
i=$((i+1))
done

# Simulate fzf for the `ft hist` command
export FZF_DEFAULT_OPTS="--filter=testspace" # Automatically select the first match
hist_output=$(ft hist 2>&1 | head -n 2)
expected_hist_output=$(echo -e "hist\n/testspace")

# Compare fzf-based output
if [[ "$hist_output" == "$expected_hist_output" ]]; then
echo "PASS: 'ft hist' output matched expected."
else
echo "FAIL: 'ft hist' output did not match expected."
echo " Expected: $expected_hist_output"
echo " Got: $hist_output"
all_tests_passed=false
fi

# Overall test result
if $all_tests_passed; then
echo "All tests passed!"
else
echo "Some tests failed."
exit 1
fi

# Generate a random simulation
# Setup script will define the commands
generateSim() {
local totalcmds=$1
# Start the simulation more towards the middle of the history stack
local sim=("[" "[" "[" "[" "[" "[")
for ((i = 0; i < totalcmds; i++)); do
local idx=$(( RANDOM % ${#commands[@]} ))
sim+=("${commands[idx]}")
done
echo "${sim[@]}"
}

# Create the simulation
simulation=($(generateSim "$sims"))

# Process the commands
for cmd in "${simulation[@]}"; do
echo -e "--- History stack: \n $(ft__phist)\n-----"
echo -e "--- Currently at directory: $(pwd)\n--->"

ft "$cmd"

echo "<---"

done
}
16 changes: 4 additions & 12 deletions testenv/shellfuncs/bashenv/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ RUN apt-get update && apt-get install -y \
RUN git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && \
~/.fzf/install --all

# Set up fzf in the shell
RUN echo 'source ~/.fzf.bash' >> ~/.bashrc
# make test script executable
RUN chmod +x ./test.sh

# Source scripts used to run tests
RUN echo 'source setup.sh' >> ~/.bashrc

RUN echo 'source ftmain.sh' >> ~/.bashrc

RUN echo 'source test.sh' >> ~/.bashrc



CMD ["/bin/bash"]
# Run the test script
CMD ["/bin/bash", "-c", "./test.sh"]
10 changes: 0 additions & 10 deletions testenv/shellfuncs/bashenv/docker-compose.yaml

This file was deleted.

39 changes: 39 additions & 0 deletions testenv/shellfuncs/bashenv/interactive/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# run docker-compose from root dir
FROM ubuntu:latest

# Set non-interactive mode for apt-get
ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /testspace

COPY ../../../shells/tests/bashscripts/ ./

COPY ../../../shells/bash/ ./

RUN chmod +x ./exe.sh

# Update/install dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
tree \
lua5.4 \
&& rm -rf /var/lib/apt/lists/*

# Install fzf
RUN git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf && \
~/.fzf/install --all

# Set up fzf in the shell
RUN echo 'source ~/.fzf.bash' >> ~/.bashrc

# Source scripts used to run tests
RUN echo 'source setup.sh' >> ~/.bashrc

RUN echo 'source ftmain.sh' >> ~/.bashrc

RUN echo 'source interactive_test.sh' >> ~/.bashrc



CMD ["/bin/bash"]
25 changes: 25 additions & 0 deletions testenv/shellfuncs/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# run from project root dir
services:

fasttravelcli_bash_script_interactive:
build:
context: ../..
dockerfile: testenv/shellfuncs/bashenv/interactive/Dockerfile
container_name: fasttravelcli_bash_script_interactive
stdin_open: true
tty: true

fasttravelcli_bash_script:
build:
context: ../..
dockerfile: testenv/shellfuncs/bashenv/Dockerfile
container_name: fasttravelcli_bash_script
stdin_open: true
tty: true

fasttravelcli_ft_command:
build:
context: ../..
dockerfile: testenv/ft/Dockerfile
container_name: ft_cli_tool

0 comments on commit 126073f

Please sign in to comment.