Unverified Commit d7892f08 authored by Jeremy Morrell's avatar Jeremy Morrell Committed by GitHub

Add uuid utility (#618)

Adds a cross-platform compatible command to generate `uuid`s
parent 6f065b32
#!/usr/bin/env bash
uuid_fallback()
{
local N B C='89ab'
for (( N=0; N < 16; ++N ))
do
B=$(( RANDOM%256 ))
case $N in
6)
printf '4%x' $(( B%16 ))
;;
8)
printf '%c%x' ${C:$RANDOM%${#C}:1} $(( B%16 ))
;;
3 | 5 | 7 | 9)
printf '%02x-' $B
;;
*)
printf '%02x' $B
;;
esac
done
echo
}
uuid() {
# On Heroku's stack, there is a uuid command
if [[ -f /proc/sys/kernel/random/uuid ]]; then
cat /proc/sys/kernel/random/uuid
# on macOS there is also a command
elif [[ -x "$(command -v uuidgen)" ]]; then
uuidgen | tr "[:upper:]" "[:lower:]"
# If you are running this buildpack on an image without either of the above binaries
# then let's provide something that approximates this functionality, but beware that
# we can make no guarantees of true randomness or uniqueness of this ID. However it is
# likely only being piped to /dev/null
#
# If that's not true for you, please file an issue and let us know:
# https://github.com/heroku/heroku-buildpack-nodejs/issues
else
uuid_fallback
fi
}
......@@ -260,6 +260,34 @@ testWebConcurrencyProfileScript() {
assertEquals "1" "$(calculate_concurrency 512 1)"
}
isUUID() {
if [[ ${1//-/} =~ ^[[:xdigit:]]{32}$ ]]; then
echo true
else
echo false
fi
}
testUUID() {
local first second
first=$(uuid)
second=$(uuid)
assertNotEquals "$first" "$second"
assertEquals "true" "$(isUUID "$first")"
assertEquals "true" "$(isUUID "$second")"
}
testUUIDFallback() {
local first second
first=$(uuid_fallback)
second=$(uuid_fallback)
assertNotEquals "$first" "$second"
assertEquals "true" "$(isUUID "$first")"
assertEquals "true" "$(isUUID "$second")"
}
testHasScript() {
local file="$(pwd)/test/fixtures/has-script-fixtures/package.json"
assertEquals "true" "$(has_script "$file" "build")"
......@@ -274,6 +302,8 @@ BP_DIR="$(pwd)"
source "$(pwd)"/test/mocks/stdlib.sh
# the modules to be tested
source "$(pwd)"/lib/uuid.sh
source "$(pwd)"/lib/environment.sh
source "$(pwd)"/lib/json.sh
source "$(pwd)"/lib/json.sh
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment