More option validation to simplify non-go-vendor-tools enabled scenarios

Test actual contents of sources
This commit is contained in:
2026-06-12 16:33:13 +02:00
parent ba8361fb70
commit d1d8aafca0
4 changed files with 140 additions and 57 deletions
+58 -43
View File
@@ -5,6 +5,27 @@
# * COPR source file
shopt -s extglob
__is_vendor_enabled() {
grep -q go-vendor-tools.toml "$SPEC_FILE"
}
# shellcheck disable=SC2046,SC2086
__find_sources() {
if [ "$SOURCES_FILE" != "" ] && [ -d "$(dirname $SOURCES_FILE)" ] && [ "$PWD" != "$(dirname $SOURCES_FILE)" ]; then
DO_POP=1
pushd "$(dirname $SOURCES_FILE)" > /dev/null || exit 98
fi
if [ -d .git ]; then
FILENAMES=$(git ls-files --exclude-standard | grep -v -e '^\.' -e sources)
else
FILENAMES=$(find . -path "./RPMS" -prune -o -path "./BUILD" -prune -o -path "./SRPMS" -prune -o -path "./.*" -prune -o \( ! \( -name '*.spec' -o -name '.*' -o -name sources \) -a -type f \) -printf ' %P')
fi
if [[ "$DO_POP" -eq 1 ]]; then
popd > /dev/null || echo $FILENAMES
fi
echo $FILENAMES
}
_usage() {
echo "Usage: $(basename "$0") [OPTION]... <SPEC_FILE>"
echo "Update generated artifacts belonging to the <SPEC_FILE>."
@@ -17,7 +38,9 @@ _usage() {
echo " -u Ignore unlicensed mods. Implies -l"
echo " -h Print this message and exit"
echo
echo "If none of the update operations are specified, all of them will be performed."
echo "If none of the update operations are specified, all of them will"
echo "be performed unless the spec is not go-vendor-tools enabled,"
echo "in which case only the sources file will be updated."
}
_parse_args() {
@@ -46,25 +69,12 @@ _parse_args() {
echo "Option -$OPTARG is invalid"
echo
_usage
exit 10
exit 9
;;
esac
done
if [ "$UPDATE_ARCHIVE" == "" ] && [ "$UPDATE_LICENSES" == "" ] && [ "$UPDATE_SOURCES" == "" ]; then
UPDATE_ARCHIVE=1
UPDATE_LICENSES=1
UPDATE_SOURCES=1
fi
if [ "$IGNORE_UNLICENSED_ARG" != "" ] && [ "$UPDATE_LICENSES" == "" ]; then
UPDATE_LICENSES=1
fi
if [ "$SOURCES_FILE" != "" ] && [ "$UPDATE_SOURCES" == "" ]; then
UPDATE_SOURCES=1
fi
# Validate required positional .spec file value
SPEC_FILE="${@:$OPTIND:1}"
if [ "$SPEC_FILE" == "" ]; then
echo "The required spec parameter is missing"
@@ -84,6 +94,36 @@ _parse_args() {
_usage
exit 3
fi
# Default UPDATE_ vars if no particular operation was requested
if [ "$UPDATE_ARCHIVE" == "" ] && [ "$UPDATE_LICENSES" == "" ] && [ "$UPDATE_SOURCES" == "" ]; then
if __is_vendor_enabled; then
UPDATE_ARCHIVE=1
UPDATE_LICENSES=1
fi
UPDATE_SOURCES=1
fi
# Set implied vars
if [ "$IGNORE_UNLICENSED_ARG" != "" ] && [ "$UPDATE_LICENSES" == "" ]; then
if ! __is_vendor_enabled; then
echo "$SPEC_FILE is not go-vendor-tools enabled"
exit 4
fi
UPDATE_LICENSES=1
fi
if [ "$SOURCES_FILE" != "" ] && [ "$UPDATE_SOURCES" == "" ]; then
UPDATE_SOURCES=1
fi
}
_check_go_vendor_enabled() {
# go2rpm creates a go-vendor-tools.toml file when using the vendor profile
if [[ "$UPDATE_ARCHIVE" -eq 1 || "$UPDATE_LICENSES" -eq 1 ]] && ! __is_vendor_enabled; then
echo The spec file is not go-vendor-tools enabled
exit 14
fi
}
_download_sources() {
@@ -95,21 +135,13 @@ _download_sources() {
spectool -g "$SPEC_FILE"
}
_check_go_vendor_enabled() {
# go2rpm creates a go-vendor-tools.toml file when using the vendor profile
if [[ "$UPDATE_ARCHIVE" -eq 1 || "$UPDATE_LICENSES" -eq 1 ]] && ! grep -q go-vendor-tools.toml "$SPEC_FILE"; then
echo The spec file is not go-vendor-tools enabled
exit 4
fi
}
_update_archive() {
# Update the go vendors archive
if [[ "$UPDATE_ARCHIVE" -eq 1 ]]; then
echo Updating go vendor archive
if ! go_vendor_archive create --config go-vendor-tools.toml --write-config "$SPEC_FILE"; then
echo Archive update failed
exit 5
exit 15
fi
fi
}
@@ -121,28 +153,11 @@ _update_licenses() {
# shellcheck disable=SC2086
if ! go_vendor_license --config go-vendor-tools.toml --path "$SPEC_FILE" report --write-config --prompt --autofill auto --update-spec$IGNORE_UNLICENSED_ARG; then
echo License update failed
exit 6
exit 16
fi
fi
}
# shellcheck disable=SC2046,SC2086
__find_sources() {
if [ "$SOURCES_FILE" != "" ] && [ -d "$(dirname $SOURCES_FILE)" ] && [ "$PWD" != "$(dirname $SOURCES_FILE)" ]; then
DO_POP=1
pushd "$(dirname $SOURCES_FILE)" > /dev/null || exit 98
fi
if [ -d .git ]; then
FILENAMES=$(git ls-files --exclude-standard | grep -v -e '^\.' -e sources)
else
FILENAMES=$(find . -path "./RPMS" -prune -o -path "./BUILD" -prune -o -path "./SRPMS" -prune -o -path "./.*" -prune -o \( ! \( -name '*.spec' -o -name '.*' -o -name sources \) -a -type f \) -printf ' %P')
fi
if [[ "$DO_POP" -eq 1 ]]; then
popd > /dev/null || echo $FILENAMES
fi
echo $FILENAMES
}
_update_sources() {
# Update the sources checksums
if [[ "$UPDATE_SOURCES" -eq 1 ]]; then
+46 -4
View File
@@ -32,8 +32,15 @@ setup() {
# bats file_tags=options
@test "Invalid option returns an error" {
run _parse_args -q a.spec
assert_failure 9
assert_line --index 0 "Option -q is invalid"
}
@test "No selection selects all actions" {
touch "$TEST_TEMP_DIR/a.spec"
echo "Source: go-vendor-tools.toml" > "$TEST_TEMP_DIR/a.spec"
_parse_args "$TEST_TEMP_DIR/a.spec"
assert_equal "$?" 0
@@ -77,7 +84,8 @@ setup() {
}
@test "Multiple selections aggregate" {
touch "$TEST_TEMP_DIR/a.spec"
echo "Source: go-vendor-tools.toml" > "$TEST_TEMP_DIR/a.spec"
_parse_args -s -l "$TEST_TEMP_DIR/a.spec"
assert_equal "$?" 0
@@ -88,7 +96,7 @@ setup() {
}
@test "The -u option configures an --ignore-unlicensed-mods parameter for go_vendor_license" {
touch "$TEST_TEMP_DIR/a.spec"
echo "Source: go-vendor-tools.toml" > "$TEST_TEMP_DIR/a.spec"
_parse_args -u "$TEST_TEMP_DIR/a.spec"
assert_equal "$?" 0
@@ -99,7 +107,8 @@ setup() {
}
@test "The -u option implies -l" {
touch "$TEST_TEMP_DIR/a.spec"
echo "Source: go-vendor-tools.toml" > "$TEST_TEMP_DIR/a.spec"
_parse_args -u -s "$TEST_TEMP_DIR/a.spec"
assert_equal "$?" 0
@@ -109,6 +118,15 @@ setup() {
assert_equal "$UPDATE_SOURCES" 1
}
@test "The -u options returns an error in a not go-vendor-tools enabled project" {
touch "$TEST_TEMP_DIR/a.spec"
run _parse_args -u -s "$TEST_TEMP_DIR/a.spec"
assert_failure 4
assert_output "$TEST_TEMP_DIR/a.spec is not go-vendor-tools enabled"
}
@test "The help option successfully short-circuits everything else" {
touch "$TEST_TEMP_DIR/a.spec"
run _parse_args -h
@@ -116,3 +134,27 @@ setup() {
assert_success
assert_line --index 0 --partial "Usage: "
}
@test "-t sets the sources file path and enables sources re-generation" {
touch "$TEST_TEMP_DIR/a.spec"
_parse_args -t "$TEST_TEMP_DIR/sources" "$TEST_TEMP_DIR/a.spec"
assert_equal "$?" 0
assert_equal "$UPDATE_ARCHIVE" ""
assert_equal "$UPDATE_LICENSES" ""
assert_equal "$SOURCES_FILE" "$TEST_TEMP_DIR/sources"
assert_equal "$UPDATE_SOURCES" 1
}
@test "-t sets the sources file path and adds sources re-generation to pre-existing requests" {
echo "Source: go-vendor-tools.toml" > "$TEST_TEMP_DIR/a.spec"
_parse_args -a -t "$TEST_TEMP_DIR/sources" "$TEST_TEMP_DIR/a.spec"
assert_equal "$?" 0
assert_equal "$UPDATE_ARCHIVE" 1
assert_equal "$UPDATE_LICENSES" ""
assert_equal "$SOURCES_FILE" "$TEST_TEMP_DIR/sources"
assert_equal "$UPDATE_SOURCES" 1
}
+32 -10
View File
@@ -18,7 +18,7 @@ setup() {
run _check_go_vendor_enabled
assert_failure 4
assert_failure 14
assert_output "The spec file is not go-vendor-tools enabled"
}
@@ -27,7 +27,7 @@ setup() {
run _check_go_vendor_enabled
assert_failure 4
assert_failure 14
assert_output "The spec file is not go-vendor-tools enabled"
}
@@ -80,14 +80,14 @@ setup() {
"$BATS_MOCK_REAL_rm" -rf "${BATS_MOCK_TMPDIR}" || true
}
@test "Some feedback is proffered when go_vendor_archive fails" {
@test "Sane feedback is proffered when go_vendor_archive fails" {
export UPDATE_ARCHIVE=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
stub go_vendor_archive "create --config go-vendor-tools.toml --write-config $TEST_TEMP_DIR/test.spec : exit 3"
run _update_archive
assert_failure 5
assert_failure 15
assert_output - <<<"Updating go vendor archive
Archive update failed"
unstub go_vendor_archive
@@ -122,14 +122,14 @@ Archive update failed"
"$BATS_MOCK_REAL_rm" -rf "${BATS_MOCK_TMPDIR}" || true
}
@test "Some feedback is proffered when go_vendor_license fails" {
@test "Sane feedback is proffered when go_vendor_license fails" {
export UPDATE_LICENSES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
stub go_vendor_license "--config go-vendor-tools.toml --path $TEST_TEMP_DIR/test.spec report --write-config --prompt --autofill auto --update-spec : exit 3"
run _update_licenses
assert_failure 6
assert_failure 16
assert_output - <<<"Updating go vendor licenses
License update failed"
unstub go_vendor_license
@@ -150,7 +150,25 @@ License update failed"
# bats file_tags=sources_update
@test "sources is updated when spec is valid and update is requested" {
@test "sources in a git directory is updated when spec is valid and update is requested" {
export UPDATE_SOURCES=1
tar -xzf "$BATS_TEST_DIRNAME/testdata/git.example.tar.gz" -C "$TEST_TEMP_DIR/"
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
run _update_sources
assert_success
assert_output "Calculating checksums"
assert_file_contains "$SOURCES_FILE" "SHA512 (v1.0.0.tar.gz)"
assert_file_contains "$SOURCES_FILE" "SHA512 (LICENSE)"
assert_file_contains "$SOURCES_FILE" "SHA512 (README.md)"
assert_file_not_contains "$SOURCES_FILE" sources
assert_file_not_contains "$SOURCES_FILE" test.spec
assert_file_not_contains "$SOURCES_FILE" BUILD
assert_file_not_contains "$SOURCES_FILE" .git
}
@test "sources in a plain directory is updated when spec is valid and update is requested" {
export UPDATE_SOURCES=1
tar -xzf "$BATS_TEST_DIRNAME/testdata/example.tar.gz" -C "$TEST_TEMP_DIR/"
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
@@ -159,7 +177,13 @@ License update failed"
assert_success
assert_output "Calculating checksums"
assert_file_not_empty "$SOURCES_FILE"
assert_file_contains "$SOURCES_FILE" "SHA512 (v1.0.0.tar.gz)"
assert_file_contains "$SOURCES_FILE" "SHA512 (LICENSE)"
assert_file_contains "$SOURCES_FILE" "SHA512 (README.md)"
assert_file_not_contains "$SOURCES_FILE" sources
assert_file_not_contains "$SOURCES_FILE" test.spec
assert_file_not_contains "$SOURCES_FILE" BUILD
assert_file_not_contains "$SOURCES_FILE" .git
}
@test "sources is only updated when an update is requested" {
@@ -195,8 +219,6 @@ No relevant sources found. Skipping checksumming."
assert_success
assert_output "LICENSE README.md changelog data/README data/somedata.csv v1.0.0.tar.gz vendor.tar.bz2"
# Some files under .git are read-only
rm -fR "$TEST_TEMP_DIR/.git"
}
# bats test_tags=find_sources
+4
View File
@@ -8,6 +8,10 @@ setup_file() {
}
teardown() {
if [ -d "$TEST_TEMP_DIR/.git" ]; then
# Some files under .git are read-only
rm -fR "$TEST_TEMP_DIR/.git"
fi
temp_del "$TEST_TEMP_DIR"
unset TEST_TEMP_DIR
}