Skip to content

Commit

Permalink
Use environment variable instead of lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
usommerl committed Jan 6, 2017
1 parent 3477d3c commit 72bf63c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
10 changes: 2 additions & 8 deletions lnkr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ __cache_directory() {
__latest_version() {
local cache="$(__cache_directory)/latest"
local cache_age="$(($(date +%s) - $(stat -c %Y "$cache" 2>&- || echo '0')))"
if [ "$cache_age" -le 3600 ]; then
if [ "$cache_age" -le 3600 ] && [ -z "${CACHING_DISABLED:-}" ]; then
head -n 1 "$cache" 2>&-
else
local url='https://api.github.com/repos/usommerl/lnkr/releases/latest'
Expand All @@ -35,15 +35,9 @@ __latest_version() {
fi
}

__determine_version() {
local lockfile='lnkr.lock'
local locked_version="$(head -n 1 "$lockfile" 2>&-)"
echo "${locked_version:-$(__latest_version)}" | tee "$lockfile"
}

__bootstrap() {
local src='lnkr_lib.sh'
local ver="$(__determine_version)"
local ver="${LNKR_VERSION:-$(__latest_version)}"
local lib="$(__cache_directory)/${src/%.sh/_$ver.sh}"
local url="https://raw.githubusercontent.com/usommerl/lnkr/$ver/$src"
[ ! -e "$lib" ] && (curl -Lfso "$lib" "$url" || wget -qO "$lib" "$url") 2>&-
Expand Down
22 changes: 6 additions & 16 deletions test/boostrap.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ setup() {
git init
cp $LNKR_REPO_ROOT/lnkr $TESTSPACE
export lnkr=$TESTSPACE/lnkr
export LNKR_VERSION='master'
}

teardown() {
Expand All @@ -20,23 +21,14 @@ teardown() {
@test '__bootstrap should download library if it does not exist' {
run $lnkr --help
[ "$status" -eq 0 ]
[ -e "$TESTSPACE/$LOCKFILE" ]
assert_lib_exists
}

@test '__bootstrap should not ignore lockfile' {
local ver='v0.1.0'
echo $ver > $TESTSPACE/$LOCKFILE
run $lnkr --help
[ "$status" -eq 0 ]
assert_lib_exists
assert_lib_exists "$LNKR_VERSION"
}

@test '__bootstrap should use cache to determine latest release' {
unset LNKR_VERSION
run $lnkr --help
[ "$status" -eq 0 ]
[ "$(ls -l1 $CACHE_DIR/lnkr_lib*.sh | wc -l)" -eq 1 ]
rm "$TESTSPACE/$LOCKFILE"
assert_lib_exists
stub_curl_and_wget
run $lnkr --help
[ "$status" -eq 0 ]
Expand All @@ -45,16 +37,16 @@ teardown() {
@test '__bootstrap should fail silently if it is not able to determine version' {
mkdir -p $CACHE_DIR
touch $CACHE_DIR/latest
unset LNKR_VERSION
run $lnkr --help
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Bootstrap failed" ]
}

@test '__bootstrap should not overwrite existing library' {
mkdir -p $CACHE_DIR
local ver='v0.1.0'
local ver="$LNKR_VERSION"
echo 'echo "Fake lnkr library"; exit 254' > "$CACHE_DIR/lnkr_lib_$ver.sh"
echo $ver > $TESTSPACE/$LOCKFILE
run $lnkr --help
[ "$output" = "Fake lnkr library" ]
[ "$status" -eq 254 ]
Expand All @@ -66,7 +58,6 @@ teardown() {
run $lnkr --help
[ "$status" -eq 0 ]
[ "${lines[0]}" = "bash: curl: command not found" ]
[ -e "$TESTSPACE/$LOCKFILE" ]
assert_lib_exists
}

Expand All @@ -75,6 +66,5 @@ teardown() {
run $lnkr --help
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Bootstrap failed" ]
[ -e "$TESTSPACE/$LOCKFILE" ]
[ "$(ls -1 $CACHE_DIR/*.sh | wc -l)" -eq 0 ]
}
3 changes: 1 addition & 2 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ rm_journal() {
}

assert_lib_exists() {
local version=$(head -n 1 "$TESTSPACE/$LOCKFILE") 2>&-
[ -e "$CACHE_DIR/${LIB_FILENAME/%.sh/_$version.sh}" ]
[ "$(ls -l1 "$CACHE_DIR"/lnkr_lib_"${1:-}"*.sh | wc -l)" -eq 1 ]
}

make_repo_with_submodule() {
Expand Down

0 comments on commit 72bf63c

Please sign in to comment.