Skip to content

Commit e5d789f

Browse files
committed
fix #2
1 parent 5ed2cbc commit e5d789f

File tree

3 files changed

+79
-91
lines changed

3 files changed

+79
-91
lines changed

lib_ini.sh

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,18 @@ function ini_validate_key_name() {
114114

115115
# Create a secure temporary file
116116
function ini_create_temp_file() {
117-
local temp_file
118-
if ! temp_file=$(mktemp "${TMPDIR:-/tmp}/ini_XXXXXXXXXX" 2>/dev/null) || [ -z "$temp_file" ]; then
117+
if (( $# < 1 )); then
118+
ini_error "ini_create_temp_file: missing variable name argument"
119+
return 1
120+
fi
121+
local -n _ini_ctf_ref=$1
122+
local _ini_ctf_path
123+
if ! _ini_ctf_path=$(mktemp "${TMPDIR:-/tmp}/ini_XXXXXXXXXX" 2>/dev/null) || [ -z "$_ini_ctf_path" ]; then
119124
ini_error "Failed to create temporary file"
120125
return 1
121126
fi
122-
_ini_temp_files+=("$temp_file")
123-
echo "$temp_file"
127+
_ini_temp_files+=("$_ini_ctf_path")
128+
_ini_ctf_ref="$_ini_ctf_path"
124129
}
125130

126131
# Trim whitespace from start and end of a string
@@ -274,29 +279,34 @@ function _ini_remove_bom() {
274279

275280
# Lock file using flock
276281
function ini_lock_file() {
277-
local file="$1"
278-
local lock_file="${file}.lock"
279-
local lock_fd
280-
local max_attempts=10
281-
local attempt=0
282+
if (( $# < 2 )); then
283+
ini_error "ini_lock_file: missing arguments (file and variable name)"
284+
return 1
285+
fi
286+
local _ini_lf_file="$1"
287+
local -n _ini_lf_ref=$2
288+
local _ini_lf_lock_file="${_ini_lf_file}.lock"
289+
local _ini_lf_fd
290+
local _ini_lf_max_attempts=10
291+
local _ini_lf_attempt=0
282292

283293
# Try to create lock file
284-
while [ $attempt -lt $max_attempts ]; do
285-
if exec {lock_fd}>"$lock_file" 2>/dev/null; then
294+
while [ $_ini_lf_attempt -lt $_ini_lf_max_attempts ]; do
295+
if exec {_ini_lf_fd}>"$_ini_lf_lock_file" 2>/dev/null; then
286296
# Try to obtain exclusive lock (timeout of 1 second per attempt)
287-
if flock -w 1 -x "$lock_fd" 2>/dev/null; then
288-
_ini_lock_fds+=("$lock_fd")
289-
echo "$lock_fd"
297+
if flock -w 1 -x "$_ini_lf_fd" 2>/dev/null; then
298+
_ini_lock_fds+=("$_ini_lf_fd")
299+
_ini_lf_ref="$_ini_lf_fd"
290300
return 0
291301
else
292-
exec {lock_fd}>&-
302+
exec {_ini_lf_fd}>&-
293303
fi
294304
fi
295-
attempt=$((attempt + 1))
305+
_ini_lf_attempt=$((_ini_lf_attempt + 1))
296306
sleep 0.1
297307
done
298308

299-
ini_error "Failed to acquire lock on file: $file (after ${max_attempts} attempts)"
309+
ini_error "Failed to acquire lock on file: $_ini_lf_file (after ${_ini_lf_max_attempts} attempts)"
300310
return 1
301311
}
302312

@@ -743,10 +753,10 @@ function ini_write() {
743753
local found_key=0
744754
# Acquire lock for file writing
745755
local lock_fd
746-
lock_fd=$(ini_lock_file "$file") || return 1
756+
ini_lock_file "$file" lock_fd || return 1
747757

748758
local temp_file
749-
temp_file=$(ini_create_temp_file) || {
759+
ini_create_temp_file temp_file || {
750760
ini_unlock_file "$lock_fd"
751761
return 1
752762
}
@@ -880,15 +890,15 @@ function ini_remove_section() {
880890

881891
# Acquire lock for file writing
882892
local lock_fd
883-
lock_fd=$(ini_lock_file "$file") || return 1
893+
ini_lock_file "$file" lock_fd || return 1
884894

885895
# Escape section for regex pattern
886896
local escaped_section
887897
escaped_section=$(ini_escape_for_regex "$section")
888898
local section_pattern="^\[$escaped_section\]"
889899
local in_section=0
890900
local temp_file
891-
temp_file=$(ini_create_temp_file) || {
901+
ini_create_temp_file temp_file || {
892902
ini_unlock_file "$lock_fd"
893903
return 1
894904
}
@@ -1002,7 +1012,7 @@ function ini_remove_key() {
10021012

10031013
# Acquire lock for file writing
10041014
local lock_fd
1005-
lock_fd=$(ini_lock_file "$file") || return 1
1015+
ini_lock_file "$file" lock_fd || return 1
10061016

10071017
# Escape section and key for regex pattern
10081018
local escaped_section
@@ -1014,7 +1024,7 @@ function ini_remove_key() {
10141024
local key_pattern="^[[:space:]]*${escaped_key}[[:space:]]*="
10151025
local in_section=0
10161026
local temp_file
1017-
temp_file=$(ini_create_temp_file) || {
1027+
ini_create_temp_file temp_file || {
10181028
ini_unlock_file "$lock_fd"
10191029
return 1
10201030
}
@@ -1709,7 +1719,7 @@ function ini_rename_section() {
17091719

17101720
# Acquire lock for file writing
17111721
local lock_fd
1712-
lock_fd=$(ini_lock_file "$file") || return 1
1722+
ini_lock_file "$file" lock_fd || return 1
17131723

17141724
# Escape sections for regex pattern
17151725
local escaped_old_section
@@ -1718,7 +1728,7 @@ function ini_rename_section() {
17181728
local old_section_pattern="^\[$escaped_old_section\]"
17191729
local in_section=0
17201730
local temp_file
1721-
temp_file=$(ini_create_temp_file) || {
1731+
ini_create_temp_file temp_file || {
17221732
ini_unlock_file "$lock_fd"
17231733
return 1
17241734
}
@@ -1868,10 +1878,10 @@ function ini_format() {
18681878

18691879
# Acquire lock for file writing
18701880
local lock_fd
1871-
lock_fd=$(ini_lock_file "$file") || return 1
1881+
ini_lock_file "$file" lock_fd || return 1
18721882

18731883
local temp_file
1874-
temp_file=$(ini_create_temp_file) || {
1884+
ini_create_temp_file temp_file || {
18751885
ini_unlock_file "$lock_fd"
18761886
return 1
18771887
}

run_tests.sh

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,43 @@ echo -e "${YELLOW}=====================================${NC}"
1010
echo -e "${YELLOW} Running All bash_ini_parser Tests ${NC}"
1111
echo -e "${YELLOW}=====================================${NC}\n"
1212

13-
# First run the basic tests
14-
echo -e "${YELLOW}Running Basic Tests...${NC}"
15-
bash tests/lib_ini_tests.sh
16-
17-
BASIC_EXIT=$?
18-
19-
# Then run the extended tests
20-
echo -e "\n${YELLOW}Running Extended Tests...${NC}"
21-
bash tests/lib_ini_extended_tests.sh
22-
23-
EXTENDED_EXIT=$?
24-
25-
# Now run the environment override tests
26-
echo -e "\n${YELLOW}Running Environment Override Tests...${NC}"
27-
bash tests/test_env_override.sh
28-
29-
ENV_OVERRIDE_EXIT=$?
30-
31-
# Now run the security tests
32-
echo -e "\n${YELLOW}Running Security Tests...${NC}"
33-
bash tests/lib_ini_security_tests.sh
34-
35-
SECURITY_EXIT=$?
36-
37-
# Now run the advanced features tests
38-
echo -e "\n${YELLOW}Running Advanced Features Tests...${NC}"
39-
bash tests/lib_ini_advanced_features_tests.sh
40-
41-
ADVANCED_EXIT=$?
42-
43-
# Now run the BOM support tests
44-
echo -e "\n${YELLOW}Running BOM Support Tests...${NC}"
45-
bash tests/lib_ini_bom_tests.sh
46-
47-
BOM_EXIT=$?
48-
49-
# Extract test counts from each test suite
50-
BASIC_TOTAL=$(bash tests/lib_ini_tests.sh 2>&1 | grep -oP 'Total tests executed: \K\d+' || echo "0")
51-
BASIC_PASSED=$(bash tests/lib_ini_tests.sh 2>&1 | grep -oP 'Tests passed: \K\d+' || echo "0")
52-
BASIC_FAILED=$(bash tests/lib_ini_tests.sh 2>&1 | grep -oP 'Tests failed: \K\d+' || echo "0")
53-
54-
EXTENDED_TOTAL=$(bash tests/lib_ini_extended_tests.sh 2>&1 | grep -oP 'Total extended tests executed: \K\d+' || echo "0")
55-
EXTENDED_PASSED=$(bash tests/lib_ini_extended_tests.sh 2>&1 | grep -oP 'Tests passed: \K\d+' || echo "0")
56-
EXTENDED_FAILED=$(bash tests/lib_ini_extended_tests.sh 2>&1 | grep -oP 'Tests failed: \K\d+' || echo "0")
57-
58-
ENV_TOTAL=$(bash tests/test_env_override.sh 2>&1 | grep -oP 'Total environment override tests executed: \K\d+' || echo "0")
59-
ENV_PASSED=$(bash tests/test_env_override.sh 2>&1 | grep -oP 'Tests passed: \K\d+' || echo "0")
60-
ENV_FAILED=$(bash tests/test_env_override.sh 2>&1 | grep -oP 'Tests failed: \K\d+' || echo "0")
61-
62-
SECURITY_TOTAL=$(bash tests/lib_ini_security_tests.sh 2>&1 | grep -oP 'Total security tests executed: \K\d+' || echo "0")
63-
SECURITY_PASSED=$(bash tests/lib_ini_security_tests.sh 2>&1 | grep -oP 'Tests passed: \K\d+' || echo "0")
64-
SECURITY_FAILED=$(bash tests/lib_ini_security_tests.sh 2>&1 | grep -oP 'Tests failed: \K\d+' || echo "0")
65-
66-
ADVANCED_TOTAL=$(bash tests/lib_ini_advanced_features_tests.sh 2>&1 | grep -oP 'Total advanced features tests executed: \K\d+' || echo "0")
67-
ADVANCED_PASSED=$(bash tests/lib_ini_advanced_features_tests.sh 2>&1 | grep -oP 'Tests passed: \K\d+' || echo "0")
68-
ADVANCED_FAILED=$(bash tests/lib_ini_advanced_features_tests.sh 2>&1 | grep -oP 'Tests failed: \K\d+' || echo "0")
69-
70-
BOM_TOTAL=$(bash tests/lib_ini_bom_tests.sh 2>&1 | grep -oP 'Total BOM tests executed: \K\d+' || echo "0")
71-
BOM_PASSED=$(bash tests/lib_ini_bom_tests.sh 2>&1 | grep -oP 'Tests passed: \K\d+' || echo "0")
72-
BOM_FAILED=$(bash tests/lib_ini_bom_tests.sh 2>&1 | grep -oP 'Tests failed: \K\d+' || echo "0")
13+
# Helper to run a test suite, capture output, and extract counts
14+
run_suite() {
15+
local suite_name="$1"
16+
local suite_cmd="$2"
17+
local total_pattern="$3"
18+
19+
echo -e "${YELLOW}Running ${suite_name}...${NC}"
20+
local output
21+
output=$(bash "$suite_cmd" 2>&1)
22+
local exit_code=$?
23+
echo "$output"
24+
25+
# Extract counts from captured output
26+
local total passed failed
27+
total=$(echo "$output" | grep -oP "${total_pattern}: \\K\\d+" || echo "0")
28+
passed=$(echo "$output" | grep -oP 'Tests passed: \K\d+' || echo "0")
29+
failed=$(echo "$output" | grep -oP 'Tests failed: \K\d+' || echo "0")
30+
31+
# Store results in global variables using nameref
32+
eval "${suite_name// /_}_TOTAL=$total"
33+
eval "${suite_name// /_}_PASSED=$passed"
34+
eval "${suite_name// /_}_FAILED=$failed"
35+
eval "${suite_name// /_}_EXIT=$exit_code"
36+
}
37+
38+
# Run all test suites (each only once)
39+
run_suite "BASIC" "tests/lib_ini_tests.sh" "Total tests executed"
40+
echo ""
41+
run_suite "EXTENDED" "tests/lib_ini_extended_tests.sh" "Total extended tests executed"
42+
echo ""
43+
run_suite "ENV" "tests/test_env_override.sh" "Total environment override tests executed"
44+
echo ""
45+
run_suite "SECURITY" "tests/lib_ini_security_tests.sh" "Total security tests executed"
46+
echo ""
47+
run_suite "ADVANCED" "tests/lib_ini_advanced_features_tests.sh" "Total advanced features tests executed"
48+
echo ""
49+
run_suite "BOM" "tests/lib_ini_bom_tests.sh" "Total BOM tests executed"
7350

7451
# Calculate totals
7552
TOTAL_TESTS=$((BASIC_TOTAL + EXTENDED_TOTAL + ENV_TOTAL + SECURITY_TOTAL + ADVANCED_TOTAL + BOM_TOTAL))
@@ -88,16 +65,17 @@ echo -e "BOM support tests: ${BOM_TOTAL} executed, ${GREEN}${BOM_PASSED} passed
8865
echo -e "${YELLOW}-------------------------------------${NC}"
8966
echo -e "Total: ${TOTAL_TESTS} executed, ${GREEN}${TOTAL_PASSED} passed${NC}, ${RED}${TOTAL_FAILED} failed${NC}"
9067

91-
if [ $BASIC_EXIT -eq 0 ] && [ $EXTENDED_EXIT -eq 0 ] && [ $ENV_OVERRIDE_EXIT -eq 0 ] && [ $SECURITY_EXIT -eq 0 ] && [ $ADVANCED_EXIT -eq 0 ] && [ $BOM_EXIT -eq 0 ]; then
68+
if [ $BASIC_EXIT -eq 0 ] && [ $EXTENDED_EXIT -eq 0 ] && [ $ENV_EXIT -eq 0 ] && [ $SECURITY_EXIT -eq 0 ] && [ $ADVANCED_EXIT -eq 0 ] && [ $BOM_EXIT -eq 0 ]; then
9269
echo -e "\n${GREEN}ALL TESTS PASSED!${NC}"
9370
exit 0
9471
else
9572
echo -e "\n${RED}SOME TESTS FAILED!${NC}"
9673
[ $BASIC_EXIT -ne 0 ] && echo -e "${RED}Basic tests failed.${NC}"
9774
[ $EXTENDED_EXIT -ne 0 ] && echo -e "${RED}Extended tests failed.${NC}"
98-
[ $ENV_OVERRIDE_EXIT -ne 0 ] && echo -e "${RED}Environment override tests failed.${NC}"
75+
[ $ENV_EXIT -ne 0 ] && echo -e "${RED}Environment override tests failed.${NC}"
9976
[ $SECURITY_EXIT -ne 0 ] && echo -e "${RED}Security tests failed.${NC}"
10077
[ $ADVANCED_EXIT -ne 0 ] && echo -e "${RED}Advanced features tests failed.${NC}"
10178
[ $BOM_EXIT -ne 0 ] && echo -e "${RED}BOM support tests failed.${NC}"
10279
exit 1
103-
fi
80+
fi
81+

tests/lib_ini_security_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ run_test "ini_create_temp_file exists" "type ini_create_temp_file > /dev/null 2>
131131

132132
# Test that the function actually creates a temp file
133133
TESTS_TOTAL=$((TESTS_TOTAL + 1))
134-
TEMP_TEST_FILE=$(ini_create_temp_file 2>/dev/null)
134+
ini_create_temp_file TEMP_TEST_FILE 2>/dev/null
135135
if [ -n "$TEMP_TEST_FILE" ] && [ -f "$TEMP_TEST_FILE" ]; then
136136
echo -e "${GREEN}✓ ini_create_temp_file creates valid temp file${NC}"
137137
TESTS_PASSED=$((TESTS_PASSED + 1))

0 commit comments

Comments
 (0)