-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_history_functionality.sh
102 lines (82 loc) · 2.96 KB
/
test_history_functionality.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/bash
cp ~/.bash_history_backup ~/.bash_history_backup_BEFORE_TEST
main_script_path="/usr/local/bin/bash_history_backup.sh"
# Test helper functions
function create_test_file() {
local filename=$1
local lines=$2
rm -f "$filename"
for i in $(seq 1 $lines); do
echo "Line $i" >> "$filename"
done
}
function assert_history_length() {
local expected=$1
local actual=$(wc -l < ~/.bash_history)
if [ "$actual" -eq "$expected" ]; then
echo "Test passed: Expected $expected lines, found $actual lines"
else
echo "Test failed: Expected $expected lines, found $actual lines"
fi
}
# Test cases
# Test 1: Restore when backup has more lines
create_test_file ~/.bash_history 10
create_test_file ~/.bash_history_backup 20
$main_script_path restore
assert_history_length 20
# Test 2: Restore when current history has more lines
create_test_file ~/.bash_history 30
create_test_file ~/.bash_history_backup 20
$main_script_path restore
assert_history_length 30
# Test 3: Backup command
create_test_file ~/.bash_history 40
$main_script_path backup
assert_history_length 40
backup_lines=$(wc -l < ~/.bash_history_backup)
if [ "$backup_lines" -eq 40 ]; then
echo "Test passed: Backup has the same number of lines as the original file"
else
echo "Test failed: Backup has a different number of lines than the original file"
fi
# Test 4: Restore when backup file does not exist
rm ~/.bash_history_backup
create_test_file ~/.bash_history 50
$main_script_path restore
assert_history_length 50
# Test 5: Restore when both files have the same number of lines
create_test_file ~/.bash_history 60
create_test_file ~/.bash_history_backup 60
$main_script_path restore
assert_history_length 60
# Test 6: Restore when backup file is empty
create_test_file ~/.bash_history 70
echo -n > ~/.bash_history_backup
$main_script_path restore
assert_history_length 70
# Test 7: Restore when current history is empty
echo -n > ~/.bash_history
create_test_file ~/.bash_history_backup 80
$main_script_path restore
assert_history_length 80
# Test 8: Start and stop the service only if systemd is running
if [ "$(cat /proc/1/comm)" == "systemd" ]; then
sudo systemctl restart bash_history_backup.service
sudo systemctl stop bash_history_backup.service
current_history_lines=$(wc -l < ~/.bash_history)
backup_history_lines=$(wc -l < ~/.bash_history_backup)
if [ "$backup_history_lines" -eq "$current_history_lines" ]; then
echo "Test passed: Starting and stopping the service preserves history"
else
echo "Test failed: Starting and stopping the service alters history"
fi
# reset the history
sudo systemctl restart bash_history_backup.service
cp ~/.bash_history_backup_BEFORE_TEST ~/.bash_history_backup
else
echo "systemd is not running as PID 1, skipping service tests"
fi
cp ~/.bash_history_backup_BEFORE_TEST ~/.bash_history_backup
# the true test... bring the original bash_history back
bash_history_backup.sh restore