File: lib.sh

package info (click to toggle)
mailsync 5.2.7-3.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 916 kB
  • sloc: sh: 3,519; cpp: 2,199; ansic: 107; makefile: 89
file content (145 lines) | stat: -rwxr-xr-x 3,101 bytes parent folder | download | duplicates (2)
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
help() {
  echo "usage: $0 [--gdb|--strace] [--debug] [--keep]"
  echo "       $0 [--help]"
  echo
  echo "    run this test"
  echo
  echo "    --debug    - run mailsync in debug mode"
  echo "    --strace   - strace mailsync"
  echo "    --gdb      - run mailsync in gdb"
  echo "    --keep     - keep the test environmane after the test"
  echo
  echo "    --gdb and --strace are mutually exclusive"
  echo
  exit 1
}

# usage: parse_args "$@"
#
#  will set these global variables:
#
#  * DEBUG
#  * STRACE
#  * GDB
#  * KEEP
#  * NO_CHECK
#
parse_args() {
  DEBUG=
  STRACE=
  GDB=
  KEEP=
  NO_CHECK=
  
  for arg in "${script_args[@]}"; do
    case "$arg" in
      --debug)  DEBUG=-d            ; NO_CHECK=true ;;
      --strace) STRACE="strace -ff" ; NO_CHECK=true ;;
      --gdb)    GDB="gdb --args"    ; NO_CHECK=true ;;
      --keep)   KEEP="keep"                         ;;
      --help)   help                                ;;
      *)        help                                ;;
    esac
  done
  
  [ "$STRACE" != "" -a "$GDB" != "" ] && echo "ERROR: cant't use both --strace and --gdb" && help || true
}

# usage: set_up_test_state
#
# will set the following global variables:
#
#  TMP_DIR
#  HOME_DURING_TEST
#  blackbox_dir
#  mailsync_conf
#  mailsync
#
set_up_test_state() {

  TMP_DIR=$( mktemp -d )
  
  echo "Creating test environment in '$TMP_DIR'"
  echo
  
  HOME_DURING_TEST="$TMP_DIR"
  
  blackbox_dir="$HOME_DURING_TEST/mail"
  mkdir "$blackbox_dir"
  cp -a dir_with_mails_a "$blackbox_dir"/
  cp -a dir_with_mails_b "$blackbox_dir"/
  
  mailsync_conf="$TMP_DIR/mailsync.conf"
  cp -a mailsync.conf "$mailsync_conf"
  
  # current working directory is tests/1, therefore:
  mailsync=../../src/mailsync
}

# usage: run_mailsync "$@"
#
# "$@" contains additional arguments for mailsync
#
run_mailsync() {
  HOME="$HOME_DURING_TEST" $GDB $STRACE $mailsync $DEBUG -f "$mailsync_conf" "$@" | tee "$TMP_DIR/output"
}

verify_output_matches() {
  local test_name="$( basename "$0" )"

  verify_output_matches_this "${test_name}.output"
}

# usage: verify_output_matches_this NAME_OF_FILE
#
# FILE has to be inside `expected_output/`
#
verify_output_matches_this() {
  local output_file="$1"
  echo
  
  if [ "$NO_CHECK" == "true" ]; then
    echo "Not verifying test results because of given command line switch"
  
  else
  
    if diff -u "expected_output/$output_file" "$TMP_DIR/output"; then
      echo "-------- Output matched expected output"
      return 0
    else
      echo "-------- Output did NOT match expected output"
      return 1
    fi
  fi
}

# cleanup everything that the test generated
#
# usage: clean_up
#
clean_up() {
  if [ "$KEEP" != "keep" ]; then
    rm -r "$TMP_DIR"
  fi
}

# do before running mailsync
#
pre_run() {
  set -e          # stop on error
  set -o pipefail # stop part of pipeline failing
  set -u          # stop on undefined variable
  
  # cd into where this script is
  cd "$( dirname "$( realpath $0 )" )"

  parse_args "$@"
  set_up_test_state
}

# do after running mailsync
#
post_run() {
  verify_output_matches
  clean_up
}