diff --git a/lyrics/lyrics b/lyrics/lyrics index be3962d..9f218b1 100755 --- a/lyrics/lyrics +++ b/lyrics/lyrics @@ -4,6 +4,7 @@ currentVersion="1.18.1" configuredClient="" artist="false" song="false" +filePath="" ## This function determines which http get tool the system has installed and returns an error if there isnt one getConfiguredClient() @@ -112,16 +113,16 @@ getLyrics() printLyrics() { - if [ -z $filePath ];then echo -e "$lyrics" + if [[ $filePath == "" ]];then echo -e "$lyrics" else - if [ -f $filePath ];then + if [ -f "$filePath" ];then echo -n "File already exists, do you want to overwrite it [Y/n]: " read -r answer if [[ "$answer" == [Yy] ]]; then - echo -e "$lyrics" > $filePath; + echo -e "$lyrics" > "$filePath"; fi else - echo -e "$lyrics" > $filePath; + echo -e "$lyrics" > "$filePath"; fi fi } @@ -156,20 +157,24 @@ while getopts "f:a:s:uvh" opt; do v) echo "Version $currentVersion" exit 0 ;; - u) checkInternet || exit 1 + u) + getConfiguredClient || exit 1 + checkInternet || exit 1 update exit 0 ;; f) - filePath=$OPTARG + filePath="$OPTARG" ;; a) artist="true" if [[ "$(echo "$@" | grep -Eo "\-s")" == "-s" ]];then song="true";fi # wont go through both options if arg spaced and not quoted this solves that issue (dont need this but once had bug on system where it was necessary) + if [[ "$(echo "$@" | grep -Eo "\-f")" == "-f" ]];then filePath=$(echo "$@" | grep -Eo "\-f [ a-z A-Z / 0-9 . \ ]*[ -]?" | sed s/-f//g | sed s/-//g | sed s/^" "//g);fi ;; s) song="true" if [[ "$(echo "$@" | grep -Eo "\-a")" == "-a" ]];then artist="true";fi # wont go through both options if arg spaced and not quoted this solves that issue (dont need this but once had bug on system where it was necessary) + if [[ "$(echo "$@" | grep -Eo "\-f")" == "-f" ]];then filePath=$(echo "$@" | grep -Eo "\-f [ a-z A-Z / 0-9 . \ ]*[ -]?" | sed s/-f//g | sed s/-//g | sed s/^" "//g);fi ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 diff --git a/tests/lyrics.bats b/tests/lyrics.bats new file mode 100755 index 0000000..b92314c --- /dev/null +++ b/tests/lyrics.bats @@ -0,0 +1,39 @@ +#!/bin/env bats + +@test "Testing lyrics tool" { + echo lyrics +} + +@test "Check for latest version of bash-snippets on update" { + if [[ "$(uname)" == "Linux" ]];then + run lyrics update + [ "$status" -eq 0 ] + [ "$output" = "Bash-Snippets is already the latest version" ] +fi +} + +@test "The -h option should print usage" { + run lyrics -h + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Lyrics" ] +} + +@test "No arguments prints usage instructions" { + run lyrics + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Lyrics" ] +} + +@test "Getting some lyrics" { + run lyrics -a logic -s run it + [ "$status" -eq 0 ] + [ "${lines[0]}" = "Me and my team gotta intervene" ] +} + +@test "Getting some lyrics to file" { + rm -f ~/templyrics.txt + run lyrics -a logic -s run it -f ~/templyrics.txt + [ "$status" -eq 0 ] + [ "$(cat $HOME/templyrics.txt | grep -Eo "Me and my team gotta intervene")" = "Me and my team gotta intervene" ] + rm -f ~/templyrics.txt +}