Skip to content

Commit 6d9ee18

Browse files
committed
test: Improve tests
1 parent eeefad9 commit 6d9ee18

File tree

108 files changed

+7293
-842
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+7293
-842
lines changed

pkg/lib/util/pkg.sh

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ pkg.phase_download_tarball() {
8383
local download_dest="$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
8484
mkdir -p "${download_dest%/*}"
8585

86-
if [ ${DEBUG+x} ]; then
87-
print.indent-light-cyan "Downloading" "$package_id | $download_dest"
88-
fi
89-
9086
# Use cache if it already exists
9187
if [ -e "$download_dest" ]; then
9288
print.indent-green "Downloaded" "$package_id (cached)"
@@ -104,13 +100,12 @@ pkg.phase_download_tarball() {
104100
print.indent-die "File '$download_dest' is not actually a tarball"
105101
fi
106102

107-
print.indent-green "Downloaded" "$site/$package@$version"
103+
print.indent-green "Downloaded" "$package_id"
108104
return
109-
else
110-
# This is OK, since the 'version' could be an actual ref. In that case,
111-
# download the package as below
112-
:
113105
fi
106+
107+
# If cURL fails, this is OK, since the 'version' could be an actual ref. In that case,
108+
# download the package as below. It does this automatically for 'local' packages
114109
fi
115110

116111
# TODO Print warning if a local dependency has a dirty index
@@ -120,12 +115,12 @@ pkg.phase_download_tarball() {
120115
fi
121116

122117
rm -rf "$BASALT_GLOBAL_DATA_DIR/scratch"
123-
if ! git clone --quiet "$url" "$BASALT_GLOBAL_DATA_DIR/scratch/$package_id" 2>/dev/null; then
118+
if ! git clone --quiet "$url" "$BASALT_GLOBAL_DATA_DIR/scratch/$package_id"; then
124119
print.indent-die "Could not clone repository for $package_id"
125120
fi
126121

127-
if ! git -C "$BASALT_GLOBAL_DATA_DIR/scratch/$package_id" archive --prefix="prefix/" -o "$download_dest" "$version"; then
128-
rm -rf "$BASALT_GLOBAL_DATA_DIR/scratch"
122+
if ! git -C "$BASALT_GLOBAL_DATA_DIR/scratch/$package_id" archive --prefix="prefix/" -o "$download_dest" "$version" 2>/dev/null; then
123+
rm -rf "$BASALT_GLOBAL_DATA_DIR/scratch" "$download_dest"
129124
print.indent-die "Could not download archive or extract archive from temporary Git repository of $package_id"
130125
fi
131126
rm -rf "$BASALT_GLOBAL_DATA_DIR/scratch"
@@ -146,10 +141,6 @@ pkg.phase_extract_tarball() {
146141
local tarball_src="$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
147142
local tarball_dest="$BASALT_GLOBAL_DATA_DIR/store/packages/$package_id"
148143

149-
if [ ${DEBUG+x} ]; then
150-
print.indent-light-cyan "Extracting" "$package_id | $tarball_dest"
151-
fi
152-
153144
# Use cache if it already exists
154145
if [ -d "$tarball_dest" ]; then
155146
print.indent-green "Extracted" "$package_id (cached)"
@@ -178,10 +169,6 @@ pkg.phase_global_integration() {
178169

179170
local project_dir="$BASALT_GLOBAL_DATA_DIR/store/packages/$package_id"
180171

181-
if [ ${DEBUG+x} ]; then
182-
print.indent-light-cyan "Transforming" "$project_dir"
183-
fi
184-
185172
ensure.dir "$project_dir"
186173
if [ -f "$project_dir/basalt.toml" ]; then
187174
# Install dependencies

pkg/lib/util/symlink.sh

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ symlink.package() {
1313
local target="$BASALT_GLOBAL_DATA_DIR/store/packages/$package_id"
1414
local link_name="$install_dir/$package_id"
1515

16-
if [ ${DEBUG+x} ]; then
17-
print.indent-light-cyan "Symlinking" "$link_name -> $target"
18-
fi
19-
2016
mkdir -p "${link_name%/*}"
2117
if ! ln -sf "$target" "$link_name"; then
2218
print.indent-die "Could not symlink directory '${target##*/}' for package $package_id"
@@ -42,11 +38,6 @@ symlink.bin_strict() {
4238
for target in "$package_dir/$dir"/*; do
4339
local link_name="$install_dir/bin/${target##*/}"
4440

45-
if [ ${DEBUG+x} ]; then
46-
print.indent-light-cyan "Symlinking" "target $target"
47-
print.indent-light-cyan "Symlinking" "link_name $link_name"
48-
fi
49-
5041
if [ -e "$link_name" ]; then
5142
print-indent 'Warning' "Executable file '${target##*/} for package $package_id will clobber an identically-named file owned by a different package"
5243
fi

pkg/lib/util/util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ util.get_package_id() {
108108
if [ "$repo_type" = 'remote' ]; then
109109
REPLY="$site/$package@$version"
110110
elif [ "$repo_type" = 'local' ]; then
111-
REPLY="local/${url##*/}"
111+
REPLY="local/${url##*/}@$version"
112112
else
113113
util.die_unexpected_value 'repo_type'
114114
fi

tests/pkg-phase-download-tarball.bats

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,68 @@
22

33
load './util/init.sh'
44

5+
@test "Succeeds for valid repository" {
6+
local package_id="github.com/hyperupcall/[email protected]"
7+
util.get_package_info "https://$package_id"
8+
9+
run pkg.phase_download_tarball "$REPLY1" "$REPLY2" "$REPLY3" "$REPLY4" "$REPLY5"
10+
11+
assert_success
12+
assert_line -p "Downloaded $package_id"
13+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
14+
}
15+
16+
@test "Succeeds with caching" {
17+
local package_id="github.com/hyperupcall/[email protected]"
18+
util.get_package_info "https://$package_id"
19+
20+
run pkg.phase_download_tarball "$REPLY1" "$REPLY2" "$REPLY3" "$REPLY4" "$REPLY5"
21+
22+
assert_success
23+
assert_line -p "Downloaded $package_id"
24+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
25+
26+
run pkg.phase_download_tarball "$REPLY1" "$REPLY2" "$REPLY3" "$REPLY4" "$REPLY5"
27+
assert_line -p "Downloaded $package_id (cached)"
28+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
29+
30+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
31+
}
32+
33+
# Note that by the time we get to 'util.get_package_info', we expect the version (REPLY5)
34+
# to properly be calculated in the code via checking the version with -z, and calling
35+
# `util.get_latest_package_version`
36+
@test "Succeeds with local file" {
37+
test_util.create_fake_remote 'user/repo' 'v0.0.1'; dir="$REPLY"
38+
util.get_package_info "file://$dir"
39+
40+
run pkg.phase_download_tarball "$REPLY1" "$REPLY2" "$REPLY3" "$REPLY4" 'v0.0.1'
41+
42+
assert_success
43+
assert_line -p "Downloaded local/[email protected]"
44+
assert_file_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/local/[email protected]"
45+
}
46+
547
@test "Fails for invalid repository" {
48+
local package_id="github.com/hyperupcall/[email protected]"
49+
util.get_package_info "https://$package_id"
50+
51+
run pkg.phase_download_tarball "$REPLY1" "$REPLY2" "$REPLY3" "$REPLY4" "$REPLY5"
52+
53+
assert_failure
54+
assert_line -p "Could not clone repository for $package_id"
55+
assert_not_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
56+
}
57+
58+
@test "Fails for invalid repository version" {
659
skip
760

8-
pkg.phase_download_tarball 'remote' 'https://github.com/hyperupcall/bash-object.git' 'github.com' 'hyperupcall/bash-object' 'v0.3.0'
61+
local package_id="github.com/hyperupcall/[email protected]"
62+
util.get_package_info "https://$package_id"
63+
64+
run pkg.phase_download_tarball "$REPLY1" "$REPLY2" "$REPLY3" "$REPLY4" "$REPLY5"
65+
66+
assert_failure
67+
assert_line -p "Could not download archive or extract archive from temporary Git repository of $package_id"
68+
assert_not_exist "$BASALT_GLOBAL_DATA_DIR/store/tarballs/$package_id.tar.gz"
969
}

tests/util/init.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# shellcheck shell=bash
22

33
# Source testing dependencies
4-
load './vendor/bats-assert/assert.sh'
5-
load './vendor/bats-file/file.sh'
6-
load './vendor/bats-file/temp.sh'
7-
load './vendor/bats-support/error.sh'
8-
load './vendor/bats-support/lang.sh'
9-
load './vendor/bats-support/output.sh'
4+
load './vendor/bats-common-utils/load.bash'
105
load './util/test_util.sh'
116

127
# Get the current directory of the Basalt git repository
@@ -27,6 +22,7 @@ basalt-package-init() { basalt-package-init.main "$@"; }
2722
basalt() { basalt.main "$@"; }
2823

2924
# Testing variables
25+
export NO_COLOR=
3026
export XDG_DATA_HOME=
3127
export BASALT_GLOBAL_REPO="$BATS_TEST_TMPDIR/source"
3228
export BASALT_GLOBAL_DATA_DIR="$BATS_TEST_TMPDIR/localshare"

tests/util/test_util.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test_util.get_repo_root() {
2424
test_util.create_fake_remote() {
2525
unset REPLY; REPLY=
2626
local package="$1"
27+
local version="${2:-v0.0.1}"
2728

2829
local git_dir="$BATS_TEST_TMPDIR/fake_remote_${package%/*}_${package#*/}"
2930

@@ -35,8 +36,8 @@ test_util.create_fake_remote() {
3536
git add .
3637
git commit -m 'Initial commit'
3738
git branch -M main
38-
git commit --allow-empty -m 'v0.0.1'
39-
git tag -m 'v0.0.1' 'v0.0.1'
39+
git commit --allow-empty -m "$version"
40+
git tag -m "$version" "$version"
4041
} >/dev/null 2>&1
4142

4243
REPLY="$git_dir"

0 commit comments

Comments
 (0)