Skip to content

Commit

Permalink
✨ (Dockerfile): add BATS testing framework and test directory to cont…
Browse files Browse the repository at this point in the history
…ainer

🔧 (Dockerfile): set entrypoint to run BATS tests
📝 (README.md): update instructions for running tests and using autocomplete
⬆️ (autocomplete.sh): bump version from 0.3.4 to 0.3.5
✨ (autocomplete.sh): add support for multiple API keys and providers
✨ (autocomplete.sh): enhance model_command to accept provider and model name
🔧 (docs/install.sh): allow specifying branch or version for installation
✅ (tests/test_autocomplete.bats): add comprehensive BATS tests for autocomplete.sh
  • Loading branch information
closedLoop committed Jul 22, 2024
1 parent 149c4ce commit f3c09e8
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 24 deletions.
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ LABEL maintainer="sean@closedloop.tech"

# Update the package list and install wget and bash
RUN apt-get update && \
apt-get install -y wget bash jq bash-completion vim && \
apt-get install -y wget bash jq bash-completion vim bats && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Create directory for tests
RUN mkdir tests
# Copy the BATS tests to the container
COPY tests tests

# Add bash-completion sourcing to .bashrc
RUN echo "\nif [ -f /etc/bash_completion ] && ! shopt -oq posix; then\n . /etc/bash_completion\nfi" >> /root/.bashrc

# Set the default command to bash
CMD ["bash"]

# Set the entrypoint to run BATS tests
ENTRYPOINT ["bats", "tests"]
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,16 @@ Tests via Bats
Test Installation

docker build -t autocomplete-sh .
docker run -it autocomplete-sh
. autocomplete.sh install

# To run tests
docker run --rm -e OPENAI_API_KEY=$OPENAI_API_KEY autocomplete-sh

# To use the autocomplete
docker run --rm -it \
-e OPENAI_API_KEY=$OPENAI_API_KEY \
--entrypoint /bin/bash \
autocomplete-sh

autocomplete --help

### Inspiration
Expand Down
61 changes: 46 additions & 15 deletions autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Do not use `set -euo pipefail` or similar because this a
# bash completion script and it will change the behavior of the shell invoking it

export ACSH_VERSION=0.3.4
export ACSH_VERSION=0.3.5

###############################################################################
#
Expand Down Expand Up @@ -864,11 +864,28 @@ load_config() {
key=${key//[^[:alnum:]]/_}

# Set the variable dynamically if it's not api_key or if api_key is not empty
if [[ $key != "api_key" ]] || [[ -n $value ]]; then
if [[ -n $value ]]; then
export "ACSH_$key"="$value"
fi
done < "$config_file"

# Assign the API keys
if [[ -z "$ACSH_OPENAI_API_KEY" && -n "$OPENAI_API_KEY" ]]; then
export ACSH_OPENAI_API_KEY="$OPENAI_API_KEY"
fi
# Anthropic
if [[ -z "$ACSH_ANTHROPIC_API_KEY" && -n "$ANTHROPIC_API_KEY" ]]; then
export ACSH_ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY"
fi
# Groq
if [[ -z "$ACSH_GROQ_API_KEY" && -n "$GROQ_API_KEY" ]]; then
export ACSH_GROQ_API_KEY="$GROQ_API_KEY"
fi
# Ollama
if [[ -z "$ACSH_CUSTOM_API_KEY" && -n "$LLM_API_KEY" ]]; then
export ACSH_CUSTOM_API_KEY="$LLM_API_KEY"
fi

# Now assign ACSH_ACTIVE_API_KEY based on ACSH_PROVIDER
case "${ACSH_PROVIDER:-openai}" in
"openai")
Expand All @@ -881,7 +898,7 @@ load_config() {
export ACSH_ACTIVE_API_KEY="$ACSH_GROQ_API_KEY"
;;
"ollama")
export ACSH_ACTIVE_API_KEY="$ACSH_CUSTOM_API_KEY"
export ACSH_ACTIVE_API_KEY="$ACSH_OLLAMA_API_KEY"
;;
*)
echo_error "Unknown provider: $ACSH_PROVIDER"
Expand Down Expand Up @@ -920,6 +937,7 @@ Please follow the install instructions on https://github.com/closedloop-technolo

# $HOME/.autocomplete/config
build_config
load_config

