test-release.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #!/usr/bin/env bash
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. # Tested on Linux, Armbian, Raspbian, MSYS2
  11. # cfg.sh must implement:
  12. # list_avail_tests()
  13. # init_groups()
  14. # init_tests()
  15. . 'test/test-release.d/cfg.sh'
  16. run_test() {
  17. set +x
  18. tests="t_$1"
  19. skips="t_$1_skip"
  20. while read skip test; do
  21. [ "$test" ] || continue
  22. echo "${!skips}" | grep -q $skip && continue
  23. if [ "$LIST_CMDS" ]; then
  24. echo $test
  25. else
  26. test_disp=$YELLOW${test/\#/$RESET$MAGENTA\#}$RESET
  27. if [ "${test:0:1}" == '#' ]; then
  28. echo -e "$test_disp"
  29. else
  30. echo -e "${GREEN}Running:$RESET $test_disp"
  31. eval "$test" || {
  32. echo -e $RED"test-release.sh: test '$CUR_TEST' failed at command '$test'"$RESET
  33. exit 1
  34. }
  35. fi
  36. fi
  37. done <<<${!tests}
  38. }
  39. prompt_skip() {
  40. echo -n "Enter 's' to skip, or ENTER to continue: "; read -n1; echo
  41. [ "$REPLY" == 's' ] && return 0
  42. return 1
  43. }
  44. list_avail_tests() {
  45. echo "AVAILABLE TESTS:"
  46. init_tests
  47. for i in $all_tests; do
  48. z="d_$i"
  49. printf " %-8s - %s\n" $i "${!z}"
  50. done
  51. echo
  52. echo "AVAILABLE TEST GROUPS:"
  53. while read a b c; do
  54. [ "$a" ] && printf " %-8s - %s\n" $a "$c"
  55. done <<<$groups_desc
  56. echo
  57. echo "By default, all tests are run"
  58. }
  59. run_tests() {
  60. [ "$LIST_CMDS" ] || echo "Running tests: $1"
  61. for t in $1; do
  62. desc_id="d_$t" desc=${!desc_id}
  63. if [ "$SKIP_ALT_DEP" ]; then
  64. ok=$(for a in $noalt_tests $noalt_ok_tests; do if [ $t == $a ]; then echo 'ok'; fi; done)
  65. if [ ! "$ok" ]; then
  66. echo -e "${BLUE}Skipping altcoin test '$t'$RESET"
  67. continue
  68. fi
  69. fi
  70. if [ "$LIST_CMDS" ]; then
  71. echo -e "\n### $t: $desc"
  72. else
  73. echo -e "\n${BLUE}Testing:$RESET $GREEN$desc$RESET"
  74. fi
  75. [ "$PAUSE" ] && prompt_skip && continue
  76. CUR_TEST=$t
  77. run_test $t
  78. [ "$LIST_CMDS" ] || echo -e "${BLUE}Finished testing:$RESET $GREEN$desc$RESET"
  79. done
  80. }
  81. check_tests() {
  82. for i in $tests; do
  83. echo "$dfl_tests $extra_tests" | grep -q "\<$i\>" || {
  84. echo "$i: unrecognized argument"
  85. exit 1
  86. }
  87. done
  88. }
  89. remove_skipped_tests() {
  90. tests=$(for t in $tests; do
  91. [ "$(for s in $SKIP_LIST; do [ $t == $s ] && echo y; done)" ] && continue
  92. echo $t
  93. done)
  94. tests=$(echo $tests)
  95. }
  96. list_group_symbols() {
  97. echo -e "Default tests:\n $dfl_tests"
  98. echo -e "Extra tests:\n $extra_tests"
  99. echo -e "'noalt' test group:\n $noalt_tests"
  100. echo -e "'quick' test group:\n $quick_tests"
  101. echo -e "'qskip' test group:\n $qskip_tests"
  102. }
  103. print_ver_hash() {
  104. python3 -m pip freeze | grep "^$repo\>" | sed 's/.*sha256=//' | cut -c 1-12
  105. }
  106. do_typescript() {
  107. if [ "$DARWIN" ]; then script "$1" $2; else script -O "$1" -c "$2"; fi
  108. }
  109. install_package() {
  110. echo -e "${BLUE}Installing package$YELLOW $repo$RESET"
  111. rm -rf build dist *.egg-info
  112. ver=$(print_ver_hash)
  113. echo -e "${BLUE}Currently installed version is$MAGENTA $ver$RESET"
  114. cmd="python3 -m build --no-isolation --wheel --config-setting=quiet $STDOUT_DEVNULL"
  115. echo -e "${BLUE}Executing:$CYAN $cmd$RESET"
  116. eval $cmd
  117. cmd="python3 -m pip $QUIET install --break-system-packages dist/*.whl"
  118. echo -e "${BLUE}Executing:$CYAN $cmd$RESET"
  119. eval $cmd
  120. new_ver=$(print_ver_hash)
  121. if [ "$ver" == "$new_ver" ]; then
  122. echo -ne "${YELLOW}Version hash is unchanged. Force install? (y/N):$RESET "
  123. read -n1
  124. if [ "$REPLY" == 'y' ]; then
  125. echo
  126. cmd="python3 -m pip $QUIET install --break-system-packages --force --no-deps dist/*.whl"
  127. echo -e "${BLUE}Executing:$CYAN $cmd$RESET"
  128. eval $cmd
  129. elif [ "$REPLY" ]; then
  130. echo; return
  131. else
  132. return
  133. fi
  134. fi
  135. new_ver=$(print_ver_hash)
  136. if [ "$ver" == "$new_ver" ]; then
  137. echo -e "${RED}ERROR: version hash is unchanged$RESET"
  138. exit 1
  139. else
  140. echo -e "${GREEN}OK$RESET"
  141. fi
  142. }
  143. do_reexec() {
  144. [ -z "$exec_prog" ] && exec_prog="test/test-release.sh -X $ORIG_ARGS"
  145. if [ "$sdist_dir" ]; then
  146. target_dir=$sdist_dir
  147. elif [ "$clone_dir" ]; then
  148. target_dir="$orig_cwd/.clone-test"
  149. clone_dir=$target_dir
  150. else # TYPESCRIPT=1
  151. do_typescript "$orig_cwd/$typescript_file" "$exec_prog"
  152. return
  153. fi
  154. rm -rf $target_dir
  155. mkdir $target_dir
  156. if [ "$repo" != 'mmgen-wallet' ]; then
  157. echo -e "${BLUE}Cloning repo $MAGENTA'mmgen-wallet'$RESET ${BLUE}to $YELLOW$target_dir/mmgen-wallet$RESET"
  158. mkdir -p "$target_dir/mmgen-wallet"
  159. eval "git clone $orig_cwd/../mmgen-wallet $target_dir/mmgen-wallet $STDOUT_DEVNULL $STDERR_DEVNULL"
  160. fi
  161. if [ "$clone_dir" ]; then
  162. [ "$(git status --porcelain)" ] && VIM_GIT_COMMIT=1 git commit -a
  163. dest="$clone_dir/$repo"
  164. rm -rf $dest
  165. mkdir -p $dest
  166. echo -e "${BLUE}Cloning repo $MAGENTA'$repo'$BLUE to $YELLOW$dest$RESET"
  167. eval "git clone . $dest $STDOUT_DEVNULL $STDERR_DEVNULL"
  168. cd $dest
  169. echo -e "${BLUE}cd -> $YELLOW$PWD$RESET"
  170. fi
  171. if [ "$sdist_dir" ]; then
  172. rm -rf build dist *.egg-info
  173. echo -n 'Building sdist...'
  174. eval "python3 -m build --no-isolation --sdist --config-setting=quiet $STDOUT_DEVNULL"
  175. echo -e "done\n${BLUE}Unpacking sdist archive to $YELLOW$target_dir$RESET"
  176. tar -C $target_dir -zxf dist/*.tar.gz
  177. cd $target_dir/${repo//-/[-_]}-*
  178. echo -e "${BLUE}cd -> $YELLOW$PWD$RESET"
  179. if [ "$clone_dir" ]; then rm -rf $clone_dir; fi
  180. fi
  181. [ -e 'test/init.sh' ] && test/init.sh $VERBOSE_SHORTOPT
  182. [ "$repo" == 'mmgen-wallet' ] && eval "python3 setup.py build_ext --inplace $STDOUT_DEVNULL"
  183. echo -e "\n${BLUE}Executing test runner: ${CYAN}test/test-release $ORIG_ARGS$RESET\n"
  184. if [ "$TYPESCRIPT" ]; then
  185. do_typescript "$orig_cwd/$typescript_file" "$exec_prog"
  186. else
  187. eval $exec_prog
  188. fi
  189. }
  190. # start execution
  191. set -e
  192. set -o functrace
  193. set -o errtrace
  194. trap 'echo -e "${GREEN}Exiting at user request$RESET"; exit' INT
  195. umask 0022
  196. orig_cwd=$(pwd)
  197. repo=$(basename $orig_cwd)
  198. if [ "$(uname -m)" == 'armv7l' ]; then
  199. ARM32=1
  200. elif [ "$(uname -m)" == 'aarch64' ]; then
  201. ARM64=1
  202. elif [ "$(uname -s)" == 'Darwin' ]; then
  203. DARWIN=1
  204. DISTRO='DARWIN'
  205. elif [ "$MSYSTEM" ] && uname -a | grep -qi 'msys'; then
  206. MSYS2=1
  207. DISTRO='MSYS2'
  208. fi
  209. [ "$ARM32" -o "$ARM64" ] && {
  210. PEXPECT_LONG_TIMEOUT=' --pexpect-timeout=300'
  211. HTTP_LONG_TIMEOUT='MMGEN_HTTP_TIMEOUT=300 '
  212. }
  213. if [ -e '/etc/os-release' ]; then
  214. DISTRO=$(grep '^ID=' '/etc/os-release' | cut -c 4-)
  215. [ "$DISTRO" ] || {
  216. echo 'Unable to determine distro from /etc/os-release. Aborting'
  217. exit 1
  218. }
  219. fi
  220. cmdtest_py='test/cmdtest.py -n'
  221. objtest_py='test/objtest.py'
  222. objattrtest_py='test/objattrtest.py'
  223. modtest_py='test/modtest.py --names --quiet'
  224. daemontest_py='test/daemontest.py --names --quiet'
  225. tooltest_py='test/tooltest.py'
  226. tooltest2_py='test/tooltest2.py --names --quiet'
  227. gentest_py='test/gentest.py --quiet'
  228. scrambletest_py='test/scrambletest.py'
  229. altcoin_mod_opts='--quiet'
  230. mmgen_tool='cmds/mmgen-tool'
  231. pylint='PYTHONPATH=. pylint' # PYTHONPATH required by older Pythons (e.g. v3.9)
  232. python='python3'
  233. rounds=10
  234. typescript_file='test-release.out'
  235. STDOUT_DEVNULL='>/dev/null'
  236. STDERR_DEVNULL='2>/dev/null'
  237. QUIET='--quiet'
  238. ORIG_ARGS=$@
  239. PROGNAME=$(basename $0)
  240. init_groups
  241. while getopts hAbcCdDe:fFILlNOps:StTvVX OPT
  242. do
  243. case "$OPT" in
  244. h) printf " %-16s Test MMGen release\n" "${PROGNAME}:"
  245. echo " USAGE: $PROGNAME [options] [tests or test group]"
  246. echo " OPTIONS: -h Print this help message"
  247. echo " -A Skip tests requiring altcoin modules or daemons"
  248. echo " -b Buffer keypresses for all invocations of 'test/cmdtest.py'"
  249. echo " -c Run tests in coverage mode"
  250. echo " -C Test from cloned repo (can be combined with -S)"
  251. echo " -d Enable Python Development Mode"
  252. echo " -D Run tests in deterministic mode"
  253. echo " -e PROG With -C, -S or -T, execute PROG instead of this script"
  254. echo " -f Speed up the tests by using fewer rounds"
  255. echo " -F Reduce rounds even further"
  256. echo " -I Install the package"
  257. echo " -L List available tests and test groups with description"
  258. echo " -l List the test name symbols"
  259. echo " -N Pass the --no-timings switch to test/cmdtest.py"
  260. echo " -O Use pexpect.spawn rather than popen_spawn where applicable"
  261. echo " -p Pause between tests"
  262. echo " -s LIST Skip tests in LIST (space-separated)"
  263. echo " -S Build sdist distribution, unpack, and run test"
  264. echo " -t Print the tests without running them"
  265. echo " -T Record a typescript of the screen output in '$typescript_file'"
  266. echo " -v Run test/cmdtest.py with '--exact-output' and other commands"
  267. echo " with '--verbose' switch"
  268. echo " -V Run test/cmdtest.py and other commands with '--verbose' switch"
  269. echo
  270. echo " For traceback output and error file support, set the EXEC_WRAPPER_TRACEBACK"
  271. echo " environment variable"
  272. exit ;;
  273. A) SKIP_ALT_DEP=1
  274. cmdtest_py+=" --no-altcoin"
  275. modtest_py+=" --no-altcoin-deps"
  276. daemontest_py+=" --no-altcoin-deps"
  277. scrambletest_py+=" --no-altcoin"
  278. tooltest2_py+=" --no-altcoin" ;;
  279. b) cmdtest_py+=" --buf-keypress" ;;
  280. c) mkdir -p 'test/trace'
  281. touch 'test/trace.acc'
  282. cmdtest_py+=" --coverage"
  283. tooltest_py+=" --coverage"
  284. tooltest2_py+=" --fork --coverage"
  285. scrambletest_py+=" --coverage"
  286. python="python3 -m trace --count --file=test/trace.acc --coverdir=test/trace"
  287. modtest_py="$python $modtest_py"
  288. daemontest_py="$python $daemontest_py"
  289. objtest_py="$python $objtest_py"
  290. objattrtest_py="$python $objattrtest_py"
  291. gentest_py="$python $gentest_py"
  292. mmgen_tool="$python $mmgen_tool" ;&
  293. C) REEXEC=1 clone_dir="$orig_cwd/.cloned-repo" ;;
  294. d) export PYTHONDEVMODE=1
  295. export PYTHONWARNINGS='error' ;;
  296. D) export MMGEN_TEST_SUITE_DETERMINISTIC=1
  297. export MMGEN_DISABLE_COLOR=1 ;;
  298. e) exec_prog=$(realpath $OPTARG) ;;
  299. f) rounds=6 FAST=1 fast_opt='--fast' modtest_py+=" --fast" daemontest_py+=" --fast" ;;
  300. F) rounds=3 FAST=1 fast_opt='--fast' modtest_py+=" --fast" daemontest_py+=" --fast" ;;
  301. I) INSTALL_PACKAGE=1 ;;
  302. L) list_avail_tests; exit ;;
  303. l) list_group_symbols; exit ;;
  304. N) cmdtest_py+=" --no-timings" ;;
  305. O) cmdtest_py+=" --pexpect-spawn" ;;
  306. p) PAUSE=1 ;;
  307. s) SKIP_LIST+=" $OPTARG" ;;
  308. S) REEXEC=1 sdist_dir="$orig_cwd/.sdist-test" ;;
  309. t) LIST_CMDS=1 ;;
  310. T) REEXEC=1 TYPESCRIPT=1 ;;
  311. v) EXACT_OUTPUT=1 cmdtest_py+=" --exact-output" ;&
  312. V) VERBOSE='--verbose' VERBOSE_SHORTOPT='-v' QUIET=''
  313. [ "$EXACT_OUTPUT" ] || cmdtest_py+=" --verbose"
  314. STDOUT_DEVNULL='' STDERR_DEVNULL=''
  315. modtest_py="${modtest_py/--quiet/--verbose}"
  316. daemontest_py="${daemontest_py/--quiet/--verbose}"
  317. altcoin_mod_opts="${altcoin_mod_opts/--quiet/--verbose}"
  318. tooltest2_py="${tooltest2_py/--quiet/--verbose}"
  319. gentest_py="${gentest_py/--quiet/--verbose}"
  320. tooltest_py+=" --verbose"
  321. mmgen_tool+=" --verbose"
  322. objattrtest_py+=" --verbose"
  323. scrambletest_py+=" --verbose"
  324. pylint+=" --verbose" ;;
  325. X) IN_REEXEC=1 ;;
  326. *) exit ;;
  327. esac
  328. done
  329. [ "$MMGEN_DISABLE_COLOR" ] || {
  330. RED="\e[31;1m" GREEN="\e[32;1m" YELLOW="\e[33;1m" BLUE="\e[34;1m" MAGENTA="\e[35;1m" CYAN="\e[36;1m"
  331. RESET="\e[0m"
  332. }
  333. [ "$REEXEC" -a -z "$IN_REEXEC" ] && { do_reexec; exit; }
  334. [ "$exec_prog" ] && { echo "option -e makes no sense without -C, -S, or -T" ; exit; }
  335. [ "$INSTALL_PACKAGE" ] && { install_package; exit; }
  336. [ "$MSYS2" -a ! "$FAST" ] && tooltest2_py+=' --fork'
  337. [ "$EXACT_OUTPUT" -o "$VERBOSE" ] || objtest_py+=" -S"
  338. shift $((OPTIND-1))
  339. case $1 in
  340. '') tests=$dfl_tests ;;
  341. 'default') tests=$dfl_tests ;;
  342. 'extra') tests=$extra_tests ;;
  343. 'noalt') tests=$noalt_tests
  344. SKIP_ALT_DEP=1
  345. cmdtest_py+=" --no-altcoin"
  346. modtest_py+=" --no-altcoin-deps"
  347. daemontest_py+=" --no-altcoin-deps"
  348. scrambletest_py+=" --no-altcoin" ;;
  349. 'quick') tests=$quick_tests ;;
  350. 'qskip') tests=$qskip_tests ;;
  351. *) tests="$*" ;;
  352. esac
  353. rounds_min=$((rounds / 2))
  354. for n in 2 5 10 20 50 100 200 500 1000; do
  355. eval "rounds${n}x=$((rounds*n))"
  356. done
  357. init_tests
  358. remove_skipped_tests
  359. check_tests
  360. test/clean.py
  361. start_time=$(date +%s)
  362. run_tests "$tests"
  363. elapsed=$(($(date +%s)-start_time))
  364. elapsed_fmt=$(printf %02d:%02d $((elapsed/60)) $((elapsed%60)))
  365. [ "$LIST_CMDS" ] || {
  366. if [ "$MMGEN_TEST_SUITE_DETERMINISTIC" ]; then
  367. echo -e "\n${GREEN}All OK"
  368. else
  369. echo -e "\n${GREEN}All OK. Total elapsed time: $elapsed_fmt$RESET"
  370. fi
  371. }