Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
heroku-buildpack-nodejs
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Python-Dev
heroku-buildpack-nodejs
Commits
76d0f059
Unverified
Commit
76d0f059
authored
Oct 02, 2018
by
Jeremy Morrell
Committed by
GitHub
Oct 02, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add key-value store (#567)
parent
3678c3b9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
1 deletion
+120
-1
kvstore.sh
lib/kvstore.sh
+53
-0
unit
test/unit
+67
-1
No files found.
lib/kvstore.sh
0 → 100644
View file @
76d0f059
kv_create
()
{
local
f
=
$1
mkdir
-p
$(
dirname
$f
)
touch
$f
}
kv_clear
()
{
local
f
=
$1
echo
""
>
$f
}
kv_set
()
{
if
[[
$#
-eq
3
]]
;
then
local
f
=
$1
if
[[
-f
$f
]]
;
then
echo
"
$2
=
$3
"
>>
$f
fi
fi
}
kv_get
()
{
if
[[
$#
-eq
2
]]
;
then
local
f
=
$1
if
[[
-f
$f
]]
;
then
grep
"^
$2
="
$f
|
sed
-e
"s/^
$2
=//"
|
tail
-n
1
fi
fi
}
kv_keys
()
{
local
f
=
$1
local
keys
=()
if
[[
-f
$f
]]
;
then
# get list of keys
while
IFS
=
"="
read
-r
key value
;
do
keys+
=(
"
$key
"
)
done
<
$f
echo
"
${
keys
[@]
}
"
|
tr
' '
'\n'
|
sort
-u
fi
}
kv_list
()
{
local
f
=
$1
kv_keys
$f
|
tr
' '
'\n'
|
while
read
-r
key
;
do
if
[[
-n
$key
]]
;
then
echo
"
$key
=
$(
kv_get
$f
$key
)
"
fi
done
}
test/unit
View file @
76d0f059
...
...
@@ -46,9 +46,75 @@ testOutput() {
assertEquals
'should preserve unescaped backslashes'
' Foo \ bar'
"
${
stdout
}
"
}
testKeyValue
()
{
local
store
=
$(
mktemp
)
kv_create
$store
kv_set
$store
key value
kv_set
$store
foo bar
kv_set
$store
key other_value
kv_set
$store
bar baz
assertEquals
"other_value"
"
$(
kv_get
$store
key
)
"
assertEquals
"bar"
"
$(
kv_get
$store
foo
)
"
assertEquals
"baz"
"
$(
kv_get
$store
bar
)
"
# if the key isn't there it should return an empty string
assertEquals
""
"
$(
kv_get
$store
not_there
)
"
# kv_keys returns each key on a new line
assertEquals
"
$(
printf
"%s
\n
"
bar foo key
)
"
"
$(
kv_keys
$store
)
"
# kv_list returns key=value on individual lines
assertEquals
"
$(
printf
"%s
\n
"
bar
=
baz
foo
=
bar
key
=
other_value
)
"
"
$(
kv_list
$store
)
"
# calling create on an existing store doesn't erase it
kv_create
$store
assertEquals
"
$(
printf
"%s
\n
"
bar
=
baz
foo
=
bar
key
=
other_value
)
"
"
$(
kv_list
$store
)
"
# now clear the store
kv_clear
$store
assertEquals
""
"
$(
kv_get
$store
key
)
"
assertEquals
""
"
$(
kv_keys
$store
)
"
assertEquals
""
"
$(
kv_list
$store
)
"
}
# if the file doesn't exist, everything should be a no-op
testKeyValueNoFile
()
{
# empty file argument
local
empty
=
""
kv_set
$empty
key value
assertEquals
"
$(
kv_get
$empty
key
)
"
""
assertEquals
"
$(
kv_keys
$empty
)
"
""
assertEquals
"
$(
kv_list
$empty
)
"
""
local
store
=
"/tmp/does-not-exist"
kv_set
$store
key value
assertEquals
""
"
$(
kv_get
$store
key
)
"
assertEquals
""
"
$(
kv_keys
$store
)
"
assertEquals
""
"
$(
kv_list
$store
)
"
# running these commands has not created this file
assertTrue
"[[ ! -e
$store
]]"
local
space
=
" "
kv_set
$space
key value
assertEquals
"
$(
kv_get
$space
key
)
"
""
assertEquals
"
$(
kv_keys
$space
)
"
""
assertEquals
"
$(
kv_list
$space
)
"
""
}
# the modules to be tested
source
"
$(
pwd
)
"
/lib/monitor.sh
source
"
$(
pwd
)
"
/lib/output.sh
source
"
$(
pwd
)
"
/lib/kvstore.sh
# import the testing framework
source
"
$(
pwd
)
"
/test/shunit2
\ No newline at end of file
source
"
$(
pwd
)
"
/test/shunit2
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment