1+ #! /usr/bin/env bats
2+
3+ # Test error handling utilities (bash implementation)
4+
5+ # Load the error handling utilities
6+ load " ../../bin/lib/error-handling.bash"
7+
8+ # Test capture_error with no command provided
9+ @test " capture_error returns error when no command provided" {
10+ run capture_error
11+ [ " $status " -eq 1 ]
12+ [[ " $output " =~ " Error: No command provided to capture_error" ]]
13+ }
14+
15+ # Test capture_error with successful command
16+ @test " capture_error executes successful command and returns 0" {
17+ run capture_error " echo 'Hello World'"
18+ [ " $status " -eq 0 ]
19+ [[ " $output " == " Hello World" ]]
20+ }
21+
22+ # Test capture_error with failing command
23+ @test " capture_error handles failing command and provides context" {
24+ run capture_error " exit 42" " Test operation"
25+ [ " $status " -eq 42 ]
26+ [[ " $output " =~ " Error: Test operation failed (exit code: 42)" ]]
27+ [[ " $output " =~ " Command: exit 42" ]]
28+ }
29+
30+ # Test capture_error preserves command output before showing error
31+ @test " capture_error shows command output before error message" {
32+ run capture_error " echo 'Output before failure' && exit 1" " Command with output"
33+ [ " $status " -eq 1 ]
34+ [[ " ${lines[0]} " == " Output before failure" ]]
35+ [[ " $output " =~ " Error: Command with output failed" ]]
36+ }
37+
38+ # Test retry_with_backoff with no command provided
39+ @test " retry_with_backoff returns error when no command provided" {
40+ run retry_with_backoff
41+ [ " $status " -eq 1 ]
42+ [[ " $output " =~ " Error: No command provided to retry_with_backoff" ]]
43+ }
44+
45+ # Test retry_with_backoff with successful command on first try
46+ @test " retry_with_backoff succeeds on first attempt" {
47+ run retry_with_backoff " exit 0"
48+ [ " $status " -eq 0 ]
49+ }
50+
51+ # Test retry_with_backoff exhausts all attempts and fails
52+ @test " retry_with_backoff fails after exhausting max attempts" {
53+ run retry_with_backoff " exit 1" 2 0
54+ [ " $status " -eq 1 ]
55+ }
56+
57+ # Test handle_error with no command provided
58+ @test " handle_error returns error when no command provided" {
59+ run handle_error
60+ [ " $status " -eq 1 ]
61+ [[ " $output " =~ " Error: No command provided to handle_error" ]]
62+ }
63+
64+ # Test handle_error displays error message and generic suggestion
65+ @test " handle_error displays error message and generic suggestion" {
66+ run handle_error " test-command" " 999" " Custom error message"
67+ [ " $status " -eq 1 ]
68+ [[ " $output " =~ " ❌ Error occurred while running: test-command" ]]
69+ [[ " $output " =~ " Error: Custom error message" ]]
70+ [[ " $output " =~ " 💡 Suggestion: Check the command syntax and try again" ]]
71+ }
72+
73+ # Test handle_error provides permission-specific suggestion
74+ @test " handle_error provides permission-specific suggestion for EACCES" {
75+ run handle_error " test-command" " EACCES" " Permission denied"
76+ [ " $status " -eq 1 ]
77+ [[ " $output " =~ " ❌ Error occurred while running: test-command" ]]
78+ [[ " $output " =~ " Error: Permission denied" ]]
79+ [[ " $output " =~ " 💡 Suggestion: Try running with sudo or check file permissions" ]]
80+ }
81+
82+ # Test handle_error provides file-not-found suggestion
83+ @test " handle_error provides file-not-found suggestion for ENOENT" {
84+ run handle_error " test-command" " ENOENT" " No such file or directory"
85+ [ " $status " -eq 1 ]
86+ [[ " $output " =~ " ❌ Error occurred while running: test-command" ]]
87+ [[ " $output " =~ " Error: No such file or directory" ]]
88+ [[ " $output " =~ " 💡 Suggestion: Check if the file or directory exists" ]]
89+ }
90+
91+ # Test handle_error provides network-specific suggestion
92+ @test " handle_error provides network-specific suggestion for ECONNREFUSED" {
93+ run handle_error " test-command" " ECONNREFUSED" " Connection refused"
94+ [ " $status " -eq 1 ]
95+ [[ " $output " =~ " ❌ Error occurred while running: test-command" ]]
96+ [[ " $output " =~ " Error: Connection refused" ]]
97+ [[ " $output " =~ " 💡 Suggestion: Check your internet connection or try again later" ]]
98+ }
99+
100+ # Test handle_error provides disk-space suggestion
101+ @test " handle_error provides disk-space suggestion for ENOSPC" {
102+ run handle_error " test-command" " ENOSPC" " No space left on device"
103+ [ " $status " -eq 1 ]
104+ [[ " $output " =~ " ❌ Error occurred while running: test-command" ]]
105+ [[ " $output " =~ " Error: No space left on device" ]]
106+ [[ " $output " =~ " 💡 Suggestion: Free up disk space and try again" ]]
107+ }
108+
109+ # Test retry_with_backoff succeeds on second attempt
110+ @test " retry_with_backoff succeeds on second attempt after one failure" {
111+ # Create a file that tracks attempts
112+ local attempt_file=$( mktemp)
113+ echo " 0" > " $attempt_file "
114+
115+ # Command that fails first time, succeeds second time
116+ local test_command="
117+ current=\$ (cat '$attempt_file ')
118+ next=\$ ((current + 1))
119+ echo \$ next > '$attempt_file '
120+ [ \$ next -eq 2 ]
121+ "
122+
123+ run retry_with_backoff " $test_command " 3 0
124+ [ " $status " -eq 0 ]
125+
126+ # Clean up
127+ rm -f " $attempt_file "
128+ }
129+
130+ # Test capture_error with default context
131+ @test " capture_error uses default context when none provided" {
132+ run capture_error " exit 1"
133+ [ " $status " -eq 1 ]
134+ [[ " $output " =~ " Error: Command execution failed" ]]
135+ }
0 commit comments