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

dynamotn / dybatpho / 16619628884

30 Jul 2025 10:07AM UTC coverage: 84.588% (-0.9%) from 85.451%
16619628884

push

github

dynamotn
test: correct test case name

472 of 558 relevant lines covered (84.59%)

42.08 hits per line

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

89.47
/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_USED_KILLED_HANDLER** (bool): Flag that script used dybatpho::register_killed_handler
9
: "${DYBATPHO_DIR:?DYBATPHO_DIR must be set. Please source dybatpho/init.sh before other scripts from dybatpho.}"
208✔
10

11
DYBATPHO_USED_ERR_HANDLER=false
208✔
12
DYBATPHO_USED_KILLED_HANDLER=false
208✔
13
DRY_RUN="${DRY_RUN:-}"
208✔
14
export DRY_RUN
208✔
15

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

30
#######################################
31
# @description Register error handler.
32
# @set DYBATPHO_USED_ERR_HANDLING
33
# @noargs
34
#######################################
35
function dybatpho::register_err_handler {
36
  set -E
2✔
37
  # shellcheck disable=SC2034
38
  DYBATPHO_USED_ERR_HANDLER=true
2✔
39
  dybatpho::trap 'dybatpho::run_err_handler $?' ERR
2✔
40
}
41

42
#######################################
43
# @description Register killed process handler.
44
# @set DYBATPHO_USED_ERR_HANDLING
45
# @noargs
46
#######################################
47
function dybatpho::register_killed_handler {
48
  # shellcheck disable=SC2034
49
  DYBATPHO_USED_KILLED_HANDLER=true
2✔
50
  dybatpho::trap 'dybatpho::killed_process_handler SIGINT' SIGINT
2✔
51
  dybatpho::trap 'dybatpho::killed_process_handler SIGTERM' SIGTERM
2✔
52
}
53

54
#######################################
55
# @description Register all handlers
56
# @noargs
57
#######################################
58
function dybatpho::register_common_handlers {
59
  dybatpho::register_err_handler
1✔
60
  dybatpho::register_killed_handler
1✔
61
}
62

63
#######################################
64
# @description Handle error when running process. If you activate by `dybatpho::register_err_handler`, you don't need to invoke this function.
65
# @arg $1 number Exit code of last command
66
#######################################
67
function dybatpho::run_err_handler {
68
  local exit_code
3✔
69
  dybatpho::expect_args exit_code -- "$@"
3✔
70
  local i=0
3✔
71
  printf -- '%s\n' "Aborting on error ${exit_code}:" \
3✔
72
    "--------------------" >&2
73
  while caller "${i}" >&2; do
18✔
74
    ((i++))
15✔
75
  done
76
  exit "${exit_code}"
3✔
77
}
78

79
#######################################
80
# @description Handle killed process. If you activate by `dybatpho::register_killed_handler`, you don't need to invoke this function.
81
# @arg $1 string Signal
82
#######################################
83
function dybatpho::killed_process_handler {
84
  local signal
×
85
  dybatpho::expect_args signal -- "$@"
×
86

87
  case ${signal} in
×
88
    SIGINT)
89
      dybatpho::error 'Interrupt by CTRL+C'
×
90
      ;;
91
    SIGTERM)
92
      dybatpho::error 'Terminated'
×
93
      ;;
94
  esac
95
  trap - SIGTERM && kill -- -$$
×
96
}
97

98
#######################################
99
# @description Trap multiple signals
100
# @arg $1 string Command run when trapped
101
# @arg $@ string Signals to trap
102
#######################################
103
function dybatpho::trap {
104
  local command
7✔
105
  dybatpho::expect_args command -- "$@"
7✔
106
  shift
7✔
107
  # shellcheck disable=SC2317
108
  _gen_finalize_command() {
109
    # shellcheck disable=SC2086
110
    local cmds=$(trap -p "$1")
16✔
111
    cmds="${cmds#*\'}"
8✔
112
    cmds="${cmds%\'*}"
8✔
113
    echo "${cmds}"
8✔
114
  }
115

116
  local finalize_command
7✔
117
  for signal in "$@"; do
8✔
118
    finalize_command=$(_gen_finalize_command "${signal}")
16✔
119
    finalize_command="${finalize_command}${finalize_command:+; }${command}"
8✔
120
    # shellcheck disable=SC2064,SC2086
121
    trap "${finalize_command}" "${signal}"
8✔
122
  done
123
}
124

125
#######################################
126
# @description Clean up file on exit
127
# @arg $1 string File path
128
#######################################
129
function dybatpho::cleanup_file_on_exit {
130
  local filepath
12✔
131
  dybatpho::expect_args filepath -- "$@"
12✔
132

133
  local pid="$$"
12✔
134
  local cleanup_file
12✔
135
  if hash "mktemp" > /dev/null 2>&1; then
12✔
136
    cleanup_file=$(mktemp --tmpdir="${TMPDIR:-/tmp}" "dybatpho_cleanup-${pid}-XXXXXXXX.sh")
24✔
137
  else
138
    cleanup_file="/tmp/dybatpho_cleanup-${pid}.sh" # kcov(skip)
139
  fi
140
  touch "${cleanup_file}" "${cleanup_file}.new"
12✔
141
  ( # kcov(skip)
142
    grep -vF "${cleanup_file}" "${cleanup_file}" \
12✔
143
      || (
144
        echo "rm -r '${filepath}' 2>/dev/null || :"
12✔
145
        echo "rm -r '${cleanup_file}' 2>/dev/null || :"
12✔
146
      )                     # kcov(skip)
147
  ) > "${cleanup_file}.new" # kcov(skip)
148
  mv -f "${cleanup_file}.new" "${cleanup_file}"
12✔
149

150
  # kcov(disabled)
151
  local trap_command="dybatpho::trap"
152
  if [[ "${BATS_ROOT:-}" != "" ]]; then
153
    trap_command="trap"
154
  fi
155
  "${trap_command}" ". ${cleanup_file}" EXIT HUP INT TERM
156
  # kcov(enabled)
157
}
158

159
#######################################
160
# @description Show dry run message or run command.
161
# @arg $@ string Command to run
162
# @stdout Show details of command if DRY_RUN is set to true
163
#######################################
164
function dybatpho::dry_run {
165
  if dybatpho::is true "${DRY_RUN}"; then
2✔
166
    echo "DRY RUN: $@"
1✔
167
  else
168
    "$@"
1✔
169
  fi
170
}
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