Skip to content

Commit d924fce

Browse files
committed
Test package cache
1 parent d999db7 commit d924fce

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

bin/ruby-build

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ verify_checksum() {
147147
# If there's no MD5 support, return success
148148
[ -n "$HAS_MD5_SUPPORT" ] || return 0
149149

150-
# If the specified filename doesn't exist, return failure
150+
# If the specified filename doesn't exist, return success
151151
local filename="$1"
152-
[ -e "$filename" ] || return 1
152+
[ -e "$filename" ] || return 0
153153

154154
# If there's no expected checksum, return success
155155
local expected_checksum="$2"
@@ -236,9 +236,9 @@ symlink_tarball_from_cache() {
236236
local cached_package_filename="${RUBY_BUILD_CACHE_PATH}/$package_filename"
237237
local checksum="$2"
238238

239-
{ verify_checksum "$cached_package_filename" "$checksum"
240-
ln -s "$cached_package_filename" "$package_filename"
241-
} >&4 2>&1 || return 1
239+
[ -e "$cached_package_filename" ] || return 1
240+
verify_checksum "$cached_package_filename" "$checksum" >&4 2>&1 || return 1
241+
ln -s "$cached_package_filename" "$package_filename" >&4 2>&1 || return 1
242242
}
243243

244244
download_tarball() {

test/cache.bats

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env bats
2+
3+
load test_helper
4+
export RUBY_BUILD_SKIP_MIRROR=1
5+
export RUBY_BUILD_CACHE_PATH="$TMP/cache"
6+
7+
setup() {
8+
mkdir "$RUBY_BUILD_CACHE_PATH"
9+
}
10+
11+
12+
@test "packages are saved to download cache" {
13+
stub md5 true
14+
stub curl "-*S* : cat package-1.0.0.tar.gz"
15+
16+
install_fixture definitions/without-checksum
17+
[ "$status" -eq 0 ]
18+
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
19+
20+
unstub curl
21+
unstub md5
22+
}
23+
24+
25+
@test "cached package without checksum" {
26+
stub md5 true
27+
stub curl
28+
29+
cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH"
30+
31+
install_fixture definitions/without-checksum
32+
[ "$status" -eq 0 ]
33+
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
34+
35+
unstub curl
36+
unstub md5
37+
}
38+
39+
40+
@test "cached package with valid checksum" {
41+
stub md5 true "echo 83e6d7725e20166024a1eb74cde80677"
42+
stub curl
43+
44+
cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH"
45+
46+
install_fixture definitions/with-checksum
47+
[ "$status" -eq 0 ]
48+
[ -x "${INSTALL_ROOT}/bin/package" ]
49+
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
50+
51+
unstub curl
52+
unstub md5
53+
}
54+
55+
56+
@test "cached package with invalid checksum falls back to mirror and updates cache" {
57+
export RUBY_BUILD_SKIP_MIRROR=
58+
local checksum="83e6d7725e20166024a1eb74cde80677"
59+
60+
stub md5 true "echo invalid" "echo $checksum"
61+
stub curl "-*I* : true" "-*S* http://?*/$checksum : cat package-1.0.0.tar.gz"
62+
63+
touch "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz"
64+
65+
install_fixture definitions/with-checksum
66+
[ "$status" -eq 0 ]
67+
[ -x "${INSTALL_ROOT}/bin/package" ]
68+
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
69+
diff -q "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" "${FIXTURE_ROOT}/package-1.0.0.tar.gz"
70+
71+
unstub curl
72+
unstub md5
73+
}
74+
75+
76+
@test "nonexistent cache directory is ignored" {
77+
stub md5 true
78+
stub curl "-*S* : cat package-1.0.0.tar.gz"
79+
80+
export RUBY_BUILD_CACHE_PATH="${TMP}/nonexistent"
81+
82+
install_fixture definitions/without-checksum
83+
[ "$status" -eq 0 ]
84+
[ -x "${INSTALL_ROOT}/bin/package" ]
85+
[ ! -d "$RUBY_BUILD_CACHE_PATH" ]
86+
87+
unstub curl
88+
unstub md5
89+
}

test/test_helper.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ stub() {
2020
export PATH="${BATS_TEST_DIRNAME}/stubs/${program}:$PATH"
2121

2222
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
23+
touch "${TMP}/${program}-stub-plan"
2324
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
2425
}
2526

0 commit comments

Comments
 (0)