test-release.sh 11 KB

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