%ansible_collection_url: Don't require control macros

Reimplement %ansible_collection_url to accept the collection namespace
and name as arguments instead of requiring oblique control macros. This
also adds some basic tests to ensure that the macro behaves properly.
epel8
Maxwell G 2 years ago
parent e3d150ec90
commit c67bd58e79
No known key found for this signature in database
GPG Key ID: F79E4E25E8C661F8

@ -77,6 +77,57 @@ install -Dpm0644 -t %{buildroot}%{_rpmmacrodir} macros.ansible-srpm
install -Dpm0755 -t %{buildroot}%{_rpmconfigdir} ansible-generator install -Dpm0755 -t %{buildroot}%{_rpmconfigdir} ansible-generator
install -Dpm0755 -t %{buildroot}%{_rpmconfigdir} ansible_collection.py install -Dpm0755 -t %{buildroot}%{_rpmconfigdir} ansible_collection.py
%check
# TODO: Currently, this only tests %%{ansible_collection_url}.
rpm_eval() {
default_macros_path="$(rpm --showrc | grep 'Macro path' | awk -F ': ' '{print $2}')"
rpm --macros="${default_macros_path}:%{buildroot}%{_rpmmacrodir}/macros.*" "$@"
}
errors() {
error="error: %%ansible_collection_url: You must pass the collection namespace as the first arg and the collection name as the second"
"$@" && exit 1
"$@" |& grep -q "${error}"
}
echo "Ensure macro fails when only collection_namespace macro is defined"
errors rpm_eval -D 'collection_namespace cc' -E '%%ansible_collection_url'
echo
echo "Ensure macro fails when only collection_name macro is defined"
errors rpm_eval -D 'collection_name cc' -E '%%ansible_collection_url'
echo
echo "Ensure macro fails when second argument is missing"
errors rpm_eval -E '%%ansible_collection_url a'
echo
echo "Ensure macro fails when second argument is missing"
errors rpm_eval -D 'collection_name b' -E '%%ansible_collection_url a'
echo
echo "Ensure macro fails when neither the control macros nor macro arguments are passed"
errors rpm_eval -E '%%ansible_collection_url'
echo
echo
echo "Ensure macro works when both arguments are passed and no control macros are set"
[[ $(rpm_eval -E '%%ansible_collection_url community general') == \
"https://galaxy.ansible.com/community/general" ]]
echo
echo "Ensure macro works with the control macros"
[[ $(rpm_eval -D 'collection_namespace ansible' -D 'collection_name posix' \
-E '%%ansible_collection_url') == "https://galaxy.ansible.com/ansible/posix" ]]
echo
echo "Ensure macro prefers the collection namespace and name passed as an argument over the control macros"
[[ $(rpm_eval -D 'collection_namespace ansible' -D 'collection_name posix' \
-E '%%ansible_collection_url community general') == "https://galaxy.ansible.com/community/general" ]]
%files %files
%license COPYING %license COPYING

@ -1 +1,25 @@
%ansible_collection_url() https://galaxy.ansible.com/%{collection_namespace}/%{collection_name} # Note(gotmax23): I'm trying to get rid of the need for control macros in favor
# of a metadata based approach. %%ansible_collection_url is the only macro that
# requires manually specifying the collection namespace and name, as it is used
# at the SRPM build stage.
#
# Currently, this macro supports either passing this information as arguments
# or defining the control macros. In order to reduce confusion, this is not an
# either or approach. Both arguments must be passed OR both control macros must
# be defined.
%ansible_collection_url() %{lua:
local namespace_name = nil
if rpm.expand("%collection_namespace") ~= "%collection_namespace"
and rpm.expand("%collection_name") ~= "%collection_name" then
namespace_name = rpm.expand("%collection_namespace") .. "/" .. rpm.expand("%collection_name")
end
if rpm.expand("%1") ~= "%1" and rpm.expand("%2") ~= "%2" then
namespace_name = rpm.expand("%1") .. "/" .. rpm.expand("%2")
end
if not namespace_name then
rpm.expand("%{error:%%ansible_collection_url: You must pass the collection " ..
"namespace as the first arg and the collection name as the second}")
end
print("https://galaxy.ansible.com/" .. namespace_name)
}

Loading…
Cancel
Save