From c755d092f1c3c38a665ea22da054d99d4b119855 Mon Sep 17 00:00:00 2001 From: Travis Goldie Date: Sat, 14 Jan 2017 15:17:04 -0500 Subject: [PATCH] Format improvements, Doc update, unit tests! - Added unit tests and suites - Updated the README - Whitespace fixes --- README.md | 20 +++--- run-tests.sh | 6 ++ scripts/download_speed.sh | 1 - scripts/helpers.sh | 115 ++++++++++++++++-------------- scripts/net_speed.sh | 4 +- scripts/upload_speed.sh | 1 - tests/suites/helpers.test.sh | 132 +++++++++++++++++++++++++++++++++++ tests/test_utils.sh | 91 ++++++++++++++++++++++++ 8 files changed, 302 insertions(+), 68 deletions(-) create mode 100755 run-tests.sh create mode 100755 tests/suites/helpers.test.sh create mode 100644 tests/test_utils.sh diff --git a/README.md b/README.md index dfe2226..838828d 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,7 @@ Tmux plugin to monitor upload and download speed of one or all interfaces. ## Usage - Add one of the following format string to `status-right` tmux option. -NOTE: Currently sums "eth0" and "wlan0" interfaces ## Special Credit This plugin is roughly based on the various plugins in [https://github.com/tmux-plugins]("tmux-plugins"). @@ -15,27 +13,29 @@ Shows value in either MB/s, KB/s, or B/s. - `#{download_speed}` - Shows only download speed, - `#{upload_speed}` - Shows only upload speed, - `#{net_speed}` - Shows both the upload and download speeds. - example: "D: 123 MB/s U: 25 MB/s" + **Example**: "D: 123 MB/s U: 25 MB/s" NOTE: Shows value in either MB/s, KB/s, or B/s. ## Past Values Since this is a difference, the old values are stored in files in `/tmp/`. The user must be able to read and write to this directory. -### Set Interfaces +### Set Options To change which interfaces to pull from, set the following options in your `.tmux.conf`. -Use a commaseperated list. +Use a comma separated list. If not set, grabs all the interfaces listed in "/sys/class/net/" + + set -g @net_speed_inf "eth0,eth1" -- set -g @net_speed_inf "eth0,eth1" +To change the formatter sting passed to `printf`. Default is "D:%10s U:%10s" -**DEFAULT**: "eth0,wlan0" + set -g @net_speed_format "Down: %100s" ### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended) Add plugin to the list of TPM plugins in `.tmux.conf`: - set -g @plugin 'tmux-plugins/tmux-cpu' + set -g @plugin 'tmux-plugins/tmux-net-speed' Hit `prefix + I` to fetch the plugin and source it. @@ -51,9 +51,8 @@ Add this line to the bottom of `.tmux.conf`: run-shell ~/clone/path/net_speed.tmux -Reload TMUX environment: +Reload TMUX environment (type this in terminal) - # type this in terminal $ tmux source-file ~/.tmux.conf If format strings are added to `status-right`, they should now be visible. @@ -64,6 +63,7 @@ This plugin stores the total output for all the interfaces in a file in `/tmp/`. ### TODO +- Add unit tests - Add error handling - Configure which interfaces to calculate - Configure format string for `#{net_speed}` diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..70f0d41 --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,6 @@ +#!/bin/bash - +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +for testSuite in $CURRENT_DIR/tests/suites/* ; do + $testSuite +done diff --git a/scripts/download_speed.sh b/scripts/download_speed.sh index d9aa296..0d88dea 100755 --- a/scripts/download_speed.sh +++ b/scripts/download_speed.sh @@ -18,7 +18,6 @@ main() local new_val=$(sum_download_speed) write_file $file $new_val - get_velocity $new_val $old_val } main diff --git a/scripts/helpers.sh b/scripts/helpers.sh index dcdb18d..cd57cd6 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -7,20 +7,21 @@ DOWNLOAD_FILE="/tmp/tmux_net_speed.download" UPLOAD_FILE="/tmp/tmux_net_speed.upload" get_tmux_option() { - local option="$1" - local default_value="$2" - local option_value="$(tmux show-option -gqv "$option")" - if [[ -z "$option_value" ]]; then - echo "$default_value" - else - echo "$option_value" - fi + local option=$1 + local default_value=$2 + local option_value="$(tmux show-option -gqv "$option")" + + if [[ -z "$option_value" ]]; then + echo "$default_value" + else + echo "$option_value" + fi } set_tmux_option() { - local option=$1 - local value=$2 - tmux set-option -gq "$option" "$value" + local option=$1 + local value=$2 + tmux set-option -gq "$option" "$value" } get_velocity() @@ -33,13 +34,13 @@ get_velocity() local MILLION=1048576 local vel=$(( new_value - old_value )) - local velKB=$(( vel / THOUSAND )) - local velMB=$(( vel / MILLION )) + local vel_kb=$(( vel / THOUSAND )) + local vel_mb=$(( vel / MILLION )) - if [[ $velMB != 0 ]] ; then - echo -n "$velMB MB/s" - elif [[ $velKB != 0 ]] ; then - echo -n "$velKB KB/s"; + if [[ $vel_mb != 0 ]] ; then + echo -n "$vel_mb MB/s" + elif [[ $vel_kb != 0 ]] ; then + echo -n "$vel_kb KB/s"; else echo -n "$vel B/s"; fi @@ -49,47 +50,55 @@ get_velocity() # is empty, or not readable, starts back at 0 read_file() { - local path="$1" - local val=0 - - if [[ ! -f "$path" ]] ; then - echo $val - return - elif [[ ! -r "$path" ]]; then - echo $val - return - fi - - # Ok, file exists and is readdable. Check contents - tmp=$(< "$path") - if [[ "x${tmp}" == "x" ]] ; then - echo $val - return - fi - - # else all good, echo value - echo $tmp + local path="$1" + local fallback_val=0 + + # File exists and is readdable? + if [[ ! -f "$path" ]] ; then + echo $fallback_val + return 1 + elif [[ ! -r "$path" ]]; then + echo $fallback_val + return 1 + fi + + + # Does the file have content? + tmp=$(< "$path") + if [[ "x${tmp}" == "x" ]] ; then + echo $fallback_val + return 1 + fi + + # Now return known value + echo $tmp } # Update values in file write_file() { - local path="$1" - local val="$2" + local path="$1" + local val="$2" - # TODO Add error checking - echo "$val" > "$path" + # TODO Add error checking + echo "$val" > "$path" +} + +get_interfaces() +{ + local interfaces="" + for interface in /sys/class/net/*; do + interfaces+=$(echo $(basename $interface) " "); + done + + # Do not quote the variable. This way will handle trailing whitespace + echo -n $interfaces } sum_speed() { local column=$1 - - # TODO Make this a parameter option. Set through tmux config - declare -a interfaces=() - for interface in /sys/class/net/*; do - interfaces+=("$(basename $interface)"); - done + declare -a interfaces=$(get_interfaces) local line="" local val=0 @@ -98,21 +107,19 @@ sum_speed() speed="$(echo -n $line | cut -d' ' -f $column)" let val+=${speed:=0} done - + echo $val } is_osx() { - [ $(uname) == "Darwin" ] + [ $(uname) == "Darwin" ] } is_cygwin() { - command -v WMIC > /dev/null + command -v WMIC > /dev/null } command_exists() { - local command="$1" - type "$command" >/dev/null 2>&1 + local command="$1" + type "$command" >/dev/null 2>&1 } - - diff --git a/scripts/net_speed.sh b/scripts/net_speed.sh index d9fa52f..60b0dc5 100755 --- a/scripts/net_speed.sh +++ b/scripts/net_speed.sh @@ -7,8 +7,8 @@ main() { local download=$("$CURRENT_DIR/download_speed.sh") local upload=$("$CURRENT_DIR/upload_speed.sh") + local format=$(get_tmux_option net_speed_format "D:%10s U:%10s") - # TODO - make the text format configurable - printf "D:%10s U:%10s" "$download" "$upload" + printf "$format" "$download" "$upload" } main diff --git a/scripts/upload_speed.sh b/scripts/upload_speed.sh index 254d350..bf6a47b 100755 --- a/scripts/upload_speed.sh +++ b/scripts/upload_speed.sh @@ -18,7 +18,6 @@ main() local new_val=$(sum_upload_speed) write_file $file $new_val - get_velocity $new_val $old_val } main diff --git a/tests/suites/helpers.test.sh b/tests/suites/helpers.test.sh new file mode 100755 index 0000000..a68bbb1 --- /dev/null +++ b/tests/suites/helpers.test.sh @@ -0,0 +1,132 @@ +#!/bin/bash - + +CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPTS="$CURRENT_DIR/../../scripts" +source "$CURRENT_DIR/../test_utils.sh" + +## +# Description: +# General setup before each test +## +setup() +{ + source "$SCRIPTS/helpers.sh" +} + +## +# get_tmux_option +## +setup_get_tmux_option() +{ + setup + + ## Set generic property + tmux set @get_tmux_option_name "get_tmux_option_val" +} + +cleanup_get_tmux_option() +{ + # Unset value + tmux set -u @get_tmux_option_name +} + +test_get_tmux_option() +{ + ## + # Should return set value + ## + setup_get_tmux_option + + result=$( get_tmux_option "@get_tmux_option_name") + assertEqual "$result" "get_tmux_option_val" "should get tmux prop when given no default" -v + + cleanup_get_tmux_option + + ## + # Should show set value + ## + setup_get_tmux_option + + result=$( get_tmux_option "@get_tmux_option_name_unset") + assertEqual "$result" "" "should get empty string when tmux prop unset" + + cleanup_get_tmux_option + + ## + # Should show set value + ## + setup_get_tmux_option + + result=$( get_tmux_option @get_tmux_option_name_unset my_default) + assertEqual "$result" "my_default" "should get default if provided for unset prop" + + cleanup_get_tmux_option +} + +## +# get_velocity +## +setup_get_velocity() +{ + setup +} + +cleanup_get_velocity() { :; } + +test_get_velocity() +{ + setup_get_velocity + let input=200 + result=$(get_velocity $input 0) + assertEqual "$result" "200 B/s" "should show B/s" + cleanup_get_velocity + + setup_get_velocity + let input=2*1024 + result=$(get_velocity $input 0) + assertEqual "$result" "2 KB/s" "should show KB/s" + cleanup_get_velocity + + setup_get_velocity + let input=3*1048576 + result=$(get_velocity $input 0) + assertEqual "$result" "3 MB/s" "should show MB/s" + cleanup_get_velocity + + + setup_get_velocity + let subtract_from=100*1048576 + let subtract_with=50*1048576 + result=$(get_velocity $subtract_from subtract_with) + assertEqual "$result" "50 MB/s" "should show MB/s if subraction done" + cleanup_get_velocity +} + +## +# get_interfaces +## +setup_get_interfaces() +{ + setup +} + +cleanup_get_interfaces() { :; } + +test_get_interfaces() +{ + setup_get_interfaces + result=$(get_interfaces) + assertEqual "$result" "eth0 lo wlan0" "should output space-delimited list of interfaces" -v + cleanup_get_interfaces +} + +suites() +{ + #test_get_tmux_option + #test_get_velocity + test_get_interfaces +} + +# Run tests +suites + diff --git a/tests/test_utils.sh b/tests/test_utils.sh new file mode 100644 index 0000000..045d3e5 --- /dev/null +++ b/tests/test_utils.sh @@ -0,0 +1,91 @@ +#!/bin/bash - +############################################################################### +# Author: Travis Goldie +# Purpose: Util funcs for unit tests +############################################################################### +## +# Description: +# Asserts if `$1` equals `$2` +## +assertEqual() +{ + local input=$1 + local expected=$2 + local msg=$3 + + if [[ "$4" == "-v" ]] ; then + echo $@ + fi + + if [[ -z msg ]] ; then + msg="Failed test" + fi + + test "$1" == "$2" + err=$? + + if [[ $err == 0 ]] ; then + echo "[PASSED]: ${msg}" + echo + return $err + fi + + if [[ $err != 0 ]] ; then + echo "[FAILED]: ${msg}" + echo + return $err + fi +} + +## +# Description: +# Asserts if `$1` not equals `$2` +## +assertNotEqual() +{ + local input=$1 + local expected=$2 + local msg=$3 + + if [[ -z msg ]] ; then + msg="Failed test" + fi + + test "$1" != "$2" + err=$? + + if [[ $err == 0 ]] ; then + echo "[PASSED]: ${msg}" + echo + return $err + fi + + if [[ $err != 0 ]] ; then + echo "[FAILED]: ${msg}" + echo + return $err + fi +} + +## +# @description +# Checks to see if error code is non-zero. If so, echo message and exit +# with the given error code. +# +# ``` bash +# $ someCmd blah blah2 +# $ err=$? +# $ die $err "If err is non-zero this script will exit" +# ``` +## +die() +{ + local err=$1 + local msg=$2 + local name=$( basename $0 ) + + if [[ $err != 0 ]] ; then + echo "[ERROR]:${name}:code $err:${msg}" + exit $err + fi +}