• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dynamotn / dybatpho / 14388961063

10 Apr 2025 07:42PM UTC coverage: 98.462% (-0.4%) from 98.828%
14388961063

push

github

dynamotn
fix: multiple traps not work

12 of 13 new or added lines in 1 file covered. (92.31%)

256 of 260 relevant lines covered (98.46%)

36.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.67
/src/process.sh
1
#!/usr/bin/env bash
2
# @file process.sh
3
# @brief Utilities for process handling
4
# @description
5
#   This module contains functions to error handling, fork process...
6
#
7
# DYBATPHO_USED_ERR_HANDLER bool Flag that script used dybatpho::register_err_handler
8
: "${DYBATPHO_DIR:?DYBATPHO_DIR must be set. Please source dybatpho/init.sh before other scripts from dybatpho.}"
132✔
9

10
DYBATPHO_USED_ERR_HANDLER=false
132✔
11

12
#######################################
13
# @description Stop script/process.
14
# @arg $1 string Message
15
# @arg $2 number Exit code, default is 1
16
# @exitcode $2 Stop to process anything else
17
#######################################
18
function dybatpho::die {
19
  local message exit_code
10✔
20
  dybatpho::expect_args message -- "$@"
10✔
21
  exit_code=${2:-1}
10✔
22
  dybatpho::fatal "${message}" "${BASH_SOURCE[-1]}:${BASH_LINENO[0]}"
10✔
23
  exit "${exit_code}"
10✔
24
}
25

26
#######################################
27
# @description Register error handling.
28
# @set DYBATPHO_USED_ERR_HANDLING
29
# @noargs
30
#######################################
31
function dybatpho::register_err_handler {
32
  set -E
1✔
33
  # shellcheck disable=SC2034
34
  DYBATPHO_USED_ERR_HANDLER=true
1✔
35
  dybatpho::trap 'dybatpho::run_err_handler $?' ERR
1✔
36
}
37

38
#######################################
39
# @description Run error handling. If you activate by `dybatpho::register_err_handler`, you don't need to invoke this function.
40
# @arg $1 number Exit code of last command
41
#######################################
42
function dybatpho::run_err_handler {
43
  local exit_code
1✔
44
  dybatpho::expect_args exit_code -- "$@"
1✔
45
  local i=0
1✔
46
  printf -- '%s\n' "Aborting on error ${exit_code}:" \
1✔
47
    "--------------------" >&2
48
  while caller "${i}"; do
6✔
49
    ((i++))
5✔
50
  done
51
  exit "${exit_code}"
1✔
52
}
53

54
#######################################
55
# @description Trap multiple signals
56
# @arg $1 string Command run when trapped
57
# @arg $@ string Signals to trap
58
#######################################
59
function dybatpho::trap {
60
  local command
2✔
61
  dybatpho::expect_args command -- "$@"
2✔
62
  shift
2✔
63
  # shellcheck disable=SC2317
64
  _gen_finalize_command() {
65
    # shellcheck disable=SC2086
66
    local cmds=$(trap -p "$1")
6✔
67
    cmds="${cmds#*\'}"
3✔
68
    cmds="${cmds%\'*}"
3✔
69
    echo "${cmds}"
3✔
70
  }
71

72
  local finalize_command
2✔
73
  for signal in "$@"; do
3✔
74
    finalize_command=$(_gen_finalize_command "${signal}")
6✔
75
    finalize_command="${finalize_command}${finalize_command:+; }${command}"
3✔
76
    # shellcheck disable=SC2064,SC2086
77
    trap "${finalize_command}" "${signal}"
3✔
78
  done
79
}
80

81
#######################################
82
# @description Clean up file on exit
83
# @arg $1 string File path
84
#######################################
85
function dybatpho::cleanup_file_on_exit {
86
  local filepath
1✔
87
  dybatpho::expect_args filepath -- "$@"
1✔
88

89
  local pid="$$"
1✔
90
  local cleanup_file=$(mktemp --tmpdir="${TMPDIR:-/tmp}" "dybatpho_cleanup-${pid}-XXXXX.sh")
2✔
91
  touch "${cleanup_file}" "${cleanup_file}.new"
1✔
92
  (
93
    grep -vF "${cleanup_file}" "${cleanup_file}" \
1✔
94
      || (
95
        echo "rm -r '${filepath}'"
1✔
96
        echo "rm -r '${cleanup_file}'"
1✔
97
      )
NEW
98
  ) > "${cleanup_file}.new"
×
99
  mv -f "${cleanup_file}.new" "${cleanup_file}"
1✔
100

101
  local trap_command="dybatpho::trap"
1✔
102
  if [[ "${BATS_ROOT:-}" != "" ]]; then
1✔
103
    trap_command="trap"
1✔
104
  fi
105

106
  "${trap_command}" ". ${cleanup_file}" EXIT HUP INT TERM
1✔
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc