Extract list of sources from spec file if available

This commit is contained in:
2026-07-03 11:22:03 +02:00
parent a526626df1
commit 5bc2518cce
5 changed files with 127 additions and 64 deletions
+51 -33
View File
@@ -5,22 +5,38 @@
# * COPR source file
shopt -s extglob
__has_spec_file() {
test -n "$SPEC_FILE"
}
__is_vendor_enabled() {
grep -q go-vendor-tools.toml "$SPEC_FILE"
__has_spec_file && 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
if [ -n "$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 ] && which git &>/dev/null; then
if __has_spec_file; then
SPECTOOL_SOURCES="$(spectool --list-files --sources "$SPEC_FILE")"
if [ $? -ne 0 ]; then
echo "The spec file $SPEC_FILE couldn't be parsed"
exit 96
fi
FILTERED_SOURCES="$(echo "$SPECTOOL_SOURCES" | sed -e '/^Source[0-9]*:[[:blank:]]*sources/d' -e 's/^Source[0-9]*:[[:blank:]]*//')"
if [ -n "$FILTERED_SOURCES" ]; then
FILENAMES=$(basename -a "$FILTERED_SOURCES")
fi
elif [ -d .git ] && which git &>/dev/null; then
FILENAMES=$(git ls-files --exclude-standard | grep -v -e '^\.' -e sources)
elif which find &>/dev/null; then
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')
else
echo "No way to search for relevant source files was found. Please install find (and git if this is a git repository directory)."
echo "No way to search for relevant source files was found. Please install find (and git if this is a git repository directory) or add a .spec file to the directory."
exit 97
fi
if [[ "$DO_POP" -eq 1 ]]; then
@@ -30,8 +46,10 @@ __find_sources() {
}
_usage() {
echo "Usage: $(basename "$0") [OPTION]... <SPEC_FILE>"
echo "Update generated artifacts belonging to the <SPEC_FILE>."
echo "Usage: $(basename "$0") [OPTION]... [<SPEC_FILE>]"
echo "Update generated artifacts. If <SPEC_FILE> is specified the referenced sources and artifacts are processed"
echo " otherwise a source file is created or updated in the current directory or in the directory the source"
echo " file parameter points to."
echo
echo "Options:"
echo " -a Update archive"
@@ -79,27 +97,23 @@ _parse_args() {
# Validate required positional .spec file value
SPEC_FILE="${@:$OPTIND:1}"
if [ "$SPEC_FILE" == "" ]; then
echo "The required spec parameter is missing"
echo
_usage
exit 1
fi
if ! [[ "$SPEC_FILE" =~ .*.\.spec$ ]]; then
echo "The spec file $SPEC_FILE doesn't have a .spec extension"
echo
_usage
exit 2
fi
if [ ! -f "$SPEC_FILE" ]; then
echo "The spec file $SPEC_FILE is not readable"
echo
_usage
exit 3
if __has_spec_file; then
if ! [[ "$SPEC_FILE" =~ .*.\.spec$ ]]; then
echo "The spec file $SPEC_FILE doesn't have a .spec extension"
echo
_usage
exit 2
fi
if [ ! -f "$SPEC_FILE" ]; then
echo "The spec file $SPEC_FILE is not readable"
echo
_usage
exit 3
fi
fi
# Default UPDATE_ vars if no particular operation was requested
if [ "$UPDATE_ARCHIVE" == "" ] && [ "$UPDATE_LICENSES" == "" ] && [ "$UPDATE_SOURCES" == "" ]; then
if [ -z "$UPDATE_ARCHIVE" ] && [ -z "$UPDATE_LICENSES" ] && [ -z "$UPDATE_SOURCES" ]; then
if __is_vendor_enabled; then
UPDATE_ARCHIVE=1
UPDATE_LICENSES=1
@@ -108,7 +122,7 @@ _parse_args() {
fi
# Set implied vars
if [ "$IGNORE_UNLICENSED_ARG" != "" ] && [ "$UPDATE_LICENSES" == "" ]; then
if [ -n "$IGNORE_UNLICENSED_ARG" ] && [ -z "$UPDATE_LICENSES" ]; then
if ! __is_vendor_enabled; then
echo "$SPEC_FILE is not go-vendor-tools enabled"
exit 4
@@ -116,7 +130,7 @@ _parse_args() {
UPDATE_LICENSES=1
fi
if [ "$SOURCES_FILE" != "" ] && [ "$UPDATE_SOURCES" == "" ]; then
if [ -n "$SOURCES_FILE" ] && [ -z "$UPDATE_SOURCES" ]; then
UPDATE_SOURCES=1
fi
}
@@ -130,15 +144,19 @@ _check_go_vendor_enabled() {
}
_download_sources() {
if [ ! -v SPEC_FILE ]; then
echo ERROR: Variable SPEC_FILE in not set
exit 99
if __has_spec_file; then
if ! spectool -g "$SPEC_FILE"; then
echo The spec file $SPEC_FILE is not parsable
exit 96
fi
fi
# Ensure remote sources are downloaded
spectool -g "$SPEC_FILE"
}
_update_archive() {
if ! __has_spec_file; then
echo ERROR: Variable SPEC_FILE is not set
exit 97
fi
# Update the go vendors archive
if [[ "$UPDATE_ARCHIVE" -eq 1 ]]; then
echo Updating go vendor archive
@@ -164,7 +182,7 @@ _update_licenses() {
_update_sources() {
# Update the sources checksums
if [[ "$UPDATE_SOURCES" -eq 1 ]]; then
if [ "$SOURCES_FILE" != "" ] && [ -d "$(dirname $SOURCES_FILE)" ]; then
if [ -n "$SOURCES_FILE" ] && [ -d "$(dirname $SOURCES_FILE)" ]; then
DO_POP=1
pushd "$(dirname $SOURCES_FILE)" > /dev/null || exit 98
fi
@@ -172,7 +190,7 @@ _update_sources() {
echo Calculating checksums
# shellcheck disable=SC2046
SOURCES="$(__find_sources)"
if [ "$SOURCES" == "" ]; then
if [ -z "$SOURCES" ]; then
echo No relevant sources found. Skipping checksumming.
return 0
fi
+9 -6
View File
@@ -7,13 +7,16 @@ setup() {
common_setup
}
# bats file_tags=mandatory_spec_file_argument
# bats file_tags=spec_file_argument
@test "Missing spec file argument returns an error" {
run _parse_args
@test "No selection without a spec file selects sources update" {
_parse_args
assert_failure 1
assert_line --index 0 "The required spec parameter is missing"
assert_equal "$?" 0
assert_equal "$UPDATE_ARCHIVE" ""
assert_equal "$UPDATE_LICENSES" ""
assert_equal "$UPDATE_SOURCES" 1
assert_equal "$IGNORE_UNLICENSED_ARG" ""
}
@test "A not .spec file returns an error" {
@@ -39,7 +42,7 @@ setup() {
assert_line --index 0 "Option -q is invalid"
}
@test "No selection selects all actions" {
@test "No selection with a spec file selects all actions" {
echo "Source: go-vendor-tools.toml" > "$TEST_TEMP_DIR/a.spec"
_parse_args "$TEST_TEMP_DIR/a.spec"
+65 -21
View File
@@ -6,11 +6,29 @@ bats_load_library 'bats-mock'
setup() {
load ../spec-update-artifacts.sh
common_setup
echo "" > "$TEST_TEMP_DIR/test.spec"
export SPEC_FILE="$TEST_TEMP_DIR/test.spec"
export SOURCES_FILE="$TEST_TEMP_DIR/sources"
}
_make_valid_spec() {
export SPEC_FILE="$TEST_TEMP_DIR/test.spec"
cat > $SPEC_FILE <<EOL
Name: Gurli
Version: 0
Release: 0
Summary: Test
License: MIT
Source: v1.0.0.tar.gz
Source: vendor.tar.bz2
Source: go-vendor-tools.toml
Source: sources
Source: changelog
Source: README.md
Source: LICENSE
%description
Fnuggi
EOL
}
# bats file_tags=vendor_update_validation
@test "Script returns error when trying to update go vendor archive on a spec that is not go vendor enabled" {
@@ -43,7 +61,7 @@ setup() {
@test "Script succeeds when a spec is go vendor enabled" {
export UPDATE_LICENSES=1
export UPDATE_SOURCES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
run _check_go_vendor_enabled
@@ -55,7 +73,7 @@ setup() {
@test "Archive is updated when spec is valid and update is requested" {
export UPDATE_ARCHIVE=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
stub go_vendor_archive "create --config go-vendor-tools.toml --write-config $TEST_TEMP_DIR/test.spec : echo"
run _update_archive
@@ -68,7 +86,7 @@ setup() {
@test "Archive is only updated when an update is requested" {
export UPDATE_LICENSES=1
export UPDATE_SOURCES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
stub go_vendor_archive "create --config go-vendor-tools.toml --write-config $TEST_TEMP_DIR/test.spec : echo"
run _update_archive
@@ -82,7 +100,7 @@ setup() {
@test "Sane feedback is proffered when go_vendor_archive fails" {
export UPDATE_ARCHIVE=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
stub go_vendor_archive "create --config go-vendor-tools.toml --write-config $TEST_TEMP_DIR/test.spec : exit 3"
run _update_archive
@@ -97,7 +115,7 @@ Archive update failed"
@test "Licenses are updated when spec is valid and update is requested" {
export UPDATE_LICENSES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
stub go_vendor_license "--config go-vendor-tools.toml --path $TEST_TEMP_DIR/test.spec report --write-config --prompt --autofill auto --update-spec : echo"
run _update_licenses
@@ -110,7 +128,7 @@ Archive update failed"
@test "Licenses are only updated when an update is requested" {
export UPDATE_ARCHIVE=1
export UPDATE_SOURCES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
stub go_vendor_license "--config go-vendor-tools.toml --path $TEST_TEMP_DIR/test.spec report --write-config --prompt --autofill auto --update-spec : echo"
run _update_licenses
@@ -124,7 +142,7 @@ Archive update failed"
@test "Sane feedback is proffered when go_vendor_license fails" {
export UPDATE_LICENSES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
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
@@ -138,7 +156,7 @@ License update failed"
@test "--ignore-unlicensed-mods to the go_vendor_license command from IGNORE_UNLICENSED_ARG" {
export UPDATE_LICENSES=1
export IGNORE_UNLICENSED_ARG=" --ignore-unlicensed-mods"
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
stub go_vendor_license "--config go-vendor-tools.toml --path $TEST_TEMP_DIR/test.spec report --write-config --prompt --autofill auto --update-spec --ignore-unlicensed-mods : echo"
run _update_licenses
@@ -150,10 +168,10 @@ License update failed"
# bats file_tags=sources_update
@test "sources in a git directory is updated when spec is valid and update is requested" {
@test "sources 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
tar -xzf "$BATS_TEST_DIRNAME/testdata/example.tar.gz" -C "$TEST_TEMP_DIR/"
_make_valid_spec
run _update_sources
@@ -168,10 +186,26 @@ License update failed"
assert_file_not_contains "$SOURCES_FILE" .git
}
@test "sources in a plain directory is updated when spec is valid and update is requested" {
@test "sources is updated in a git directory without spec and update is requested" {
export UPDATE_SOURCES=1
tar -xzf "$BATS_TEST_DIRNAME/testdata/git.example.tar.gz" -C "$TEST_TEMP_DIR/"
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" BUILD
assert_file_not_contains "$SOURCES_FILE" .git
}
@test "sources is updated in a plain directory 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
_make_valid_spec
run _update_sources
@@ -190,7 +224,7 @@ License update failed"
export UPDATE_LICENSES=1
export UPDATE_ARCHIVE=1
tar -xzf "$BATS_TEST_DIRNAME/testdata/example.tar.gz" -C "$TEST_TEMP_DIR/"
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
_make_valid_spec
run _update_sources
@@ -201,7 +235,17 @@ License update failed"
@test "When there are no relevant sources, checksumming is skipped" {
export UPDATE_SOURCES=1
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
export SPEC_FILE="$TEST_TEMP_DIR/test.spec"
cat > $SPEC_FILE <<EOL
Name: Gurli
Version: 0
Release: 0
Summary: Test
License: MIT
Source: sources
%description
Fnuggi
EOL
run _update_sources
@@ -214,7 +258,7 @@ No relevant sources found. Skipping checksumming."
stub cksum "exit 1"
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
_make_valid_spec
run _update_sources
@@ -241,11 +285,11 @@ Sources checksum calculation failed"
run __find_sources
assert_success
assert_output "vendor.tar.bz2 v1.0.0.tar.gz README.md LICENSE data/somedata.csv data/README changelog"
assert_output "go-vendor-tools.toml vendor.tar.bz2 v1.0.0.tar.gz README.md LICENSE data/somedata.csv data/README changelog"
}
# bats test_tags=find_sources
@test "If neither git or find are installed you get an error and explanation" {
@test "If neither git or find are installed and there is no .spec file, you get an error and explanation" {
stub which "return 1" "return 1"
tar -xzf "$BATS_TEST_DIRNAME/testdata/git.example.tar.gz" -C "$TEST_TEMP_DIR/"
@@ -253,5 +297,5 @@ Sources checksum calculation failed"
unstub which
assert_failure 97
assert_output "No way to search for relevant source files was found. Please install find (and git if this is a git repository directory)."
assert_output "No way to search for relevant source files was found. Please install find (and git if this is a git repository directory) or add a .spec file to the directory."
}
+2 -4
View File
@@ -11,13 +11,11 @@ setup() {
# bats file_tags=download_sources
@test "Missing spec variable exits with status 99" {
@test "Missing spec variable skips action" {
stub spectool ''
run _download_sources
_download_sources
assert_failure 99
assert_output "ERROR: Variable SPEC_FILE in not set"
assert_file_exists ${SPECTOOL_STUB_PLAN}
# unstub fails the test because spectool wasn't invoked
"$BATS_MOCK_REAL_rm" -rf "${BATS_MOCK_TMPDIR}" || true
BIN
View File
Binary file not shown.