test-release.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #!/bin/bash
  2. #
  3. # mmgen = Multi-Mode GENerator, a command-line 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; 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. install_package() {
  107. echo -e "${BLUE}Installing package$YELLOW $repo$RESET"
  108. rm -rf build dist *.egg-info
  109. ver=$(print_ver_hash)
  110. echo -e "${BLUE}Currently installed version is$MAGENTA $ver$RESET"
  111. cmd="python3 -m build --no-isolation --wheel --config-setting=quiet $STDOUT_DEVNULL"
  112. echo -e "${BLUE}Executing:$CYAN $cmd$RESET"
  113. eval $cmd
  114. cmd="python3 -m pip $QUIET install --break-system-packages dist/*.whl"
  115. echo -e "${BLUE}Executing:$CYAN $cmd$RESET"
  116. eval $cmd
  117. new_ver=$(print_ver_hash)
  118. if [ "$ver" == "$new_ver" ]; then
  119. echo -ne "${YELLOW}Version hash is unchanged. Force install? (y/N):$RESET "
  120. read -n1
  121. if [ "$REPLY" == 'y' ]; then
  122. echo
  123. cmd="python3 -m pip $QUIET install --break-system-packages --force --no-deps dist/*.whl"
  124. echo -e "${BLUE}Executing:$CYAN $cmd$RESET"
  125. eval $cmd
  126. elif [ "$REPLY" ]; then
  127. echo; return
  128. else
  129. return
  130. fi
  131. fi
  132. new_ver=$(print_ver_hash)
  133. if [ "$ver" == "$new_ver" ]; then
  134. echo -e "${RED}ERROR: version hash is unchanged$RESET"
  135. exit 1
  136. else
  137. echo -e "${GREEN}OK$RESET"
  138. fi
  139. }
  140. do_reexec() {
  141. [ -z "$exec_prog" ] && exec_prog="test/test-release.sh -X $ORIG_ARGS"
  142. if [ "$sdist_dir" ]; then
  143. target_dir=$sdist_dir
  144. elif [ "$clone_dir" ]; then
  145. target_dir="$orig_cwd/.clone-test"
  146. clone_dir=$target_dir
  147. else # TYPESCRIPT=1
  148. script -O "$orig_cwd/$typescript_file" -c "$exec_prog"
  149. return
  150. fi
  151. rm -rf $target_dir
  152. mkdir $target_dir
  153. if [ "$repo" != 'mmgen-wallet' ]; then
  154. echo -e "${BLUE}Cloning repo $MAGENTA'mmgen-wallet'$RESET ${BLUE}to $YELLOW$target_dir/mmgen-wallet$RESET"
  155. mkdir -p "$target_dir/mmgen-wallet"
  156. eval "git clone $orig_cwd/../mmgen-wallet $target_dir/mmgen-wallet $STDOUT_DEVNULL $STDERR_DEVNULL"
  157. fi
  158. if [ "$clone_dir" ]; then
  159. [ "$(git status --porcelain)" ] && VIM_GIT_COMMIT=1 git commit -a
  160. dest="$clone_dir/$repo"
  161. rm -rf $dest
  162. mkdir -p $dest
  163. echo -e "${BLUE}Cloning repo $MAGENTA'$repo'$BLUE to $YELLOW$dest$RESET"
  164. eval "git clone . $dest $STDOUT_DEVNULL $STDERR_DEVNULL"
  165. cd $dest
  166. echo -e "${BLUE}cd -> $YELLOW$PWD$RESET"
  167. fi
  168. if [ "$sdist_dir" ]; then
  169. rm -rf build dist *.egg-info
  170. echo -n 'Building sdist...'
  171. eval "python3 -m build --no-isolation --sdist --config-setting=quiet $STDOUT_DEVNULL"
  172. echo -e "done\n${BLUE}Unpacking sdist archive to $YELLOW$target_dir$RESET"
  173. tar -C $target_dir -axf dist/*.tar.gz
  174. cd $(echo $target_dir/$repo-*)
  175. echo -e "${BLUE}cd -> $YELLOW$PWD$RESET"
  176. if [ "$clone_dir" ]; then rm -rf $clone_dir; fi
  177. fi
  178. [ -e 'test/init.sh' ] && test/init.sh $VERBOSE_SHORTOPT
  179. [ "$repo" == 'mmgen-wallet' ] && eval "python3 setup.py build_ext --inplace $STDOUT_DEVNULL"
  180. echo -e "\n${BLUE}Executing test runner: ${CYAN}test/test-release $ORIG_ARGS$RESET\n"
  181. if [ "$TYPESCRIPT" ]; then
  182. script -O "$orig_cwd/$typescript_file" -c "$exec_prog"
  183. else
  184. eval $exec_prog
  185. fi
  186. }
  187. # start execution
  188. set -e
  189. set -o functrace
  190. set -o errtrace
  191. trap 'echo -e "${GREEN}Exiting at user request$RESET"; exit' INT
  192. umask 0022
  193. orig_cwd=$(pwd)
  194. repo=$(basename $orig_cwd)
  195. if [ "$(uname -m)" == 'armv7l' ]; then
  196. ARM32=1
  197. elif [ "$(uname -m)" == 'aarch64' ]; then
  198. ARM64=1
  199. elif [ "$MSYSTEM" ] && uname -a | grep -qi 'msys'; then
  200. MSYS2=1
  201. fi
  202. if [ "$MSYS2" ]; then
  203. DISTRO='MSYS2'
  204. else
  205. DISTRO=$(grep '^ID=' '/etc/os-release' | cut -c 4-)
  206. [ "$DISTRO" ] || { echo 'Unable to determine distro. Aborting'; exit 1; }
  207. fi
  208. cmdtest_py='test/cmdtest.py -n'
  209. objtest_py='test/objtest.py'
  210. objattrtest_py='test/objattrtest.py'
  211. unit_tests_py='test/unit_tests.py --names --quiet'
  212. tooltest_py='test/tooltest.py'
  213. tooltest2_py='test/tooltest2.py --names --quiet'
  214. gentest_py='test/gentest.py --quiet'
  215. scrambletest_py='test/scrambletest.py'
  216. altcoin_mod_opts='--quiet'
  217. mmgen_tool='cmds/mmgen-tool'
  218. pylint='PYTHONPATH=. pylint' # PYTHONPATH required by older Pythons (e.g. v3.9)
  219. python='python3'
  220. rounds=10
  221. typescript_file='test-release.out'
  222. STDOUT_DEVNULL='>/dev/null'
  223. STDERR_DEVNULL='2>/dev/null'
  224. QUIET='--quiet'
  225. ORIG_ARGS=$@
  226. PROGNAME=$(basename $0)
  227. init_groups
  228. while getopts hAbcCdDe:fFILlNOps:StTvVX OPT
  229. do
  230. case "$OPT" in
  231. h) printf " %-16s Test MMGen release\n" "${PROGNAME}:"
  232. echo " USAGE: $PROGNAME [options] [tests or test group]"
  233. echo " OPTIONS: -h Print this help message"
  234. echo " -A Skip tests requiring altcoin modules or daemons"
  235. echo " -b Buffer keypresses for all invocations of 'test/cmdtest.py'"
  236. echo " -c Run tests in coverage mode"
  237. echo " -C Test from cloned repo (can be combined with -S)"
  238. echo " -d Enable Python Development Mode"
  239. echo " -D Run tests in deterministic mode"
  240. echo " -e PROG With -C, -S or -T, execute PROG instead of this script"
  241. echo " -f Speed up the tests by using fewer rounds"
  242. echo " -F Reduce rounds even further"
  243. echo " -I Install the package"
  244. echo " -L List available tests and test groups with description"
  245. echo " -l List the test name symbols"
  246. echo " -N Pass the --no-timings switch to test/cmdtest.py"
  247. echo " -O Use pexpect.spawn rather than popen_spawn where applicable"
  248. echo " -p Pause between tests"
  249. echo " -s LIST Skip tests in LIST (space-separated)"
  250. echo " -S Build sdist distribution, unpack, and run test"
  251. echo " -t Print the tests without running them"
  252. echo " -T Record a typescript of the screen output in '$typescript_file'"
  253. echo " -v Run test/cmdtest.py with '--exact-output' and other commands"
  254. echo " with '--verbose' switch"
  255. echo " -V Run test/cmdtest.py and other commands with '--verbose' switch"
  256. echo
  257. echo " For traceback output and error file support, set the EXEC_WRAPPER_TRACEBACK"
  258. echo " environment variable"
  259. exit ;;
  260. A) SKIP_ALT_DEP=1
  261. cmdtest_py+=" --no-altcoin"
  262. unit_tests_py+=" --no-altcoin-deps"
  263. scrambletest_py+=" --no-altcoin"
  264. tooltest2_py+=" --no-altcoin" ;;
  265. b) cmdtest_py+=" --buf-keypress" ;;
  266. c) mkdir -p 'test/trace'
  267. touch 'test/trace.acc'
  268. cmdtest_py+=" --coverage"
  269. tooltest_py+=" --coverage"
  270. tooltest2_py+=" --fork --coverage"
  271. scrambletest_py+=" --coverage"
  272. python="python3 -m trace --count --file=test/trace.acc --coverdir=test/trace"
  273. unit_tests_py="$python $unit_tests_py"
  274. objtest_py="$python $objtest_py"
  275. objattrtest_py="$python $objattrtest_py"
  276. gentest_py="$python $gentest_py"
  277. mmgen_tool="$python $mmgen_tool" ;&
  278. C) REEXEC=1 clone_dir="$orig_cwd/.cloned-repo" ;;
  279. d) export PYTHONDEVMODE=1
  280. export PYTHONWARNINGS='error' ;;
  281. D) export MMGEN_TEST_SUITE_DETERMINISTIC=1
  282. export MMGEN_DISABLE_COLOR=1 ;;
  283. e) exec_prog=$(realpath $OPTARG) ;;
  284. f) rounds=6 FAST=1 fast_opt='--fast' unit_tests_py+=" --fast" ;;
  285. F) rounds=3 FAST=1 fast_opt='--fast' unit_tests_py+=" --fast" ;;
  286. I) INSTALL_PACKAGE=1 ;;
  287. L) list_avail_tests; exit ;;
  288. l) list_group_symbols; exit ;;
  289. N) cmdtest_py+=" --no-timings" ;;
  290. O) cmdtest_py+=" --pexpect-spawn" ;;
  291. p) PAUSE=1 ;;
  292. s) SKIP_LIST+=" $OPTARG" ;;
  293. S) REEXEC=1 sdist_dir="$orig_cwd/.sdist-test" ;;
  294. t) LIST_CMDS=1 ;;
  295. T) REEXEC=1 TYPESCRIPT=1 ;;
  296. v) EXACT_OUTPUT=1 cmdtest_py+=" --exact-output" ;&
  297. V) VERBOSE='--verbose' VERBOSE_SHORTOPT='-v' QUIET=''
  298. [ "$EXACT_OUTPUT" ] || cmdtest_py+=" --verbose"
  299. STDOUT_DEVNULL='' STDERR_DEVNULL=''
  300. unit_tests_py="${unit_tests_py/--quiet/--verbose}"
  301. altcoin_mod_opts="${altcoin_mod_opts/--quiet/--verbose}"
  302. tooltest2_py="${tooltest2_py/--quiet/--verbose}"
  303. gentest_py="${gentest_py/--quiet/--verbose}"
  304. tooltest_py+=" --verbose"
  305. mmgen_tool+=" --verbose"
  306. objattrtest_py+=" --verbose"
  307. scrambletest_py+=" --verbose"
  308. pylint+=" --verbose" ;;
  309. X) IN_REEXEC=1 ;;
  310. *) exit ;;
  311. esac
  312. done
  313. [ "$MMGEN_DISABLE_COLOR" ] || {
  314. RED="\e[31;1m" GREEN="\e[32;1m" YELLOW="\e[33;1m" BLUE="\e[34;1m" MAGENTA="\e[35;1m" CYAN="\e[36;1m"
  315. RESET="\e[0m"
  316. }
  317. [ "$REEXEC" -a -z "$IN_REEXEC" ] && { do_reexec; exit; }
  318. [ "$exec_prog" ] && { echo "option -e makes no sense without -C, -S, or -T" ; exit; }
  319. [ "$INSTALL_PACKAGE" ] && { install_package; exit; }
  320. [ "$MSYS2" -a ! "$FAST" ] && tooltest2_py+=' --fork'
  321. [ "$EXACT_OUTPUT" -o "$VERBOSE" ] || objtest_py+=" -S"
  322. shift $((OPTIND-1))
  323. case $1 in
  324. '') tests=$dfl_tests ;;
  325. 'default') tests=$dfl_tests ;;
  326. 'extra') tests=$extra_tests ;;
  327. 'noalt') tests=$noalt_tests
  328. SKIP_ALT_DEP=1
  329. cmdtest_py+=" --no-altcoin"
  330. unit_tests_py+=" --no-altcoin-deps"
  331. scrambletest_py+=" --no-altcoin" ;;
  332. 'quick') tests=$quick_tests ;;
  333. 'qskip') tests=$qskip_tests ;;
  334. *) tests="$*" ;;
  335. esac
  336. rounds_min=$((rounds / 2))
  337. for n in 2 5 10 20 50 100 200 500 1000; do
  338. eval "rounds${n}x=$((rounds*n))"
  339. done
  340. init_tests
  341. remove_skipped_tests
  342. check_tests
  343. test/cmdtest.py clean
  344. start_time=$(date +%s)
  345. run_tests "$tests"
  346. elapsed=$(($(date +%s)-start_time))
  347. elapsed_fmt=$(printf %02d:%02d $((elapsed/60)) $((elapsed%60)))
  348. [ "$LIST_CMDS" ] || {
  349. if [ "$MMGEN_TEST_SUITE_DETERMINISTIC" ]; then
  350. echo -e "\n${GREEN}All OK"
  351. else
  352. echo -e "\n${GREEN}All OK. Total elapsed time: $elapsed_fmt$RESET"
  353. fi
  354. }