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

dynamotn / dybatpho / 13649959108

04 Mar 2025 09:00AM UTC coverage: 99.595% (-0.4%) from 100.0%
13649959108

push

github

dynamotn
refactor: cleanup file instead of generate temp file

20 of 21 new or added lines in 2 files covered. (95.24%)

246 of 247 relevant lines covered (99.6%)

40.61 hits per line

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

97.5
/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 before other scripts from dybatpho.}"
134✔
9

10
DYBATPHO_USED_ERR_HANDLER=false
134✔
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
  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
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 $2 string_list Signals to trap
58
#######################################
59
function dybatpho::trap {
60
  local command signal
2✔
61
  dybatpho::expect_args command signal -- "$@"
2✔
62
  shift
2✔
63
  # shellcheck disable=SC2317
64
  _gen_finalize_command() {
65
    # shellcheck disable=SC2086
66
    local cmds=$(trap -p $1)
12✔
67
    cmds="${cmds#*\'}"
6✔
68
    cmds="${cmds%\'*}"
6✔
69
    echo "$cmds"
6✔
70
  }
71

72
  local finalize_command
2✔
73
  for signal in "$@"; do
6✔
74
    finalize_command=$(_gen_finalize_command "$signal")
12✔
75
    finalize_command="$finalize_command${finalize_command:+; }$command"
6✔
76
    # shellcheck disable=SC2064,SC2086
77
    trap "$command" $signal
6✔
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="/tmp/dybatpho_cleanup-${pid}.sh"
1✔
91
  touch $cleanup_file
1✔
92
  (
93
    grep -vF $cleanup_file $cleanup_file ||
1✔
94
      (
95
        echo "/bin/rm -rf '$filepath'"
1✔
96
        echo "/bin/rm -rf $cleanup_file"
1✔
97
      )
NEW
98
  ) >$cleanup_file.new
×
99
  mv -f $cleanup_file.new $cleanup_file
1✔
100
  dybatpho::trap "bash $cleanup_file" EXIT HUP INT TERM
1✔
101
}
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