Skip to content

Commit

Permalink
Add unit test + travisfile
Browse files Browse the repository at this point in the history
This closes #1
This closes #2
  • Loading branch information
Lilja authored and Erik lilja committed Jun 25, 2017
1 parent 28339f3 commit 25cf70c
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 40 deletions.
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: bash
sudo: false
before_script:
- chmod +x test/unittest.sh
- chmod +x bin/timelog
- export PATH=$PATH:$PWD/bin/

script:
- export PATH=$PATH:$PWD/bin/
- pushd test/
- source unittest.sh
- popd
179 changes: 139 additions & 40 deletions bin/timelog
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,56 @@ function list_projects {
done
}

function get_project_meta_info {
file=$(cat "$log_path/def/$1")
target_hours=$(echo "$file" | grep -o 'target_hours\ *\=\ *.*' | cut -d '=' -f 2- | awk '{$1=$1};1')
money=$(echo "$file" | grep -o 'money_per_hour\ *\=\ *.*' | cut -d '=' -f 2- | awk '{$1=$1};1')
logger_debug "target_hours: $target_hours"
logger_debug "Money: $money"
}

function week_stats {
# $1 = the week to look at
# $2 = content of a project file(string)
# RELIES ON THAT $target_hours has been set!
l=$(echo "$2" | grep "2017-$1-[1-7]*\/" | grep -o '\[[0-9]\+\.*[0-9]*\]')
if [ -z $target_hours ]; then { echo "Target hours not specified." ; exit 1 ; } fi

dec_time=0
for f in $l; do
k=$(echo "$f" | grep -o '[0-9]*\.*[0-9]*')
dec_time=$(awk "BEGIN {print ($hours+$k)}")
done

weekly_info=$(weekly_stats_days "$1" "$2")
amount_of_days_worked=$(echo $weekly_info | wc -w)
days=$((5-$amount_of_days_worked))
remaining=$(awk "BEGIN {print ($target_hours-$dec_time)}")
estimate=$(awk "BEGIN {print ($remaining/$days)}")

if [ $(awk 'BEGIN{ if (ARGV[1]>0) { print 0 } else { print 1 } }' "$dec_time") -eq 0 ]; then
echo "You have worked for $dec_time hours at the following days: $(weekly_stats_days "$1" "$2")"
echo "You have $remaining hours out of $target_hours hours giving you an estimate of"
echo "$estimate hours for $days more days."
[ ! -z "$money" ] && {
echo "You have earned $(awk "BEGIN {print ($dec_time*$money)}") pre-tax!"
}
else
echo "Nothing worked on week $1 for project $proj_name"
fi
}

function weekly_stats_days {
k=(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)
days=$(echo "$2" | grep -o "2017-$1-[1-7]*\/" | grep -o '\-[1-7]\/' | grep -o '[1-7]' | sort)
day_str=""
for day in $days; do
day_str="$day_str, ${k[((day-1))]}"
done
day_str=$(echo "$day_str" | sed 's/^,\ //')
echo "$day_str"
}

