~mapperr/trk

ref: 3474574c7b3ff561f294076cd15caafc6ece058a trk/test -rwxr-xr-x 3.7 KiB
3474574cmapperr Add license 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /bin/sh

# taken from gradlew
PRG="$0"
while [ -h "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`"/$link"
    fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
SCRIPTDIR="`pwd -P`"
# =========================

if test -z "$SHUNIT_SCRIPT"; then
    echo "
    to run those tests set the SHUNIT_SCRIPT
    env var to the path of shunit2 script
    
    es: export SHUNIT_SCRIPT=\$HOME/shunit2/shunit2

    see this link for more infos:
    https://github.com/kward/shunit2"
    exit 1
fi

trk="./trk"

export TRK_DEBUG=1
export TRK_DIR="/tmp/trkdir_test"
test -z "$TMPDIR" || \
    export TRK_DIR="$TMPDIR/trkdir_test"

# import variables definitions
eval `$trk env`

export trk_bare_git_test_repo_dir="$trkdir-bare_git_repo"

logt() {
    local msg="$1"
    echo "TEST - $msg" >&2
}

cleanup() {
    if test -d "$trkdir"; then
       rm -rf "$trkdir"
    fi

    if test -d "$trk_bare_git_test_repo_dir"; then
       rm -rf "$trk_bare_git_test_repo_dir"
    fi
}

setUp() {
    cleanup
}

test_start_a_timer() {
    $trk test
    assertTrue "test -f $trk_active_file"
}

test_stop_a_timer() {
    $trk testing
    sleep 1
    $trk t
    assertTrue 'trk active file is not empty after a stop' \
        "test -z \"`head -n1 $trk_active_file`\""
    assertTrue "cat $trkfile | grep '[12]s'"
    $trk testing
    sleep 2
    $trk t
    assertTrue "cat $trkfile | grep '[34]s'"
}

test_add_entry() {
    $trk 1h testing
    assertTrue "grep 'testing 1h' $trkfile >/dev/null"
    $trk 1h testing
    assertTrue "grep 'testing 2h' $trkfile >/dev/null"

    $trk 2d testing
    assertTrue "grep 'testing 2d2h' $trkfile >/dev/null"
    $trk 30m testing
    assertTrue "grep 'testing 2d2h30m' $trkfile >/dev/null"
    # seconds under the minute are worthless xD
    $trk 30s testing
    assertTrue "grep 'testing 2d2h30m' $trkfile >/dev/null"
    $trk 120s testing
    assertTrue "grep 'testing 2d2h32m' $trkfile >/dev/null"
}

test_report() {
    $trk 1h testing
    assertTrue "$trk | grep '1h spent on testing' >/dev/null"
    assertFalse "$trk | grep 'active entry' >/dev/null"

    today_month="`date '+%m'`"
    nextday_month="`date -d '1 day' '+%m'`"
    newtrkfile="$trkdir/`date -d '-1 day' '+%Y-%m-%d'`.log"
    test "$today_month" = "$nextday_month" && \
        newtrkfile="$trkdir/`date -d '1 day' '+%Y-%m-%d'`.log"
    echo "testing 1h" >> "$newtrkfile"
    echo "monthtesting 7h" >> "$newtrkfile"
    assertTrue "$trk r month | grep '2h spent on testing' >/dev/null"
    assertTrue "$trk r month | grep '7h spent on monthtesting' >/dev/null"
    assertFalse "$trk | grep 'active entry' >/dev/null"

    $trk activetesting
    sleep 1
    assertTrue "$trk | grep 'activetesting' >/dev/null"
    assertTrue "$trk | grep 'active entry' >/dev/null"
}

test_trkfile_creation() {
    $trk testing
    assertFalse "trkfile is created empty on start timer!" "test -f $trkfile"
    $trk
    assertFalse "trkfile is created empty on today report!" "test -f $trkfile"
}

test_git_initial_handling() {
    hash git >/dev/null || return 1
    mkdir $trkdir
    mkdir $trk_bare_git_test_repo_dir

    logt "setup a bare repo"
    cd $trk_bare_git_test_repo_dir
    local test_repo_name="trktesting.git"
    git init --bare $test_repo_name
    cd -

    logt "init user trkfiles repo"
    cd $trkdir
    git init
    git remote add origin "$trk_bare_git_test_repo_dir/$test_repo_name"
    cd -

    logt "doing the first push"
    $trk 1h testing
    $trk g add -A
    $trk g commit -m 'Init'
    $trk g push -u origin master

    logt "using autosync from now on"
    $trk 1h testing
    $trk y
}

. $SHUNIT_SCRIPT