-
Notifications
You must be signed in to change notification settings - Fork 1
/
dirdb.bash
105 lines (86 loc) · 2.31 KB
/
dirdb.bash
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
103
104
105
#!/usr/bin/env bash
dirdb::read() {
local location="$1"
local key="$2"
local default="$3"
local path="$location/$key"
if [ -f "$path" ];then
cat "$path" 2>/dev/null
else
echo -n "$default"
fi
}
dirdb::is_exists() {
local location="$1"
local key="$2"
local path="$location/$key"
if [ -f "$path" ];then
return 0
fi
return 1
}
dirdb::delete() {
local location="$1"
local key="$2"
local path="$location/$key"
if [ -e "$path" ];then
rm -r "$path"
fi
}
dirdb::list() {
local _location="$1"
local _key="$2"
local -n output_list=$3
local _path="$_location/$_key"
output_list=()
if [ -e "$_path" ];then
local _list=( "$_path"/* )
for _item in "${_list[@]}";do
if [ -f "$_item" ];then
output_list+=("${_item##$_path/}")
fi
done
fi
}
dirdb::write() {
local location="$1"
local key="$2"
local value="$3"
local path="$location/$key"
mkdir -p "$( dirname "$path")"
echo -n "$value" > "$path"
}
dirdb_test::test_operations() {
import tmp
local tempdb=""
tmp::create_dir tempdb "dirdb_unit_test"
unit::assert_eq "$(dirdb::read "$tempdb" some/key "default")" "default"
unit::assert_failed dirdb::is_exists "$tempdb" some/key
dirdb::write "$tempdb" some/key "content"
unit::assert_eq "$(dirdb::read "$tempdb" some/key "default")" "content"
unit::assert_success dirdb::is_exists "$tempdb" some/key
dirdb::delete "$tempdb" some/key
unit::assert_eq "$(dirdb::read "$tempdb" some/key "default")" "default"
unit::assert_failed dirdb::is_exists "$tempdb" some/key
tmp::cleanup
}
dirdb_test::test_lists() {
import tmp
local tempdb=""
tmp::create_dir tempdb "dirdb_unit_test"
dirdb::write "$tempdb" list/a "1"
dirdb::write "$tempdb" list/b "2"
local items=""
dirdb::list "$tempdb" list items
unit::assert_eq "${items[*]}" "a b"
dirdb::delete "$tempdb" list
unit::assert_failed dirdb::is_exists "$tempdb" list/a
unit::assert_failed dirdb::is_exists "$tempdb" list/b
dirdb::list "$tempdb" list items
unit::assert_eq "${items[*]}" ""
tmp::cleanup
}
dirdb_test::all() {
unit::test dirdb_test::test_operations
unit::test dirdb_test::test_lists
}