generated from demus/basic-rpm-template
Add tests
This commit is contained in:
+49
-24
@@ -3,10 +3,10 @@
|
||||
# This should be run in the directory containing the spec file.
|
||||
# * Go vendor archive and config
|
||||
# * COPR source file
|
||||
set +e
|
||||
# set +e
|
||||
shopt -s extglob
|
||||
|
||||
usage() {
|
||||
_usage() {
|
||||
echo "Usage: $(basename "$0") [OPTION]... <SPEC_FILE>"
|
||||
echo "Update generated artifacts belonging to the <SPEC_FILE>."
|
||||
echo
|
||||
@@ -14,14 +14,15 @@ usage() {
|
||||
echo " -a Update archive"
|
||||
echo " -l Update licenses"
|
||||
echo " -s Update sources file"
|
||||
echo " -u Ignore unlicensed mods"
|
||||
echo " -t Path to a file to write the source hashes to. Implies -s"
|
||||
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."
|
||||
}
|
||||
|
||||
parse_args() {
|
||||
while getopts ":alsuh" flag; do
|
||||
_parse_args() {
|
||||
while getopts ":alsuht:" flag; do
|
||||
case "$flag" in
|
||||
a)
|
||||
UPDATE_ARCHIVE=1
|
||||
@@ -32,97 +33,121 @@ parse_args() {
|
||||
s)
|
||||
UPDATE_SOURCES=1
|
||||
;;
|
||||
t)
|
||||
SOURCES_FILE="$OPTARG"
|
||||
;;
|
||||
u)
|
||||
IGNORE_UNLICENSED_ARG="-u "
|
||||
IGNORE_UNLICENSED_ARG=" --ignore-unlicensed-mods"
|
||||
;;
|
||||
h)
|
||||
usage
|
||||
_usage
|
||||
exit
|
||||
;;
|
||||
\?)
|
||||
echo "Option -$OPTARG is invalid"
|
||||
echo
|
||||
usage
|
||||
exit 1
|
||||
_usage
|
||||
exit 10
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ "$UPDATE_ARCHIVE" == "" ] && [ "$UPDATE_LICENSES" == "" ] && [ "$UPDATE_SOURCES" == "" ]
|
||||
if [ $? -eq 0 ]; then
|
||||
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
|
||||
|
||||
SPEC_FILE="${@:$OPTIND:1}"
|
||||
if [ "$SPEC_FILE" == "" ]; then
|
||||
echo "The required spec parameter is missing"
|
||||
echo
|
||||
usage
|
||||
_usage
|
||||
exit 1
|
||||
fi
|
||||
if ! [[ "$SPEC_FILE" =~ .*.\.spec$ ]]; then
|
||||
echo "The spec file $SPEC_FILE doesn't have a .spec extension"
|
||||
echo
|
||||
usage
|
||||
_usage
|
||||
exit 2
|
||||
fi
|
||||
if [ ! -f "$SPEC_FILE" ]; then
|
||||
echo "The spec file $SPEC_FILE is not readable"
|
||||
echo
|
||||
usage
|
||||
_usage
|
||||
exit 3
|
||||
fi
|
||||
}
|
||||
|
||||
download_sources() {
|
||||
_download_sources() {
|
||||
if [ ! -v SPEC_FILE ]; then
|
||||
echo ERROR: Variable SPEC_FILE in not set
|
||||
exit 10
|
||||
exit 99
|
||||
fi
|
||||
# Ensure remote sources are downloaded
|
||||
spectool -g "$SPEC_FILE"
|
||||
}
|
||||
|
||||
main() {
|
||||
parse_args "$@"
|
||||
download_sources
|
||||
|
||||
_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
|
||||
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
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
_update_licenses() {
|
||||
# Update the go vendors license config if it has changed
|
||||
if [ "$UPDATE_ARCHIVE" -eq 1 ]; then
|
||||
if [[ "$UPDATE_LICENSES" -eq 1 ]]; then
|
||||
echo Updating go vendor 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
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
_update_sources() {
|
||||
# Update the sources checksums
|
||||
if [ "$UPDATE_ARCHIVE" -eq 1 ]; then
|
||||
if [[ "$UPDATE_SOURCES" -eq 1 ]]; then
|
||||
# The spec file and rpm build directories are excluded
|
||||
echo Calculating checksums
|
||||
# shellcheck disable=SC2094
|
||||
if ! cksum -a sha512 !(sources|*.spec|RPMS|SRPMS|BUILD) > sources; then
|
||||
files=$(find . -path "./RPMS" -prune -o -path "./BUILD" -prune -o -path "./SRPMS" -prune -o -path "./.*" -prune -o \( ! \( -name '*.spec' -o -name '.*' \) -a -type f \) -printf ' %P')
|
||||
if ! cksum -a sha512 $files > "${SOURCES_FILE:-sources}"; then
|
||||
echo Sources checksum update failed
|
||||
exit 7
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
main() {
|
||||
_parse_args "$@"
|
||||
_download_sources
|
||||
_check_go_vendor_enabled
|
||||
_update_archive
|
||||
_update_licenses
|
||||
_update_sources
|
||||
|
||||
echo Done
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load 'test_helper'
|
||||
|
||||
setup() {
|
||||
load ../spec-update-artifacts.sh
|
||||
common_setup
|
||||
}
|
||||
|
||||
# bats file_tags=mandatory_spec_file_argument
|
||||
|
||||
@test "Missing spec file argument returns an error" {
|
||||
run _parse_args
|
||||
|
||||
assert_failure 1
|
||||
assert_line --index 0 "The required spec parameter is missing"
|
||||
}
|
||||
|
||||
@test "A not .spec file returns an error" {
|
||||
run _parse_args gurli.test
|
||||
|
||||
assert_failure 2
|
||||
assert_line --index 0 "The spec file gurli.test doesn't have a .spec extension"
|
||||
}
|
||||
|
||||
@test "A non-existent spec file returns an error" {
|
||||
run _parse_args gurli.spec
|
||||
|
||||
assert_failure 3
|
||||
assert_line --index 0 "The spec file gurli.spec is not readable"
|
||||
}
|
||||
|
||||
# bats file_tags=options
|
||||
|
||||
@test "No selection selects all actions" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$UPDATE_ARCHIVE" 1
|
||||
assert_equal "$UPDATE_LICENSES" 1
|
||||
assert_equal "$UPDATE_SOURCES" 1
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" ""
|
||||
}
|
||||
|
||||
@test "Only select update archive" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args -a "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$UPDATE_ARCHIVE" 1
|
||||
assert_equal "$UPDATE_LICENSES" ""
|
||||
assert_equal "$UPDATE_SOURCES" ""
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" ""
|
||||
}
|
||||
|
||||
@test "Only select update license" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args -l "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$UPDATE_ARCHIVE" ""
|
||||
assert_equal "$UPDATE_LICENSES" 1
|
||||
assert_equal "$UPDATE_SOURCES" ""
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" ""
|
||||
}
|
||||
|
||||
@test "Only select update sources" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args -s "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$UPDATE_ARCHIVE" ""
|
||||
assert_equal "$UPDATE_LICENSES" ""
|
||||
assert_equal "$UPDATE_SOURCES" 1
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" ""
|
||||
}
|
||||
|
||||
@test "Multiple selections aggregate" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args -s -l "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$UPDATE_ARCHIVE" ""
|
||||
assert_equal "$UPDATE_LICENSES" 1
|
||||
assert_equal "$UPDATE_SOURCES" 1
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" ""
|
||||
}
|
||||
|
||||
@test "The -u option configures an --ignore-unlicensed-mods parameter for go_vendor_license" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args -u "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" " --ignore-unlicensed-mods"
|
||||
assert_equal "$UPDATE_ARCHIVE" 1
|
||||
assert_equal "$UPDATE_LICENSES" 1
|
||||
assert_equal "$UPDATE_SOURCES" 1
|
||||
}
|
||||
|
||||
@test "The -u option implies -l" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
_parse_args -u -s "$TEST_TEMP_DIR/a.spec"
|
||||
|
||||
assert_equal "$?" 0
|
||||
assert_equal "$IGNORE_UNLICENSED_ARG" " --ignore-unlicensed-mods"
|
||||
assert_equal "$UPDATE_ARCHIVE" ""
|
||||
assert_equal "$UPDATE_LICENSES" 1
|
||||
assert_equal "$UPDATE_SOURCES" 1
|
||||
}
|
||||
|
||||
@test "The help option successfully short-circuits everything else" {
|
||||
touch "$TEST_TEMP_DIR/a.spec"
|
||||
run _parse_args -h
|
||||
|
||||
assert_success
|
||||
assert_line --index 0 --partial "Usage: "
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load 'test_helper'
|
||||
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"
|
||||
}
|
||||
|
||||
# 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" {
|
||||
export UPDATE_ARCHIVE=1
|
||||
|
||||
run _check_go_vendor_enabled
|
||||
|
||||
assert_failure 4
|
||||
assert_output "The spec file is not go-vendor-tools enabled"
|
||||
}
|
||||
|
||||
@test "Script returns error when trying to update go vendor licenses on a spec that is not go vendor enabled" {
|
||||
export UPDATE_LICENSES=1
|
||||
|
||||
run _check_go_vendor_enabled
|
||||
|
||||
assert_failure 4
|
||||
assert_output "The spec file is not go-vendor-tools enabled"
|
||||
}
|
||||
|
||||
@test "Script succeeds when only updating sources on a spec that is not go vendor enabled" {
|
||||
export UPDATE_SOURCES=1
|
||||
|
||||
run _check_go_vendor_enabled
|
||||
|
||||
assert_success
|
||||
refute_output
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
run _check_go_vendor_enabled
|
||||
|
||||
assert_success
|
||||
refute_output
|
||||
}
|
||||
|
||||
# bats file_tags=archive_update
|
||||
|
||||
@test "Archive is updated when spec is valid and update is requested" {
|
||||
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 : echo"
|
||||
|
||||
run _update_archive
|
||||
|
||||
assert_success
|
||||
assert_output "Updating go vendor archive"
|
||||
unstub go_vendor_archive
|
||||
}
|
||||
|
||||
@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
|
||||
stub go_vendor_archive "create --config go-vendor-tools.toml --write-config $TEST_TEMP_DIR/test.spec : echo"
|
||||
|
||||
run _update_archive
|
||||
|
||||
assert_success
|
||||
refute_output
|
||||
assert_file_exists ${GO_VENDOR_ARCHIVE_STUB_PLAN}
|
||||
# unstub fails the test because spectool wasn't invoked
|
||||
"$BATS_MOCK_REAL_rm" -rf "${BATS_MOCK_TMPDIR}" || true
|
||||
}
|
||||
|
||||
@test "Some 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_output <<<"
|
||||
Updating go vendor archive
|
||||
Archive update failed"
|
||||
unstub go_vendor_archive
|
||||
}
|
||||
|
||||
# bats file_tags=license_update
|
||||
|
||||
@test "Licenses are updated when spec is valid and update is requested" {
|
||||
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 : echo"
|
||||
|
||||
run _update_licenses
|
||||
|
||||
assert_success
|
||||
assert_output "Updating go vendor licenses"
|
||||
unstub go_vendor_license
|
||||
}
|
||||
|
||||
@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
|
||||
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
|
||||
|
||||
assert_success
|
||||
refute_output
|
||||
assert_file_exists ${GO_VENDOR_LICENSE_STUB_PLAN}
|
||||
# unstub fails the test because spectool wasn't invoked
|
||||
"$BATS_MOCK_REAL_rm" -rf "${BATS_MOCK_TMPDIR}" || true
|
||||
}
|
||||
|
||||
@test "Some 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_output <<<"
|
||||
Updating go vendor licenses
|
||||
License update failed"
|
||||
unstub go_vendor_license
|
||||
}
|
||||
|
||||
@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
|
||||
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
|
||||
|
||||
assert_success
|
||||
assert_output "Updating go vendor licenses"
|
||||
unstub go_vendor_license
|
||||
}
|
||||
|
||||
# bats file_tags=sources_update
|
||||
|
||||
@test "sources is updated when spec is valid and update is requested" {
|
||||
export UPDATE_SOURCES=1
|
||||
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
|
||||
|
||||
run _update_sources
|
||||
|
||||
assert_success
|
||||
assert_output "Calculating checksums"
|
||||
assert_file_not_empty "$SOURCES_FILE"
|
||||
}
|
||||
|
||||
@test "sources is only updated when an update is requested" {
|
||||
export UPDATE_LICENSES=1
|
||||
export UPDATE_ARCHIVE=1
|
||||
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
|
||||
|
||||
run _update_sources
|
||||
|
||||
assert_success
|
||||
refute_output
|
||||
assert_file_empty "$SOURCES_FILE"
|
||||
}
|
||||
|
||||
@test "Some feedback is proffered when cksum fails" {
|
||||
stub cksum "exit 1"
|
||||
export UPDATE_SOURCES=1
|
||||
echo "Source: go-vendor-tools.toml" > $SPEC_FILE
|
||||
|
||||
run _update_sources
|
||||
|
||||
assert_failure 7
|
||||
assert_output <<<"
|
||||
Calculating checksums
|
||||
Sources checksum update failed"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load 'test_helper'
|
||||
bats_load_library 'bats-file'
|
||||
bats_load_library 'bats-mock'
|
||||
|
||||
setup() {
|
||||
load ../spec-update-artifacts.sh
|
||||
common_setup
|
||||
}
|
||||
|
||||
# bats file_tags=download_sources
|
||||
|
||||
@test "Missing spec variable exits with status 99" {
|
||||
stub spectool ''
|
||||
|
||||
run _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
|
||||
}
|
||||
|
||||
@test "Correct parameters are passed to spectool" {
|
||||
stub spectool '-g gurli.spec : echo OK'
|
||||
|
||||
export SPEC_FILE=gurli.spec
|
||||
run _download_sources
|
||||
|
||||
assert_success
|
||||
unstub spectool
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
bats_require_minimum_version 1.5.0
|
||||
bats_load_library 'bats-support'
|
||||
bats_load_library 'bats-assert'
|
||||
bats_load_library 'bats-file'
|
||||
|
||||
setup_file() {
|
||||
BATS_TEST_TIMEOUT=3
|
||||
}
|
||||
|
||||
teardown() {
|
||||
temp_del "$TEST_TEMP_DIR"
|
||||
unset TEST_TEMP_DIR
|
||||
}
|
||||
|
||||
common_setup() {
|
||||
export TEST_TEMP_DIR="$(temp_make)"
|
||||
}
|
||||
|
||||
bats::on_failure() {
|
||||
echo "Failed test run output:"
|
||||
echo "$output"
|
||||
}
|
||||
Reference in New Issue
Block a user