function get_all_projects {
all_projects=$(list_projects)
}
Expand All @@ -61,35 +111,39 @@ function does_project_exist {

function delete_project {
if [ ! -z "$all_projects" ]; then
echo "The projects"
echo -e "$all_projects"

echo "Which project do you want deleted?"
read proj

echo "Are you sure you want to delete it? (y/n)"
read ans
if [ "$ans" = "y" ]; then
logger_debug "Sending $proj as param info to project info"
proj=$(get_project_from_all_projects $proj)
logger_debug "Matched '$proj'"
[ -f "$log_path/def/$proj" ] && {
rm "$log_path/$proj.logs" ;
rm "$log_path/def/$proj" ;
logger_debug "Deleting logs '$proj_name.logs'" ;
logger_debug "Deleting definition 'def/$proj_name'" ;
all_projects=$(list_projects) ;
} || {
echo "No such project file: '$log_path/def/$proj'" ;
}
fi
echo "The projects"
echo -e "$all_projects"

echo "Which project do you want deleted?"
read proj

echo "Are you sure you want to delete it? (y/n)"
read ans
if [ "$ans" = "y" ]; then
logger_debug "Sending $proj as param info to project info"
proj=$(get_project_from_all_projects $proj)
logger_debug "Matched '$proj'"
[ -f "$log_path/def/$proj" ] && {
rm "$log_path/$proj.logs" ;
rm "$log_path/def/$proj" ;
logger_debug "Deleting logs '$proj_name.logs'" ;
logger_debug "Deleting definition 'def/$proj_name'" ;
all_projects=$(list_projects) ;
} || {
echo "No such project file: '$log_path/def/$proj'" ;
}
fi
else
echo "No projects, can't delete nothing that doesn't exist!"
echo "No projects, can't delete nothing that doesn't exist!"
fi
}

function get_project_from_all_projects {
echo "$all_projects" | grep "^$1" | grep -o ':\ .*\[' | sed 's#^:\ *\(.*\)\[$#\1#' | sed 's/*//;s/ *$//'
echo "$all_projects" | grep "^$1" | grep -o ':\ .*\[' | sed 's#^:\ *\(.*\)\[$#\1#' | sed 's/*//;s/ *$//'
}

function get_project_from_short {
echo "$all_projects" | grep "[$1]" | grep -o ':\ .*\[' | sed 's#^:\ *\(.*\)\[$#\1#' | sed 's/*//;s/ *$//'
}

function describe_project {
Expand All @@ -101,15 +155,24 @@ function describe_project {
echo "(This is used to specify which project you would like to submit time to)"
read project_short

echo "(Optional) What does the project pay(per hour)?"
echo "What does the project pay(per hour)?"
read money_per_hour

[ ! -f "$log_path/def/$project_short" ] && {
logger_debug "Initalizing project $project_name" ;
touch $log_path/def/$project_name
echo "project_name=$project_name" > $log_path/def/$project_name ;
echo "short_hand_name=$project_short" >> $log_path/def/$project_name ;
echo "money_per_hour=$money_per_hour" >> $log_path/def/$project_name ;
echo "What is the target hours per week?"
read target_hours

echo "What is the currency paid?"
read currency

[ ! -f "$log_path/def/$project_name" ] && {
logger_debug "Initalizing project $project_name" ;
touch $log_path/def/$project_name
touch $log_path/$project_name.logs
echo "project_name=$project_name" > $log_path/def/$project_name ;
echo "short_hand_name=$project_short" >> $log_path/def/$project_name ;
echo "money_per_hour=$money_per_hour" >> $log_path/def/$project_name ;
echo "target_hours=$target_hours" >> $log_path/def/$project_name ;
echo "currency=$currency" >> $log_path/def/$project_name ;
}
}

Expand All @@ -136,14 +199,14 @@ function get_project {
# $1 = project, if $1 = empty then $default. If $1 != default then default. else $1
if [ -z "$1" ]; then echo $default;
elif [ "$1" != "$default" ]; then echo $1;
else; echo $default ; fi
else echo $default ; fi;
}

function is_program_inited {
if [ -f $log_path/config ] ; then exit 0
else
logger_debug "The program is not initialized"
exit 1
logger_debug "The program is not initialized"
exit 1
fi
}

Expand All @@ -160,7 +223,7 @@ function write_to_disk {
break_min=$5
note=$6
date=$(date +%Y-%m-%d)
week_date=$(date +%Y-%V-%d)
week_date=$(date +%Y-%V-%u)
proj_log_path=$(get_log_path_for_project $project)
entry="$week_date/$date [$dec_time] ($start_time $end_time $break_min) [$note]"
echo "$entry" >> $proj_log_path
Expand Down Expand Up @@ -287,8 +350,8 @@ while [[ $# -ge 1 ]]; do
case "$2" in
project)
logger_debug "About to describe project project"
describe_project
new_project="y"
specify_project="n"
shift ; shift
;;
*)
Expand All @@ -299,7 +362,8 @@ while [[ $# -ge 1 ]]; do
show)
case "$2" in
logs)
''
show_logs=$3
shift ; shift ; shift
;;
esac
;;
Expand Down Expand Up @@ -327,12 +391,21 @@ while [[ $# -ge 1 ]]; do
note=$2
shift; shift
;;
--dev)
log_path=$2
shift ; shift
;;
log)
case "$2" in
project)
echo "short proj name matched: $3"
proj_name=$3
short_matched="y"
shift ; shift
;;
*)
shift
;;
esac

start_time=$(parse_timestamp $2)
Expand Down Expand Up @@ -381,11 +454,21 @@ done
if [ -z "$uninstall" ]; then
k=$(is_program_inited)
if [ $? -eq 1 ]; then init_program; fi
fi

logger_debug "Project input: $proj_name"
proj_name=$(get_project $proj_name)
logger_debug "Project is set as $proj_name"
if [ "$specify_project" != "y" ]; then
get_all_projects
logger_debug "Project input: $proj_name"
[ "$short_matched" = "y" ] && {
proj_name=$(get_project_from_short $proj_name) ;
} || {
proj_name=$(get_project $proj_name) ;
}
logger_debug "Project is set as '$proj_name'"
fi

if [ ! -z $new_project ]; then
describe_project
fi

if [ ! -z "$list_projects" ]; then
Expand All @@ -402,6 +485,22 @@ if [ ! -z "$delete_project" ];
then
delete_project
exit 0
elif [ ! -z "$show_logs" ]; then
if [ ! -z "$all_projects" ]; then
if [ -z "$proj_name" ]; then
#echo "You did not specify a project to log time for."
#echo "Here are the projects you have created:"
#echo "$all_projects"
#echo "What project are you trying to log time for?"
#read prompt_new_proj_name
#proj_name=$prompt_new_proj_name
#proj_name=$(get_project_from_all_projects $prompt_new_proj_name)
proj_name="Qvantel"
get_project_meta_info $proj_name
logger_debug "Showing logs for $proj_name given week $show_logs"
fi
week_stats $show_logs "$(cat $log_path/$proj_name.logs)"
fi
elif [ ! -z $start_time ]; then
if [ -z "$proj_name" ]; then
if [ ! -z "$all_projects" ]; then
Expand Down
84 changes: 84 additions & 0 deletions test/unittest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash

# Create test directory
mkdir dev/
dir=$(echo "$PWD/dev")

echo "-----------------"
echo " Tests"
echo "-----------------"

test_case_name="Can create files on filesystem"
touch foo
if [ -f foo ]; then echo "PASSED: $test_case_name"
else echo "FAILED: $test_case_name"; fi
rm foo

# Test of create project
timelog -v --dev $dir create project <<END
Test
ts
140
40
kr
END

test_case_name="Created project should have def. and .logs file"
target=$(grep -o 'target_hours\ *\=\ *40' $dir/def/Test)
if [[ -f "$dir/def/Test" &&
-f "$dir/Test.logs" &&
! -z $target ]]; then
echo "PASSED: $test_case_name"
else
echo "FAILED: $test_case_name"
echo "$(test -f $dir/def/Test ; echo $?) : $(test -f $dir/Test.logs ; echo $?) : $(test ! -z $target ; echo $?) "
exit 1
fi

echo "-----------------"
echo "List projects"
echo "-----------------"
k=$(timelog -v --dev $dir list projects)
match=$(echo "$k" | grep "1:\ Test\ \[ts\]")
test_case_name="List projects should list the newly created project"
[ -z "$match" ] && {
echo "FAILED: $test_case_name"; exit 1;
} || { echo "PASSED: $test_case_name"; }

echo "-----------------"
echo "Log time for given project"
echo "-----------------"
test_case_name="A log entry should be created to file"
timelog -v --dev $dir log project ts 0800 1000 0
logs=$(cat "$dir/Test.logs")
k=$(echo "$logs" | wc -l)

[ $k -ne 1 ] && {
echo "$test_case_name"; exit 1;
} || { echo "PASSED: $test_case_name"; }

dec_time=$(echo "$logs" | grep -o '\[2\]' | grep -o '2')
test_case_name="Decimal time did not equal to 2"
[ $dec_time -ne 2 ] && {
echo "$test_case_name"; exit 1;
} || { echo "PASSED: $test_case_name"; }

echo "-----------------"
echo "Delete project is deleted from filesystem"
echo "-----------------"
timelog --dev $dir delete project <<END
1
y
END
test_case_name="Deleted project does no longer exists"
if [ ! -f "$dev/def/Test" ] &&
[ ! -f "$dev/Test.logs" ]; then
echo "PASSED: $test_case_name"
else
echo "FAILED: $test_case_name"
exit 1
fi

rm dev/config
rmdir dev/def/
rmdir dev/

0 comments on commit 25cf70c

Please sign in to comment.