Skip to content

Commit

Permalink
WIP - Improve test coverage (#80)
Browse files Browse the repository at this point in the history
Improve test coverage by adding a matrix build with supported bash versions and more tests
  • Loading branch information
brujoand authored Jul 12, 2020
1 parent 009ffc4 commit 3e2eba8
Show file tree
Hide file tree
Showing 29 changed files with 255 additions and 140 deletions.
24 changes: 16 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
language: bash
language: shell
dist: bionic
email: false
before_install:
- sudo apt-get install npm -y
- sudo npm install -g bats
script: ./bin/run_tests && ./bin/package
env:
- BASH=rc
- BASH=latest
- BASH=5.0.17
- BASH=4.4.23
- BASH=4.3.48

before_script:
- docker pull bash:$BASH
- docker run bash:$BASH --version
script:
- ./bin/run_linter && docker run -v $PWD:/bash bash:$BASH /bash/bin/ci_run_tests
deploy:
provider: releases
api_key:
script: ./bin/package
token:
secure: c5h0E4QkBWe7Bka6mkowP/z6QEiqwKGV5/E9MhX9COzxhtQGlDa7rYJ7AlS43o9sXOqJR7P8Hq5KJAXqYoOamKpXB7M+1IGlDjeVB8uHBKZ/53Iwj6ZUrnXvmIrbY3X6Cr2cK2JVkmKeHx4MhT3HZKCv+MGbA3EfcIghEk/KSi8GcP84oqMLLvOhXStvVgOh2n80w4KUlqXct7DWBTc8kZAhlp2jFXBXOTdk7ihwGzS98qvHyFEO+asW63PwqpCAj74Qmj9KECIStTWGr/ieArpIuoWMVB3yvmmmZkAcm0hckwdgnwcwS47MWo3PDoUO/QuAy0CoVdWGmkz+NekudXrN7xTuTgP0afA5VF0hX7DtDsu18BoiQonauAfImNz5dYVonqMXqHSVZXcwCuyAs/zfbpFVjrngUnPrZN+rD/NwdAB3EbTIFHdQz3dqsrFhKbeHzg2dQv6NNUn9FY/hD7kol2xODONLk0A8tnZwEVRLEKSA/aBPcjh1gq63im4DpONUpf2EjtHSWVJ/kaqiKgc+kQGeF/KdH36O57qP5ttiZuYmjftiNS2n6vlu2ODgQiBrwVlbTXOdR2+lYLrQKF26L7IvslLX7PJUG9S79OjSxob/+NyuFgKYbD0SUT7nN6enwS1UVi3vO+LOJkWmq3fFULjy3stbcreH9L48iTs=
file: sbp-${TRAVIS_TAG}.tar.gz
on:
tags: true
skip_cleanup: 'true'
cleanup: 'false'
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections && \
apt-get install -y git bash curl apt-utils dialog
apt-get install -y git bash curl apt-utils dialog vim

RUN adduser --system --shell /bin/bash --disabled-password sbp && \
apt-get install -y locales && \
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ of subshells. This led to return by reference which is now the standard way of
returning results in the code base. So if you're upgrading from an older version
it's probably a good idea to make a copy of your sbp config and start fresh.

## Hard Requirements
- Bash 4.3+

## Soft Requirements
If you want the fancy pointy segment separators, you need the powerline fonts _installed_ and _enabled_. Both.
You can get them [here](https://github.com/powerline/fonts) which also has
Expand All @@ -42,7 +45,7 @@ This will get you the latest release.
When you clone this repo, there is an install script located at ´bin/install´.
It will add two lines to `$HOME/.bashrc`:
```
SBP_PATH=/the/path/to/sbp/
SBP_PATH=/the/path/to/sbp
source ${SBP_PATH}/sbp.bash
```
You could also just add these two lines to some bash config file of your own
Expand All @@ -66,8 +69,8 @@ You can use the `sbp` command for a lot of things:
hooks - List all available hooks
themes - List all available color themes and layouts
edit
config - Opens the sbp config in \$EDITOR (${EDITOR:-'not set'})
colors - Opens the colors config in \$EDITOR (${EDITOR:-'not set'})
config - Opens the sbp config in $EDITOR
colors - Opens the colors config in $EDITOR
set
color - Set [color] for the current session
layout - Set [layout] for the current session
Expand Down
5 changes: 5 additions & 0 deletions bin/ci_run_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

apk -U add bats git

/bash/bin/run_tests
8 changes: 8 additions & 0 deletions bin/run_linter
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /usr/bin/env bash

set -e

SBP_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
export SBP_PATH

shellcheck -x --severity=error "${SBP_PATH}/sbp.bash" "${SBP_PATH}/src/main.bash"
4 changes: 1 addition & 3 deletions bin/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ set -e
SBP_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
export SBP_PATH

shellcheck -x --severity=error sbp.bash src/main.bash

bats -r test/
bats -r "${SBP_PATH}/test/"
6 changes: 3 additions & 3 deletions config/colors.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ SEGMENTS_HOST_COLOR_SECONDARY_HIGHLIGHT=$color0

SEGMENTS_PATH_COLOR_PRIMARY=$color14
SEGMENTS_PATH_COLOR_SECONDARY=$color7
SEGMENTS_PATH_SPLITTER_COLOR=$color4
SEGMENTS_PATH_COLOR_SPLITTER=$color4

SEGMENTS_PATH_READONLY_COLOR_SECONDARY=$color15
SEGMENTS_PATH_READONLY_COLOR_PRIMARY=$color1
Expand All @@ -43,8 +43,8 @@ SEGMENTS_K8S_COLOR_SECONDARY=$color7

SEGMENTS_RESCUETIME_COLOR_PRIMARY=$color11
SEGMENTS_RESCUETIME_COLOR_SECONDARY=$color4
SEGMENTS_RESCUETIME_SPLITTER_COLOR=$color7
SEGMENTS_RESCUETIME_COLOR_SPLITTER=$color7

SEGMENTS_WTTR_COLOR_PRIMARY=$color11
SEGMENTS_WTTR_COLOR_SECONDARY=$color4
SEGMENTS_WTTR_SPLITTER_COLOR=$color7
SEGMENTS_WTTR_COLOR_SPLITTER=$color7
22 changes: 0 additions & 22 deletions sbp.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,6 @@
# Simple Bash Prompt (SBP) #
#################################

# For catching installs using the old config
if [[ -z "$SBP_PATH" && -n "$sbp_path" ]]; then
read -r -d '' alert_text <<'EOF'
Unfortunatly your current configuration is incompatible with the latest version of SBP
Two changes are required:
1. Please backup and remove your configuration ${HOME}/.config/sbp
2. Please change the casing of the 'sbp_path' variable to 'SBP_PATH' in your '$HOME/.bashrc'
Start a new bash shell or source "${HOME}/.bashrc" when these changes have been made.
In the mean time, your prompt has ben set to '\h@\h:\w'
EOF
# Alert the user
>&2 printf '\e[38;5;76m%s\e[00m\n' "$alert_text"
# Set a usable prompt
PS1='\u@\h:\w '
# Override the prompt command
alias _sbp_set_prompt='true'
return 1
fi

# shellcheck source=src/interact.bash
source "${SBP_PATH}/src/interact.bash"
# shellcheck source=src/debug.bash
Expand Down
14 changes: 2 additions & 12 deletions src/colors/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
# Themes

## Creating your own theme layout
You can create your own theme layout by placing a file in:
```
${HOME}/.config/sbp/themes/layouts/${your_layout_name}.bash
```

Layouts define both which characters are being used, and how the prompt is
drawn. For instance if there should be one or two lines.

# Colors

## Creating your own theme colors
You can create your own theme layout by placing a file in:
```
${HOME}/.config/sbp/themes/colors/${your_colors_name}.bash
${HOME}/.config/sbp/themes/colors/${your_color_theme_name}.bash
```

Theme colors are basically 16 RGB values. And I've added a template
Expand Down
24 changes: 12 additions & 12 deletions src/configure.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ configure::list_feature_files() {
}

configure::get_feature_file() {
local -n return_value=$1
local -n get_feature_file_result=$1
local feature_type=$2
local feature_name=$3

local local_file="${SBP_PATH}/src/${feature_type}s/${feature_name}.bash"
local global_file="${SBP_CONFIG}/${feature_type}s/${feature_name}.bash"

if [[ -f "$local_file" ]]; then
return_value="$local_file"
get_feature_file_result="$local_file"
elif [[ -f "$global_file" ]]; then
return_value="$global_file"
get_feature_file_result="$global_file"
else
debug::log_error "Could not find $local_file"
debug::log_error "Could not find $global_file"
debug::log_error "Make sure at least on of them exists"
debug::log "Could not find $local_file"
debug::log "Could not find $global_file"
debug::log "Make sure at least on of them exists"
fi

}
Expand All @@ -57,7 +57,7 @@ configure::set_colors() {
if [[ -n "$colors_file" ]]; then
source "$colors_file"
else
debug::log_info "Using the default color config"
debug::log "Using the default color config"
source "${SBP_PATH}/src/colors/default.bash"
fi
}
Expand All @@ -71,7 +71,7 @@ configure::set_layout() {
if [[ -n "$layout_file" ]]; then
source "$layout_file"
else
debug::log_info "Using the default layout"
debug::log "Using the default layout"
source "${SBP_PATH}/src/layouts/plain.bash"
fi
}
Expand All @@ -80,14 +80,14 @@ configure::load_config() {
[[ -d "$SBP_CACHE" ]] || mkdir -p "$SBP_CACHE"

if [[ ! -f "$config_file" ]]; then
debug::log_info "Config file not found: ${config_file}"
debug::log_info "Creating it.."
debug::log "Config file not found: ${config_file}"
debug::log "Creating it.."
cp "$default_config_file" "$config_file"
fi

if [[ ! -f "$colors_file" ]]; then
debug::log_info "Color config file not found: ${colors_file}"
debug::log_info "Creating it.."
debug::log "Color config file not found: ${colors_file}"
debug::log "Creating it.."
cp "$default_colors_file" "$colors_file"
fi

Expand Down
25 changes: 12 additions & 13 deletions src/debug.bash
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
#! /usr/bin/env bash

debug::log() {
local timestamp file function
timestamp=$(date +'%y.%m.%d %H:%M:%S')
file="${BASH_SOURCE[1]##*/}"
function="${FUNCNAME[1]}"
>&2 printf '\n[%s] [%s - %s]: \e[31m%s\e[0m\n' "$timestamp" "$file" "$function" "${*}"
}

if [[ "$OSTYPE" == "darwin"* ]]; then
date_cmd='gdate'
if type -P gdate &>/dev/null; then
date_cmd='gdate'
fi
else
date_cmd='date'
fi

debug::log_error() {
local timestamp=$(date +'%y.%m.%d %H:%M:%S')
local context="${timestamp}:${BASH_SOURCE[1]}:${FUNCNAME[1]}"
>&2 printf '%s: \e[38;5;196m%s\e[00m\n' "${context}" "${*}"
}

debug::log_info() {
local timestamp=$(date +'%y.%m.%d %H:%M:%S')
local context="${timestamp}:${BASH_SOURCE[1]}:${FUNCNAME[1]}"
>&2 printf '%s: \e[38;5;76m%s\e[00m\n' "${context}" "${*}"
}

debug::start_timer() {
timer_start=$("$date_cmd" +'%s%3N')
}

debug::tick_timer() {
[[ -z "$date_cmd" ]] && return 0
timer_stop=$("$date_cmd" +'%s%3N')
timer_spent=$(( timer_stop - timer_start))
>&2 echo "${timer_spent}ms: $1"
Expand Down
69 changes: 32 additions & 37 deletions src/decorate.bash
Original file line number Diff line number Diff line change
@@ -1,77 +1,72 @@
#! /usr/bin/env bash

decorate::calculate_complementing_color() {
local -n return_value=$1
local -n calculate_complementing_color_result=$1
local source_color=$2
input_colors=()
output_colors=()

if [[ -z "${source_color//[0123456789]}" ]]; then
# This is not accurate
return_value="$(( 255 - source_color ))"
calculate_complementing_color_result="$(( 255 - source_color ))"
else
mapfile -t input_colors < <(tr ';' '\n' <<< "$source_color")
r_lum=$(( ${input_colors[0]} * 2126 ))
g_lum=$(( ${input_colors[1]} * 7152 ))
b_lum=$(( ${input_colors[2]} * 722 ))
local red=${input_colors[0]}
local green=${input_colors[1]}
local blue=${input_colors[2]}

lum=$(( r_lum + g_lum + b_lum ))
red=$(( red * 2126 ))
green=$(( green * 7152 ))
blue=$(( blue * 722 ))

lum=$(( red + green + blue ))
if [[ "$lum" -gt 1400000 ]]; then
return_value='0;0;0'
calculate_complementing_color_result='0;0;0'
else
return_value='255;255;255'
calculate_complementing_color_result='255;255;255'
fi

fi
}

decorate::print_colors() {
local -n return_value=$1
local -n print_colors_result=$1
local fg_code=$2
local bg_code=$3
local fg_color bg_color

decorate::print_fg_color 'fg_color' "$fg_code"
decorate::print_bg_color 'bg_color' "$bg_code"
return_value="${fg_color}${bg_color}"
print_colors_result="${fg_color}${bg_color}"
}

decorate::print_bg_color() {
local -n return_value=$1
local -n print_bg_color_result=$1
local bg_code=$2
local escaped=$3

if [[ -z "$bg_code" ]]; then
bg_escaped="\e[49m"
elif [[ -z "${bg_code//[0123456789]}" ]]; then
bg_escaped="\e[48;5;${bg_code}m"
else
bg_escaped="\e[48;2;${bg_code}m"
fi

if [[ -z "$escaped" ]]; then
return_value="\[${bg_escaped}\]"
else
return_value="${bg_escaped}"
fi
decorate::format_color 'print_bg_color_result' 48 "$bg_code"
}

decorate::print_fg_color() {
local -n return_value=$1
local -n print_fg_color_result=$1
local fg_code=$2
local escaped=$3

if [[ -z "$fg_code" ]]; then
fg_escaped="\e[39m"
elif [[ -z "${fg_code//[0123456789]}" ]]; then
fg_escaped="\e[38;5;${fg_code}m"
else
fg_escaped="\e[38;2;${fg_code}m"
fi
decorate::format_color 'print_fg_color_result' 38 "$fg_code"
}

decorate::format_color() {
local -n format_color_result=$1
local color_code=$2
local color_value=$3
local color_reset_code=$(( color_code + 1 ))

if [[ -z "$escaped" ]]; then
return_value="\[${fg_escaped}\]"
if [[ -z "$color_value" ]]; then
format_color_result="\e[${color_reset_code}m"
elif [[ -z "${color_value//[0123456789]}" ]]; then
format_color_result="\e[${color_code};5;${color_value}m"
else
return_value="${fg_escaped}"
format_color_result="\e[${color_code};2;${color_value}m"
fi

format_color_result="\[${format_color_result}\]"
}
Loading

0 comments on commit 3e2eba8

Please sign in to comment.