# Append autocomplete.sh setup to .bashrc if it doesn't exist
if ! grep -qF "$autocomplete_setup" "$bashrc_file"; then
Expand Down Expand Up @@ -1253,18 +1271,31 @@ model_command() {
clear
local selected_model options=()

# Collect and sort the keys
mapfile -t sorted_keys < <(for key in "${!_autocomplete_modellist[@]}"; do echo "$key"; done | sort)
if [[ $# -ne 3 ]]; then
# Collect and sort the keys
mapfile -t sorted_keys < <(for key in "${!_autocomplete_modellist[@]}"; do echo "$key"; done | sort)

for key in "${sorted_keys[@]}"; do
options+=("$key")
done

echo -e "\e[1;32mAutocomplete.sh - Model Configuration\e[0m"
menu_selector "${options[@]}"
selected_option=$?
echo -e "\e[1;32mAutocomplete.sh - Model Configuration\e[0m"
selected_model="${options[selected_option]}"
selected_value="${_autocomplete_modellist[$selected_model]}"
else
provider="$2"
model_name="$3"
selected_value="${_autocomplete_modellist["$provider:\t$model_name"]}"

if [[ -z "$selected_value" ]]; then
echo "ERROR: Invalid provider or model name."
return 1
fi
fi

for key in "${sorted_keys[@]}"; do
options+=("$key")
done
echo -e "\e[1;32mAutocomplete.sh - Model Configuration\e[0m"
menu_selector "${options[@]}"
selected_option=$?
echo -e "\e[1;32mAutocomplete.sh - Model Configuration\e[0m"
selected_model="${options[selected_option]}"
selected_value="${_autocomplete_modellist[$selected_model]}"
set_config "model" "$(echo "$selected_value" | jq -r '.model')"
set_config "endpoint" "$(echo "$selected_value" | jq -r '.endpoint')"
set_config "provider" "$(echo "$selected_value" | jq -r '.provider')"
Expand Down Expand Up @@ -1367,7 +1398,7 @@ usage)
usage_command
;;
model)
model_command
model_command "$@"
;;
config)
config_command "$@"
Expand Down
5 changes: 3 additions & 2 deletions docs/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# This install script downloads the latest version of the LLMs

# The URL of the latest version of the LLMs
ACSH_VERSION="0.3.4"
URL="https://raw.githubusercontent.com/closedloop-technologies/autocomplete-sh/v${ACSH_VERSION}/autocomplete.sh"
ACSH_VERSION="v0.3.4"
BRANCH_OR_VERSION=${1:-$ACSH_VERSION}
URL="https://raw.githubusercontent.com/closedloop-technologies/autocomplete-sh/${BRANCH_OR_VERSION}/autocomplete.sh"

# The default location to install the LLMs
INSTALL_LOCATION="$HOME/.local/bin/autocomplete"
Expand Down
57 changes: 53 additions & 4 deletions tests/test_autocomplete.bats
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
# tests/test_autocomplete.bats
#!/usr/bin/env bats

@test "Autocomplete script runs without errors" {
run ./autocomplete.sh
[ "$status" -eq 0 ]
# Set a per-test timeout of 10 seconds
export BATS_TEST_TIMEOUT=10

setup() {
# Install autocomplete.sh and run testing against the main branch
wget -qO- https://autocomplete.sh/install.sh | bash -s -- main

# Source bashrc to make sure autocomplete is available in the current session
source ~/.bashrc
}

teardown() {
# Remove autocomplete.sh installation
autocomplete remove
}

@test "which autocomplete returns something" {
run which autocomplete
[ "$status" -eq 0 ]
[ -n "$output" ]
}

@test "autocomplete returns a string containing autocomplete.sh (case insensitive)" {
run autocomplete
[ "$status" -eq 0 ]
[[ "$output" =~ [Aa]utocomplete\.sh ]]
}

@test "autocomplete config should not have the word DISABLED" {
run autocomplete config
[ "$status" -eq 0 ]
[[ ! "$output" =~ DISABLED ]]
}

@test "autocomplete model gpt4o-mini and then config should have the string gpt4o-mini" {
run autocomplete model openai gpt-4o-mini
[ "$status" -eq 0 ]

run autocomplete config
[ "$status" -eq 0 ]
[[ "$output" =~ gpt4o-mini ]]
}

@test "autocomplete command 'ls # show largest files' should return something" {
run autocomplete command "ls # show largest files"
[ "$status" -eq 0 ]
[ -n "$output" ]
}

@test "autocomplete config sets environment variables" {
run env | grep ACSH | wc -l
[ "$status" -eq 0 ]
[ "$output" -gt 1 ]
}

0 comments on commit f3c09e8

Please sign in to comment.