Files
demus-packaging/test/02-updates_test.bats
T

302 lines
8.9 KiB
Bash

#!/usr/bin/env bats
load 'test_helper'
bats_load_library 'bats-mock'
setup() {
load ../spec-update-artifacts.sh
common_setup
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" {
export UPDATE_ARCHIVE=1
run _check_go_vendor_enabled
assert_failure 14
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 14
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
_make_valid_spec
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
_make_valid_spec
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
_make_valid_spec
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 "Sane feedback is proffered when go_vendor_archive fails" {
export UPDATE_ARCHIVE=1
_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
assert_failure 15
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
_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
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
_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
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 "Sane feedback is proffered when go_vendor_license fails" {
export UPDATE_LICENSES=1
_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
assert_failure 16
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"
_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
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
tar -xzf "$BATS_TEST_DIRNAME/testdata/example.tar.gz" -C "$TEST_TEMP_DIR/"
_make_valid_spec
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 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/"
_make_valid_spec
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 is only updated when an update is requested" {
export UPDATE_LICENSES=1
export UPDATE_ARCHIVE=1
tar -xzf "$BATS_TEST_DIRNAME/testdata/example.tar.gz" -C "$TEST_TEMP_DIR/"
_make_valid_spec
run _update_sources
assert_success
refute_output
assert_file_empty "$SOURCES_FILE"
}
@test "When there are no relevant sources, checksumming is skipped" {
export UPDATE_SOURCES=1
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
assert_success
assert_output - <<<"Calculating checksums
No relevant sources found. Skipping checksumming."
}
@test "Sane feedback is proffered when cksum fails" {
stub cksum "exit 1"
export UPDATE_SOURCES=1
tar -xzf "$BATS_TEST_DIRNAME/testdata/git.example.tar.gz" -C "$TEST_TEMP_DIR/"
_make_valid_spec
run _update_sources
unstub cksum
assert_failure 17
assert_output - <<<"Calculating checksums
Sources checksum calculation failed"
}
# bats test_tags=find_sources
@test "The list of files to include in sources excludes the correct things in a git repo" {
tar -xzf "$BATS_TEST_DIRNAME/testdata/git.example.tar.gz" -C "$TEST_TEMP_DIR/"
run __find_sources
assert_success
assert_output "LICENSE README.md changelog data/README data/somedata.csv v1.0.0.tar.gz vendor.tar.bz2"
}
# bats test_tags=find_sources
@test "The list of files to include in sources excludes the correct things in plain directory" {
tar -xzf "$BATS_TEST_DIRNAME/testdata/example.tar.gz" -C "$TEST_TEMP_DIR/"
run __find_sources
assert_success
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 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/"
run __find_sources
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) or add a .spec file to the directory."
}